summaryrefslogtreecommitdiff
path: root/nix/libstore/misc.cc
diff options
context:
space:
mode:
Diffstat (limited to 'nix/libstore/misc.cc')
-rw-r--r--nix/libstore/misc.cc21
1 files changed, 21 insertions, 0 deletions
diff --git a/nix/libstore/misc.cc b/nix/libstore/misc.cc
index bc5dec88bf..e9904f3c4f 100644
--- a/nix/libstore/misc.cc
+++ b/nix/libstore/misc.cc
@@ -1,4 +1,5 @@
#include "misc.hh"
+#include <math.h>
#include "store-api.hh"
#include "local-store.hh"
#include "globals.hh"
@@ -94,5 +95,25 @@ Paths topoSortPaths(StoreAPI & store, const PathSet & paths)
return sorted;
}
+/* Max of LLONG_MAX is 8 EiB */
+string showBytes(long long bytes)
+{
+ if (llabs(bytes > exp2l(60))) {
+ return (format("%7.2f EiB") % (bytes / exp2l(60))).str();
+ } else if (llabs(bytes > exp2l(50))) {
+ return (format("%7.2f PiB") % (bytes / exp2l(50))).str();
+ } else if (llabs(bytes > exp2l(40))) {
+ return (format("%7.2f TiB") % (bytes / exp2l(40))).str();
+ } else if (llabs(bytes > exp2l(30))) {
+ return (format("%7.2f GiB") % (bytes / exp2l(30))).str();
+ } else if (llabs(bytes > exp2l(20))) {
+ return (format("%7.2f MiB") % (bytes / exp2l(20))).str();
+ } else if (llabs(bytes > exp2l(10))) {
+ return (format("%7.2f KiB") % (bytes / exp2l(10))).str();
+ } else {
+ return (format("%4f bytes") % bytes).str();
+ }
+}
+
}