]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
conf-files: introduce _at() variants of conf_files_list() or friends
authorYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 6 Apr 2023 21:09:15 +0000 (06:09 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 7 Apr 2023 20:09:48 +0000 (05:09 +0900)
src/basic/conf-files.c
src/basic/conf-files.h

index 9e7428a3c8ead4eae772170dd36aba6aa20898f9..c31fe79ebdaaae922cbdb39e2fba4ad5d3318930 100644 (file)
@@ -180,6 +180,41 @@ int conf_files_list_strv(
         return copy_and_sort_files_from_hashmap(fh, ret);
 }
 
+int conf_files_list_strv_at(
+                char ***ret,
+                const char *suffix,
+                int rfd,
+                unsigned flags,
+                const char * const *dirs) {
+
+        _cleanup_hashmap_free_ Hashmap *fh = NULL;
+        _cleanup_set_free_ Set *masked = NULL;
+        int r;
+
+        assert(rfd >= 0 || rfd == AT_FDCWD);
+        assert(ret);
+
+        STRV_FOREACH(p, dirs) {
+                _cleanup_closedir_ DIR *dir = NULL;
+                _cleanup_free_ char *path = NULL;
+
+                r = chase_and_opendirat(rfd, *p, CHASE_AT_RESOLVE_IN_ROOT, &path, &dir);
+                if (r < 0) {
+                        if (r != -ENOENT)
+                                log_debug_errno(r, "Failed to chase and open directory '%s', ignoring: %m", *p);
+                        continue;
+                }
+
+                r = files_add(dir, path, &fh, &masked, suffix, flags);
+                if (r == -ENOMEM)
+                        return r;
+                if (r < 0)
+                        log_debug_errno(r, "Failed to search for files in '%s', ignoring: %m", path);
+        }
+
+        return copy_and_sort_files_from_hashmap(fh, ret);
+}
+
 int conf_files_insert(char ***strv, const char *root, char **dirs, const char *path) {
         /* Insert a path into strv, at the place honouring the usual sorting rules:
          * - we first compare by the basename
@@ -252,6 +287,10 @@ int conf_files_list(char ***ret, const char *suffix, const char *root, unsigned
         return conf_files_list_strv(ret, suffix, root, flags, STRV_MAKE_CONST(dir));
 }
 
+int conf_files_list_at(char ***ret, const char *suffix, int rfd, unsigned flags, const char *dir) {
+        return conf_files_list_strv_at(ret, suffix, rfd, flags, STRV_MAKE_CONST(dir));
+}
+
 int conf_files_list_nulstr(char ***ret, const char *suffix, const char *root, unsigned flags, const char *dirs) {
         _cleanup_strv_free_ char **d = NULL;
 
@@ -264,6 +303,18 @@ int conf_files_list_nulstr(char ***ret, const char *suffix, const char *root, un
         return conf_files_list_strv(ret, suffix, root, flags, (const char**) d);
 }
 
+int conf_files_list_nulstr_at(char ***ret, const char *suffix, int rfd, unsigned flags, const char *dirs) {
+        _cleanup_strv_free_ char **d = NULL;
+
+        assert(ret);
+
+        d = strv_split_nulstr(dirs);
+        if (!d)
+                return -ENOMEM;
+
+        return conf_files_list_strv_at(ret, suffix, rfd, flags, (const char**) d);
+}
+
 int conf_files_list_with_replacement(
                 const char *root,
                 char **config_dirs,
index 7774ed705413aff5b8a0b9505e6880f53b09149c..547c2fc137f0debca34a4d0d48c1c70601d14676 100644 (file)
@@ -12,8 +12,11 @@ enum {
 };
 
 int conf_files_list(char ***ret, const char *suffix, const char *root, unsigned flags, const char *dir);
+int conf_files_list_at(char ***ret, const char *suffix, int rfd, unsigned flags, const char *dir);
 int conf_files_list_strv(char ***ret, const char *suffix, const char *root, unsigned flags, const char* const* dirs);
+int conf_files_list_strv_at(char ***ret, const char *suffix, int rfd, unsigned flags, const char * const *dirs);
 int conf_files_list_nulstr(char ***ret, const char *suffix, const char *root, unsigned flags, const char *dirs);
+int conf_files_list_nulstr_at(char ***ret, const char *suffix, int rfd, unsigned flags, const char *dirs);
 int conf_files_insert(char ***strv, const char *root, char **dirs, const char *path);
 int conf_files_list_with_replacement(
                 const char *root,