about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEelco Dolstra <eelco.dolstra@logicblox.com>2014-08-01T15·20+0200
committerEelco Dolstra <eelco.dolstra@logicblox.com>2014-08-01T15·20+0200
commite0d7d0e45c5652bccc160ec49fc0c2c26d9c4d4e (patch)
treea8fd254bdea1c915d28f7fc3cf8b181b58e581df
parentdaf3f2c11ff467b600473a2fda7bd513aacc1efa (diff)
findRoots(): Prevent a call to lstat()
This means that getting the roots from /nix/var/nix/.../hydra-roots
doesn't need any I/O other than reading the directory.
-rw-r--r--src/libstore/gc.cc23
1 files changed, 14 insertions, 9 deletions
diff --git a/src/libstore/gc.cc b/src/libstore/gc.cc
index df6f26c776c5..e5836150c7b3 100644
--- a/src/libstore/gc.cc
+++ b/src/libstore/gc.cc
@@ -294,18 +294,23 @@ static void foundRoot(StoreAPI & store,
 }
 
 
-static void findRoots(StoreAPI & store, const Path & path, Roots & roots)
+static void findRoots(StoreAPI & store, const Path & path, unsigned char type, Roots & roots)
 {
     try {
 
-        struct stat st = lstat(path);
+        if (type == DT_UNKNOWN) {
+            struct stat st = lstat(path);
+            if (S_ISDIR(st.st_mode)) type = DT_DIR;
+            else if (S_ISLNK(st.st_mode)) type = DT_LNK;
+            else if (S_ISREG(st.st_mode)) type = DT_REG;
+        }
 
-        if (S_ISDIR(st.st_mode)) {
+        if (type == DT_DIR) {
             for (auto & i : readDirectory(path))
-                findRoots(store, path + "/" + i.name, roots);
+                findRoots(store, path + "/" + i.name, i.type, roots);
         }
 
-        else if (S_ISLNK(st.st_mode)) {
+        else if (type == DT_LNK) {
             Path target = readLink(path);
             if (isInStore(target))
                 foundRoot(store, path, target, roots);
@@ -327,7 +332,7 @@ static void findRoots(StoreAPI & store, const Path & path, Roots & roots)
             }
         }
 
-        else if (S_ISREG(st.st_mode)) {
+        else if (type == DT_REG) {
             Path storePath = settings.nixStore + "/" + baseNameOf(path);
             if (store.isValidPath(storePath))
                 roots[path] = storePath;
@@ -350,9 +355,9 @@ Roots LocalStore::findRoots()
     Roots roots;
 
     /* Process direct roots in {gcroots,manifests,profiles}. */
-    nix::findRoots(*this, settings.nixStateDir + "/" + gcRootsDir, roots);
-    nix::findRoots(*this, settings.nixStateDir + "/manifests", roots);
-    nix::findRoots(*this, settings.nixStateDir + "/profiles", roots);
+    nix::findRoots(*this, settings.nixStateDir + "/" + gcRootsDir, DT_UNKNOWN, roots);
+    nix::findRoots(*this, settings.nixStateDir + "/manifests", DT_UNKNOWN, roots);
+    nix::findRoots(*this, settings.nixStateDir + "/profiles", DT_UNKNOWN, roots);
 
     return roots;
 }