1 /* SPDX-License-Identifier: LGPL-2.1+ */
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
;
37 assert((flags
& CONF_FILES_FILTER_MASKED
) == 0 || masked
);
40 dirpath
= prefix_roota(root
, path
);
42 dir
= opendir(dirpath
);
47 return log_debug_errno(errno
, "Failed to open directory '%s': %m", dirpath
);
50 FOREACH_DIRENT(de
, dir
, return -errno
) {
54 /* Does this match the suffix? */
55 if (suffix
&& !endswith(de
->d_name
, suffix
))
58 /* Has this file already been found in an earlier directory? */
59 if (hashmap_contains(h
, de
->d_name
)) {
60 log_debug("Skipping overridden file '%s/%s'.", dirpath
, de
->d_name
);
64 /* Has this been masked in an earlier directory? */
65 if ((flags
& CONF_FILES_FILTER_MASKED
) && set_contains(masked
, de
->d_name
)) {
66 log_debug("File '%s/%s' is masked by previous entry.", dirpath
, de
->d_name
);
70 /* Read file metadata if we shall validate the check for file masks, for node types or whether the node is marked executable. */
71 if (flags
& (CONF_FILES_FILTER_MASKED
|CONF_FILES_REGULAR
|CONF_FILES_DIRECTORY
|CONF_FILES_EXECUTABLE
))
72 if (fstatat(dirfd(dir
), de
->d_name
, &st
, 0) < 0) {
73 log_debug_errno(errno
, "Failed to stat '%s/%s', ignoring: %m", dirpath
, de
->d_name
);
77 /* Is this a masking entry? */
78 if ((flags
& CONF_FILES_FILTER_MASKED
))
79 if (null_or_empty(&st
)) {
80 /* Mark this one as masked */
81 r
= set_put_strdup(masked
, de
->d_name
);
85 log_debug("File '%s/%s' is a mask.", dirpath
, de
->d_name
);
89 /* Does this node have the right type? */
90 if (flags
& (CONF_FILES_REGULAR
|CONF_FILES_DIRECTORY
))
91 if (!((flags
& CONF_FILES_DIRECTORY
) && S_ISDIR(st
.st_mode
)) &&
92 !((flags
& CONF_FILES_REGULAR
) && S_ISREG(st
.st_mode
))) {
93 log_debug("Ignoring '%s/%s', as it is not a of the right type.", dirpath
, de
->d_name
);
97 /* Does this node have the executable bit set? */
98 if (flags
& CONF_FILES_EXECUTABLE
)
99 /* As requested: check if the file is marked executable. Note that we don't check access(X_OK)
100 * here, as we care about whether the file is marked executable at all, and not whether it is
101 * executable for us, because if so, such errors are stuff we should log about. */
103 if ((st
.st_mode
& 0111) == 0) { /* not executable */
104 log_debug("Ignoring '%s/%s', as it is not marked executable.", dirpath
, de
->d_name
);
108 if (flags
& CONF_FILES_BASENAME
) {
109 p
= strdup(de
->d_name
);
115 p
= path_join(dirpath
, de
->d_name
);
122 r
= hashmap_put(h
, key
, p
);
125 return log_debug_errno(r
, "Failed to add item to hashmap: %m");
134 static int base_cmp(char * const *a
, char * const *b
) {
135 return strcmp(basename(*a
), basename(*b
));
138 static int conf_files_list_strv_internal(char ***strv
, const char *suffix
, const char *root
, unsigned flags
, char **dirs
) {
139 _cleanup_hashmap_free_ Hashmap
*fh
= NULL
;
140 _cleanup_set_free_free_ Set
*masked
= NULL
;
146 /* This alters the dirs string array */
147 if (!path_strv_resolve_uniq(dirs
, root
))
150 fh
= hashmap_new(&path_hash_ops
);
154 if (flags
& CONF_FILES_FILTER_MASKED
) {
155 masked
= set_new(&path_hash_ops
);
160 STRV_FOREACH(p
, dirs
) {
161 r
= files_add(fh
, masked
, suffix
, root
, flags
, *p
);
165 log_debug_errno(r
, "Failed to search for files in %s, ignoring: %m", *p
);
168 files
= hashmap_get_strv(fh
);
172 typesafe_qsort(files
, hashmap_size(fh
), base_cmp
);
178 int conf_files_insert(char ***strv
, const char *root
, char **dirs
, const char *path
) {
179 /* Insert a path into strv, at the place honouring the usual sorting rules:
180 * - we first compare by the basename
181 * - and then we compare by dirname, allowing just one file with the given
183 * This means that we will
184 * - add a new entry if basename(path) was not on the list,
185 * - do nothing if an entry with higher priority was already present,
186 * - do nothing if our new entry matches the existing entry,
187 * - replace the existing entry if our new entry has higher priority.
193 n
= strv_length(*strv
);
194 for (i
= 0; i
< n
; i
++) {
197 c
= base_cmp((char* const*) *strv
+ i
, (char* const*) &path
);
201 /* Oh, there already is an entry with a matching name (the last component). */
203 STRV_FOREACH(dir
, dirs
) {
204 _cleanup_free_
char *rdir
= NULL
;
207 rdir
= path_join(root
, *dir
);
211 p1
= path_startswith((*strv
)[i
], rdir
);
213 /* Existing entry with higher priority
214 * or same priority, no need to do anything. */
217 p2
= path_startswith(path
, *dir
);
219 /* Our new entry has higher priority */
221 t
= path_join(root
, path
);
225 return free_and_replace((*strv
)[i
], t
);
230 /* Following files have lower priority, let's go insert our
234 /* … we are not there yet, let's continue */
237 /* The new file has lower priority than all the existing entries */
238 t
= path_join(root
, path
);
242 r
= strv_insert(strv
, i
, t
);
249 int conf_files_list_strv(char ***strv
, const char *suffix
, const char *root
, unsigned flags
, const char* const* dirs
) {
250 _cleanup_strv_free_
char **copy
= NULL
;
254 copy
= strv_copy((char**) dirs
);
258 return conf_files_list_strv_internal(strv
, suffix
, root
, flags
, copy
);
261 int conf_files_list(char ***strv
, const char *suffix
, const char *root
, unsigned flags
, const char *dir
) {
262 _cleanup_strv_free_
char **dirs
= NULL
;
266 dirs
= strv_new(dir
);
270 return conf_files_list_strv_internal(strv
, suffix
, root
, flags
, dirs
);
273 int conf_files_list_nulstr(char ***strv
, const char *suffix
, const char *root
, unsigned flags
, const char *dirs
) {
274 _cleanup_strv_free_
char **d
= NULL
;
278 d
= strv_split_nulstr(dirs
);
282 return conf_files_list_strv_internal(strv
, suffix
, root
, flags
, d
);
285 int conf_files_list_with_replacement(
288 const char *replacement
,
290 char **replace_file
) {
292 _cleanup_strv_free_
char **f
= NULL
;
293 _cleanup_free_
char *p
= NULL
;
298 assert(replace_file
|| !replacement
);
300 r
= conf_files_list_strv(&f
, ".conf", root
, 0, (const char* const*) config_dirs
);
302 return log_error_errno(r
, "Failed to enumerate config files: %m");
305 r
= conf_files_insert(&f
, root
, config_dirs
, replacement
);
307 return log_error_errno(r
, "Failed to extend config file list: %m");
309 p
= path_join(root
, replacement
);
314 *files
= TAKE_PTR(f
);
316 *replace_file
= TAKE_PTR(p
);