Merge topic 'curl-tls-verify'
[kiteware-cmake.git] / Source / cmFileLock.cxx
blob548e3279b00957e5da9d0de298d477ebfd326401
1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
3 #include "cmFileLock.h"
5 #include <cassert>
6 #include <utility>
8 #include "cmFileLockResult.h"
10 // Common implementation
12 cmFileLock::cmFileLock(cmFileLock&& other) noexcept
14 this->File = other.File;
15 other.File = (decltype(other.File))-1;
16 this->Filename = std::move(other.Filename);
19 cmFileLock::~cmFileLock()
21 if (!this->Filename.empty()) {
22 const cmFileLockResult result = this->Release();
23 static_cast<void>(result);
24 assert(result.IsOk());
28 cmFileLock& cmFileLock::operator=(cmFileLock&& other) noexcept
30 this->File = other.File;
31 other.File = (decltype(other.File))-1;
32 this->Filename = std::move(other.Filename);
34 return *this;
37 cmFileLockResult cmFileLock::Lock(const std::string& filename,
38 unsigned long timeout)
40 if (filename.empty()) {
41 // Error is internal since all the directories and file must be created
42 // before actual lock called.
43 return cmFileLockResult::MakeInternal();
46 if (!this->Filename.empty()) {
47 // Error is internal since double-lock must be checked in class
48 // cmFileLockPool by the cmFileLock::IsLocked method.
49 return cmFileLockResult::MakeInternal();
52 this->Filename = filename;
53 cmFileLockResult result = this->OpenFile();
54 if (result.IsOk()) {
55 if (timeout == static_cast<unsigned long>(-1)) {
56 result = this->LockWithoutTimeout();
57 } else {
58 result = this->LockWithTimeout(timeout);
62 if (!result.IsOk()) {
63 this->Filename.clear();
66 return result;
69 bool cmFileLock::IsLocked(const std::string& filename) const
71 return filename == this->Filename;
74 #if defined(_WIN32)
75 // NOLINTNEXTLINE(bugprone-suspicious-include)
76 # include "cmFileLockWin32.cxx"
77 #else
78 // NOLINTNEXTLINE(bugprone-suspicious-include)
79 # include "cmFileLockUnix.cxx"
80 #endif