From f8e71de7270d91d18f93af0e6fd1c92a571bae04 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
Date: Sun, 2 Mar 2014 00:05:16 -0500
Subject: [PATCH] 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 9480794b277b5ce33e467578ed669996df576bb9)
Conflicts:
src/test/test-util.c
---
src/shared/util.c | 24 ++++++++++++++++++++++--
1 file changed, 22 insertions(+), 2 deletions(-)
diff --git a/src/shared/util.c b/src/shared/util.c
index 75007792cc..e75474781b 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -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;