anitazha / rpms / systemd

Forked from rpms/systemd 3 years ago
Clone

Blame SOURCES/0396-time-util-Introduce-parse_sec_def_infinity.patch

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