1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
36 #include "path-util.h"
38 #include "conf-files.h"
40 static int files_add(Hashmap *h, const char *root, const char *path, const char *suffix) {
41 _cleanup_closedir_ DIR *dir = NULL;
47 dirpath = strappenda(root ? root : "", path);
49 dir = opendir(dirpath);
63 if (!de && errno != 0)
69 if (!dirent_is_file_with_suffix(de, suffix))
72 p = strjoin(dirpath, "/", de->d_name, NULL);
76 r = hashmap_put(h, basename(p), p);
78 log_debug("Skipping overridden file: %s.", p);
84 log_debug("Duplicate file %s", p);
92 static int base_cmp(const void *a, const void *b) {
95 s1 = *(char * const *)a;
96 s2 = *(char * const *)b;
97 return strcmp(basename(s1), basename(s2));
100 static int conf_files_list_strv_internal(char ***strv, const char *suffix, const char *root, char **dirs) {
101 _cleanup_hashmap_free_ Hashmap *fh = NULL;
108 /* This alters the dirs string array */
109 if (!path_strv_resolve_uniq(dirs, root))
112 fh = hashmap_new(string_hash_func, string_compare_func);
116 STRV_FOREACH(p, dirs) {
117 r = files_add(fh, root, *p, suffix);
121 log_debug("Failed to search for files in %s: %s",
125 files = hashmap_get_strv(fh);
130 qsort_safe(files, hashmap_size(fh), sizeof(char *), base_cmp);
136 int conf_files_list_strv(char ***strv, const char *suffix, const char *root, const char* const* dirs) {
137 _cleanup_strv_free_ char **copy = NULL;
142 copy = strv_copy((char**) dirs);
146 return conf_files_list_strv_internal(strv, suffix, root, copy);
149 int conf_files_list(char ***strv, const char *suffix, const char *root, const char *dir, ...) {
150 _cleanup_strv_free_ char **dirs = NULL;
157 dirs = strv_new_ap(dir, ap);
163 return conf_files_list_strv_internal(strv, suffix, root, dirs);
166 int conf_files_list_nulstr(char ***strv, const char *suffix, const char *root, const char *d) {
167 _cleanup_strv_free_ char **dirs = NULL;
172 dirs = strv_split_nulstr(d);
176 return conf_files_list_strv_internal(strv, suffix, root, dirs);