diff options
author | Harshavardhana <fharshav@redhat.com> | 2012-02-21 10:45:32 -0800 |
---|---|---|
committer | Vijay Bellur <vijay@gluster.com> | 2012-02-21 22:23:09 -0800 |
commit | 2d470cd910b49976befea664ae5cc7ea6aa10020 (patch) | |
tree | 227857d16bd8a235de00bdd3b8afddd9413ab869 /libglusterfs/src/common-utils.c | |
parent | 311de6f96c4fabf30f734e62996ec18817447e2f (diff) |
libglusterfs/options: Cleanup xlator percent_sizet code.
Change-Id: I1a39252d6c26f7e30b77ef682b8b3cdcde8a4a51
BUG: 769691
Signed-off-by: Harshavardhana <fharshav@redhat.com>
Reviewed-on: http://review.gluster.com/2619
Tested-by: Gluster Build System <jenkins@build.gluster.com>
Reviewed-by: Amar Tumballi <amarts@redhat.com>
Diffstat (limited to 'libglusterfs/src/common-utils.c')
-rw-r--r-- | libglusterfs/src/common-utils.c | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/libglusterfs/src/common-utils.c b/libglusterfs/src/common-utils.c index 068bd8460..952af10cf 100644 --- a/libglusterfs/src/common-utils.c +++ b/libglusterfs/src/common-utils.c @@ -1267,6 +1267,63 @@ gf_string2bytesize (const char *str, uint64_t *n) return 0; } +int +gf_string2percent_or_bytesize (const char *str, + uint64_t *n, + gf_boolean_t *is_percent) +{ + uint64_t value = 0ULL; + char *tail = NULL; + int old_errno = 0; + const char *s = NULL; + + if (str == NULL || n == NULL) { + gf_log_callingfn (THIS->name, GF_LOG_WARNING, + "argument invalid"); + errno = EINVAL; + return -1; + } + + for (s = str; *s != '\0'; s++) { + if (isspace (*s)) + continue; + if (*s == '-') + return -1; + break; + } + + old_errno = errno; + errno = 0; + value = strtoull (str, &tail, 10); + + if (errno == ERANGE || errno == EINVAL) + return -1; + + if (errno == 0) + errno = old_errno; + + if (tail[0] != '\0') { + if (strcasecmp (tail, GF_UNIT_KB_STRING) == 0) + value *= GF_UNIT_KB; + else if (strcasecmp (tail, GF_UNIT_MB_STRING) == 0) + value *= GF_UNIT_MB; + else if (strcasecmp (tail, GF_UNIT_GB_STRING) == 0) + value *= GF_UNIT_GB; + else if (strcasecmp (tail, GF_UNIT_TB_STRING) == 0) + value *= GF_UNIT_TB; + else if (strcasecmp (tail, GF_UNIT_PB_STRING) == 0) + value *= GF_UNIT_PB; + else if (strcasecmp (tail, GF_UNIT_PERCENT_STRING) == 0) + *is_percent = _gf_true; + else + return -1; + } + + *n = value; + + return 0; +} + int64_t gf_str_to_long_long (const char *number) { |