2020-05-17 17:31:57 +02:00
|
|
|
#include <cerrno>
|
|
|
|
#include <iostream>
|
2020-05-19 16:54:39 +02:00
|
|
|
|
|
|
|
#include <glog/logging.h>
|
|
|
|
|
2015-04-22 14:21:52 +02:00
|
|
|
#include "globals.hh"
|
2018-10-26 11:35:46 +02:00
|
|
|
#include "legacy.hh"
|
2020-05-17 17:31:57 +02:00
|
|
|
#include "profiles.hh"
|
|
|
|
#include "shared.hh"
|
|
|
|
#include "store-api.hh"
|
2015-04-22 14:21:52 +02:00
|
|
|
|
|
|
|
using namespace nix;
|
|
|
|
|
2015-05-21 16:26:03 +02:00
|
|
|
std::string deleteOlderThan;
|
2015-04-22 14:21:52 +02:00
|
|
|
bool dryRun = false;
|
|
|
|
|
|
|
|
/* If `-d' was specified, remove all old generations of all profiles.
|
|
|
|
* Of course, this makes rollbacks to before this point in time
|
|
|
|
* impossible. */
|
|
|
|
|
2020-05-17 17:31:57 +02:00
|
|
|
void removeOldGenerations(std::string dir) {
|
2020-05-19 19:55:58 +02:00
|
|
|
if (access(dir.c_str(), R_OK) != 0) {
|
|
|
|
return;
|
|
|
|
}
|
2020-05-17 17:31:57 +02:00
|
|
|
|
|
|
|
bool canWrite = access(dir.c_str(), W_OK) == 0;
|
|
|
|
|
|
|
|
for (auto& i : readDirectory(dir)) {
|
|
|
|
checkInterrupt();
|
|
|
|
|
|
|
|
auto path = dir + "/" + i.name;
|
|
|
|
auto type = i.type == DT_UNKNOWN ? getFileType(path) : i.type;
|
|
|
|
|
|
|
|
if (type == DT_LNK && canWrite) {
|
|
|
|
std::string link;
|
|
|
|
try {
|
|
|
|
link = readLink(path);
|
|
|
|
} catch (SysError& e) {
|
2020-05-19 19:55:58 +02:00
|
|
|
if (e.errNo == ENOENT) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-05-17 17:31:57 +02:00
|
|
|
}
|
|
|
|
if (link.find("link") != string::npos) {
|
2020-05-19 05:52:47 +02:00
|
|
|
LOG(INFO) << "removing old generations of profile " << path;
|
2020-05-19 21:47:23 +02:00
|
|
|
if (deleteOlderThan != "") {
|
2020-05-17 17:31:57 +02:00
|
|
|
deleteGenerationsOlderThan(path, deleteOlderThan, dryRun);
|
2020-05-19 21:47:23 +02:00
|
|
|
} else {
|
2020-05-17 17:31:57 +02:00
|
|
|
deleteOldGenerations(path, dryRun);
|
2020-05-19 21:47:23 +02:00
|
|
|
}
|
2020-05-17 17:31:57 +02:00
|
|
|
}
|
|
|
|
} else if (type == DT_DIR) {
|
|
|
|
removeOldGenerations(path);
|
2015-04-22 14:21:52 +02:00
|
|
|
}
|
2020-05-17 17:31:57 +02:00
|
|
|
}
|
2015-04-22 14:21:52 +02:00
|
|
|
}
|
|
|
|
|
2020-05-17 17:31:57 +02:00
|
|
|
static int _main(int argc, char** argv) {
|
|
|
|
{
|
|
|
|
bool removeOld = false;
|
|
|
|
|
|
|
|
GCOptions options;
|
|
|
|
|
|
|
|
parseCmdLine(
|
|
|
|
argc, argv, [&](Strings::iterator& arg, const Strings::iterator& end) {
|
2020-05-19 21:47:23 +02:00
|
|
|
if (*arg == "--help") {
|
2020-05-17 17:31:57 +02:00
|
|
|
showManPage("nix-collect-garbage");
|
2020-05-19 21:47:23 +02:00
|
|
|
} else if (*arg == "--version") {
|
2020-05-17 17:31:57 +02:00
|
|
|
printVersion("nix-collect-garbage");
|
2020-05-19 21:47:23 +02:00
|
|
|
} else if (*arg == "--delete-old" || *arg == "-d") {
|
2020-05-17 17:31:57 +02:00
|
|
|
removeOld = true;
|
2020-05-19 21:47:23 +02:00
|
|
|
} else if (*arg == "--delete-older-than") {
|
2020-05-17 17:31:57 +02:00
|
|
|
removeOld = true;
|
|
|
|
deleteOlderThan = getArg(*arg, arg, end);
|
2020-05-19 21:47:23 +02:00
|
|
|
} else if (*arg == "--dry-run") {
|
2020-05-17 17:31:57 +02:00
|
|
|
dryRun = true;
|
2020-05-19 21:47:23 +02:00
|
|
|
} else if (*arg == "--max-freed") {
|
2020-05-17 17:31:57 +02:00
|
|
|
long long maxFreed = getIntArg<long long>(*arg, arg, end, true);
|
|
|
|
options.maxFreed = maxFreed >= 0 ? maxFreed : 0;
|
2020-05-19 21:47:23 +02:00
|
|
|
} else {
|
2020-05-17 17:31:57 +02:00
|
|
|
return false;
|
2020-05-19 21:47:23 +02:00
|
|
|
}
|
2020-05-17 17:31:57 +02:00
|
|
|
return true;
|
2015-04-22 14:21:52 +02:00
|
|
|
});
|
|
|
|
|
2020-05-17 17:31:57 +02:00
|
|
|
initPlugins();
|
2015-04-22 14:21:52 +02:00
|
|
|
|
2020-05-17 17:31:57 +02:00
|
|
|
auto profilesDir = settings.nixStateDir + "/profiles";
|
2020-05-19 19:55:58 +02:00
|
|
|
if (removeOld) {
|
|
|
|
removeOldGenerations(profilesDir);
|
|
|
|
}
|
2018-10-26 11:35:46 +02:00
|
|
|
|
2020-05-17 17:31:57 +02:00
|
|
|
// Run the actual garbage collector.
|
|
|
|
if (!dryRun) {
|
|
|
|
auto store = openStore();
|
|
|
|
options.action = GCOptions::gcDeleteDead;
|
|
|
|
GCResults results;
|
|
|
|
PrintFreed freed(true, results);
|
|
|
|
store->collectGarbage(options, results);
|
2018-10-26 11:35:46 +02:00
|
|
|
}
|
2020-05-17 17:31:57 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2015-04-22 14:21:52 +02:00
|
|
|
}
|
2018-10-26 11:35:46 +02:00
|
|
|
|
|
|
|
static RegisterLegacyCommand s1("nix-collect-garbage", _main);
|