1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
9 #include "conf-files.h"
10 #include "constants.h"
11 #include "dirent-util.h"
16 #include "nulstr-util.h"
17 #include "path-util.h"
19 #include "sort-util.h"
20 #include "stat-util.h"
21 #include "string-util.h"
23 #include "terminal-util.h"
40 FOREACH_DIRENT(de
, dir
, return -errno
) {
41 _cleanup_free_
char *n
= NULL
, *p
= NULL
;
44 /* Does this match the suffix? */
45 if (suffix
&& !endswith(de
->d_name
, suffix
))
48 /* Has this file already been found in an earlier directory? */
49 if (hashmap_contains(*files
, de
->d_name
)) {
50 log_debug("Skipping overridden file '%s/%s'.", dirpath
, de
->d_name
);
54 /* Has this been masked in an earlier directory? */
55 if ((flags
& CONF_FILES_FILTER_MASKED
) && set_contains(*masked
, de
->d_name
)) {
56 log_debug("File '%s/%s' is masked by previous entry.", dirpath
, de
->d_name
);
60 /* Read file metadata if we shall validate the check for file masks, for node types or whether the node is marked executable. */
61 if (flags
& (CONF_FILES_FILTER_MASKED
|CONF_FILES_REGULAR
|CONF_FILES_DIRECTORY
|CONF_FILES_EXECUTABLE
))
62 if (fstatat(dirfd(dir
), de
->d_name
, &st
, 0) < 0) {
63 log_debug_errno(errno
, "Failed to stat '%s/%s', ignoring: %m", dirpath
, de
->d_name
);
67 /* Is this a masking entry? */
68 if ((flags
& CONF_FILES_FILTER_MASKED
))
69 if (null_or_empty(&st
)) {
70 /* Mark this one as masked */
71 r
= set_put_strdup(masked
, de
->d_name
);
75 log_debug("File '%s/%s' is a mask.", dirpath
, de
->d_name
);
79 /* Does this node have the right type? */
80 if (flags
& (CONF_FILES_REGULAR
|CONF_FILES_DIRECTORY
))
81 if (!((flags
& CONF_FILES_DIRECTORY
) && S_ISDIR(st
.st_mode
)) &&
82 !((flags
& CONF_FILES_REGULAR
) && S_ISREG(st
.st_mode
))) {
83 log_debug("Ignoring '%s/%s', as it does not have the right type.", dirpath
, de
->d_name
);
87 /* Does this node have the executable bit set? */
88 if (flags
& CONF_FILES_EXECUTABLE
)
89 /* As requested: check if the file is marked executable. Note that we don't check access(X_OK)
90 * here, as we care about whether the file is marked executable at all, and not whether it is
91 * executable for us, because if so, such errors are stuff we should log about. */
93 if ((st
.st_mode
& 0111) == 0) { /* not executable */
94 log_debug("Ignoring '%s/%s', as it is not marked executable.", dirpath
, de
->d_name
);
98 n
= strdup(de
->d_name
);
102 if ((flags
& CONF_FILES_BASENAME
))
103 r
= hashmap_ensure_put(files
, &string_hash_ops_free
, n
, n
);
105 p
= path_join(dirpath
, de
->d_name
);
109 r
= hashmap_ensure_put(files
, &string_hash_ops_free_free
, n
, p
);
122 static int base_cmp(char * const *a
, char * const *b
) {
125 return path_compare_filename(*a
, *b
);
128 static int copy_and_sort_files_from_hashmap(Hashmap
*fh
, char ***ret
) {
129 _cleanup_free_
char **sv
= NULL
;
134 sv
= hashmap_get_strv(fh
);
138 /* The entries in the array given by hashmap_get_strv() are still owned by the hashmap. */
139 files
= strv_copy(sv
);
143 typesafe_qsort(files
, strv_length(files
), base_cmp
);
149 int conf_files_list_strv(
154 const char * const *dirs
) {
156 _cleanup_hashmap_free_ Hashmap
*fh
= NULL
;
157 _cleanup_set_free_ Set
*masked
= NULL
;
162 STRV_FOREACH(p
, dirs
) {
163 _cleanup_closedir_
DIR *dir
= NULL
;
164 _cleanup_free_
char *path
= NULL
;
166 r
= chase_and_opendir(*p
, root
, CHASE_PREFIX_ROOT
, &path
, &dir
);
169 log_debug_errno(r
, "Failed to chase and open directory '%s', ignoring: %m", *p
);
173 r
= files_add(dir
, path
, &fh
, &masked
, suffix
, flags
);
177 log_debug_errno(r
, "Failed to search for files in '%s', ignoring: %m", path
);
180 return copy_and_sort_files_from_hashmap(fh
, ret
);
183 int conf_files_list_strv_at(
188 const char * const *dirs
) {
190 _cleanup_hashmap_free_ Hashmap
*fh
= NULL
;
191 _cleanup_set_free_ Set
*masked
= NULL
;
194 assert(rfd
>= 0 || rfd
== AT_FDCWD
);
197 STRV_FOREACH(p
, dirs
) {
198 _cleanup_closedir_
DIR *dir
= NULL
;
199 _cleanup_free_
char *path
= NULL
;
201 r
= chase_and_opendirat(rfd
, *p
, CHASE_AT_RESOLVE_IN_ROOT
, &path
, &dir
);
204 log_debug_errno(r
, "Failed to chase and open directory '%s', ignoring: %m", *p
);
208 r
= files_add(dir
, path
, &fh
, &masked
, suffix
, flags
);
212 log_debug_errno(r
, "Failed to search for files in '%s', ignoring: %m", path
);
215 return copy_and_sort_files_from_hashmap(fh
, ret
);
218 int conf_files_insert(char ***strv
, const char *root
, char **dirs
, const char *path
) {
219 /* Insert a path into strv, at the place honouring the usual sorting rules:
220 * - we first compare by the basename
221 * - and then we compare by dirname, allowing just one file with the given
223 * This means that we will
224 * - add a new entry if basename(path) was not on the list,
225 * - do nothing if an entry with higher priority was already present,
226 * - do nothing if our new entry matches the existing entry,
227 * - replace the existing entry if our new entry has higher priority.
233 n
= strv_length(*strv
);
234 for (i
= 0; i
< n
; i
++) {
237 c
= base_cmp((char* const*) *strv
+ i
, (char* const*) &path
);
239 /* Oh, there already is an entry with a matching name (the last component). */
240 STRV_FOREACH(dir
, dirs
) {
241 _cleanup_free_
char *rdir
= NULL
;
244 rdir
= path_join(root
, *dir
);
248 p1
= path_startswith((*strv
)[i
], rdir
);
250 /* Existing entry with higher priority
251 * or same priority, no need to do anything. */
254 p2
= path_startswith(path
, *dir
);
256 /* Our new entry has higher priority */
258 t
= path_join(root
, path
);
262 return free_and_replace((*strv
)[i
], t
);
267 /* Following files have lower priority, let's go insert our
271 /* … we are not there yet, let's continue */
274 /* The new file has lower priority than all the existing entries */
275 t
= path_join(root
, path
);
279 r
= strv_insert(strv
, i
, t
);
286 int conf_files_list(char ***ret
, const char *suffix
, const char *root
, unsigned flags
, const char *dir
) {
287 return conf_files_list_strv(ret
, suffix
, root
, flags
, STRV_MAKE_CONST(dir
));
290 int conf_files_list_at(char ***ret
, const char *suffix
, int rfd
, unsigned flags
, const char *dir
) {
291 return conf_files_list_strv_at(ret
, suffix
, rfd
, flags
, STRV_MAKE_CONST(dir
));
294 int conf_files_list_nulstr(char ***ret
, const char *suffix
, const char *root
, unsigned flags
, const char *dirs
) {
295 _cleanup_strv_free_
char **d
= NULL
;
299 d
= strv_split_nulstr(dirs
);
303 return conf_files_list_strv(ret
, suffix
, root
, flags
, (const char**) d
);
306 int conf_files_list_nulstr_at(char ***ret
, const char *suffix
, int rfd
, unsigned flags
, const char *dirs
) {
307 _cleanup_strv_free_
char **d
= NULL
;
311 d
= strv_split_nulstr(dirs
);
315 return conf_files_list_strv_at(ret
, suffix
, rfd
, flags
, (const char**) d
);
318 int conf_files_list_with_replacement(
321 const char *replacement
,
323 char **ret_replace_file
) {
325 _cleanup_strv_free_
char **f
= NULL
;
326 _cleanup_free_
char *p
= NULL
;
331 assert(ret_replace_file
|| !replacement
);
333 r
= conf_files_list_strv(&f
, ".conf", root
, 0, (const char* const*) config_dirs
);
335 return log_error_errno(r
, "Failed to enumerate config files: %m");
338 r
= conf_files_insert(&f
, root
, config_dirs
, replacement
);
340 return log_error_errno(r
, "Failed to extend config file list: %m");
342 p
= path_join(root
, replacement
);
347 *ret_files
= TAKE_PTR(f
);
348 if (ret_replace_file
)
349 *ret_replace_file
= TAKE_PTR(p
);
354 int conf_files_list_dropins(
356 const char *dropin_dirname
,
358 const char * const *dirs
) {
360 _cleanup_strv_free_
char **dropin_dirs
= NULL
;
365 assert(dropin_dirname
);
368 suffix
= strjoina("/", dropin_dirname
);
369 r
= strv_extend_strv_concat(&dropin_dirs
, (char**) dirs
, suffix
);
373 return conf_files_list_strv(ret
, ".conf", root
, 0, (const char* const*) dropin_dirs
);