cmFileLockWin32.cxx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2014 Ruslan Baratov
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmFileLock.h"
  11. #include <windows.h> // CreateFileW
  12. #include "cmSystemTools.h"
  13. cmFileLock::cmFileLock(): File(INVALID_HANDLE_VALUE)
  14. {
  15. }
  16. cmFileLockResult cmFileLock::Release()
  17. {
  18. if (this->Filename.empty())
  19. {
  20. return cmFileLockResult::MakeOk();
  21. }
  22. const unsigned long len = static_cast<unsigned long>(-1);
  23. static OVERLAPPED overlapped;
  24. const DWORD reserved = 0;
  25. const BOOL unlockResult = UnlockFileEx(
  26. File,
  27. reserved,
  28. len,
  29. len,
  30. &overlapped
  31. );
  32. this->Filename = "";
  33. if (unlockResult)
  34. {
  35. return cmFileLockResult::MakeOk();
  36. }
  37. else
  38. {
  39. return cmFileLockResult::MakeSystem();
  40. }
  41. }
  42. cmFileLockResult cmFileLock::OpenFile()
  43. {
  44. const DWORD access = GENERIC_READ | GENERIC_WRITE;
  45. const DWORD shareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
  46. const PSECURITY_ATTRIBUTES security = NULL;
  47. const DWORD attr = 0;
  48. const HANDLE templ = NULL;
  49. this->File = CreateFileW(
  50. cmSystemTools::ConvertToWindowsExtendedPath(this->Filename).c_str(),
  51. access,
  52. shareMode,
  53. security,
  54. OPEN_EXISTING,
  55. attr,
  56. templ
  57. );
  58. if (this->File == INVALID_HANDLE_VALUE)
  59. {
  60. return cmFileLockResult::MakeSystem();
  61. }
  62. else
  63. {
  64. return cmFileLockResult::MakeOk();
  65. }
  66. }
  67. cmFileLockResult cmFileLock::LockWithoutTimeout()
  68. {
  69. if (!this->LockFile(LOCKFILE_EXCLUSIVE_LOCK))
  70. {
  71. return cmFileLockResult::MakeSystem();
  72. }
  73. else
  74. {
  75. return cmFileLockResult::MakeOk();
  76. }
  77. }
  78. cmFileLockResult cmFileLock::LockWithTimeout(unsigned long seconds)
  79. {
  80. const DWORD flags = LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY;
  81. while (true)
  82. {
  83. const BOOL result = this->LockFile(flags);
  84. if (result)
  85. {
  86. return cmFileLockResult::MakeOk();
  87. }
  88. const DWORD error = GetLastError();
  89. if (error != ERROR_LOCK_VIOLATION)
  90. {
  91. return cmFileLockResult::MakeSystem();
  92. }
  93. if (seconds == 0)
  94. {
  95. return cmFileLockResult::MakeTimeout();
  96. }
  97. --seconds;
  98. cmSystemTools::Delay(1000);
  99. }
  100. }
  101. BOOL cmFileLock::LockFile(DWORD flags)
  102. {
  103. const DWORD reserved = 0;
  104. const unsigned long len = static_cast<unsigned long>(-1);
  105. static OVERLAPPED overlapped;
  106. return LockFileEx(
  107. this->File,
  108. flags,
  109. reserved,
  110. len,
  111. len,
  112. &overlapped
  113. );
  114. }