Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 1 addition & 13 deletions src/libmain/include/nix/main/shared.hh
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,7 @@ extern volatile ::sig_atomic_t blockInt;

struct GCResults;

struct PrintFreed
{
bool show;
const GCResults & results;

PrintFreed(bool show, const GCResults & results)
: show(show)
, results(results)
{
}

~PrintFreed();
};
void printFreed(bool dryRun, const GCResults & results);

#ifndef _WIN32
/**
Expand Down
7 changes: 5 additions & 2 deletions src/libmain/shared.cc
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,12 @@ RunPager::~RunPager()
}
}

PrintFreed::~PrintFreed()
void printFreed(bool dryRun, const GCResults & results)
{
if (show)
/* bytesFreed cannot be reliably computed without actually deleting store paths because of hardlinking. */
if (dryRun)
std::cout << fmt("%d store paths would be deleted\n", results.paths.size());
else
std::cout << fmt("%d store paths deleted, %s freed\n", results.paths.size(), renderSize(results.bytesFreed));
}

Expand Down
3 changes: 1 addition & 2 deletions src/libstore/include/nix/store/gc-store.hh
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ struct GCResults
PathSet paths;

/**
* For `gcReturnDead`, `gcDeleteDead` and `gcDeleteSpecific`, the
* number of bytes that would be or was freed.
* For `gcDeleteDead` and `gcDeleteSpecific`, the number of bytes that were freed.
*/
uint64_t bytesFreed = 0;
};
Expand Down
19 changes: 10 additions & 9 deletions src/nix/nix-collect-garbage/nix-collect-garbage.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "nix/util/file-system.hh"
#include "nix/util/signals.hh"
#include "nix/util/error.hh"
#include "nix/store/store-open.hh"
#include "nix/store/store-cast.hh"
#include "nix/store/gc-store.hh"
Expand Down Expand Up @@ -87,6 +88,9 @@ static int main_nix_collect_garbage(int argc, char ** argv)
return true;
});

if (options.maxFreed != std::numeric_limits<uint64_t>::max() && dryRun)
throw UsageError("options --max-freed and --dry-run cannot be combined");

if (removeOld) {
std::set<std::filesystem::path> dirsToClean = {
profilesDir(),
Expand All @@ -97,15 +101,12 @@ static int main_nix_collect_garbage(int argc, char ** argv)
removeOldGenerations(dir);
}

// Run the actual garbage collector.
if (!dryRun) {
auto store = openStore();
auto & gcStore = require<GcStore>(*store);
options.action = GCOptions::gcDeleteDead;
GCResults results;
PrintFreed freed(true, results);
gcStore.collectGarbage(options, results);
}
auto store = openStore();
auto & gcStore = require<GcStore>(*store);
options.action = dryRun ? GCOptions::gcReturnDead : GCOptions::gcDeleteDead;
GCResults results;
Finally printer([&] { printFreed(dryRun, results); });
gcStore.collectGarbage(options, results);

return 0;
}
Expand Down
20 changes: 14 additions & 6 deletions src/nix/nix-store/nix-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include "nix/store/export-import.hh"
#include "nix/util/strings.hh"
#include "nix/store/posix-fs-canonicalise.hh"
#include "nix/util/error.hh"
#include "nix/store/gc-store.hh"

#include "man-pages.hh"

Expand Down Expand Up @@ -679,6 +681,10 @@ static void opGC(Strings opFlags, Strings opArgs)
if (!opArgs.empty())
throw UsageError("no arguments expected");

if (options.maxFreed != std::numeric_limits<uint64_t>::max()
&& (options.action == GCOptions::gcReturnDead || options.action == GCOptions::gcReturnLive || printRoots))
throw UsageError("option --max-freed cannot be combined with --print-live, --print-dead, or --print-roots");

auto & gcStore = require<GcStore>(*store);

if (printRoots) {
Expand All @@ -693,12 +699,14 @@ static void opGC(Strings opFlags, Strings opArgs)
}

else {
PrintFreed freed(options.action == GCOptions::gcDeleteDead, results);
Finally printer([&] {
if (options.action != GCOptions::gcDeleteDead)
for (auto & i : results.paths)
cout << i << std::endl;
else
printFreed(false, results);
});
gcStore.collectGarbage(options, results);

if (options.action != GCOptions::gcDeleteDead)
for (auto & i : results.paths)
cout << i << std::endl;
}
}

Expand All @@ -722,7 +730,7 @@ static void opDelete(Strings opFlags, Strings opArgs)
auto & gcStore = require<GcStore>(*store);

GCResults results;
PrintFreed freed(true, results);
Finally printer([&] { printFreed(false, results); });
gcStore.collectGarbage(options, results);
}

Expand Down
2 changes: 1 addition & 1 deletion src/nix/store-delete.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct CmdStoreDelete : StorePathsCommand
options.pathsToDelete.insert(path);

GCResults results;
PrintFreed freed(true, results);
Finally printer([&] { printFreed(false, results); });
gcStore.collectGarbage(options, results);
}
};
Expand Down
9 changes: 7 additions & 2 deletions src/nix/store-gc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "nix/store/store-api.hh"
#include "nix/store/store-cast.hh"
#include "nix/store/gc-store.hh"
#include "nix/util/error.hh"
#include "nix/util/logging.hh"

using namespace nix;

Expand All @@ -15,7 +17,7 @@ struct CmdStoreGC : StoreCommand, MixDryRun
{
addFlag({
.longName = "max",
.description = "Stop after freeing *n* bytes of disk space.",
.description = "Stop after freeing *n* bytes of disk space. Cannot be combined with --dry-run.",
.labels = {"n"},
.handler = {&options.maxFreed},
});
Expand All @@ -35,11 +37,14 @@ struct CmdStoreGC : StoreCommand, MixDryRun

void run(ref<Store> store) override
{
if (options.maxFreed != std::numeric_limits<uint64_t>::max() && dryRun)
throw UsageError("options --max and --dry-run cannot be combined");

auto & gcStore = require<GcStore>(*store);

options.action = dryRun ? GCOptions::gcReturnDead : GCOptions::gcDeleteDead;
GCResults results;
PrintFreed freed(options.action == GCOptions::gcDeleteDead, results);
Finally printer([&] { printFreed(dryRun, results); });
gcStore.collectGarbage(options, results);
}
};
Expand Down
Loading