1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
8 #include "conf-files.h"
10 #include "dirent-util.h"
15 #include "path-util.h"
17 #include "sort-util.h"
18 #include "stat-util.h"
19 #include "string-util.h"
21 #include "terminal-util.h"
31 _cleanup_closedir_
DIR *dir
= NULL
;
36 assert((flags
& CONF_FILES_FILTER_MASKED
) == 0 || masked
);
39 dirpath
= prefix_roota(root
, path
);
41 dir
= opendir(dirpath
);
46 return log_debug_errno(errno
, "Failed to open directory '%s': %m", dirpath
);
49 FOREACH_DIRENT(de
, dir
, return -errno
) {
53 /* Does this match the suffix? */
54 if (suffix
&& !endswith(de
->d_name
, suffix
))
57 /* Has this file already been found in an earlier directory? */
58 if (hashmap_contains(h
, de
->d_name
)) {
59 log_debug("Skipping overridden file '%s/%s'.", dirpath
, de
->d_name
);
63 /* Has this been masked in an earlier directory? */
64 if ((flags
& CONF_FILES_FILTER_MASKED
) && set_contains(masked
, de
->d_name
)) {
65 log_debug("File '%s/%s' is masked by previous entry.", dirpath
, de
->d_name
);
69 /* Read file metadata if we shall validate the check for file masks, for node types or whether the node is marked executable. */
70 if (flags
& (CONF_FILES_FILTER_MASKED
|CONF_FILES_REGULAR
|CONF_FILES_DIRECTORY
|CONF_FILES_EXECUTABLE
))
71 if (fstatat(dirfd(dir
), de
->d_name
, &st
, 0) < 0) {
72 log_debug_errno(errno
, "Failed to stat '%s/%s', ignoring: %m", dirpath
, de
->d_name
);
76 /* Is this a masking entry? */
77 if ((flags
& CONF_FILES_FILTER_MASKED
))
78 if (null_or_empty(&st
)) {
81 /* Mark this one as masked */
82 r
= set_put_strdup(&masked
, de
->d_name
);
86 log_debug("File '%s/%s' is a mask.", dirpath
, de
->d_name
);
90 /* Does this node have the right type? */
91 if (flags
& (CONF_FILES_REGULAR
|CONF_FILES_DIRECTORY
))
92 if (!((flags
& CONF_FILES_DIRECTORY
) && S_ISDIR(st
.st_mode
)) &&
93 !((flags
& CONF_FILES_REGULAR
) && S_ISREG(st
.st_mode
))) {
94 log_debug("Ignoring '%s/%s', as it does not have the right type.", dirpath
, de
->d_name
);
98 /* Does this node have the executable bit set? */
99 if (flags
& CONF_FILES_EXECUTABLE
)
100 /* As requested: check if the file is marked executable. Note that we don't check access(X_OK)
101 * here, as we care about whether the file is marked executable at all, and not whether it is
102 * executable for us, because if so, such errors are stuff we should log about. */
104 if ((st
.st_mode
& 0111) == 0) { /* not executable */
105 log_debug("Ignoring '%s/%s', as it is not marked executable.", dirpath
, de
->d_name
);
109 if (flags
& CONF_FILES_BASENAME
) {
110 p
= strdup(de
->d_name
);
116 p
= path_join(dirpath
, de
->d_name
);
123 r
= hashmap_put(h
, key
, p
);
126 return log_debug_errno(r
, "Failed to add item to hashmap: %m");
135 static int base_cmp(char * const *a
, char * const *b
) {
136 return strcmp(basename(*a
), basename(*b
));
139 static int conf_files_list_strv_internal(
146 _cleanup_hashmap_free_ Hashmap
*fh
= NULL
;
147 _cleanup_set_free_free_ Set
*masked
= NULL
;
153 /* This alters the dirs string array */
154 if (!path_strv_resolve_uniq(dirs
, root
))
157 fh
= hashmap_new(&path_hash_ops
);
161 if (flags
& CONF_FILES_FILTER_MASKED
) {
162 masked
= set_new(&path_hash_ops
);
167 STRV_FOREACH(p
, dirs
) {
168 r
= files_add(fh
, masked
, suffix
, root
, flags
, *p
);
172 log_debug_errno(r
, "Failed to search for files in %s, ignoring: %m", *p
);
175 files
= hashmap_get_strv(fh
);
179 typesafe_qsort(files
, hashmap_size(fh
), base_cmp
);
185 int conf_files_insert(char ***strv
, const char *root
, char **dirs
, const char *path
) {
186 /* Insert a path into strv, at the place honouring the usual sorting rules:
187 * - we first compare by the basename
188 * - and then we compare by dirname, allowing just one file with the given
190 * This means that we will
191 * - add a new entry if basename(path) was not on the list,
192 * - do nothing if an entry with higher priority was already present,
193 * - do nothing if our new entry matches the existing entry,
194 * - replace the existing entry if our new entry has higher priority.
200 n
= strv_length(*strv
);
201 for (i
= 0; i
< n
; i
++) {
204 c
= base_cmp((char* const*) *strv
+ i
, (char* const*) &path
);
208 /* Oh, there already is an entry with a matching name (the last component). */
210 STRV_FOREACH(dir
, dirs
) {
211 _cleanup_free_
char *rdir
= NULL
;
214 rdir
= path_join(root
, *dir
);
218 p1
= path_startswith((*strv
)[i
], rdir
);
220 /* Existing entry with higher priority
221 * or same priority, no need to do anything. */
224 p2
= path_startswith(path
, *dir
);
226 /* Our new entry has higher priority */
228 t
= path_join(root
, path
);
232 return free_and_replace((*strv
)[i
], t
);
237 /* Following files have lower priority, let's go insert our
241 /* … we are not there yet, let's continue */
244 /* The new file has lower priority than all the existing entries */
245 t
= path_join(root
, path
);
249 r
= strv_insert(strv
, i
, t
);
256 int conf_files_list_strv(char ***ret
, const char *suffix
, const char *root
, unsigned flags
, const char* const* dirs
) {
257 _cleanup_strv_free_
char **copy
= NULL
;
261 copy
= strv_copy((char**) dirs
);
265 return conf_files_list_strv_internal(ret
, suffix
, root
, flags
, copy
);
268 int conf_files_list(char ***ret
, const char *suffix
, const char *root
, unsigned flags
, const char *dir
) {
269 _cleanup_strv_free_
char **dirs
= NULL
;
273 dirs
= strv_new(dir
);
277 return conf_files_list_strv_internal(ret
, suffix
, root
, flags
, dirs
);
280 int conf_files_list_nulstr(char ***ret
, 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(ret
, suffix
, root
, flags
, d
);
292 int conf_files_list_with_replacement(
295 const char *replacement
,
297 char **ret_replace_file
) {
299 _cleanup_strv_free_
char **f
= NULL
;
300 _cleanup_free_
char *p
= NULL
;
305 assert(ret_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
= path_join(root
, replacement
);
321 *ret_files
= TAKE_PTR(f
);
322 if (ret_replace_file
)
323 *ret_replace_file
= TAKE_PTR(p
);