2016-09-27 22:01:08 +03:00
|
|
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
|
|
|
file Copyright.txt or https://cmake.org/licensing for details. */
|
2014-11-26 01:49:25 +03:00
|
|
|
#include "cmFileLock.h"
|
|
|
|
|
|
|
|
#include "cmSystemTools.h"
|
2016-04-29 17:53:13 +03:00
|
|
|
#include <windows.h> // CreateFileW
|
2014-11-26 01:49:25 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
cmFileLock::cmFileLock()
|
|
|
|
: File(INVALID_HANDLE_VALUE)
|
2014-11-26 01:49:25 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
cmFileLockResult cmFileLock::Release()
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (this->Filename.empty()) {
|
2014-11-26 01:49:25 +03:00
|
|
|
return cmFileLockResult::MakeOk();
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-11-26 01:49:25 +03:00
|
|
|
const unsigned long len = static_cast<unsigned long>(-1);
|
|
|
|
static OVERLAPPED overlapped;
|
|
|
|
const DWORD reserved = 0;
|
2016-05-16 17:34:04 +03:00
|
|
|
const BOOL unlockResult =
|
|
|
|
UnlockFileEx(File, reserved, len, len, &overlapped);
|
2014-11-26 01:49:25 +03:00
|
|
|
|
|
|
|
this->Filename = "";
|
|
|
|
|
2015-03-23 23:32:12 +03:00
|
|
|
CloseHandle(this->File);
|
|
|
|
this->File = INVALID_HANDLE_VALUE;
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (unlockResult) {
|
2014-11-26 01:49:25 +03:00
|
|
|
return cmFileLockResult::MakeOk();
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2014-11-26 01:49:25 +03:00
|
|
|
return cmFileLockResult::MakeSystem();
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-11-26 01:49:25 +03: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-05-16 17:34:04 +03:00
|
|
|
cmSystemTools::ConvertToWindowsExtendedPath(this->Filename).c_str(),
|
|
|
|
access, shareMode, security, OPEN_EXISTING, attr, templ);
|
|
|
|
if (this->File == INVALID_HANDLE_VALUE) {
|
2014-11-26 01:49:25 +03:00
|
|
|
return cmFileLockResult::MakeSystem();
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2014-11-26 01:49:25 +03:00
|
|
|
return cmFileLockResult::MakeOk();
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-11-26 01:49:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
cmFileLockResult cmFileLock::LockWithoutTimeout()
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!this->LockFile(LOCKFILE_EXCLUSIVE_LOCK)) {
|
2014-11-26 01:49:25 +03:00
|
|
|
return cmFileLockResult::MakeSystem();
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2014-11-26 01:49:25 +03:00
|
|
|
return cmFileLockResult::MakeOk();
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-11-26 01:49:25 +03:00
|
|
|
}
|
|
|
|
|
2014-12-05 17:18:11 +03:00
|
|
|
cmFileLockResult cmFileLock::LockWithTimeout(unsigned long seconds)
|
2014-11-26 01:49:25 +03:00
|
|
|
{
|
|
|
|
const DWORD flags = LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY;
|
2016-05-16 17:34:04 +03:00
|
|
|
while (true) {
|
2014-11-26 01:49:25 +03:00
|
|
|
const BOOL result = this->LockFile(flags);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (result) {
|
2014-11-26 01:49:25 +03:00
|
|
|
return cmFileLockResult::MakeOk();
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-11-26 01:49:25 +03:00
|
|
|
const DWORD error = GetLastError();
|
2016-05-16 17:34:04 +03:00
|
|
|
if (error != ERROR_LOCK_VIOLATION) {
|
2014-11-26 01:49:25 +03:00
|
|
|
return cmFileLockResult::MakeSystem();
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
|
|
|
if (seconds == 0) {
|
2014-11-26 01:49:25 +03:00
|
|
|
return cmFileLockResult::MakeTimeout();
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-11-26 01:49:25 +03:00
|
|
|
--seconds;
|
|
|
|
cmSystemTools::Delay(1000);
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2014-11-26 01:49:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL cmFileLock::LockFile(DWORD flags)
|
|
|
|
{
|
|
|
|
const DWORD reserved = 0;
|
|
|
|
const unsigned long len = static_cast<unsigned long>(-1);
|
|
|
|
static OVERLAPPED overlapped;
|
2016-05-16 17:34:04 +03:00
|
|
|
return LockFileEx(this->File, flags, reserved, len, len, &overlapped);
|
2014-11-26 01:49:25 +03:00
|
|
|
}
|