fix(libc): make flock advisory on Windows so other descriptors can read and write

This commit is contained in:
David 2026-10-13 09:44:23 +04:00 committed by milescoviello
parent 30f961266a
commit 32fdaa65ae
1 changed files with 8 additions and 2 deletions

View File

@ -15,6 +15,8 @@ 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;
@ -55,14 +57,18 @@ int GuestFileLock_nid_no_patch(int fd, int operation) {
if (state->mode == mode) return 0;
if (state->mode != 0) {
OVERLAPPED overlapped{};
if (!::UnlockFileEx(handle, 0, MAXDWORD, MAXDWORD, &overlapped)) return -1;
overlapped.Offset = LockByteLow;
overlapped.OffsetHigh = LockByteHigh;
if (!::UnlockFileEx(handle, 0, 1, 0, &overlapped)) return -1;
state->mode = 0;
}
if (mode == LockUnlock) return 0;
DWORD flags = mode == LockExclusive ? LOCKFILE_EXCLUSIVE_LOCK : 0;
if (operation & LockNonblocking) flags |= LOCKFILE_FAIL_IMMEDIATELY;
OVERLAPPED overlapped{};
if (!::LockFileEx(handle, flags, 0, MAXDWORD, MAXDWORD, &overlapped)) return -1;
overlapped.Offset = LockByteLow;
overlapped.OffsetHigh = LockByteHigh;
if (!::LockFileEx(handle, flags, 0, 1, 0, &overlapped)) return -1;
state->mode = mode;
return 0;
}