diff --git a/dcapi/common/cfg-client.c b/dcapi/common/cfg-client.c index 2b1b7c333d..f0db454fcb 100644 --- a/dcapi/common/cfg-client.c +++ b/dcapi/common/cfg-client.c @@ -154,11 +154,16 @@ int DC_getCfgInt(const char *key, int defaultValue) val = strtol(pairs[i].value, &p, 10); /* Check for unit suffixes */ - if (p && *p && _DC_processSuffix(&val, p)) + if (p && *p) { - DC_log(LOG_WARNING, "Configuration value for key %s is not " - "a valid number, ignoring", key); - return defaultValue; + long mult = _DC_processSuffix(p); + if (mult == -1) + { + DC_log(LOG_WARNING, "Configuration value for key %s " + "is not a valid number, ignoring", key); + return defaultValue; + } + val *= mult; } return val; } diff --git a/dcapi/common/cfg-master.c b/dcapi/common/cfg-master.c index 7fd28f7034..45d932f5ba 100644 --- a/dcapi/common/cfg-master.c +++ b/dcapi/common/cfg-master.c @@ -90,15 +90,56 @@ static int getCfgInt(const char *group, const char *key, int defaultValue, retval = strtol(value, &p, 10); /* Check for unit suffixes */ - if (p && *p && _DC_processSuffix(&retval, p)) + if (p && *p) + { + long mult = _DC_processSuffix(p); + if (mult == -1) + { + DC_log(LOG_WARNING, "Configuration value for key %s " + "is not a valid number, ignoring", key); + g_free(value); + *err = 1; + return defaultValue; + } + retval *= mult; + } + + g_free(value); + *err = 0; + return retval; +} + +static double getCfgDouble(const char *group, const char *key, + double defaultValue, int *err) +{ + char *value, *p; + double retval; + + if (!config || !key) + return defaultValue; + + value = g_key_file_get_value(config, group, key, NULL); + if (!value) { - DC_log(LOG_WARNING, "Configuration value for key %s is not " - "a valid number, ignoring", key); - g_free(value); *err = 1; return defaultValue; } + retval = strtod(value, &p); + if (p && *p) + { + long long mult = _DC_processSuffix(p); + if (mult == -1) + { + DC_log(LOG_WARNING, "Configuration value for key %s " + "is not a valid number, ignoring", key); + g_free(value); + *err = 1; + return defaultValue; + } + retval *= mult; + } + g_free(value); *err = 0; return retval; @@ -148,3 +189,21 @@ int DC_getClientCfgInt(const char *clientName, const char *key, val = getCfgInt(MASTER_GROUP, key, defaultValue, &err); return val; } + +double DC_getClientCfgDouble(const char *clientName, const char *key, + double defaultValue, int fallbackGlobal) +{ + char *group; + double val; + int err; + + if (!clientName) + return defaultValue; + + group = g_strdup_printf("Client-%s", clientName); + val = getCfgDouble(group, key, defaultValue, &err); + g_free(group); + if (err) + val = getCfgDouble(MASTER_GROUP, key, defaultValue, &err); + return val; +} diff --git a/dcapi/common/util.c b/dcapi/common/util.c index ac1b9d062c..b93027def2 100644 --- a/dcapi/common/util.c +++ b/dcapi/common/util.c @@ -101,23 +101,21 @@ error: return -1; } -int _DC_processSuffix(long *value, const char *suffix) +long long _DC_processSuffix(const char *suffix) { while (*suffix == ' ' || *suffix == '\t') suffix++; if (!strcasecmp(suffix, "kb") || !strcasecmp(suffix, "kib")) - *value <<= 10; + return 1ll << 10; else if (!strcasecmp(suffix, "mb") || !strcasecmp(suffix, "mib")) - *value <<= 20; + return 1ll << 20; else if (!strcasecmp(suffix, "gb") || !strcasecmp(suffix, "gib")) - *value <<= 30; + return 1ll << 30; else if (!strcasecmp(suffix, "min")) - *value *= 60; + return 60ll; else if (!strcasecmp(suffix, "h") || !strcasecmp(suffix, "hour")) - *value *= 60 * 60; + return 60ll * 60; else if (!strcasecmp(suffix, "day")) - *value *= 24 * 60 * 60; - else - return -1; - return 0; + return 24ll * 60 * 60; + return -1; } diff --git a/dcapi/include/dc.h b/dcapi/include/dc.h index 5ace9ac5c2..de2b9fa25e 100644 --- a/dcapi/include/dc.h +++ b/dcapi/include/dc.h @@ -136,6 +136,10 @@ char *DC_getClientCfgStr(const char *clientName, const char *key, int DC_getClientCfgInt(const char *clientName, const char *key, int defaultValue, int fallbackGlobal); +/* Queries per-client configuration variables */ +double DC_getClientCfgDouble(const char *clientName, const char *key, + double defaultValue, int fallbackGlobal); + /******************************************************************** * Function prototypes: Work unit management */ diff --git a/dcapi/include/dc_internal.h b/dcapi/include/dc_internal.h index 1a2cbe20e6..e5d19b77e9 100644 --- a/dcapi/include/dc_internal.h +++ b/dcapi/include/dc_internal.h @@ -29,7 +29,7 @@ int _DC_parseCfg(const char *cfgfile); int _DC_copyFile(const char *src, const char *dst); /* Processes a unit suffix and adjust the value accordingly */ -int _DC_processSuffix(long *value, const char *suffix); +long long _DC_processSuffix(const char *suffix); #ifdef __cplusplus }