diff options
-rw-r--r-- | quota.h | 2 | ||||
-rw-r--r-- | quotasys.c | 31 | ||||
-rw-r--r-- | quotasys.h | 3 | ||||
-rw-r--r-- | setquota.8 | 7 | ||||
-rw-r--r-- | setquota.c | 17 |
5 files changed, 58 insertions, 2 deletions
@@ -2,9 +2,11 @@ #define GUARD_QUOTA_H #include <sys/types.h> +#include <stdint.h> typedef u_int32_t qid_t; /* Type in which we store ids in memory */ typedef int64_t qsize_t; /* Type in which we store size limitations */ +#define QSIZE_MAX INT64_MAX /* Maximum value storable in qsize_t */ #define MAXQUOTAS 2 #define USRQUOTA 0 /* element used for user quotas */ @@ -367,6 +367,37 @@ void space2str(qsize_t space, char *buf, int format) } /* + * Convert block number with unit from string to quota blocks. + * Return NULL on success, static error message otherwise. + */ +const char *str2space(const char *string, qsize_t *space) +{ + char *unit; + unsigned long long int number; + int unit_shift; + + number = strtoull(string, &unit, 0); + if (ULLONG_MAX == number) + return _("Integer overflow while parsing space number."); + + if (!unit || unit[0] == '\0' || !strcmp(unit, _("K"))) + unit_shift = 0; + else if (!strcmp(unit, _("M"))) + unit_shift = 10; + else if (!strcmp(unit, _("G"))) + unit_shift = 20; + else if (!strcmp(unit, _("T"))) + unit_shift = 30; + else + return _("Unknown space binary unit. " + "Valid units are K, M, G, T."); + if (number > (QSIZE_MAX >> unit_shift)) + return _("Integer overflow while interpreting space unit."); + *space = number << unit_shift; + return NULL; +} + +/* * Convert number to some nice short form for printing */ void number2str(unsigned long long num, char *buf, int format) @@ -103,6 +103,9 @@ int str2timeunits(time_t, char *, time_t *); /* Convert number in quota blocks to short printable form */ void space2str(qsize_t, char *, int); +/* Convert block number with unit from string to quota blocks */ +const char *str2space(const char *string, qsize_t *space); + /* Convert number to short printable form */ void number2str(unsigned long long, char *, int); @@ -182,6 +182,13 @@ Go through all filesystems with quota in .B /etc/mtab and perform setting. .PP +.I block-softlimit +and +.I block-hardlimit +are interpreted as multiples of kibibyte (1024 bytes) blocks by default. +Symbols K, M, G, and T can be appended to numeric value to express kibibytes, +mebibytes, gibibytes, and tebibytes. +.PP To disable a quota, set the corresponding parameter to 0. To change quotas for several filesystems, invoke once for each filesystem. .PP @@ -93,6 +93,19 @@ static qsize_t parse_unum(char *str, char *msg) return ret; } +/* Convert block size to number - print errstr message in case of failure */ +static qsize_t parse_blocksize(const char *str, const char *msg) +{ + qsize_t ret; + const char *error = str2space(str, &ret); + + if (error) { + errstr(_("%s: %s: %s\n"), msg, str, error); + usage(); + } + return ret; +} + /* Convert our flags to quota type */ static inline int flag2type(int flags) { @@ -226,8 +239,8 @@ static void parse_options(int argcnt, char **argstr) if (!(flags & (FL_GRACE | FL_BATCH))) { id = name2id(argstr[optind++], flag2type(flags), !!(flags & FL_NUMNAMES), NULL); if (!(flags & (FL_GRACE | FL_INDIVIDUAL_GRACE | FL_PROTO))) { - toset.dqb_bsoftlimit = parse_unum(argstr[optind++], _("Bad block softlimit")); - toset.dqb_bhardlimit = parse_unum(argstr[optind++], _("Bad block hardlimit")); + toset.dqb_bsoftlimit = parse_blocksize(argstr[optind++], _("Bad block softlimit")); + toset.dqb_bhardlimit = parse_blocksize(argstr[optind++], _("Bad block hardlimit")); toset.dqb_isoftlimit = parse_unum(argstr[optind++], _("Bad inode softlimit")); toset.dqb_ihardlimit = parse_unum(argstr[optind++], _("Bad inode hardlimit")); } |