From aa165301d1ae3b306319a6a834dc1d4e340a7112 Mon Sep 17 00:00:00 2001 From: Dylan Green <67574902+cidkidnix@users.noreply.github.com> Date: Mon, 22 Apr 2024 10:08:10 -0500 Subject: [PATCH] Pathlocks Implementation for Windows (#10586) Based on Volth's original port. Co-authored-by: volth --- src/build-remote/build-remote.cc | 1 - src/libstore/pathlocks.hh | 34 +++++- src/libstore/unix/lock.cc | 2 - src/libstore/unix/pathlocks-impl.hh | 38 ------- src/libstore/unix/pathlocks.cc | 24 ++--- src/libstore/windows/pathlocks-impl.hh | 2 - src/libstore/windows/pathlocks.cc | 139 ++++++++++++++++++++++++- 7 files changed, 178 insertions(+), 62 deletions(-) delete mode 100644 src/libstore/unix/pathlocks-impl.hh delete mode 100644 src/libstore/windows/pathlocks-impl.hh diff --git a/src/build-remote/build-remote.cc b/src/build-remote/build-remote.cc index 2a4723643..18eee830b 100644 --- a/src/build-remote/build-remote.cc +++ b/src/build-remote/build-remote.cc @@ -22,7 +22,6 @@ #include "experimental-features.hh" using namespace nix; -using namespace nix::unix; using std::cin; static void handleAlarm(int sig) { diff --git a/src/libstore/pathlocks.hh b/src/libstore/pathlocks.hh index b97fbecb9..42a84a1a3 100644 --- a/src/libstore/pathlocks.hh +++ b/src/libstore/pathlocks.hh @@ -5,10 +5,26 @@ namespace nix { +/** + * Open (possibly create) a lock file and return the file descriptor. + * -1 is returned if create is false and the lock could not be opened + * because it doesn't exist. Any other error throws an exception. + */ +AutoCloseFD openLockFile(const Path & path, bool create); + +/** + * Delete an open lock file. + */ +void deleteLockFile(const Path & path, Descriptor desc); + +enum LockType { ltRead, ltWrite, ltNone }; + +bool lockFile(Descriptor desc, LockType lockType, bool wait); + class PathLocks { private: - typedef std::pair FDPair; + typedef std::pair FDPair; std::list fds; bool deletePaths; @@ -24,6 +40,18 @@ public: void setDeletion(bool deletePaths); }; -} +struct FdLock +{ + Descriptor desc; + bool acquired = false; -#include "pathlocks-impl.hh" + FdLock(Descriptor desc, LockType lockType, bool wait, std::string_view waitMsg); + + ~FdLock() + { + if (acquired) + lockFile(desc, ltNone, false); + } +}; + +} diff --git a/src/libstore/unix/lock.cc b/src/libstore/unix/lock.cc index fd7af171f..023c74e34 100644 --- a/src/libstore/unix/lock.cc +++ b/src/libstore/unix/lock.cc @@ -9,8 +9,6 @@ namespace nix { -using namespace nix::unix; - #if __linux__ static std::vector get_group_list(const char *username, gid_t group_id) diff --git a/src/libstore/unix/pathlocks-impl.hh b/src/libstore/unix/pathlocks-impl.hh deleted file mode 100644 index 31fe968bb..000000000 --- a/src/libstore/unix/pathlocks-impl.hh +++ /dev/null @@ -1,38 +0,0 @@ -#pragma once -///@file - -#include "file-descriptor.hh" - -namespace nix::unix { - -/** - * Open (possibly create) a lock file and return the file descriptor. - * -1 is returned if create is false and the lock could not be opened - * because it doesn't exist. Any other error throws an exception. - */ -AutoCloseFD openLockFile(const Path & path, bool create); - -/** - * Delete an open lock file. - */ -void deleteLockFile(const Path & path, int fd); - -enum LockType { ltRead, ltWrite, ltNone }; - -bool lockFile(int fd, LockType lockType, bool wait); - -struct FdLock -{ - int fd; - bool acquired = false; - - FdLock(int fd, LockType lockType, bool wait, std::string_view waitMsg); - - ~FdLock() - { - if (acquired) - lockFile(fd, ltNone, false); - } -}; - -} diff --git a/src/libstore/unix/pathlocks.cc b/src/libstore/unix/pathlocks.cc index 32c1b9ff4..af21319a7 100644 --- a/src/libstore/unix/pathlocks.cc +++ b/src/libstore/unix/pathlocks.cc @@ -14,9 +14,7 @@ namespace nix { -using namespace nix::unix; - -AutoCloseFD unix::openLockFile(const Path & path, bool create) +AutoCloseFD openLockFile(const Path & path, bool create) { AutoCloseFD fd; @@ -28,20 +26,20 @@ AutoCloseFD unix::openLockFile(const Path & path, bool create) } -void unix::deleteLockFile(const Path & path, int fd) +void deleteLockFile(const Path & path, Descriptor desc) { /* Get rid of the lock file. Have to be careful not to introduce races. Write a (meaningless) token to the file to indicate to other processes waiting on this lock that the lock is stale (deleted). */ unlink(path.c_str()); - writeFull(fd, "d"); + writeFull(desc, "d"); /* Note that the result of unlink() is ignored; removing the lock file is an optimisation, not a necessity. */ } -bool unix::lockFile(int fd, LockType lockType, bool wait) +bool lockFile(Descriptor desc, LockType lockType, bool wait) { int type; if (lockType == ltRead) type = LOCK_SH; @@ -50,7 +48,7 @@ bool unix::lockFile(int fd, LockType lockType, bool wait) else abort(); if (wait) { - while (flock(fd, type) != 0) { + while (flock(desc, type) != 0) { checkInterrupt(); if (errno != EINTR) throw SysError("acquiring/releasing lock"); @@ -58,7 +56,7 @@ bool unix::lockFile(int fd, LockType lockType, bool wait) return false; } } else { - while (flock(fd, type | LOCK_NB) != 0) { + while (flock(desc, type | LOCK_NB) != 0) { checkInterrupt(); if (errno == EWOULDBLOCK) return false; if (errno != EINTR) @@ -149,16 +147,16 @@ void PathLocks::unlock() } -FdLock::FdLock(int fd, LockType lockType, bool wait, std::string_view waitMsg) - : fd(fd) +FdLock::FdLock(Descriptor desc, LockType lockType, bool wait, std::string_view waitMsg) + : desc(desc) { if (wait) { - if (!lockFile(fd, lockType, false)) { + if (!lockFile(desc, lockType, false)) { printInfo("%s", waitMsg); - acquired = lockFile(fd, lockType, true); + acquired = lockFile(desc, lockType, true); } } else - acquired = lockFile(fd, lockType, false); + acquired = lockFile(desc, lockType, false); } diff --git a/src/libstore/windows/pathlocks-impl.hh b/src/libstore/windows/pathlocks-impl.hh deleted file mode 100644 index ba3ad28d9..000000000 --- a/src/libstore/windows/pathlocks-impl.hh +++ /dev/null @@ -1,2 +0,0 @@ -#pragma once -///@file Needed because Unix-specific counterpart diff --git a/src/libstore/windows/pathlocks.cc b/src/libstore/windows/pathlocks.cc index ab4294c2a..738057f68 100644 --- a/src/libstore/windows/pathlocks.cc +++ b/src/libstore/windows/pathlocks.cc @@ -1,16 +1,149 @@ #include "logging.hh" #include "pathlocks.hh" +#include "signals.hh" +#include "util.hh" +#include +#include +#include +#include "windows-error.hh" namespace nix { -bool PathLocks::lockPaths(const PathSet & _paths, const std::string & waitMsg, bool wait) +void deleteLockFile(const Path & path, Descriptor desc) { - return true; + + int exit = DeleteFileA(path.c_str()); + if (exit == 0) + warn("%s: &s", path, std::to_string(GetLastError())); } void PathLocks::unlock() { - warn("PathLocks::unlock: not yet implemented"); + for (auto & i : fds) { + if (deletePaths) + deleteLockFile(i.second, i.first); + + if (CloseHandle(i.first) == -1) + printError("error (ignored): cannot close lock file on '%1%'", i.second); + + debug("lock released on '%1%'", i.second); + } + + fds.clear(); +} + +AutoCloseFD openLockFile(const Path & path, bool create) +{ + AutoCloseFD desc = CreateFileA( + path.c_str(), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, + create ? OPEN_ALWAYS : OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_POSIX_SEMANTICS, NULL); + if (desc.get() == INVALID_HANDLE_VALUE) + warn("%s: %s", path, std::to_string(GetLastError())); + + return desc; +} + +bool lockFile(Descriptor desc, LockType lockType, bool wait) +{ + switch (lockType) { + case ltNone: { + OVERLAPPED ov = {0}; + if (!UnlockFileEx(desc, 0, 2, 0, &ov)) { + WinError winError("Failed to unlock file desc %s", desc); + throw winError; + } + return true; + } + case ltRead: { + OVERLAPPED ov = {0}; + if (!LockFileEx(desc, wait ? 0 : LOCKFILE_FAIL_IMMEDIATELY, 0, 1, 0, &ov)) { + WinError winError("Failed to lock file desc %s", desc); + if (winError.lastError == ERROR_LOCK_VIOLATION) + return false; + throw winError; + } + + ov.Offset = 1; + if (!UnlockFileEx(desc, 0, 1, 0, &ov)) { + WinError winError("Failed to unlock file desc %s", desc); + if (winError.lastError != ERROR_NOT_LOCKED) + throw winError; + } + return true; + } + case ltWrite: { + OVERLAPPED ov = {0}; + ov.Offset = 1; + if (!LockFileEx(desc, LOCKFILE_EXCLUSIVE_LOCK | (wait ? 0 : LOCKFILE_FAIL_IMMEDIATELY), 0, 1, 0, &ov)) { + WinError winError("Failed to lock file desc %s", desc); + if (winError.lastError == ERROR_LOCK_VIOLATION) + return false; + throw winError; + } + + ov.Offset = 0; + if (!UnlockFileEx(desc, 0, 1, 0, &ov)) { + WinError winError("Failed to unlock file desc %s", desc); + if (winError.lastError != ERROR_NOT_LOCKED) + throw winError; + } + return true; + } + default: + assert(false); + } +} + +bool PathLocks::lockPaths(const PathSet & paths, const std::string & waitMsg, bool wait) +{ + assert(fds.empty()); + + for (auto & path : paths) { + checkInterrupt(); + Path lockPath = path + ".lock"; + debug("locking path '%1%'", path); + + AutoCloseFD fd; + + while (1) { + fd = openLockFile(lockPath, true); + if (!lockFile(fd.get(), ltWrite, false)) { + if (wait) { + if (waitMsg != "") + printError(waitMsg); + lockFile(fd.get(), ltWrite, true); + } else { + unlock(); + return false; + } + } + + debug("lock aquired on '%1%'", lockPath); + + struct _stat st; + if (_fstat(fromDescriptorReadOnly(fd.get()), &st) == -1) + throw SysError("statting lock file '%1%'", lockPath); + if (st.st_size != 0) + debug("open lock file '%1%' has become stale", lockPath); + else + break; + } + + fds.push_back(FDPair(fd.release(), lockPath)); + } + return true; +} + +FdLock::FdLock(Descriptor desc, LockType lockType, bool wait, std::string_view waitMsg) + : desc(desc) +{ + if (wait) { + if (!lockFile(desc, lockType, false)) { + printInfo("%s", waitMsg); + acquired = lockFile(desc, lockType, true); + } + } else + acquired = lockFile(desc, lockType, false); } }