@@ -18,7 +18,6 @@ namespace nix {
1818
1919
2020static string gcLockName = " gc.lock" ;
21- static string tempRootsDir = " temproots" ;
2221static string gcRootsDir = " gcroots" ;
2322
2423
@@ -153,30 +152,25 @@ void LocalStore::addTempRoot(const Path & path)
153152 if (!state->fdTempRoots ) {
154153
155154 while (1 ) {
156- Path dir = (format (" %1%/%2%" ) % stateDir % tempRootsDir).str ();
157- createDirs (dir);
158-
159- state->fnTempRoots = (format (" %1%/%2%" ) % dir % getpid ()).str ();
160-
161155 AutoCloseFD fdGCLock = openGCLock (ltRead);
162156
163- if (pathExists (state-> fnTempRoots ))
157+ if (pathExists (fnTempRoots))
164158 /* It *must* be stale, since there can be no two
165159 processes with the same pid. */
166- unlink (state-> fnTempRoots .c_str ());
160+ unlink (fnTempRoots.c_str ());
167161
168- state->fdTempRoots = openLockFile (state-> fnTempRoots , true );
162+ state->fdTempRoots = openLockFile (fnTempRoots, true );
169163
170164 fdGCLock = -1 ;
171165
172- debug (format (" acquiring read lock on '%1%'" ) % state-> fnTempRoots );
166+ debug (format (" acquiring read lock on '%1%'" ) % fnTempRoots);
173167 lockFile (state->fdTempRoots .get (), ltRead, true );
174168
175169 /* Check whether the garbage collector didn't get in our
176170 way. */
177171 struct stat st;
178172 if (fstat (state->fdTempRoots .get (), &st) == -1 )
179- throw SysError (format (" statting '%1%'" ) % state-> fnTempRoots );
173+ throw SysError (format (" statting '%1%'" ) % fnTempRoots);
180174 if (st.st_size == 0 ) break ;
181175
182176 /* The garbage collector deleted this file before we could
@@ -188,14 +182,14 @@ void LocalStore::addTempRoot(const Path & path)
188182
189183 /* Upgrade the lock to a write lock. This will cause us to block
190184 if the garbage collector is holding our lock. */
191- debug (format (" acquiring write lock on '%1%'" ) % state-> fnTempRoots );
185+ debug (format (" acquiring write lock on '%1%'" ) % fnTempRoots);
192186 lockFile (state->fdTempRoots .get (), ltWrite, true );
193187
194188 string s = path + ' \0 ' ;
195189 writeFull (state->fdTempRoots .get (), s);
196190
197191 /* Downgrade to a read lock. */
198- debug (format (" downgrading to read lock on '%1%'" ) % state-> fnTempRoots );
192+ debug (format (" downgrading to read lock on '%1%'" ) % fnTempRoots);
199193 lockFile (state->fdTempRoots .get (), ltRead, true );
200194}
201195
@@ -204,11 +198,10 @@ void LocalStore::readTempRoots(PathSet & tempRoots, FDs & fds)
204198{
205199 /* Read the `temproots' directory for per-process temporary root
206200 files. */
207- DirEntries tempRootFiles = readDirectory (
208- (format (" %1%/%2%" ) % stateDir % tempRootsDir).str ());
201+ DirEntries tempRootFiles = readDirectory (tempRootsDir);
209202
210203 for (auto & i : tempRootFiles) {
211- Path path = ( format ( " %1%/%2%/%3% " ) % stateDir % tempRootsDir % i.name ). str () ;
204+ Path path = tempRootsDir + " / " + i.name ;
212205
213206 debug (format (" reading temporary root file '%1%'" ) % path);
214207 FDPtr fd (new AutoCloseFD (open (path.c_str (), O_CLOEXEC | O_RDWR, 0666 )));
@@ -222,21 +215,25 @@ void LocalStore::readTempRoots(PathSet & tempRoots, FDs & fds)
222215 // FDPtr fd(new AutoCloseFD(openLockFile(path, false)));
223216 // if (*fd == -1) continue;
224217
225- /* Try to acquire a write lock without blocking. This can
226- only succeed if the owning process has died. In that case
227- we don't care about its temporary roots. */
228- if (lockFile (fd->get (), ltWrite, false )) {
229- printError (format (" removing stale temporary roots file '%1%'" ) % path);
230- unlink (path.c_str ());
231- writeFull (fd->get (), " d" );
232- continue ;
233- }
218+ if (path != fnTempRoots) {
234219
235- /* Acquire a read lock. This will prevent the owning process
236- from upgrading to a write lock, therefore it will block in
237- addTempRoot(). */
238- debug (format (" waiting for read lock on '%1%'" ) % path);
239- lockFile (fd->get (), ltRead, true );
220+ /* Try to acquire a write lock without blocking. This can
221+ only succeed if the owning process has died. In that case
222+ we don't care about its temporary roots. */
223+ if (lockFile (fd->get (), ltWrite, false )) {
224+ printError (format (" removing stale temporary roots file '%1%'" ) % path);
225+ unlink (path.c_str ());
226+ writeFull (fd->get (), " d" );
227+ continue ;
228+ }
229+
230+ /* Acquire a read lock. This will prevent the owning process
231+ from upgrading to a write lock, therefore it will block in
232+ addTempRoot(). */
233+ debug (format (" waiting for read lock on '%1%'" ) % path);
234+ lockFile (fd->get (), ltRead, true );
235+
236+ }
240237
241238 /* Read the entire file. */
242239 string contents = readFile (fd->get ());
0 commit comments