* Defensive programming against POSIX locking idiocy.

* Simplified realiseSlice().
This commit is contained in:
Eelco Dolstra 2003-08-01 15:06:23 +00:00
parent 545145cd58
commit d99d04e644
3 changed files with 18 additions and 25 deletions

View file

@ -240,35 +240,10 @@ void realiseSlice(const FSId & id, FSIdSet pending)
if (fs.type != FState::fsSlice) if (fs.type != FState::fsSlice)
throw Error(format("expected slice in %1%") % (string) id); throw Error(format("expected slice in %1%") % (string) id);
/* Perhaps all paths already contain the right id? */
bool missing = false;
for (SliceElems::const_iterator i = fs.slice.elems.begin(); for (SliceElems::const_iterator i = fs.slice.elems.begin();
i != fs.slice.elems.end(); i++) i != fs.slice.elems.end(); i++)
{ {
SliceElem elem = *i; SliceElem elem = *i;
string id;
if (!nixDB.queryString(noTxn, dbPath2Id, elem.path, id)) {
if (pathExists(elem.path))
throw Error(format("path `%1%' obstructed") % elem.path);
missing = true;
break;
}
if (parseHash(id) != elem.id)
throw Error(format("path `%1%' obstructed") % elem.path);
}
if (!missing) {
debug(format("already installed"));
return;
}
/* For each element, expand its id at its path. */
for (SliceElems::const_iterator i = fs.slice.elems.begin();
i != fs.slice.elems.end(); i++)
{
SliceElem elem = *i;
debug(format("expanding %1% in `%2%'") % (string) elem.id % elem.path);
expandId(elem.id, elem.path, "/", pending); expandId(elem.id, elem.path, "/", pending);
} }
} }

View file

@ -3,6 +3,14 @@
#include "pathlocks.hh" #include "pathlocks.hh"
/* This enables us to check whether are not already holding a lock on
a file ourselves. POSIX locks (fcntl) suck in this respect: if we
close a descriptor, the previous lock will be closed as well. And
there is no way to query whether we already have a lock (F_GETLK
only works on locks held by other processes). */
static StringSet lockedPaths; /* !!! not thread-safe */
PathLocks::PathLocks(const Strings & _paths) PathLocks::PathLocks(const Strings & _paths)
{ {
/* Note that `fds' is built incrementally so that the destructor /* Note that `fds' is built incrementally so that the destructor
@ -20,12 +28,16 @@ PathLocks::PathLocks(const Strings & _paths)
debug(format("locking path `%1%'") % path); debug(format("locking path `%1%'") % path);
if (lockedPaths.find(lockPath) != lockedPaths.end())
throw Error(format("already holding lock on `%1%'") % lockPath);
/* Open/create the lock file. */ /* Open/create the lock file. */
int fd = open(lockPath.c_str(), O_WRONLY | O_CREAT, 0666); int fd = open(lockPath.c_str(), O_WRONLY | O_CREAT, 0666);
if (fd == -1) if (fd == -1)
throw SysError(format("opening lock file `%1%'") % lockPath); throw SysError(format("opening lock file `%1%'") % lockPath);
fds.push_back(fd); fds.push_back(fd);
this->paths.push_back(lockPath);
/* Lock it. */ /* Lock it. */
struct flock lock; struct flock lock;
@ -37,6 +49,8 @@ PathLocks::PathLocks(const Strings & _paths)
while (fcntl(fd, F_SETLKW, &lock) == -1) while (fcntl(fd, F_SETLKW, &lock) == -1)
if (errno != EINTR) if (errno != EINTR)
throw SysError(format("acquiring lock on `%1%'") % lockPath); throw SysError(format("acquiring lock on `%1%'") % lockPath);
lockedPaths.insert(lockPath);
} }
} }
@ -45,4 +59,7 @@ PathLocks::~PathLocks()
{ {
for (list<int>::iterator i = fds.begin(); i != fds.end(); i++) for (list<int>::iterator i = fds.begin(); i != fds.end(); i++)
close(*i); close(*i);
for (Strings::iterator i = paths.begin(); i != paths.end(); i++)
lockedPaths.erase(*i);
} }

View file

@ -8,6 +8,7 @@ class PathLocks
{ {
private: private:
list<int> fds; list<int> fds;
Strings paths;
public: public:
PathLocks(const Strings & _paths); PathLocks(const Strings & _paths);