From 92eea8fc4e7a2e4d6d0dda604ecd22c60367b76e Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Thu, 23 Oct 2003 10:51:55 +0000 Subject: [PATCH] * Fix a race condition in addTextToStore(). --- src/libnix/store.cc | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/libnix/store.cc b/src/libnix/store.cc index e32f403b6..c83316cf6 100644 --- a/src/libnix/store.cc +++ b/src/libnix/store.cc @@ -302,16 +302,21 @@ void addTextToStore(const Path & dstPath, const string & s) { 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 (fd == -1) throw SysError(format("creating store file `%1%'") % dstPath); + if (!isValidPath(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); - registerValidPath(txn, dstPath); - txn.commit(); + writeFull(fd, (unsigned char *) s.c_str(), s.size()); + + Transaction txn(nixDB); + registerValidPath(txn, dstPath); + txn.commit(); + } } }