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