summaryrefslogtreecommitdiff
path: root/src/util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2011-12-31 02:31:54 +0100
committerLennart Poettering <lennart@poettering.net>2011-12-31 02:31:54 +0100
commitbabfc09177c1e71cec6c1ef9602e265ed40cfe4f (patch)
tree24da4e0e20fd1e07a0a43ef34989fec6b8eba6cb /src/util.c
parent3a22a969c5746e24b2fc584b0395fec627f5f4c5 (diff)
journal: automatically deduce journal metrics from file system sizes
Diffstat (limited to 'src/util.c')
-rw-r--r--src/util.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/util.c b/src/util.c
index 7191750c52..0d1d16d9ac 100644
--- a/src/util.c
+++ b/src/util.c
@@ -5940,3 +5940,37 @@ unsigned long cap_last_cap(void) {
return p;
}
+
+char *format_bytes(char *buf, size_t l, off_t t) {
+ int i;
+
+ static const struct {
+ const char *suffix;
+ off_t factor;
+ } table[] = {
+ { "T", 1024ULL*1024ULL*1024ULL*1024ULL },
+ { "G", 1024ULL*1024ULL*1024ULL },
+ { "M", 1024ULL*1024ULL },
+ { "K", 1024ULL },
+ };
+
+ for (i = 0; i < ELEMENTSOF(table); i++) {
+
+ if (t >= table[i].factor) {
+ snprintf(buf, l,
+ "%llu.%llu%s",
+ (unsigned long long) (t / table[i].factor),
+ (unsigned long long) (((t*10ULL) / table[i].factor) % 10ULL),
+ table[i].suffix);
+
+ goto finish;
+ }
+ }
+
+ snprintf(buf, l, "%lluB", (unsigned long long) t);
+
+finish:
+ buf[l-1] = 0;
+ return buf;
+
+}