diff options
Diffstat (limited to 'libglusterfs/src')
-rw-r--r-- | libglusterfs/src/common-utils.c | 68 | ||||
-rw-r--r-- | libglusterfs/src/common-utils.h | 1 |
2 files changed, 53 insertions, 16 deletions
diff --git a/libglusterfs/src/common-utils.c b/libglusterfs/src/common-utils.c index 6c5f65f6987..b57066d41da 100644 --- a/libglusterfs/src/common-utils.c +++ b/libglusterfs/src/common-utils.c @@ -1450,13 +1450,17 @@ err: int gf_string2bytesize_range (const char *str, uint64_t *n, uint64_t max) { - double value = 0.0; - char *tail = NULL; - int old_errno = 0; - const char *s = NULL; + double value = 0.0; + uint64_t int_value = 0; + uint64_t unit = 0; + char *tail = NULL; + int old_errno = 0; + const char *s = NULL; + gf_boolean_t fraction = _gf_false; if (str == NULL || n == NULL) { - gf_log_callingfn (THIS->name, GF_LOG_WARNING, "argument invalid"); + gf_log_callingfn (THIS->name, GF_LOG_WARNING, + "argument invalid"); errno = EINVAL; return -1; } @@ -1469,9 +1473,16 @@ gf_string2bytesize_range (const char *str, uint64_t *n, uint64_t max) break; } + if (strrchr (str, '.')) + fraction = _gf_true; + old_errno = errno; errno = 0; - value = strtod (str, &tail); + if (fraction) + value = strtod (str, &tail); + else + int_value = strtoll (str, &tail, 10); + if (str == tail) errno = EINVAL; @@ -1484,25 +1495,39 @@ gf_string2bytesize_range (const char *str, uint64_t *n, uint64_t max) if (tail[0] != '\0') { if (strcasecmp (tail, GF_UNIT_KB_STRING) == 0) - value *= GF_UNIT_KB; + unit = GF_UNIT_KB; else if (strcasecmp (tail, GF_UNIT_MB_STRING) == 0) - value *= GF_UNIT_MB; + unit = GF_UNIT_MB; else if (strcasecmp (tail, GF_UNIT_GB_STRING) == 0) - value *= GF_UNIT_GB; + unit = GF_UNIT_GB; else if (strcasecmp (tail, GF_UNIT_TB_STRING) == 0) - value *= GF_UNIT_TB; + unit = GF_UNIT_TB; else if (strcasecmp (tail, GF_UNIT_PB_STRING) == 0) - value *= GF_UNIT_PB; + unit = GF_UNIT_PB; else if (strcasecmp (tail, GF_UNIT_B_STRING) != 0) return -1; - } - if ((max - value) < 0) { - errno = ERANGE; - return -1; + if (unit > 0) { + if (fraction) + value *= unit; + else + int_value *= unit; + } } - *n = (uint64_t) value; + if (fraction) { + if ((max - value) < 0) { + errno = ERANGE; + return -1; + } + *n = (uint64_t) value; + } else { + if ((max - int_value) < 0) { + errno = ERANGE; + return -1; + } + *n = int_value; + } return 0; } @@ -1530,6 +1555,17 @@ gf_string2bytesize_uint64 (const char *str, uint64_t *n) } int +gf_string2bytesize_int64 (const char *str, int64_t *n) +{ + uint64_t u64 = 0; + int ret = 0; + + ret = gf_string2bytesize_range(str, &u64, INT64_MAX); + *n = (int64_t) u64; + return ret; +} + +int gf_string2percent_or_bytesize (const char *str, double *n, gf_boolean_t *is_percent) { diff --git a/libglusterfs/src/common-utils.h b/libglusterfs/src/common-utils.h index ec7e772e6cb..a93c6233a4e 100644 --- a/libglusterfs/src/common-utils.h +++ b/libglusterfs/src/common-utils.h @@ -604,6 +604,7 @@ int gf_string2uint64_base10 (const char *str, uint64_t *n); int gf_string2bytesize (const char *str, uint64_t *n); int gf_string2bytesize_size (const char *str, size_t *n); int gf_string2bytesize_uint64 (const char *str, uint64_t *n); +int gf_string2bytesize_int64 (const char *str, int64_t *n); int gf_string2percent_or_bytesize (const char *str, double *n, gf_boolean_t *is_percent); |