about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorEelco Dolstra <eelco.dolstra@logicblox.com>2015-05-21T13·04+0200
committerEelco Dolstra <eelco.dolstra@logicblox.com>2015-05-21T13·04+0200
commit4441e4cc13ded59e1a57c3d47e7920dd6a3053fa (patch)
tree80f7518b48f02100ede70dd91f7fcca6afe16c62 /src
parent8d813fe3e0c2c6c4236d03f8271c21ffce1dea6f (diff)
nix-collect-garbage: Don't barf on unreadable directories
And don't try to delete generations from unwritable directories.
Diffstat (limited to 'src')
-rw-r--r--src/nix-collect-garbage/nix-collect-garbage.cc12
1 files changed, 7 insertions, 5 deletions
diff --git a/src/nix-collect-garbage/nix-collect-garbage.cc b/src/nix-collect-garbage/nix-collect-garbage.cc
index d8ddf9ec4c4c..a8f6c03c2377 100644
--- a/src/nix-collect-garbage/nix-collect-garbage.cc
+++ b/src/nix-collect-garbage/nix-collect-garbage.cc
@@ -34,21 +34,23 @@ void runProgramSimple(Path program, const Strings & args)
 
 void removeOldGenerations(std::string dir)
 {
+    if (access(dir.c_str(), R_OK) != 0) return;
+
+    bool canWrite = access(dir.c_str(), W_OK) == 0;
+
     for (auto & i : readDirectory(dir)) {
         checkInterrupt();
 
-        auto path = dir + "/" + i.name; 
+        auto path = dir + "/" + i.name;
         auto type = i.type == DT_UNKNOWN ? getFileType(path) : i.type;
 
-        if (type == DT_LNK) {
+        if (type == DT_LNK && canWrite) {
             auto link = readLink(path);
             if (link.find("link") != string::npos) {
                 printMsg(lvlInfo, format("removing old generations of profile %1%") % path);
 
                 auto args = Strings{"-p", path, "--delete-generations", gen};
-                if (dryRun) {
-                    args.push_back("--dry-run");
-                }
+                if (dryRun) args.push_back("--dry-run");
                 runProgramSimple(settings.nixBinDir + "/nix-env", args);
             }
         } else if (type == DT_DIR) {