1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include "conf-files.h"
12 #include "dirent-util.h"
18 #include "path-util.h"
20 #include "sort-util.h"
21 #include "stat-util.h"
22 #include "string-util.h"
24 #include "terminal-util.h"
34 _cleanup_closedir_
DIR *dir
= NULL
;
40 assert((flags
& CONF_FILES_FILTER_MASKED
) == 0 || masked
);
43 dirpath
= prefix_roota(root
, path
);
45 dir
= opendir(dirpath
);
50 return log_debug_errno(errno
, "Failed to open directory '%s': %m", dirpath
);
53 FOREACH_DIRENT(de
, dir
, return -errno
) {
57 /* Does this match the suffix? */
58 if (suffix
&& !endswith(de
->d_name
, suffix
))
61 /* Has this file already been found in an earlier directory? */
62 if (hashmap_contains(h
, de
->d_name
)) {
63 log_debug("Skipping overridden file '%s/%s'.", dirpath
, de
->d_name
);
67 /* Has this been masked in an earlier directory? */
68 if ((flags
& CONF_FILES_FILTER_MASKED
) && set_contains(masked
, de
->d_name
)) {
69 log_debug("File '%s/%s' is masked by previous entry.", dirpath
, de
->d_name
);
73 /* Read file metadata if we shall validate the check for file masks, for node types or whether the node is marked executable. */
74 if (flags
& (CONF_FILES_FILTER_MASKED
|CONF_FILES_REGULAR
|CONF_FILES_DIRECTORY
|CONF_FILES_EXECUTABLE
))
75 if (fstatat(dirfd(dir
), de
->d_name
, &st
, 0) < 0) {
76 log_debug_errno(errno
, "Failed to stat '%s/%s', ignoring: %m", dirpath
, de
->d_name
);
80 /* Is this a masking entry? */
81 if ((flags
& CONF_FILES_FILTER_MASKED
))
82 if (null_or_empty(&st
)) {
83 /* Mark this one as masked */
84 r
= set_put_strdup(masked
, de
->d_name
);
88 log_debug("File '%s/%s' is a mask.", dirpath
, de
->d_name
);
92 /* Does this node have the right type? */
93 if (flags
& (CONF_FILES_REGULAR
|CONF_FILES_DIRECTORY
))
94 if (!((flags
& CONF_FILES_DIRECTORY
) && S_ISDIR(st
.st_mode
)) &&
95 !((flags
& CONF_FILES_REGULAR
) && S_ISREG(st
.st_mode
))) {
96 log_debug("Ignoring '%s/%s', as it is not a of the right type.", dirpath
, de
->d_name
);
100 /* Does this node have the executable bit set? */
101 if (flags
& CONF_FILES_EXECUTABLE
)
102 /* As requested: check if the file is marked executable. Note that we don't check access(X_OK)
103 * here, as we care about whether the file is marked executable at all, and not whether it is
104 * executable for us, because if so, such errors are stuff we should log about. */
106 if ((st
.st_mode
& 0111) == 0) { /* not executable */
107 log_debug("Ignoring '%s/%s', as it is not marked executable.", dirpath
, de
->d_name
);
111 if (flags
& CONF_FILES_BASENAME
) {
112 p
= strdup(de
->d_name
);
118 p
= strjoin(dirpath
, "/", de
->d_name
);
125 r
= hashmap_put(h
, key
, p
);
128 return log_debug_errno(r
, "Failed to add item to hashmap: %m");
137 static int base_cmp(char * const *a
, char * const *b
) {
138 return strcmp(basename(*a
), basename(*b
));
141 static int conf_files_list_strv_internal(char ***strv
, const char *suffix
, const char *root
, unsigned flags
, char **dirs
) {
142 _cleanup_hashmap_free_ Hashmap
*fh
= NULL
;
143 _cleanup_set_free_free_ Set
*masked
= NULL
;
149 /* This alters the dirs string array */
150 if (!path_strv_resolve_uniq(dirs
, root
))
153 fh
= hashmap_new(&path_hash_ops
);
157 if (flags
& CONF_FILES_FILTER_MASKED
) {
158 masked
= set_new(&path_hash_ops
);
163 STRV_FOREACH(p
, dirs
) {
164 r
= files_add(fh
, masked
, suffix
, root
, flags
, *p
);
168 log_debug_errno(r
, "Failed to search for files in %s, ignoring: %m", *p
);
171 files
= hashmap_get_strv(fh
);
175 typesafe_qsort(files
, hashmap_size(fh
), base_cmp
);
181 int conf_files_insert(char ***strv
, const char *root
, char **dirs
, const char *path
) {
182 /* Insert a path into strv, at the place honouring the usual sorting rules:
183 * - we first compare by the basename
184 * - and then we compare by dirname, allowing just one file with the given
186 * This means that we will
187 * - add a new entry if basename(path) was not on the list,
188 * - do nothing if an entry with higher priority was already present,
189 * - do nothing if our new entry matches the existing entry,
190 * - replace the existing entry if our new entry has higher priority.
196 n
= strv_length(*strv
);
197 for (i
= 0; i
< n
; i
++) {
200 c
= base_cmp((char* const*) *strv
+ i
, (char* const*) &path
);
204 /* Oh, there already is an entry with a matching name (the last component). */
206 STRV_FOREACH(dir
, dirs
) {
207 _cleanup_free_
char *rdir
= NULL
;
210 rdir
= prefix_root(root
, *dir
);
214 p1
= path_startswith((*strv
)[i
], rdir
);
216 /* Existing entry with higher priority
217 * or same priority, no need to do anything. */
220 p2
= path_startswith(path
, *dir
);
222 /* Our new entry has higher priority */
224 t
= prefix_root(root
, path
);
228 return free_and_replace((*strv
)[i
], t
);
233 /* Following files have lower priority, let's go insert our
237 /* … we are not there yet, let's continue */
240 /* The new file has lower priority than all the existing entries */
241 t
= prefix_root(root
, path
);
245 r
= strv_insert(strv
, i
, t
);
252 int conf_files_list_strv(char ***strv
, const char *suffix
, const char *root
, unsigned flags
, const char* const* dirs
) {
253 _cleanup_strv_free_
char **copy
= NULL
;
257 copy
= strv_copy((char**) dirs
);
261 return conf_files_list_strv_internal(strv
, suffix
, root
, flags
, copy
);
264 int conf_files_list(char ***strv
, const char *suffix
, const char *root
, unsigned flags
, const char *dir
, ...) {
265 _cleanup_strv_free_
char **dirs
= NULL
;
271 dirs
= strv_new_ap(dir
, ap
);
277 return conf_files_list_strv_internal(strv
, suffix
, root
, flags
, dirs
);
280 int conf_files_list_nulstr(char ***strv
, const char *suffix
, const char *root
, unsigned flags
, const char *dirs
) {
281 _cleanup_strv_free_
char **d
= NULL
;
285 d
= strv_split_nulstr(dirs
);
289 return conf_files_list_strv_internal(strv
, suffix
, root
, flags
, d
);
292 int conf_files_list_with_replacement(
295 const char *replacement
,
297 char **replace_file
) {
299 _cleanup_strv_free_
char **f
= NULL
;
300 _cleanup_free_
char *p
= NULL
;
305 assert(replace_file
|| !replacement
);
307 r
= conf_files_list_strv(&f
, ".conf", root
, 0, (const char* const*) config_dirs
);
309 return log_error_errno(r
, "Failed to enumerate config files: %m");
312 r
= conf_files_insert(&f
, root
, config_dirs
, replacement
);
314 return log_error_errno(r
, "Failed to extend config file list: %m");
316 p
= prefix_root(root
, replacement
);
321 *files
= TAKE_PTR(f
);
323 *replace_file
= TAKE_PTR(p
);