2015-05-21 13:21:38 +00:00
|
|
|
#include "store-api.hh"
|
2022-03-09 15:27:48 +00:00
|
|
|
#include "store-cast.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"
|
2024-03-10 06:36:47 +00:00
|
|
|
#include "signals.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-06-10 17:49:38 +00:00
|
|
|
throw;
|
2015-07-17 09:24:25 +00:00
|
|
|
}
|
2022-02-25 15:00:00 +00:00
|
|
|
if (link.find("link") != std::string::npos) {
|
2023-03-02 14:44:19 +00:00
|
|
|
printInfo("removing old generations of profile %s", path);
|
2023-06-19 04:04:59 +00:00
|
|
|
if (deleteOlderThan != "") {
|
|
|
|
auto t = parseOlderThanTimeSpec(deleteOlderThan);
|
|
|
|
deleteGenerationsOlderThan(path, t, dryRun);
|
|
|
|
} else
|
2015-05-21 14:26:03 +00:00
|
|
|
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;
|
|
|
|
});
|
|
|
|
|
2023-04-03 13:13:11 +00:00
|
|
|
if (removeOld) {
|
|
|
|
std::set<Path> dirsToClean = {
|
|
|
|
profilesDir(), settings.nixStateDir + "/profiles", dirOf(getDefaultProfile())};
|
|
|
|
for (auto & dir : dirsToClean)
|
|
|
|
removeOldGenerations(dir);
|
|
|
|
}
|
2015-04-22 12:21:52 +00:00
|
|
|
|
|
|
|
// 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-09 15:27:48 +00:00
|
|
|
auto & gcStore = require<GcStore>(*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);
|