96237b5
From 4cae8946d581a6ecf0b26e154bf9c00e390024b2 Mon Sep 17 00:00:00 2001
96237b5
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
96237b5
Date: Sun, 2 Mar 2014 00:05:16 -0500
96237b5
Subject: [PATCH] Allow fractional parts in disk sizes
96237b5
96237b5
It seems natural to be able to say SystemMaxUsage=1.5G.
96237b5
96237b5
https://bugzilla.redhat.com/show_bug.cgi?id=1047568
96237b5
(cherry picked from commit 9480794b277b5ce33e467578ed669996df576bb9)
96237b5
---
96237b5
 src/shared/util.c    | 24 ++++++++++++++++++++++--
96237b5
 src/test/test-util.c | 42 +++++++++++++++++++++++++++++++++++++-----
96237b5
 2 files changed, 59 insertions(+), 7 deletions(-)
96237b5
96237b5
diff --git a/src/shared/util.c b/src/shared/util.c
96237b5
index 5cb598c..3164515 100644
96237b5
--- a/src/shared/util.c
96237b5
+++ b/src/shared/util.c
96237b5
@@ -2198,6 +2198,8 @@ int parse_size(const char *t, off_t base, off_t *size) {
96237b5
         p = t;
96237b5
         do {
96237b5
                 long long l;
96237b5
+                unsigned long long l2;
96237b5
+                double frac = 0;
96237b5
                 char *e;
96237b5
                 unsigned i;
96237b5
 
96237b5
@@ -2213,14 +2215,32 @@ int parse_size(const char *t, off_t base, off_t *size) {
96237b5
                 if (e == p)
96237b5
                         return -EINVAL;
96237b5
 
96237b5
+                if (*e == '.') {
96237b5
+                        e++;
96237b5
+                        if (*e >= '0' && *e <= '9') {
96237b5
+                                char *e2;
96237b5
+
96237b5
+                                /* strotoull itself would accept space/+/- */
96237b5
+                                l2 = strtoull(e, &e2, 10);
96237b5
+
96237b5
+                                if (errno == ERANGE)
96237b5
+                                        return -errno;
96237b5
+
96237b5
+                                /* Ignore failure. E.g. 10.M is valid */
96237b5
+                                frac = l2;
96237b5
+                                for (; e < e2; e++)
96237b5
+                                        frac /= 10;
96237b5
+                        }
96237b5
+                }
96237b5
+
96237b5
                 e += strspn(e, WHITESPACE);
96237b5
 
96237b5
                 for (i = 0; i < n_entries; i++)
96237b5
                         if (startswith(e, table[i].suffix)) {
96237b5
                                 unsigned long long tmp;
96237b5
-                                if ((unsigned long long) l > ULLONG_MAX / table[i].factor)
96237b5
+                                if ((unsigned long long) l + (frac > 0) > ULLONG_MAX / table[i].factor)
96237b5
                                         return -ERANGE;
96237b5
-                                tmp = l * table[i].factor;
96237b5
+                                tmp = l * table[i].factor + (unsigned long long) (frac * table[i].factor);
96237b5
                                 if (tmp > ULLONG_MAX - r)
96237b5
                                         return -ERANGE;
96237b5
 
96237b5
diff --git a/src/test/test-util.c b/src/test/test-util.c
96237b5
index b718206..74f83a2 100644
96237b5
--- a/src/test/test-util.c
96237b5
+++ b/src/test/test-util.c
96237b5
@@ -30,6 +30,7 @@
96237b5
 #include "strv.h"
96237b5
 #include "def.h"
96237b5
 #include "fileio.h"
96237b5
+#include "conf-parser.h"
96237b5
 
96237b5
 static void test_streq_ptr(void) {
96237b5
         assert_se(streq_ptr(NULL, NULL));
96237b5
@@ -441,17 +442,32 @@ static void test_parse_size(void) {
96237b5
         assert_se(parse_size("111", 1024, &bytes) == 0);
96237b5
         assert_se(bytes == 111);
96237b5
 
96237b5
+        assert_se(parse_size("111.4", 1024, &bytes) == 0);
96237b5
+        assert_se(bytes == 111);
96237b5
+
96237b5
         assert_se(parse_size(" 112 B", 1024, &bytes) == 0);
96237b5
         assert_se(bytes == 112);
96237b5
 
96237b5
-        assert_se(parse_size("3 K", 1024, &bytes) == 0);
96237b5
+        assert_se(parse_size(" 112.6 B", 1024, &bytes) == 0);
96237b5
+        assert_se(bytes == 112);
96237b5
+
96237b5
+        assert_se(parse_size("3.5 K", 1024, &bytes) == 0);
96237b5
+        assert_se(bytes == 3*1024 + 512);
96237b5
+
96237b5
+        assert_se(parse_size("3. K", 1024, &bytes) == 0);
96237b5
+        assert_se(bytes == 3*1024);
96237b5
+
96237b5
+        assert_se(parse_size("3.0 K", 1024, &bytes) == 0);
96237b5
         assert_se(bytes == 3*1024);
96237b5
 
96237b5
-        assert_se(parse_size(" 4 M 11K", 1024, &bytes) == 0);
96237b5
-        assert_se(bytes == 4*1024*1024 + 11 * 1024);
96237b5
+        assert_se(parse_size("3. 0 K", 1024, &bytes) == 0);
96237b5
+        assert_se(bytes == 3);
96237b5
 
96237b5
-        assert_se(parse_size("3B3G", 1024, &bytes) == 0);
96237b5
-        assert_se(bytes == 3ULL*1024*1024*1024 + 3);
96237b5
+        assert_se(parse_size(" 4 M 11.5K", 1024, &bytes) == 0);
96237b5
+        assert_se(bytes == 4*1024*1024 + 11 * 1024 + 512);
96237b5
+
96237b5
+        assert_se(parse_size("3B3.5G", 1024, &bytes) == 0);
96237b5
+        assert_se(bytes == 3ULL*1024*1024*1024 + 512*1024*1024 + 3);
96237b5
 
96237b5
         assert_se(parse_size("3B3G4T", 1024, &bytes) == 0);
96237b5
         assert_se(bytes == (4ULL*1024 + 3)*1024*1024*1024 + 3);
96237b5
@@ -464,6 +480,10 @@ static void test_parse_size(void) {
96237b5
 
96237b5
         assert_se(parse_size("12X", 1024, &bytes) == -EINVAL);
96237b5
 
96237b5
+        assert_se(parse_size("12.5X", 1024, &bytes) == -EINVAL);
96237b5
+
96237b5
+        assert_se(parse_size("12.5e3", 1024, &bytes) == -EINVAL);
96237b5
+
96237b5
         assert_se(parse_size("1024E", 1024, &bytes) == -ERANGE);
96237b5
         assert_se(parse_size("-1", 1024, &bytes) == -ERANGE);
96237b5
         assert_se(parse_size("-1024E", 1024, &bytes) == -ERANGE);
96237b5
@@ -473,6 +493,14 @@ static void test_parse_size(void) {
96237b5
         assert_se(parse_size("-10B 20K", 1024, &bytes) == -ERANGE);
96237b5
 }
96237b5
 
96237b5
+static void test_config_parse_iec_off(void) {
96237b5
+        off_t offset = 0;
96237b5
+        assert_se(config_parse_iec_off(NULL, "/this/file", 11, "Section", 22, "Size", 0, "4M", &offset, NULL) == 0);
96237b5
+        assert_se(offset == 4 * 1024 * 1024);
96237b5
+
96237b5
+        assert_se(config_parse_iec_off(NULL, "/this/file", 11, "Section", 22, "Size", 0, "4.5M", &offset, NULL) == 0);
96237b5
+}
96237b5
+
96237b5
 static void test_strextend(void) {
96237b5
         _cleanup_free_ char *str = strdup("0123");
96237b5
         strextend(&str, "456", "78", "9", NULL);
96237b5
@@ -589,6 +617,9 @@ static void test_writing_tmpfile(void) {
96237b5
 }
96237b5
 
96237b5
 int main(int argc, char *argv[]) {
96237b5
+        log_parse_environment();
96237b5
+        log_open();
96237b5
+
96237b5
         test_streq_ptr();
96237b5
         test_first_word();
96237b5
         test_close_many();
96237b5
@@ -618,6 +649,7 @@ int main(int argc, char *argv[]) {
96237b5
         test_get_process_comm();
96237b5
         test_protect_errno();
96237b5
         test_parse_size();
96237b5
+        test_config_parse_iec_off();
96237b5
         test_strextend();
96237b5
         test_strrep();
96237b5
         test_split_pair();