about summary refs log tree commit diff
path: root/src/libmain/shared.hh
diff options
context:
space:
mode:
authorEelco Dolstra <eelco.dolstra@logicblox.com>2014-02-17T13·48+0100
committerEelco Dolstra <eelco.dolstra@logicblox.com>2014-02-17T13·48+0100
commit1da6ae4f9904f7e09166085a2cfed8887e0e86d4 (patch)
tree9e126907178d7627e02114cb7137256de1996592 /src/libmain/shared.hh
parent00d30496ca32145f55891364ddcf3d4af87f05d5 (diff)
nix-store --gc --max-freed: Support a unit specifier
E.g. "--max-freed 10G" means "free ten gigabytes".
Diffstat (limited to 'src/libmain/shared.hh')
-rw-r--r--src/libmain/shared.hh21
1 files changed, 18 insertions, 3 deletions
diff --git a/src/libmain/shared.hh b/src/libmain/shared.hh
index ff89e86389..b29b08bb59 100644
--- a/src/libmain/shared.hh
+++ b/src/libmain/shared.hh
@@ -4,6 +4,8 @@
 
 #include <signal.h>
 
+#include <locale>
+
 
 /* These are not implemented here, but must be implemented by a
    program linking against libmain. */
@@ -35,14 +37,27 @@ void printMissing(const PathSet & willBuild,
     unsigned long long downloadSize, unsigned long long narSize);
 
 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;
     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;
-    if (!string2Int(*i, n))
+    if (!string2Int(s, n))
         throw UsageError(format("`%1%' requires an integer argument") % opt);
-    return n;
+    return n * multiplier;
 }
 
 /* Show the manual page for the specified program. */