2015-10-18 21:04:24 +02:00
|
|
|
#include <cerrno>
|
2015-05-21 15:21:38 +02:00
|
|
|
#include <iostream>
|
2020-05-19 16:54:39 +02:00
|
|
|
|
2020-05-19 05:52:47 +02:00
|
|
|
#include <glog/logging.h>
|
2020-05-19 16:54:39 +02:00
|
|
|
|
2015-04-22 14:21:52 +02:00
|
|
|
#include "globals.hh"
|
2018-10-26 11:35:46 +02:00
|
|
|
#include "legacy.hh"
|
2015-05-21 16:26:03 +02:00
|
|
|
#include "profiles.hh"
|
2015-04-22 14:21:52 +02:00
|
|
|
#include "shared.hh"
|
|
|
|
#include "store-api.hh"
|
|
|
|
|
|
|
|
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. */
|
|
|
|
|
|
|
|
void removeOldGenerations(std::string dir) {
|
2015-05-21 15:04:05 +02:00
|
|
|
if (access(dir.c_str(), R_OK) != 0) {
|
|
|
|
return;
|
2020-05-19 19:55:58 +02:00
|
|
|
}
|
2020-05-17 17:31:57 +02:00
|
|
|
|
2015-05-21 15:04:05 +02:00
|
|
|
bool canWrite = access(dir.c_str(), W_OK) == 0;
|
2020-05-17 17:31:57 +02:00
|
|
|
|
2015-04-22 14:21:52 +02:00
|
|
|
for (auto& i : readDirectory(dir)) {
|
|
|
|
checkInterrupt();
|
2020-05-17 17:31:57 +02:00
|
|
|
|
2015-05-21 15:04:05 +02:00
|
|
|
auto path = dir + "/" + i.name;
|
2015-05-21 14:09:34 +02:00
|
|
|
auto type = i.type == DT_UNKNOWN ? getFileType(path) : i.type;
|
2020-05-17 17:31:57 +02:00
|
|
|
|
2015-05-21 15:04:05 +02:00
|
|
|
if (type == DT_LNK && canWrite) {
|
2015-07-17 11:24:25 +02:00
|
|
|
std::string link;
|
|
|
|
try {
|
|
|
|
link = readLink(path);
|
|
|
|
} catch (SysError& e) {
|
|
|
|
if (e.errNo == ENOENT) {
|
|
|
|
continue;
|
2020-05-19 19:55:58 +02:00
|
|
|
}
|
2015-07-17 11:24:25 +02:00
|
|
|
}
|
2015-04-22 14:21:52 +02:00
|
|
|
if (link.find("link") != string::npos) {
|
2020-05-19 05:52:47 +02:00
|
|
|
LOG(INFO) << "removing old generations of profile " << path;
|
2015-05-21 16:26:03 +02:00
|
|
|
if (deleteOlderThan != "") {
|
|
|
|
deleteGenerationsOlderThan(path, deleteOlderThan, dryRun);
|
|
|
|
} else {
|
|
|
|
deleteOldGenerations(path, dryRun);
|
2020-05-19 21:47:23 +02:00
|
|
|
}
|
2015-04-22 14:21:52 +02:00
|
|
|
}
|
|
|
|
} else if (type == DT_DIR) {
|
|
|
|
removeOldGenerations(path);
|
|
|
|
}
|
2020-05-17 17:31:57 +02:00
|
|
|
}
|
2015-04-22 14:21:52 +02:00
|
|
|
}
|
|
|
|
|
2018-10-26 11:35:46 +02:00
|
|
|
static int _main(int argc, char** argv) {
|
2015-04-22 14:21:52 +02:00
|
|
|
{
|
2018-10-26 11:35:46 +02:00
|
|
|
bool removeOld = false;
|
2020-05-17 17:31:57 +02:00
|
|
|
|
2015-08-21 13:57:53 +02:00
|
|
|
GCOptions options;
|
2020-05-17 17:31:57 +02:00
|
|
|
|
2020-05-20 05:33:07 +02:00
|
|
|
parseCmdLine(argc, argv,
|
|
|
|
[&](Strings::iterator& arg, const Strings::iterator& end) {
|
|
|
|
if (*arg == "--help") {
|
|
|
|
showManPage("nix-collect-garbage");
|
|
|
|
} else if (*arg == "--version") {
|
|
|
|
printVersion("nix-collect-garbage");
|
|
|
|
} else if (*arg == "--delete-old" || *arg == "-d") {
|
|
|
|
removeOld = true;
|
|
|
|
} else if (*arg == "--delete-older-than") {
|
|
|
|
removeOld = true;
|
|
|
|
deleteOlderThan = getArg(*arg, arg, end);
|
|
|
|
} else if (*arg == "--dry-run") {
|
|
|
|
dryRun = true;
|
|
|
|
} else if (*arg == "--max-freed") {
|
|
|
|
auto maxFreed = getIntArg<long long>(*arg, arg, end, true);
|
|
|
|
options.maxFreed = maxFreed >= 0 ? maxFreed : 0;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
2015-04-22 14:21:52 +02:00
|
|
|
|
2018-02-08 17:26:18 +01:00
|
|
|
initPlugins();
|
2015-04-22 14:21:52 +02:00
|
|
|
|
2016-02-24 17:33:53 +01:00
|
|
|
auto profilesDir = settings.nixStateDir + "/profiles";
|
2015-05-21 15:21:38 +02:00
|
|
|
if (removeOld) {
|
|
|
|
removeOldGenerations(profilesDir);
|
2020-05-19 19:55:58 +02:00
|
|
|
}
|
2018-10-26 11:35:46 +02:00
|
|
|
|
|
|
|
// Run the actual garbage collector.
|
2015-05-21 15:21:38 +02:00
|
|
|
if (!dryRun) {
|
2018-10-26 11:35:46 +02:00
|
|
|
auto store = openStore();
|
|
|
|
options.action = GCOptions::gcDeleteDead;
|
2015-05-21 15:21:38 +02:00
|
|
|
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);
|