summaryrefslogtreecommitdiffstats
path: root/libglusterfs/src
diff options
context:
space:
mode:
authorJunaid <junaid@gluster.com>2011-07-04 00:47:43 +0000
committerAnand Avati <avati@gluster.com>2011-07-14 06:39:42 -0700
commit6d71641ca56e53eb8e988b4e5c1676eb782503c3 (patch)
tree2c65125e51528225fad8f2b83bc69cad0a7f4be8 /libglusterfs/src
parentc82a9d438bc47e9dff2047df1012cdd0653cffca (diff)
libglusterfs/common-utils: Added gf_uint64_2human_readable function.
This function converts the given number to its corresponding representation in KB, MB, etc. Signed-off-by: Junaid <junaid@gluster.com> Signed-off-by: Anand Avati <avati@gluster.com> BUG: 2829 (Display the quota limit as it is configured) URL: http://bugs.gluster.com/cgi-bin/bugzilla3/show_bug.cgi?id=2829
Diffstat (limited to 'libglusterfs/src')
-rw-r--r--libglusterfs/src/common-utils.c36
-rw-r--r--libglusterfs/src/common-utils.h2
2 files changed, 38 insertions, 0 deletions
diff --git a/libglusterfs/src/common-utils.c b/libglusterfs/src/common-utils.c
index e745ec0fa..8d2dac680 100644
--- a/libglusterfs/src/common-utils.c
+++ b/libglusterfs/src/common-utils.c
@@ -1258,6 +1258,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 a1df3de35..9b15b9631 100644
--- a/libglusterfs/src/common-utils.h
+++ b/libglusterfs/src/common-utils.h
@@ -361,4 +361,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 */