2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
27 #include "conf-files.h"
28 #include "dirent-util.h"
34 #include "path-util.h"
35 #include "string-util.h"
39 static int files_add(Hashmap *h, const char *root, const char *path, const char *suffix) {
40 _cleanup_closedir_ DIR *dir = NULL;
47 dirpath = prefix_roota(root, path);
49 dir = opendir(dirpath);
56 FOREACH_DIRENT(de, dir, return -errno) {
59 if (!dirent_is_file_with_suffix(de, suffix))
62 p = strjoin(dirpath, "/", de->d_name);
66 r = hashmap_put(h, basename(p), p);
68 log_debug("Skipping overridden file: %s.", p);
74 log_debug("Duplicate file %s", p);
82 static int base_cmp(const void *a, const void *b) {
85 s1 = *(char * const *)a;
86 s2 = *(char * const *)b;
87 return strcmp(basename(s1), basename(s2));
90 static int conf_files_list_strv_internal(char ***strv, const char *suffix, const char *root, char **dirs) {
91 _cleanup_hashmap_free_ Hashmap *fh = NULL;
97 /* This alters the dirs string array */
98 if (!path_strv_resolve_uniq(dirs, root))
101 fh = hashmap_new(&string_hash_ops);
105 STRV_FOREACH(p, dirs) {
106 r = files_add(fh, root, *p, suffix);
110 log_debug_errno(r, "Failed to search for files in %s, ignoring: %m", *p);
113 files = hashmap_get_strv(fh);
117 qsort_safe(files, hashmap_size(fh), sizeof(char *), base_cmp);
123 #if 0 /// UNNEEDED by elogind
124 int conf_files_list_strv(char ***strv, const char *suffix, const char *root, const char* const* dirs) {
125 _cleanup_strv_free_ char **copy = NULL;
129 copy = strv_copy((char**) dirs);
133 return conf_files_list_strv_internal(strv, suffix, root, copy);
136 int conf_files_list(char ***strv, const char *suffix, const char *root, const char *dir, ...) {
137 _cleanup_strv_free_ char **dirs = NULL;
143 dirs = strv_new_ap(dir, ap);
149 return conf_files_list_strv_internal(strv, suffix, root, dirs);
153 int conf_files_list_nulstr(char ***strv, const char *suffix, const char *root, const char *d) {
154 _cleanup_strv_free_ char **dirs = NULL;
158 dirs = strv_split_nulstr(d);
162 return conf_files_list_strv_internal(strv, suffix, root, dirs);