Files
tdesktop/Telegram/lib_base/base/file_lock_win.cpp
allhaileris afb81b8278
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
init
2026-02-16 15:50:16 +03:00

90 lines
1.9 KiB
C++

// 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 "base/platform/win/base_file_utilities_win.h"
#include <QtCore/QDir>
#include <windows.h>
#include <io.h>
#include <fileapi.h>
namespace base {
class FileLock::Lock {
public:
static int Acquire(const QFile &file);
explicit Lock(int descriptor);
~Lock();
private:
static constexpr auto offsetLow = DWORD(kLockOffset);
static constexpr auto offsetHigh = DWORD(0);
static constexpr auto limitLow = DWORD(kLockLimit);
static constexpr auto limitHigh = DWORD(0);
int _descriptor = 0;
};
int FileLock::Lock::Acquire(const QFile &file) {
const auto descriptor = file.handle();
if (!descriptor || !file.isOpen()) {
return false;
}
const auto handle = HANDLE(_get_osfhandle(descriptor));
if (!handle) {
return false;
}
return LockFile(handle, offsetLow, offsetHigh, limitLow, limitHigh)
? descriptor
: 0;
}
FileLock::Lock::Lock(int descriptor) : _descriptor(descriptor) {
}
FileLock::Lock::~Lock() {
if (const auto handle = HANDLE(_get_osfhandle(_descriptor))) {
UnlockFile(handle, offsetLow, offsetHigh, limitLow, limitHigh);
}
}
FileLock::FileLock() = default;
bool FileLock::lock(QFile &file, QIODevice::OpenMode mode) {
Expects(_lock == nullptr || file.isOpen());
unlock();
file.close();
do {
if (!file.open(mode)) {
return false;
} else if (const auto descriptor = Lock::Acquire(file)) {
_lock = std::make_unique<Lock>(descriptor);
return true;
}
file.close();
} while (Platform::CloseProcesses(file.fileName()));
return false;
}
bool FileLock::locked() const {
return (_lock != nullptr);
}
void FileLock::unlock() {
_lock = nullptr;
}
FileLock::~FileLock() = default;
} // namespace base