diff options
-rw-r--r-- | libglusterfs/src/common-utils.c | 36 | ||||
-rw-r--r-- | libglusterfs/src/common-utils.h | 2 |
2 files changed, 38 insertions, 0 deletions
diff --git a/libglusterfs/src/common-utils.c b/libglusterfs/src/common-utils.c index 29af725a07a..15371d1941f 100644 --- a/libglusterfs/src/common-utils.c +++ b/libglusterfs/src/common-utils.c @@ -1262,6 +1262,42 @@ gf_string2uint64_base10 (const char *str, uint64_t *n) return -1; } +char * +gf_uint64_2human_readable (uint64_t n) +{ + int ret = 0; + char *str = NULL; + + if (n >= GF_UNIT_PB) { + ret = gf_asprintf (&str, "%.1lfPB", ((double) n)/GF_UNIT_PB); + if (ret < 0) + goto err; + } else if (n >= GF_UNIT_TB) { + ret = gf_asprintf (&str, "%.1lfTB", ((double) n)/GF_UNIT_TB); + if (ret < 0) + goto err; + } else if (n >= GF_UNIT_GB) { + ret = gf_asprintf (&str, "%.1lfGB", ((double) n)/GF_UNIT_GB); + if (ret < 0) + goto err; + } else if (n >= GF_UNIT_MB) { + ret = gf_asprintf (&str, "%.1lfMB", ((double) n)/GF_UNIT_MB); + if (ret < 0) + goto err; + } else if (n >= GF_UNIT_KB) { + ret = gf_asprintf (&str, "%.1lfKB", ((double) n)/GF_UNIT_KB); + if (ret < 0) + goto err; + } else { + ret = gf_asprintf (&str, "%luBytes", n); + if (ret < 0) + goto err; + } + return str; +err: + return NULL; +} + int gf_string2bytesize (const char *str, uint64_t *n) { diff --git a/libglusterfs/src/common-utils.h b/libglusterfs/src/common-utils.h index d3218b0185a..e092288bf5e 100644 --- a/libglusterfs/src/common-utils.h +++ b/libglusterfs/src/common-utils.h @@ -360,4 +360,6 @@ void _get_md5_str (char *out_str, size_t outlen, void gf_array_insertionsort (void *a, int l, int r, size_t elem_size, gf_cmp cmp); int gf_is_str_int (const char *value); + +char *gf_uint64_2human_readable (uint64_t); #endif /* _COMMON_UTILS_H */ |