Repository navigation
Expand file tree
/
Copy pathFileLock.cpp
More file actions
104 lines (88 loc) · 3.05 KB
/
Copy pathFileLock.cpp
File metadata and controls
104 lines (88 loc) · 3.05 KB
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
#include "prx/libc/include/FileLock.hpp"
#ifdef _WIN32
#include <cerrno>
#include <io.h>
#include <map>
#include <memory>
#include <mutex>
#include <windows.h>
#include "prx/libc/include/WindowsDescriptor.hpp"
namespace {
constexpr int LockShared = 1;
constexpr int LockExclusive = 2;
constexpr int LockNonblocking = 4;
constexpr int LockUnlock = 8;
constexpr DWORD LockByteLow = 0xFFFFFFFE;
constexpr DWORD LockByteHigh = 0x7FFFFFFF;
struct LockState {
std::mutex mutex;
int mode = 0;
HANDLE locker = nullptr;
LockState() = default;
LockState(const LockState&) = delete;
LockState& operator=(const LockState&) = delete;
~LockState() {
if (locker != nullptr) ::CloseHandle(locker);
}
};
std::mutex g_mutex;
std::map<int, std::shared_ptr<LockState>> g_locks;
std::shared_ptr<LockState> Materialize(int fd) {
const auto found = g_locks.find(fd);
if (found != g_locks.end()) return found->second;
return g_locks.emplace(fd, std::make_shared<LockState>()).first->second;
}
}
extern "C" {
int GuestFileLock_nid_no_patch(int fd, int operation) {
const int mode = operation & (LockShared | LockExclusive | LockUnlock);
if (mode != LockShared && mode != LockExclusive && mode != LockUnlock) {
errno = EINVAL;
::SetLastError(ERROR_INVALID_PARAMETER);
return -1;
}
const auto handle = reinterpret_cast<HANDLE>(::_get_osfhandle(fd));
if (handle == INVALID_HANDLE_VALUE) {
errno = EBADF;
::SetLastError(ERROR_INVALID_HANDLE);
return -1;
}
const auto state = [&] {
std::lock_guard lock(g_mutex);
return Materialize(fd);
}();
std::lock_guard lock(state->mutex);
if (state->mode == mode) return 0;
if (state->mode != 0) {
OVERLAPPED overlapped{};
overlapped.Offset = LockByteLow;
overlapped.OffsetHigh = LockByteHigh;
if (!::UnlockFileEx(state->locker != nullptr ? state->locker : handle, 0, 1, 0, &overlapped)) return -1;
state->mode = 0;
}
if (mode == LockUnlock) return 0;
if (state->locker == nullptr && (WindowsHandleAccess_nid_no_patch(handle, fd, "flock") & (FILE_READ_DATA | FILE_WRITE_DATA)) == 0) {
const auto locker = ::ReOpenFile(handle, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, 0);
if (locker == INVALID_HANDLE_VALUE) return -1;
state->locker = locker;
}
DWORD flags = mode == LockExclusive ? LOCKFILE_EXCLUSIVE_LOCK : 0;
if (operation & LockNonblocking) flags |= LOCKFILE_FAIL_IMMEDIATELY;
OVERLAPPED overlapped{};
overlapped.Offset = LockByteLow;
overlapped.OffsetHigh = LockByteHigh;
if (!::LockFileEx(state->locker != nullptr ? state->locker : handle, flags, 0, 1, 0, &overlapped)) return -1;
state->mode = mode;
return 0;
}
void GuestFileLockShare_nid_no_patch(int from, int to) {
std::lock_guard lock(g_mutex);
auto state = Materialize(from);
g_locks[to] = std::move(state);
}
void GuestFileLockForget_nid_no_patch(int fd) {
std::lock_guard lock(g_mutex);
g_locks.erase(fd);
}
}
#endif