nix-collect-garbage: Don't barf on unreadable directories

And don't try to delete generations from unwritable directories.
This commit is contained in:
Eelco Dolstra 2015-05-21 15:04:05 +02:00
parent 8d813fe3e0
commit 4441e4cc13

View file

@ -34,21 +34,23 @@ void runProgramSimple(Path program, const Strings & args)
void removeOldGenerations(std::string dir) 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)) { for (auto & i : readDirectory(dir)) {
checkInterrupt(); checkInterrupt();
auto path = dir + "/" + i.name; auto path = dir + "/" + i.name;
auto type = i.type == DT_UNKNOWN ? getFileType(path) : i.type; auto type = i.type == DT_UNKNOWN ? getFileType(path) : i.type;
if (type == DT_LNK) { if (type == DT_LNK && canWrite) {
auto link = readLink(path); auto link = readLink(path);
if (link.find("link") != string::npos) { if (link.find("link") != string::npos) {
printMsg(lvlInfo, format("removing old generations of profile %1%") % path); printMsg(lvlInfo, format("removing old generations of profile %1%") % path);
auto args = Strings{"-p", path, "--delete-generations", gen}; auto args = Strings{"-p", path, "--delete-generations", gen};
if (dryRun) { if (dryRun) args.push_back("--dry-run");
args.push_back("--dry-run");
}
runProgramSimple(settings.nixBinDir + "/nix-env", args); runProgramSimple(settings.nixBinDir + "/nix-env", args);
} }
} else if (type == DT_DIR) { } else if (type == DT_DIR) {