Allow fractional parts in disk sizes

It seems natural to be able to say SystemMaxUsage=1.5G.

https://bugzilla.redhat.com/show_bug.cgi?id=1047568
(cherry picked from commit 9480794b27)

Conflicts:
	src/test/test-util.c
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2014-03-05 00:35:41 -05:00
parent 33b2fe7716
commit f8e71de727
+22 -2
View File
@@ -2285,6 +2285,8 @@ int parse_bytes(const char *t, off_t *bytes) {
p = t;
do {
long long l;
unsigned long long l2;
double frac = 0;
char *e;
unsigned i;
@@ -2300,14 +2302,32 @@ int parse_bytes(const char *t, off_t *bytes) {
if (e == p)
return -EINVAL;
if (*e == '.') {
e++;
if (*e >= '0' && *e <= '9') {
char *e2;
/* strotoull itself would accept space/+/- */
l2 = strtoull(e, &e2, 10);
if (errno == ERANGE)
return -errno;
/* Ignore failure. E.g. 10.M is valid */
frac = l2;
for (; e < e2; e++)
frac /= 10;
}
}
e += strspn(e, WHITESPACE);
for (i = 0; i < ELEMENTSOF(table); i++)
if (startswith(e, table[i].suffix)) {
unsigned long long tmp;
if ((unsigned long long) l > ULLONG_MAX / table[i].factor)
if ((unsigned long long) l + (frac > 0) > ULLONG_MAX / table[i].factor)
return -ERANGE;
tmp = l * table[i].factor;
tmp = l * table[i].factor + (unsigned long long) (frac * table[i].factor);
if (tmp > ULLONG_MAX - r)
return -ERANGE;