From: zacheiss Date: Mon, 22 Sep 2003 20:44:16 +0000 (+0000) Subject: Allow quotas to be specified in a form like '500mb' rather than '500000' X-Git-Url: http://andersk.mit.edu/gitweb/moira.git/commitdiff_plain/9db8c552b1d20b3ec3f10136872f9347466a70b0 Allow quotas to be specified in a form like '500mb' rather than '500000' or '1gb' instead of '1000000'. --- diff --git a/clients/moira/f_defs.h b/clients/moira/f_defs.h index b69ea23c..824ebcee 100644 --- a/clients/moira/f_defs.h +++ b/clients/moira/f_defs.h @@ -143,6 +143,7 @@ int GetQuotaByFilesys(int argc, char **argv); int AddQuota(int argc, char **argv); int UpdateQuota(int argc, char **argv); int DeleteQuota(int argc, char **argv); +char *ParseQuotaString(char *quota); /* user.c */ diff --git a/clients/moira/quota.c b/clients/moira/quota.c index 96adfad2..9ad0a40e 100644 --- a/clients/moira/quota.c +++ b/clients/moira/quota.c @@ -150,6 +150,7 @@ static char **GetQuotaArgs(Bool quota) { char **args = malloc(MAX_ARGS_SIZE * sizeof(char *)); int af; + char *canon; if (!args) { @@ -206,6 +207,11 @@ static char **GetQuotaArgs(Bool quota) return NULL; if (!ValidName(args[Q_QUOTA])) return NULL; + canon = ParseQuotaString(args[Q_QUOTA]); + if (!canon) + return NULL; + free(args[Q_QUOTA]); + args[Q_QUOTA] = canon; } return args; } @@ -362,6 +368,7 @@ static void RealUpdateQuota(char **info) { int status; char temp_buf[BUFSIZ]; + char *canon; sprintf(temp_buf, "New quota for filesystem %s (in KB)", info[Q_FILESYS]); if (GetValueFromUser(temp_buf, &info[Q_QUOTA]) == SUB_ERROR) @@ -370,6 +377,12 @@ static void RealUpdateQuota(char **info) return; } + canon = ParseQuotaString(info[Q_QUOTA]); + if (!canon) + return; + free(info[Q_QUOTA]); + info[Q_QUOTA] = canon; + if ((status = do_mr_query("update_quota", 4, info, NULL, NULL)) != MR_SUCCESS) { @@ -475,3 +488,44 @@ int DeleteQuota(int argc, char **argv) FreeQueue(top); return DM_NORMAL; } + +char *ParseQuotaString(char *quota) +{ + char *s, *value; + float ngigs, nmegs; + int calcvalue; + + s = quota; + while (*s && (isdigit(*s) || (*s == '.'))) + s++; + + /* If we didn't find a unit specifier, just return the old value. */ + if (!*s) + return strdup(quota); + + switch (*s) { + case 'm': + case 'M': + /* value specified in megabytes. */ + if (!sscanf(quota, "%f2", &nmegs)) + return strdup(quota); + calcvalue = (int)(nmegs * 1000); + break; + case 'g': + case 'G': + /* value specified in gigabytes. */ + if (!sscanf(quota, "%f2", &ngigs)) + return strdup(quota); + calcvalue = (int)(ngigs * 1000 * 1000); + break; + default: + /* Couldn't parse it. Just return the old value. */ + return strdup(quota); + } + + value = malloc(BUFSIZ); + if (!value) + return NULL; + sprintf(value, "%d", calcvalue); + return value; +}