]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
shared/conf-parser: allow sections to be silently ignored with new -Section syntax
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 22 Nov 2019 10:16:17 +0000 (11:16 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 22 Nov 2019 14:27:22 +0000 (15:27 +0100)
If we ignore any uknown section, we will not be able to show any
warning if a typo in a section name is made. Let's reverse our
approach, and explicitly list sections to ignore instead.

I opted to make use the same section list for this, instead of adding a second
list, because this list is passed through to many functions and adding yet
another parameter to the long signature would be very noisy.

src/shared/conf-parser.c
src/test/test-conf-parser.c

index 648ac1aa948270322d541c90d3230b0b9780df59..90b31148f3e66a9cc6ccbf27ed089599c53859dc 100644 (file)
@@ -221,8 +221,19 @@ static int parse_line(
                         return -ENOMEM;
 
                 if (sections && !nulstr_contains(sections, n)) {
+                        bool ignore = flags & CONFIG_PARSE_RELAXED;
+                        const char *t;
 
-                        if (!(flags & CONFIG_PARSE_RELAXED) && !startswith(n, "X-"))
+                        ignore = ignore || startswith(n, "X-");
+
+                        if (!ignore)
+                                NULSTR_FOREACH(t, sections)
+                                        if (streq_ptr(n, startswith(t, "-"))) {
+                                                ignore = true;
+                                                break;
+                                        }
+
+                        if (!ignore)
                                 log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown section '%s'. Ignoring.", n);
 
                         free(n);
index 597265efa676c947ad89d7902bc20e3b2846c0b1..b392a2b5ec58168a35d65b0781ef44e413f1fdf6 100644 (file)
@@ -299,6 +299,15 @@ static const char* const config_file[] = {
         "[Section]\n"
         "setting1="          /* many continuation lines, together above the limit */
         x1000(x1000("x") x10("abcde") "\\\n") "xxx",
+
+        "[Section]\n"
+        "setting1=2\n"
+        "[NoWarnSection]\n"
+        "setting1=3\n"
+        "[WarnSection]\n"
+        "setting1=3\n"
+        "[X-Section]\n"
+        "setting1=3\n",
 };
 
 static void test_config_parse(unsigned i, const char *s) {
@@ -325,14 +334,12 @@ static void test_config_parse(unsigned i, const char *s) {
                          const char *sections,
                          ConfigItemLookup lookup,
                          const void *table,
-                         bool relaxed,
-                         bool allow_include,
-                         bool warn,
+                         ConfigParseFlags flags,
                          void *userdata)
         */
 
         r = config_parse(NULL, name, f,
-                         "Section\0",
+                         "Section\0-NoWarnSection\0",
                          config_item_table_lookup, items,
                          CONFIG_PARSE_WARN, NULL);
 
@@ -366,6 +373,11 @@ static void test_config_parse(unsigned i, const char *s) {
                 assert_se(r == -ENOBUFS);
                 assert_se(setting1 == NULL);
                 break;
+
+        case 17:
+                assert_se(r == 0);
+                assert_se(streq(setting1, "2"));
+                break;
         }
 }