2015-05-21 13:21:38 +00:00
|
|
|
#include "store-api.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"
|
|
|
|
|
|
|
|
#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;
|
|
|
|
}
|
2015-04-22 12:21:52 +00:00
|
|
|
if (link.find("link") != string::npos) {
|
|
|
|
printMsg(lvlInfo, 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char * * argv)
|
|
|
|
{
|
|
|
|
bool removeOld = false;
|
|
|
|
|
|
|
|
return handleExceptions(argv[0], [&]() {
|
|
|
|
initNix();
|
|
|
|
|
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;
|
2015-08-21 11:57:53 +00:00
|
|
|
else if (*arg == "--max-freed") {
|
|
|
|
long long maxFreed = getIntArg<long long>(*arg, arg, end, true);
|
|
|
|
options.maxFreed = maxFreed >= 0 ? maxFreed : 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) {
|
Eliminate the "store" global variable
Also, move a few free-standing functions into StoreAPI and Derivation.
Also, introduce a non-nullable smart pointer, ref<T>, which is just a
wrapper around std::shared_ptr ensuring that the pointer is never
null. (For reference-counted values, this is better than passing a
"T&", because the latter doesn't maintain the refcount. Usually, the
caller will have a shared_ptr keeping the value alive, but that's not
always the case, e.g., when passing a reference to a std::thread via
std::bind.)
2016-02-04 13:28:26 +00:00
|
|
|
auto store = openStore(false);
|
2015-05-21 13:21:38 +00:00
|
|
|
options.action = GCOptions::gcDeleteDead;
|
|
|
|
GCResults results;
|
|
|
|
PrintFreed freed(true, results);
|
|
|
|
store->collectGarbage(options, results);
|
|
|
|
}
|
2015-04-22 12:21:52 +00:00
|
|
|
});
|
|
|
|
}
|