From: Yu Watanabe Date: Thu, 6 Apr 2023 21:09:15 +0000 (+0900) Subject: conf-files: introduce _at() variants of conf_files_list() or friends X-Git-Tag: v254-rc1~779^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b1229544a0eaf8d6a4564db3e0a21c5339c1fdd2;p=thirdparty%2Fsystemd.git conf-files: introduce _at() variants of conf_files_list() or friends --- diff --git a/src/basic/conf-files.c b/src/basic/conf-files.c index 9e7428a3c8e..c31fe79ebda 100644 --- a/src/basic/conf-files.c +++ b/src/basic/conf-files.c @@ -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, diff --git a/src/basic/conf-files.h b/src/basic/conf-files.h index 7774ed70541..547c2fc137f 100644 --- a/src/basic/conf-files.h +++ b/src/basic/conf-files.h @@ -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,