about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorGuillaume Maudoux <layus.on@gmail.com>2019-03-09T23·37+0100
committerGuillaume Maudoux <layus.on@gmail.com>2019-03-09T23·56+0100
commit38ee16ae9c277b0512559282c950c48c71e27697 (patch)
tree59e9201c82d510a111a7a181158905e0cb1e742a /src
parent9d7221183a6825d798267288ebe7d3e2adcd8cde (diff)
Unify internal findRootsXxx() api
Diffstat (limited to 'src')
-rw-r--r--src/libstore/gc.cc35
-rw-r--r--src/libstore/local-store.hh4
2 files changed, 15 insertions, 24 deletions
diff --git a/src/libstore/gc.cc b/src/libstore/gc.cc
index da6799f6fc6f..2a314035436f 100644
--- a/src/libstore/gc.cc
+++ b/src/libstore/gc.cc
@@ -197,10 +197,8 @@ void LocalStore::addTempRoot(const Path & path)
 }
 
 
-std::set<std::pair<pid_t, Path>> LocalStore::readTempRoots(FDs & fds)
+void LocalStore::findTempRoots(FDs & fds, Roots & tempRoots)
 {
-    std::set<std::pair<pid_t, Path>> tempRoots;
-
     /* Read the `temproots' directory for per-process temporary root
        files. */
     for (auto & i : readDirectory(tempRootsDir)) {
@@ -250,14 +248,12 @@ std::set<std::pair<pid_t, Path>> LocalStore::readTempRoots(FDs & fds)
             Path root(contents, pos, end - pos);
             debug("got temporary root '%s'", root);
             assertStorePath(root);
-            tempRoots.emplace(pid, root);
+            tempRoots[root].emplace(fmt("{temp:%d}", pid));
             pos = end + 1;
         }
 
         fds.push_back(fd); /* keep open */
     }
-
-    return tempRoots;
 }
 
 
@@ -321,10 +317,8 @@ void LocalStore::findRoots(const Path & path, unsigned char type, Roots & roots)
 }
 
 
-Roots LocalStore::findRootsNoTemp()
+void LocalStore::findRootsNoTemp(Roots & roots)
 {
-    Roots roots;
-
     /* Process direct roots in {gcroots,profiles}. */
     findRoots(stateDir + "/" + gcRootsDir, DT_UNKNOWN, roots);
     findRoots(stateDir + "/profiles", DT_UNKNOWN, roots);
@@ -334,23 +328,16 @@ Roots LocalStore::findRootsNoTemp()
        to add running programs to the set of roots (to prevent them
        from being garbage collected). */
     findRuntimeRoots(roots);
-
-    return roots;
 }
 
 
 Roots LocalStore::findRoots()
 {
-    Roots roots = findRootsNoTemp();
+    Roots roots;
+    findRootsNoTemp(roots);
 
     FDs fds;
-    pid_t prev = -1;
-    size_t n = 0;
-    for (auto & [pid, root] : readTempRoots(fds)) {
-        if (prev != pid) n = 0;
-        prev = pid;
-        roots[root].emplace(fmt("{temp:%d:%d}", pid, n++));
-    }
+    findTempRoots(fds, roots);
 
     return roots;
 }
@@ -752,7 +739,9 @@ void LocalStore::collectGarbage(const GCOptions & options, GCResults & results)
     /* Find the roots.  Since we've grabbed the GC lock, the set of
        permanent roots cannot increase now. */
     printError(format("finding garbage collector roots..."));
-    Roots rootMap = options.ignoreLiveness ? Roots() : findRootsNoTemp();
+    Roots rootMap;
+    if (!options.ignoreLiveness)
+       findRootsNoTemp(rootMap);
 
     for (auto & i : rootMap) state.roots.insert(i.first);
 
@@ -760,8 +749,10 @@ void LocalStore::collectGarbage(const GCOptions & options, GCResults & results)
        per-process temporary root files.  So after this point no paths
        can be added to the set of temporary roots. */
     FDs fds;
-    for (auto & root : readTempRoots(fds))
-        state.tempRoots.insert(root.second);
+    Roots tempRoots;
+    findTempRoots(fds, tempRoots);
+    for (auto & root : tempRoots)
+        state.tempRoots.insert(root.first);
     state.roots.insert(state.tempRoots.begin(), state.tempRoots.end());
 
     /* After this point the set of roots or temporary roots cannot
diff --git a/src/libstore/local-store.hh b/src/libstore/local-store.hh
index 730d4c917d34..39a34597e2b4 100644
--- a/src/libstore/local-store.hh
+++ b/src/libstore/local-store.hh
@@ -180,7 +180,7 @@ private:
     typedef std::shared_ptr<AutoCloseFD> FDPtr;
     typedef list<FDPtr> FDs;
 
-    std::set<std::pair<pid_t, Path>> readTempRoots(FDs & fds);
+    void findTempRoots(FDs & fds, Roots & roots);
 
 public:
 
@@ -267,7 +267,7 @@ private:
 
     void findRoots(const Path & path, unsigned char type, Roots & roots);
 
-    Roots findRootsNoTemp();
+    void findRootsNoTemp(Roots & roots);
 
     void findRuntimeRoots(Roots & roots);