From 4441e4cc13ded59e1a57c3d47e7920dd6a3053fa Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Thu, 21 May 2015 15:04:05 +0200 Subject: [PATCH] nix-collect-garbage: Don't barf on unreadable directories And don't try to delete generations from unwritable directories. --- src/nix-collect-garbage/nix-collect-garbage.cc | 12 +++++++----- 1 file 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 d8ddf9ec4..a8f6c03c2 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) {