52b84b
From 9692477a59c47b5fb6bd6d4702302859296db070 Mon Sep 17 00:00:00 2001
52b84b
From: Filipe Brandenburger <filbranden@google.com>
52b84b
Date: Wed, 23 Jan 2019 19:48:54 -0800
52b84b
Subject: [PATCH] time-util: Introduce parse_sec_def_infinity
52b84b
52b84b
This works like parse_sec() but defaults to USEC_INFINITY when passed an
52b84b
empty string or only whitespace.
52b84b
52b84b
Also introduce config_parse_sec_def_infinity, which can be used to parse
52b84b
config options using this function.
52b84b
52b84b
This is useful for time options that use "infinity" for default and that
52b84b
can be reset by unsetting them.
52b84b
52b84b
Introduce a test case to ensure it works as expected.
52b84b
52b84b
(cherry picked from commit 7b61ce3c44ef5908e817009ce4f9d2a7a37722be)
52b84b
52b84b
Related: #1770379
52b84b
---
52b84b
 src/basic/time-util.c     |  9 +++++++++
52b84b
 src/basic/time-util.h     |  1 +
52b84b
 src/shared/conf-parser.c  |  1 +
52b84b
 src/shared/conf-parser.h  |  1 +
52b84b
 src/test/test-time-util.c | 21 +++++++++++++++++++++
52b84b
 5 files changed, 33 insertions(+)
52b84b
52b84b
diff --git a/src/basic/time-util.c b/src/basic/time-util.c
52b84b
index fe201c398d..c36e462193 100644
52b84b
--- a/src/basic/time-util.c
52b84b
+++ b/src/basic/time-util.c
52b84b
@@ -1072,6 +1072,15 @@ int parse_sec_fix_0(const char *t, usec_t *usec) {
52b84b
         return parse_sec(t, usec);
52b84b
 }
52b84b
 
52b84b
+int parse_sec_def_infinity(const char *t, usec_t *ret) {
52b84b
+        t += strspn(t, WHITESPACE);
52b84b
+        if (isempty(t)) {
52b84b
+                *ret = USEC_INFINITY;
52b84b
+                return 0;
52b84b
+        }
52b84b
+        return parse_sec(t, ret);
52b84b
+}
52b84b
+
52b84b
 int parse_nsec(const char *t, nsec_t *nsec) {
52b84b
         static const struct {
52b84b
                 const char *suffix;
52b84b
diff --git a/src/basic/time-util.h b/src/basic/time-util.h
52b84b
index 344f2dc52e..f5c9ea6327 100644
52b84b
--- a/src/basic/time-util.h
52b84b
+++ b/src/basic/time-util.h
52b84b
@@ -116,6 +116,7 @@ int parse_timestamp(const char *t, usec_t *usec);
52b84b
 
52b84b
 int parse_sec(const char *t, usec_t *usec);
52b84b
 int parse_sec_fix_0(const char *t, usec_t *usec);
52b84b
+int parse_sec_def_infinity(const char *t, usec_t *usec);
52b84b
 int parse_time(const char *t, usec_t *usec, usec_t default_unit);
52b84b
 int parse_nsec(const char *t, nsec_t *nsec);
52b84b
 
52b84b
diff --git a/src/shared/conf-parser.c b/src/shared/conf-parser.c
52b84b
index 2d62fdf05d..246b7431e4 100644
52b84b
--- a/src/shared/conf-parser.c
52b84b
+++ b/src/shared/conf-parser.c
52b84b
@@ -509,6 +509,7 @@ DEFINE_PARSER(unsigned, unsigned, safe_atou);
52b84b
 DEFINE_PARSER(double, double, safe_atod);
52b84b
 DEFINE_PARSER(nsec, nsec_t, parse_nsec);
52b84b
 DEFINE_PARSER(sec, usec_t, parse_sec);
52b84b
+DEFINE_PARSER(sec_def_infinity, usec_t, parse_sec_def_infinity);
52b84b
 DEFINE_PARSER(mode, mode_t, parse_mode);
52b84b
 
52b84b
 int config_parse_iec_size(const char* unit,
52b84b
diff --git a/src/shared/conf-parser.h b/src/shared/conf-parser.h
52b84b
index 16f042d894..a0a5c89c27 100644
52b84b
--- a/src/shared/conf-parser.h
52b84b
+++ b/src/shared/conf-parser.h
52b84b
@@ -127,6 +127,7 @@ CONFIG_PARSER_PROTOTYPE(config_parse_string);
52b84b
 CONFIG_PARSER_PROTOTYPE(config_parse_path);
52b84b
 CONFIG_PARSER_PROTOTYPE(config_parse_strv);
52b84b
 CONFIG_PARSER_PROTOTYPE(config_parse_sec);
52b84b
+CONFIG_PARSER_PROTOTYPE(config_parse_sec_def_infinity);
52b84b
 CONFIG_PARSER_PROTOTYPE(config_parse_nsec);
52b84b
 CONFIG_PARSER_PROTOTYPE(config_parse_mode);
52b84b
 CONFIG_PARSER_PROTOTYPE(config_parse_warn_compat);
52b84b
diff --git a/src/test/test-time-util.c b/src/test/test-time-util.c
52b84b
index 87de8d172c..354a01dd1a 100644
52b84b
--- a/src/test/test-time-util.c
52b84b
+++ b/src/test/test-time-util.c
52b84b
@@ -61,6 +61,26 @@ static void test_parse_sec_fix_0(void) {
52b84b
         assert_se(u == USEC_INFINITY);
52b84b
 }
52b84b
 
52b84b
+static void test_parse_sec_def_infinity(void) {
52b84b
+        usec_t u;
52b84b
+
52b84b
+        log_info("/* %s */", __func__);
52b84b
+
52b84b
+        assert_se(parse_sec_def_infinity("5s", &u) >= 0);
52b84b
+        assert_se(u == 5 * USEC_PER_SEC);
52b84b
+        assert_se(parse_sec_def_infinity("", &u) >= 0);
52b84b
+        assert_se(u == USEC_INFINITY);
52b84b
+        assert_se(parse_sec_def_infinity("     ", &u) >= 0);
52b84b
+        assert_se(u == USEC_INFINITY);
52b84b
+        assert_se(parse_sec_def_infinity("0s", &u) >= 0);
52b84b
+        assert_se(u == 0);
52b84b
+        assert_se(parse_sec_def_infinity("0", &u) >= 0);
52b84b
+        assert_se(u == 0);
52b84b
+        assert_se(parse_sec_def_infinity(" 0", &u) >= 0);
52b84b
+        assert_se(u == 0);
52b84b
+        assert_se(parse_sec_def_infinity("-5s", &u) < 0);
52b84b
+}
52b84b
+
52b84b
 static void test_parse_time(void) {
52b84b
         usec_t u;
52b84b
 
52b84b
@@ -420,6 +440,7 @@ int main(int argc, char *argv[]) {
52b84b
 
52b84b
         test_parse_sec();
52b84b
         test_parse_sec_fix_0();
52b84b
+        test_parse_sec_def_infinity();
52b84b
         test_parse_time();
52b84b
         test_parse_nsec();
52b84b
         test_format_timespan(1);