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 *path, const char *suffix) {
42 struct dirent buffer, *de;
56 k = readdir_r(dir, &buffer, &de);
65 if (!dirent_is_file_with_suffix(de, suffix))
68 if (asprintf(&p, "%s/%s", path, de->d_name) < 0) {
73 if (hashmap_put(h, path_get_file_name(p), p) <= 0) {
74 log_debug("Skip overridden file: %s.", p);
84 static int base_cmp(const void *a, const void *b) {
87 s1 = *(char * const *)a;
88 s2 = *(char * const *)b;
89 return strcmp(path_get_file_name(s1), path_get_file_name(s2));
92 int conf_files_list_strv(char ***strv, const char *suffix, const char **dirs) {
100 fh = hashmap_new(string_hash_func, string_compare_func);
106 STRV_FOREACH(p, dirs) {
107 if (files_add(fh, *p, suffix) < 0) {
108 log_error("Failed to search for files.");
114 files = hashmap_get_strv(fh);
116 log_error("Failed to compose list of files.");
120 qsort(files, hashmap_size(fh), sizeof(char *), base_cmp);
128 int conf_files_list(char ***strv, const char *suffix, const char *dir, ...) {
134 dirs = strv_new_ap(dir, ap);
141 if (!path_strv_canonicalize(dirs)) {
147 r = conf_files_list_strv(strv, suffix, (const char **)dirs);