File: cmFileLockWin32.cxx

package info (click to toggle)
cmake 4.1.2-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 147,412 kB
  • sloc: ansic: 403,924; cpp: 290,826; sh: 4,091; python: 3,357; yacc: 3,106; lex: 1,189; f90: 532; asm: 471; lisp: 375; cs: 270; java: 266; fortran: 230; perl: 217; objc: 215; xml: 198; makefile: 98; javascript: 83; pascal: 63; tcl: 55; php: 25; ruby: 22
file content (120 lines) | stat: -rw-r--r-- 3,371 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file LICENSE.rst or https://cmake.org/licensing for details.  */
#include <cm/memory>

#include <windows.h>

#include "cmFileLock.h"
#include "cmSystemTools.h"

static unsigned long const LOCK_LEN = static_cast<unsigned long>(-1);

cmFileLock::cmFileLock()
  : Overlapped(cm::make_unique<OVERLAPPED>())
{
  ZeroMemory(this->Overlapped.get(), sizeof(*this->Overlapped));
}

cmFileLockResult cmFileLock::Release()
{
  if (this->Filename.empty()) {
    return cmFileLockResult::MakeOk();
  }
  const DWORD reserved = 0;
  ZeroMemory(this->Overlapped.get(), sizeof(*this->Overlapped));

  const BOOL unlockResult =
    UnlockFileEx(File, reserved, LOCK_LEN, LOCK_LEN, this->Overlapped.get());

  this->Filename = "";

  CloseHandle(this->File);

  this->File = INVALID_HANDLE_VALUE;

  if (unlockResult) {
    return cmFileLockResult::MakeOk();
  } else {
    return cmFileLockResult::MakeSystem();
  }
}

cmFileLockResult cmFileLock::OpenFile()
{
  const DWORD access = GENERIC_READ | GENERIC_WRITE;
  const DWORD shareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
  const PSECURITY_ATTRIBUTES security = nullptr;
  const DWORD attr = 0;
  const HANDLE templ = nullptr;
  this->File = CreateFileW(
    cmSystemTools::ConvertToWindowsExtendedPath(this->Filename).c_str(),
    access, shareMode, security, OPEN_EXISTING, attr, templ);
  if (this->File == INVALID_HANDLE_VALUE) {
    return cmFileLockResult::MakeSystem();
  } else {
    return cmFileLockResult::MakeOk();
  }
}

cmFileLockResult cmFileLock::LockWithoutTimeout()
{
  cmFileLockResult lock_result = cmFileLockResult::MakeOk();
  if (!this->LockFile(LOCKFILE_EXCLUSIVE_LOCK)) {
    lock_result = cmFileLockResult::MakeSystem();
  }
  CloseHandle(this->Overlapped->hEvent);
  return lock_result;
}

cmFileLockResult cmFileLock::LockWithTimeout(unsigned long seconds)
{
  cmFileLockResult lock_result = cmFileLockResult::MakeOk();
  const DWORD flags = LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY;
  bool in_time = true;
  while (in_time && !this->LockFile(flags)) {
    switch (GetLastError()) {
      case ERROR_INVALID_HANDLE:
        lock_result = cmFileLockResult::MakeSystem();
        break;
      case ERROR_LOCK_VIOLATION:
        if (seconds == 0) {
          in_time = false;
          lock_result = cmFileLockResult::MakeTimeout();
          continue;
        }
        --seconds;
        cmSystemTools::Delay(1000);
        continue;
      case ERROR_IO_PENDING:
        switch (
          WaitForSingleObject(this->Overlapped->hEvent, seconds * 1000)) {
          case WAIT_OBJECT_0:
            break;
          case WAIT_TIMEOUT:
            lock_result = cmFileLockResult::MakeTimeout();
            break;
          default:
            lock_result = cmFileLockResult::MakeSystem();
            break;
        }
        break;
      default:
        lock_result = cmFileLockResult::MakeSystem();
        break;
    }
  }
  CloseHandle(this->Overlapped->hEvent);
  return lock_result;
}

int cmFileLock::LockFile(int flags)
{
  const DWORD reserved = 0;

  this->Overlapped->hEvent = CreateEvent(nullptr, TRUE, FALSE, nullptr);
  if (this->Overlapped->hEvent == nullptr) {
    return false;
  }
  return LockFileEx(this->File, flags, reserved, LOCK_LEN, LOCK_LEN,
                    this->Overlapped.get());
}