cmake/Source/cmFileLockWin32.cxx

88 lines
2.3 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
2020-02-01 23:06:01 +01:00
#include <windows.h> // CreateFileW
2015-04-27 22:25:09 +02:00
2020-02-01 23:06:01 +01:00
#include "cmFileLock.h"
2015-04-27 22:25:09 +02:00
#include "cmSystemTools.h"
2016-07-09 11:21:54 +02:00
cmFileLock::cmFileLock()
2015-04-27 22:25:09 +02:00
{
}
cmFileLockResult cmFileLock::Release()
{
2016-07-09 11:21:54 +02:00
if (this->Filename.empty()) {
2015-04-27 22:25:09 +02:00
return cmFileLockResult::MakeOk();
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
const unsigned long len = static_cast<unsigned long>(-1);
static OVERLAPPED overlapped;
const DWORD reserved = 0;
2016-07-09 11:21:54 +02:00
const BOOL unlockResult =
UnlockFileEx(File, reserved, len, len, &overlapped);
2015-04-27 22:25:09 +02:00
this->Filename = "";
CloseHandle(this->File);
this->File = INVALID_HANDLE_VALUE;
2016-07-09 11:21:54 +02:00
if (unlockResult) {
2015-04-27 22:25:09 +02:00
return cmFileLockResult::MakeOk();
2016-07-09 11:21:54 +02:00
} else {
2015-04-27 22:25:09 +02:00
return cmFileLockResult::MakeSystem();
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
}
cmFileLockResult cmFileLock::OpenFile()
{
const DWORD access = GENERIC_READ | GENERIC_WRITE;
const DWORD shareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
const PSECURITY_ATTRIBUTES security = NULL;
const DWORD attr = 0;
const HANDLE templ = NULL;
this->File = CreateFileW(
2016-07-09 11:21:54 +02:00
cmSystemTools::ConvertToWindowsExtendedPath(this->Filename).c_str(),
access, shareMode, security, OPEN_EXISTING, attr, templ);
if (this->File == INVALID_HANDLE_VALUE) {
2015-04-27 22:25:09 +02:00
return cmFileLockResult::MakeSystem();
2016-07-09 11:21:54 +02:00
} else {
2015-04-27 22:25:09 +02:00
return cmFileLockResult::MakeOk();
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
}
cmFileLockResult cmFileLock::LockWithoutTimeout()
{
2016-07-09 11:21:54 +02:00
if (!this->LockFile(LOCKFILE_EXCLUSIVE_LOCK)) {
2015-04-27 22:25:09 +02:00
return cmFileLockResult::MakeSystem();
2016-07-09 11:21:54 +02:00
} else {
2015-04-27 22:25:09 +02:00
return cmFileLockResult::MakeOk();
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
}
cmFileLockResult cmFileLock::LockWithTimeout(unsigned long seconds)
{
const DWORD flags = LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY;
2016-07-09 11:21:54 +02:00
while (true) {
2015-04-27 22:25:09 +02:00
const BOOL result = this->LockFile(flags);
2016-07-09 11:21:54 +02:00
if (result) {
2015-04-27 22:25:09 +02:00
return cmFileLockResult::MakeOk();
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
const DWORD error = GetLastError();
2016-07-09 11:21:54 +02:00
if (error != ERROR_LOCK_VIOLATION) {
2015-04-27 22:25:09 +02:00
return cmFileLockResult::MakeSystem();
2016-07-09 11:21:54 +02:00
}
if (seconds == 0) {
2015-04-27 22:25:09 +02:00
return cmFileLockResult::MakeTimeout();
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
--seconds;
cmSystemTools::Delay(1000);
2016-07-09 11:21:54 +02:00
}
2015-04-27 22:25:09 +02:00
}
BOOL cmFileLock::LockFile(DWORD flags)
{
const DWORD reserved = 0;
const unsigned long len = static_cast<unsigned long>(-1);
static OVERLAPPED overlapped;
2016-07-09 11:21:54 +02:00
return LockFileEx(this->File, flags, reserved, len, len, &overlapped);
2015-04-27 22:25:09 +02:00
}