From f632a4b1243814c2568c29b18587dc5931792d06 Mon Sep 17 00:00:00 2001 From: Prasanna Kumar Kalever Date: Mon, 12 Jun 2017 16:53:13 +0530 Subject: info: show size in human readable format Also s/BLOCK CONFIG NODE(S)/EXPORTED NODE(S)/ in the info output $ gluster-block info sample/block NAME: block VOLUME: sample GBID: 6bd70984-be2c-43ac-9e9d-bad04010e42f SIZE: 1.0 GiB HA: 1 PASSWORD: EXPORTED NODE(S): 192.168.0.105 Change-Id: I473b854b939c96b99be8e0b172ac6957b8bc6006 Fixes: #23 Signed-off-by: Prasanna Kumar Kalever Signed-off-by: Pranith Kumar K --- utils/common.c | 27 ++++++++++++++++++++++++++- utils/common.h | 4 ++-- 2 files changed, 28 insertions(+), 3 deletions(-) (limited to 'utils') diff --git a/utils/common.c b/utils/common.c index c42ce03..c6d1c34 100644 --- a/utils/common.c +++ b/utils/common.c @@ -38,7 +38,7 @@ jsonResponseFormatParse(const char *opt) ssize_t -glusterBlockCreateParseSize(const char *dom, char *value) +glusterBlockParseSize(const char *dom, char *value) { char *postfix; char *tmp; @@ -85,6 +85,7 @@ glusterBlockCreateParseSize(const char *dom, char *value) case 'k': sizef *= 1024; /* fall through */ + case 'B': case 'b': case '\0': return sizef; @@ -96,3 +97,27 @@ glusterBlockCreateParseSize(const char *dom, char *value) return -1; } } + + +char * +glusterBlockFormatSize(const char *dom, size_t bytes) +{ + char *buf; + size_t i = 0; + size_t rem = 0; + const char* units[] = {"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"}; + + + while (bytes >= 1024) { + rem = (bytes % 1024); + bytes /= 1024; + i++; + } + + if (GB_ASPRINTF(&buf, "%.1f %s", (float)bytes + (float)rem / 1024.0, units[i]) < 0) { + LOG(dom, GB_LOG_ERROR, "%s", "glusterBlockFormatSize() failed"); + buf = NULL; + } + + return buf; +} diff --git a/utils/common.h b/utils/common.h index 21bf855..118d3f0 100644 --- a/utils/common.h +++ b/utils/common.h @@ -52,8 +52,8 @@ static const char *const JsonResponseFormatLookup[] = { enum JsonResponseFormat jsonResponseFormatParse(const char *opt); -int convertStringToTrillianParse(const char *opt); +ssize_t glusterBlockParseSize(const char *dom, char *value); -ssize_t glusterBlockCreateParseSize(const char *dom, char *value); +char* glusterBlockFormatSize(const char *dom, size_t bytes); # endif /* _COMMON_H */ -- cgit