init
Some checks failed
Docker. / Ubuntu (push) Has been cancelled
User-agent updater. / User-agent (push) Failing after 15s
Lock Threads / lock (push) Failing after 10s
Waiting for answer. / waiting-for-answer (push) Failing after 22s
Close stale issues and PRs / stale (push) Successful in 13s
Needs user action. / needs-user-action (push) Failing after 8s
Can't reproduce. / cant-reproduce (push) Failing after 8s

This commit is contained in:
allhaileris
2026-02-16 15:50:16 +03:00
commit afb81b8278
13816 changed files with 3689732 additions and 0 deletions

View File

@@ -0,0 +1,126 @@
// This file is part of Desktop App Toolkit,
// a set of libraries for developing nice desktop applications.
//
// For license and copyright information please follow this link:
// https://github.com/desktop-app/legal/blob/master/LEGAL
//
#include "base/file_lock.h"
#include <variant>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <signal.h>
namespace base {
namespace {
bool KillProcess(pid_t pid) {
auto signal = SIGTERM;
auto attempts = 0;
while (true) {
const auto result = kill(pid, signal);
if (result < 0) {
return (errno == ESRCH);
}
usleep(10000);
if (++attempts == 50) {
signal = SIGKILL;
}
}
}
} // namespace
struct FileLock::Descriptor {
int value;
};
struct FileLock::LockingPid {
pid_t value;
};
class FileLock::Lock {
public:
using Result = std::variant<Descriptor, LockingPid>;
static Result Acquire(const QFile &file);
explicit Lock(int descriptor);
~Lock();
private:
int _descriptor = 0;
};
FileLock::Lock::Result FileLock::Lock::Acquire(const QFile &file) {
const auto descriptor = file.handle();
if (!descriptor || !file.isOpen()) {
return Descriptor{ 0 };
}
while (true) {
struct flock lock;
lock.l_type = F_WRLCK;
lock.l_whence = SEEK_SET;
lock.l_start = kLockOffset;
lock.l_len = kLockLimit;
if (fcntl(descriptor, F_SETLK, &lock) == 0) {
return Descriptor{ descriptor };
} else if (fcntl(descriptor, F_GETLK, &lock) < 0) {
return LockingPid{ 0 };
} else if (lock.l_type != F_UNLCK) {
return LockingPid{ lock.l_pid };
}
}
}
FileLock::Lock::Lock(int descriptor) : _descriptor(descriptor) {
}
FileLock::Lock::~Lock() {
struct flock unlock;
unlock.l_type = F_UNLCK;
unlock.l_whence = SEEK_SET;
unlock.l_start = kLockOffset;
unlock.l_len = kLockLimit;
fcntl(_descriptor, F_SETLK, &unlock);
}
FileLock::FileLock() = default;
bool FileLock::lock(QFile &file, QIODevice::OpenMode mode) {
Expects(_lock == nullptr || file.isOpen());
unlock();
file.close();
if (!file.open(mode)) {
return false;
}
while (true) {
const auto result = Lock::Acquire(file);
if (const auto descriptor = std::get_if<Descriptor>(&result)) {
if (descriptor->value > 0) {
_lock = std::make_unique<Lock>(descriptor->value);
return true;
}
break;
} else if (const auto pid = std::get_if<LockingPid>(&result)) {
if (pid->value <= 0 || !KillProcess(pid->value)) {
break;
}
}
}
return false;
}
bool FileLock::locked() const {
return (_lock != nullptr);
}
void FileLock::unlock() {
_lock = nullptr;
}
FileLock::~FileLock() = default;
} // namespace base