* Fix a race condition in addTextToStore().

This commit is contained in:
Eelco Dolstra 2003-10-23 10:51:55 +00:00
parent c4e7d324b8
commit 92eea8fc4e
1 changed files with 12 additions and 7 deletions

View File

@ -302,16 +302,21 @@ void addTextToStore(const Path & dstPath, const string & s)
{ {
if (!isValidPath(dstPath)) { if (!isValidPath(dstPath)) {
/* !!! locking? -> parallel writes are probably idempotent */ PathSet lockPaths;
lockPaths.insert(dstPath);
PathLocks outputLock(lockPaths);
AutoCloseFD fd = open(dstPath.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0666); if (!isValidPath(dstPath)) {
if (fd == -1) throw SysError(format("creating store file `%1%'") % dstPath);
writeFull(fd, (unsigned char *) s.c_str(), s.size()); AutoCloseFD fd = open(dstPath.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0666);
if (fd == -1) throw SysError(format("creating store file `%1%'") % dstPath);
Transaction txn(nixDB); writeFull(fd, (unsigned char *) s.c_str(), s.size());
registerValidPath(txn, dstPath);
txn.commit(); Transaction txn(nixDB);
registerValidPath(txn, dstPath);
txn.commit();
}
} }
} }