From 13493ef97c01a9839c340cbe4e99baae65b20e15 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Thu, 21 May 2015 15:21:38 +0200 Subject: [PATCH] nix-collect-garbage: Call collectGarbage() internally --- src/libmain/shared.cc | 15 +++++++++++++ src/libmain/shared.hh | 17 ++++++++++++++ .../nix-collect-garbage.cc | 11 ++++++++-- src/nix-store/nix-store.cc | 22 ------------------- 4 files changed, 41 insertions(+), 24 deletions(-) diff --git a/src/libmain/shared.cc b/src/libmain/shared.cc index c83e997b2..f300fbf1b 100644 --- a/src/libmain/shared.cc +++ b/src/libmain/shared.cc @@ -342,4 +342,19 @@ RunPager::~RunPager() } +string showBytes(unsigned long long bytes) +{ + return (format("%.2f MiB") % (bytes / (1024.0 * 1024.0))).str(); +} + + +PrintFreed::~PrintFreed() +{ + if (show) + std::cout << format("%1% store paths deleted, %2% freed\n") + % results.paths.size() + % showBytes(results.bytesFreed); +} + + } diff --git a/src/libmain/shared.hh b/src/libmain/shared.hh index b662c6cce..a7e6ef762 100644 --- a/src/libmain/shared.hh +++ b/src/libmain/shared.hh @@ -83,4 +83,21 @@ private: extern volatile ::sig_atomic_t blockInt; + +/* GC helpers. */ + +string showBytes(unsigned long long bytes); + +class GCResults; + +struct PrintFreed +{ + bool show; + const GCResults & results; + PrintFreed(bool show, const GCResults & results) + : show(show), results(results) { } + ~PrintFreed(); +}; + + } diff --git a/src/nix-collect-garbage/nix-collect-garbage.cc b/src/nix-collect-garbage/nix-collect-garbage.cc index a8f6c03c2..740ef88f5 100644 --- a/src/nix-collect-garbage/nix-collect-garbage.cc +++ b/src/nix-collect-garbage/nix-collect-garbage.cc @@ -1,3 +1,4 @@ +#include "store-api.hh" #include "hash.hh" #include "shared.hh" #include "globals.hh" @@ -87,7 +88,13 @@ int main(int argc, char * * argv) if (removeOld) removeOldGenerations(profilesDir); // Run the actual garbage collector. - if (!dryRun) runProgramSimple(settings.nixBinDir + "/nix-store", Strings{"--gc"}); + if (!dryRun) { + store = openStore(false); + GCOptions options; + options.action = GCOptions::gcDeleteDead; + GCResults results; + PrintFreed freed(true, results); + store->collectGarbage(options, results); + } }); } - diff --git a/src/nix-store/nix-store.cc b/src/nix-store/nix-store.cc index 506c1a397..ff15875e7 100644 --- a/src/nix-store/nix-store.cc +++ b/src/nix-store/nix-store.cc @@ -625,28 +625,6 @@ static void opCheckValidity(Strings opFlags, Strings opArgs) } -static string showBytes(unsigned long long bytes) -{ - return (format("%.2f MiB") % (bytes / (1024.0 * 1024.0))).str(); -} - - -struct PrintFreed -{ - bool show; - const GCResults & results; - PrintFreed(bool show, const GCResults & results) - : show(show), results(results) { } - ~PrintFreed() - { - if (show) - cout << format("%1% store paths deleted, %2% freed\n") - % results.paths.size() - % showBytes(results.bytesFreed); - } -}; - - static void opGC(Strings opFlags, Strings opArgs) { bool printRoots = false;