2015-05-21 13:21:38 +00:00
|
|
|
#include "store-api.hh"
|
2022-03-01 18:31:36 +00:00
|
|
|
#include "gc-store.hh"
|
2015-05-21 14:26:03 +00:00
|
|
|
#include "profiles.hh"
|
2015-04-22 12:21:52 +00:00
|
|
|
#include "shared.hh"
|
|
|
|
#include "globals.hh"
|
2021-01-26 11:22:24 +00:00
|
|
|
#include "legacy.hh"
|
2015-04-22 12:21:52 +00:00
|
|
|
|
|
|
|
#include <iostream>
|
2015-10-18 19:04:24 +00:00
|
|
|
#include <cerrno>
|
2015-04-22 12:21:52 +00:00
|
|
|
|
|
|
|
using namespace nix;
|
|
|
|
|
2015-05-21 14:26:03 +00:00
|
|
|
std::string deleteOlderThan;
|
2015-04-22 12:21:52 +00: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 13:04:05 +00:00
|
|
|
if (access(dir.c_str(), R_OK) != 0) return;
|
|
|
|
|
|
|
|
bool canWrite = access(dir.c_str(), W_OK) == 0;
|
|
|
|
|
2015-04-22 12:21:52 +00:00
|
|
|
for (auto & i : readDirectory(dir)) {
|
|
|
|
checkInterrupt();
|
|
|
|
|
2015-05-21 13:04:05 +00:00
|
|
|
auto path = dir + "/" + i.name;
|
2015-05-21 12:09:34 +00:00
|
|
|
auto type = i.type == DT_UNKNOWN ? getFileType(path) : i.type;
|
2015-04-22 12:21:52 +00:00
|
|
|
|
2015-05-21 13:04:05 +00:00
|
|
|
if (type == DT_LNK && canWrite) {
|
2015-07-17 09:24:25 +00:00
|
|
|
std::string link;
|
|
|
|
try {
|
|
|
|
link = readLink(path);
|
|
|
|
} catch (SysError & e) {
|
|
|
|
if (e.errNo == ENOENT) continue;
|
|
|
|
}
|
2022-02-25 15:00:00 +00:00
|
|
|
if (link.find("link") != std::string::npos) {
|
2016-09-21 14:11:01 +00:00
|
|
|
printInfo(format("removing old generations of profile %1%") % path);
|
2015-05-21 14:26:03 +00:00
|
|
|
if (deleteOlderThan != "")
|
|
|
|
deleteGenerationsOlderThan(path, deleteOlderThan, dryRun);
|
|
|
|
else
|
|
|
|
deleteOldGenerations(path, dryRun);
|
2015-04-22 12:21:52 +00:00
|
|
|
}
|
|
|
|
} else if (type == DT_DIR) {
|
|
|
|
removeOldGenerations(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 11:36:55 +00:00
|
|
|
static int main_nix_collect_garbage(int argc, char * * argv)
|
2015-04-22 12:21:52 +00:00
|
|
|
{
|
2018-10-26 09:35:46 +00:00
|
|
|
{
|
|
|
|
bool removeOld = false;
|
2015-04-22 12:21:52 +00:00
|
|
|
|
2015-08-21 11:57:53 +00:00
|
|
|
GCOptions options;
|
|
|
|
|
2015-04-22 12:21:52 +00: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;
|
2015-05-21 14:26:03 +00:00
|
|
|
deleteOlderThan = getArg(*arg, arg, end);
|
2015-04-22 12:21:52 +00:00
|
|
|
}
|
|
|
|
else if (*arg == "--dry-run") dryRun = true;
|
2020-07-30 11:10:49 +00:00
|
|
|
else if (*arg == "--max-freed")
|
|
|
|
options.maxFreed = std::max(getIntArg<int64_t>(*arg, arg, end, true), (int64_t) 0);
|
2015-04-22 12:21:52 +00:00
|
|
|
else
|
2015-08-21 11:57:53 +00:00
|
|
|
return false;
|
2015-04-22 12:21:52 +00:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
auto profilesDir = settings.nixStateDir + "/profiles";
|
|
|
|
if (removeOld) removeOldGenerations(profilesDir);
|
|
|
|
|
|
|
|
// Run the actual garbage collector.
|
2015-05-21 13:21:38 +00:00
|
|
|
if (!dryRun) {
|
2016-02-24 16:33:53 +00:00
|
|
|
auto store = openStore();
|
2022-03-01 18:31:36 +00:00
|
|
|
auto & gcStore = requireGcStore(*store);
|
2015-05-21 13:21:38 +00:00
|
|
|
options.action = GCOptions::gcDeleteDead;
|
|
|
|
GCResults results;
|
|
|
|
PrintFreed freed(true, results);
|
2022-03-01 18:31:36 +00:00
|
|
|
gcStore.collectGarbage(options, results);
|
2015-05-21 13:21:38 +00:00
|
|
|
}
|
2018-10-26 09:35:46 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2015-04-22 12:21:52 +00:00
|
|
|
}
|
2018-10-26 09:35:46 +00:00
|
|
|
|
2020-10-06 11:36:55 +00:00
|
|
|
static RegisterLegacyCommand r_nix_collect_garbage("nix-collect-garbage", main_nix_collect_garbage);
|