2012-07-18 14:59:03 -04:00
|
|
|
#pragma once
|
2023-03-31 23:18:41 -04:00
|
|
|
///@file
|
2003-08-01 14:11:19 +00:00
|
|
|
|
2023-10-25 00:43:36 -04:00
|
|
|
#include "file-descriptor.hh"
|
2006-09-04 21:06:23 +00:00
|
|
|
|
|
|
|
namespace nix {
|
2003-08-01 14:11:19 +00:00
|
|
|
|
2024-04-22 10:08:10 -05:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
|
2017-01-25 12:51:35 +01:00
|
|
|
class PathLocks
|
2003-08-01 14:11:19 +00:00
|
|
|
{
|
|
|
|
private:
|
2024-04-22 10:08:10 -05:00
|
|
|
typedef std::pair<Descriptor, Path> FDPair;
|
2022-02-21 16:25:12 +01:00
|
|
|
std::list<FDPair> fds;
|
2003-11-21 16:05:19 +00:00
|
|
|
bool deletePaths;
|
2003-08-01 14:11:19 +00:00
|
|
|
|
|
|
|
public:
|
2004-05-11 18:05:44 +00:00
|
|
|
PathLocks();
|
2006-06-15 11:56:49 +00:00
|
|
|
PathLocks(const PathSet & paths,
|
2022-02-25 16:00:00 +01:00
|
|
|
const std::string & waitMsg = "");
|
2009-03-23 01:05:54 +00:00
|
|
|
bool lockPaths(const PathSet & _paths,
|
2022-02-25 16:00:00 +01:00
|
|
|
const std::string & waitMsg = "",
|
2009-03-23 01:05:54 +00:00
|
|
|
bool wait = true);
|
2003-08-01 14:11:19 +00:00
|
|
|
~PathLocks();
|
2009-02-16 09:24:20 +00:00
|
|
|
void unlock();
|
2003-11-21 16:05:19 +00:00
|
|
|
void setDeletion(bool deletePaths);
|
2003-08-01 14:11:19 +00:00
|
|
|
};
|
|
|
|
|
2024-04-22 10:08:10 -05:00
|
|
|
struct FdLock
|
|
|
|
{
|
|
|
|
Descriptor desc;
|
|
|
|
bool acquired = false;
|
|
|
|
|
|
|
|
FdLock(Descriptor desc, LockType lockType, bool wait, std::string_view waitMsg);
|
2024-04-17 11:34:09 -04:00
|
|
|
|
2024-04-22 10:08:10 -05:00
|
|
|
~FdLock()
|
|
|
|
{
|
|
|
|
if (acquired)
|
|
|
|
lockFile(desc, ltNone, false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|