about summary refs log tree commit diff
path: root/src/libstore/gc.cc
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2011-12-22T15·55+0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2011-12-22T15·55+0000
commitb33da599c5c1b06a32a3eeac58f95481d10f821d (patch)
treeb3f33d5a7a86becd1485ae0de06d88b6eed7729c /src/libstore/gc.cc
parent58d974336c733416756e5b396928602ea8ed8df2 (diff)
* In the garbage collector, delete invalid paths before deleting
  unreachable paths.  This matters when using --max-freed etc.:
  unreachable paths could become reachable again, so it's nicer to
  keep them if there is "real" garbage to be deleted.  Also, don't use
  readDirectory() but read the Nix store and delete invalid paths in
  parallel.  This reduces GC latency on very large Nix stores.

Diffstat (limited to 'src/libstore/gc.cc')
-rw-r--r--src/libstore/gc.cc46
1 files changed, 35 insertions, 11 deletions
diff --git a/src/libstore/gc.cc b/src/libstore/gc.cc
index a99bb1a81ced..20f194e6e2a7 100644
--- a/src/libstore/gc.cc
+++ b/src/libstore/gc.cc
@@ -617,27 +617,51 @@ void LocalStore::collectGarbage(const GCOptions & options, GCResults & results)
         
     } else {
         
-        printMsg(lvlError, format("reading the Nix store..."));
-        Paths entries = readDirectory(nixStore);
-
-        /* Randomise the order in which we delete entries to make the
-           collector less biased towards deleting paths that come
-           alphabetically first (e.g. /nix/store/000...).  This
-           matters when using --max-freed etc. */
-        vector<Path> entries_(entries.begin(), entries.end());
-        random_shuffle(entries_.begin(), entries_.end());
-
         if (shouldDelete(state.options.action))
             printMsg(lvlError, format("deleting garbage..."));
         else
             printMsg(lvlError, format("determining live/dead paths..."));
     
         try {
+
+            AutoCloseDir dir = opendir(nixStore.c_str());
+            if (!dir) throw SysError(format("opening directory `%1%'") % nixStore);
+
+            /* Read the store and immediately delete all paths that
+               aren't valid.  When using --max-freed etc., deleting
+               invalid paths is preferred over deleting unreachable
+               paths, since unreachable paths could become reachable
+               again.  We don't use readDirectory() here so that GCing
+               can start faster. */
+            Paths entries;
+            struct dirent * dirent;
+            while (errno = 0, dirent = readdir(dir)) {
+                checkInterrupt();
+                string name = dirent->d_name;
+                if (name == "." || name == "..") continue;
+                Path path = nixStore + "/" + name;
+                if (isValidPath(path))
+                    entries.push_back(path);
+                else
+                    tryToDelete(state, path);
+            }
+
+	    dir.close();
+
+            /* Now delete the unreachable valid paths.  Randomise the
+               order in which we delete entries to make the collector
+               less biased towards deleting paths that come
+               alphabetically first (e.g. /nix/store/000...).  This
+               matters when using --max-freed etc. */
+            vector<Path> entries_(entries.begin(), entries.end());
+            random_shuffle(entries_.begin(), entries_.end());
+
             foreach (vector<Path>::iterator, i, entries_)
                 tryToDelete(state, nixStore + "/" + *i);
+
         } catch (GCLimitReached & e) {
         }
-    }        
+    }
 }