Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
pathlocks.hh
Go to the documentation of this file.
1#pragma once
3
4#include <filesystem>
5
7
8namespace nix {
9
15AutoCloseFD openLockFile(const std::filesystem::path & path, bool create);
16
20void deleteLockFile(const std::filesystem::path & path, Descriptor desc);
21
22enum LockType { ltRead, ltWrite, ltNone };
23
24bool lockFile(Descriptor desc, LockType lockType, bool wait);
25
26class PathLocks
27{
28private:
29 typedef std::pair<Descriptor, std::filesystem::path> FDPair;
30 std::list<FDPair> fds;
31 bool deletePaths;
32
33public:
34 PathLocks();
35 PathLocks(const std::set<std::filesystem::path> & paths, const std::string & waitMsg = "");
36
37 PathLocks(PathLocks && other) noexcept
38 : fds(std::exchange(other.fds, {}))
39 , deletePaths(other.deletePaths)
40 {
41 }
42
43 PathLocks & operator=(PathLocks && other) noexcept
44 {
45 fds = std::exchange(other.fds, {});
46 deletePaths = other.deletePaths;
47 return *this;
48 }
49
50 PathLocks(const PathLocks &) = delete;
51 PathLocks & operator=(const PathLocks &) = delete;
52 bool lockPaths(const std::set<std::filesystem::path> & _paths, const std::string & waitMsg = "", bool wait = true);
53 ~PathLocks();
54 void unlock();
55 void setDeletion(bool deletePaths);
56};
57
58struct FdLock
59{
60 Descriptor desc;
61 bool acquired = false;
62
63 FdLock(Descriptor desc, LockType lockType, bool wait, std::string_view waitMsg);
64 FdLock(const FdLock &) = delete;
65 FdLock & operator=(const FdLock &) = delete;
66 FdLock(FdLock &&) = delete;
67 FdLock & operator=(FdLock &&) = delete;
68
69 ~FdLock()
70 {
71 if (acquired)
72 lockFile(desc, ltNone, false);
73 }
74};
75
76} // namespace nix
Definition file-descriptor.hh:265
File descriptor operations for almost arbitrary file descriptors.
int Descriptor
Definition file-descriptor.hh:36
AutoCloseFD openLockFile(const std::filesystem::path &path, bool create)
Definition pathlocks.cc:16
void deleteLockFile(const std::filesystem::path &path, Descriptor desc)
Definition pathlocks.cc:27