nix-store --gc --max-freed: Support a unit specifier

E.g. "--max-freed 10G" means "free ten gigabytes".
This commit is contained in:
Eelco Dolstra 2014-02-17 14:48:50 +01:00
parent 00d30496ca
commit 1da6ae4f99
3 changed files with 25 additions and 6 deletions

View file

@ -296,8 +296,12 @@ options control what gets deleted and in what order:
<varlistentry><term><option>--max-freed</option> <replaceable>bytes</replaceable></term> <varlistentry><term><option>--max-freed</option> <replaceable>bytes</replaceable></term>
<listitem><para>Keep deleting paths until at least <listitem><para>Keep deleting paths until at least
<replaceable>bytes</replaceable> bytes have been <replaceable>bytes</replaceable> bytes have been deleted, then
deleted, then stop.</para></listitem> stop. The argument <replaceable>bytes</replaceable> can be
followed by the multiplicative suffix <literal>K</literal>,
<literal>M</literal>, <literal>G</literal> or
<literal>T</literal>, denoting KiB, MiB, GiB or TiB
units.</para></listitem>
</varlistentry> </varlistentry>

View file

@ -4,6 +4,8 @@
#include <signal.h> #include <signal.h>
#include <locale>
/* These are not implemented here, but must be implemented by a /* These are not implemented here, but must be implemented by a
program linking against libmain. */ program linking against libmain. */
@ -35,14 +37,27 @@ void printMissing(const PathSet & willBuild,
unsigned long long downloadSize, unsigned long long narSize); unsigned long long downloadSize, unsigned long long narSize);
template<class N> N getIntArg(const string & opt, template<class N> N getIntArg(const string & opt,
Strings::iterator & i, const Strings::iterator & end) Strings::iterator & i, const Strings::iterator & end, bool allowUnit)
{ {
++i; ++i;
if (i == end) throw UsageError(format("`%1%' requires an argument") % opt); if (i == end) throw UsageError(format("`%1%' requires an argument") % opt);
string s = *i;
N multiplier = 1;
if (allowUnit && !s.empty()) {
char u = std::toupper(*s.rbegin());
if (std::isalpha(u)) {
if (u == 'K') multiplier = 1ULL << 10;
else if (u == 'M') multiplier = 1ULL << 20;
else if (u == 'G') multiplier = 1ULL << 30;
else if (u == 'T') multiplier = 1ULL << 40;
else throw UsageError(format("invalid unit specifier `%1%'") % u);
s.resize(s.size() - 1);
}
}
N n; N n;
if (!string2Int(*i, n)) if (!string2Int(s, n))
throw UsageError(format("`%1%' requires an integer argument") % opt); throw UsageError(format("`%1%' requires an integer argument") % opt);
return n; return n * multiplier;
} }
/* Show the manual page for the specified program. */ /* Show the manual page for the specified program. */

View file

@ -619,7 +619,7 @@ static void opGC(Strings opFlags, Strings opArgs)
else if (*i == "--print-dead") options.action = GCOptions::gcReturnDead; else if (*i == "--print-dead") options.action = GCOptions::gcReturnDead;
else if (*i == "--delete") options.action = GCOptions::gcDeleteDead; else if (*i == "--delete") options.action = GCOptions::gcDeleteDead;
else if (*i == "--max-freed") { else if (*i == "--max-freed") {
long long maxFreed = getIntArg<long long>(*i, i, opFlags.end()); long long maxFreed = getIntArg<long long>(*i, i, opFlags.end(), true);
options.maxFreed = maxFreed >= 0 ? maxFreed : 0; options.maxFreed = maxFreed >= 0 ? maxFreed : 0;
} }
else throw UsageError(format("bad sub-operation `%1%' in GC") % *i); else throw UsageError(format("bad sub-operation `%1%' in GC") % *i);