1 #pragma once
2 
3 #include "util.hh"
4 
5 namespace nix {
6 
7 /* Open (possibly create) a lock file and return the file descriptor.
8    -1 is returned if create is false and the lock could not be opened
9    because it doesn't exist.  Any other error throws an exception. */
10 AutoCloseFD openLockFile(const Path & path, bool create);
11 
12 /* Delete an open lock file. */
13 void deleteLockFile(const Path & path, int fd);
14 
15 enum LockType { ltRead, ltWrite, ltNone };
16 
17 bool lockFile(int fd, LockType lockType, bool wait);
18 
19 class PathLocks
20 {
21 private:
22     typedef std::pair<int, Path> FDPair;
23     list<FDPair> fds;
24     bool deletePaths;
25 
26 public:
27     PathLocks();
28     PathLocks(const PathSet & paths,
29         const string & waitMsg = "");
30     bool lockPaths(const PathSet & _paths,
31         const string & waitMsg = "",
32         bool wait = true);
33     ~PathLocks();
34     void unlock();
35     void setDeletion(bool deletePaths);
36 };
37 
38 }
39