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
|
|
|
|
2023-04-07 16:55:28 +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.
|
|
|
|
*/
|
2017-01-25 13:51:35 +02:00
|
|
|
AutoCloseFD openLockFile(const Path & path, bool create);
|
2006-06-20 20:48:10 +03:00
|
|
|
|
2023-04-07 16:55:28 +03:00
|
|
|
/**
|
|
|
|
* Delete an open lock file.
|
|
|
|
*/
|
2010-02-02 17:28:36 +02:00
|
|
|
void deleteLockFile(const Path & path, int fd);
|
2006-06-20 20:48:10 +03:00
|
|
|
|
2008-05-21 14:17:31 +03:00
|
|
|
enum LockType { ltRead, ltWrite, ltNone };
|
2003-10-14 18:33:00 +03:00
|
|
|
|
2010-02-03 23:38:41 +02:00
|
|
|
bool lockFile(int fd, LockType lockType, bool wait);
|
2003-10-14 18:33:00 +03:00
|
|
|
|
2017-01-25 13:51:35 +02:00
|
|
|
class PathLocks
|
2003-08-01 17:11:19 +03:00
|
|
|
{
|
|
|
|
private:
|
2006-09-05 00:06:23 +03:00
|
|
|
typedef std::pair<int, 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
|
|
|
};
|
|
|
|
|
2021-08-16 21:03:32 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2006-09-05 00:06:23 +03:00
|
|
|
}
|