chiark / gitweb /
shared/conf-parser: add config_parse_many which takes strv with dirs
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sat, 10 Sep 2016 11:19:41 +0000 (12:19 +0100)
committerSven Eden <yamakuzure@gmx.net>
Wed, 5 Jul 2017 06:50:53 +0000 (08:50 +0200)
This way we don't have to create a nulstr just to unpack it in a moment.

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

index af005dee06d92af44b8c3ccf4467dc582c470418..92baba60378a1c2d7b706a673786f301baab1b4d 100644 (file)
@@ -395,24 +395,18 @@ int config_parse(const char *unit,
         return 0;
 }
 
         return 0;
 }
 
-/* Parse each config file in the specified directories. */
-int config_parse_many_nulstr(
+static int config_parse_many_files(
                 const char *conf_file,
                 const char *conf_file,
-                const char *conf_file_dirs,
+                char **files,
                 const char *sections,
                 ConfigItemLookup lookup,
                 const void *table,
                 bool relaxed,
                 void *userdata) {
 
                 const char *sections,
                 ConfigItemLookup lookup,
                 const void *table,
                 bool relaxed,
                 void *userdata) {
 
-        _cleanup_strv_free_ char **files = NULL;
         char **fn;
         int r;
 
         char **fn;
         int r;
 
-        r = conf_files_list_nulstr(&files, ".conf", NULL, conf_file_dirs);
-        if (r < 0)
-                return r;
-
         if (conf_file) {
                 r = config_parse(NULL, conf_file, NULL, sections, lookup, table, relaxed, false, true, userdata);
                 if (r < 0)
         if (conf_file) {
                 r = config_parse(NULL, conf_file, NULL, sections, lookup, table, relaxed, false, true, userdata);
                 if (r < 0)
@@ -428,6 +422,56 @@ int config_parse_many_nulstr(
         return 0;
 }
 
         return 0;
 }
 
+/* Parse each config file in the directories specified as nulstr. */
+int config_parse_many_nulstr(
+                const char *conf_file,
+                const char *conf_file_dirs,
+                const char *sections,
+                ConfigItemLookup lookup,
+                const void *table,
+                bool relaxed,
+                void *userdata) {
+
+        _cleanup_strv_free_ char **files = NULL;
+        int r;
+
+        r = conf_files_list_nulstr(&files, ".conf", NULL, conf_file_dirs);
+        if (r < 0)
+                return r;
+
+        return config_parse_many_files(conf_file, files,
+                                       sections, lookup, table, relaxed, userdata);
+}
+
+/* Parse each config file in the directories specified as strv. */
+int config_parse_many(
+                const char *conf_file,
+                const char* const* conf_file_dirs,
+                const char *dropin_dirname,
+                const char *sections,
+                ConfigItemLookup lookup,
+                const void *table,
+                bool relaxed,
+                void *userdata) {
+
+        _cleanup_strv_free_ char **dropin_dirs = NULL;
+        _cleanup_strv_free_ char **files = NULL;
+        const char *suffix;
+        int r;
+
+        suffix = strjoina("/", dropin_dirname);
+        r = strv_extend_strv_concat(&dropin_dirs, (char**) conf_file_dirs, suffix);
+        if (r < 0)
+                return r;
+
+        r = conf_files_list_strv(&files, ".conf", NULL, (const char* const*) dropin_dirs);
+        if (r < 0)
+                return r;
+
+        return config_parse_many_files(conf_file, files,
+                                       sections, lookup, table, relaxed, userdata);
+}
+
 #define DEFINE_PARSER(type, vartype, conv_func)                         \
         int config_parse_##type(                                        \
                         const char *unit,                               \
 #define DEFINE_PARSER(type, vartype, conv_func)                         \
         int config_parse_##type(                                        \
                         const char *unit,                               \
index 19257abedcf3a66a65c8a91610c3955dabf98b87..2ca5aa63cde90a392a997d8902bcdc9609fa6b75 100644 (file)
@@ -105,6 +105,16 @@ int config_parse_many_nulstr(
                 bool relaxed,
                 void *userdata);
 
                 bool relaxed,
                 void *userdata);
 
+int config_parse_many(
+                const char *conf_file,      /* possibly NULL */
+                const char* const* conf_file_dirs,
+                const char *dropin_dirname,
+                const char *sections,       /* nulstr */
+                ConfigItemLookup lookup,
+                const void *table,
+                bool relaxed,
+                void *userdata);
+
 /* Generic parsers */
 int config_parse_int(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
 int config_parse_unsigned(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
 /* Generic parsers */
 int config_parse_int(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
 int config_parse_unsigned(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);