1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
15 #include "conf-files.h"
16 #include "dirent-util.h"
22 #include "path-util.h"
23 #include "stat-util.h"
24 #include "string-util.h"
28 static int files_add(Hashmap
*h
, const char *suffix
, const char *root
, unsigned flags
, const char *path
) {
29 _cleanup_closedir_
DIR *dir
= NULL
;
36 dirpath
= prefix_roota(root
, path
);
38 dir
= opendir(dirpath
);
45 FOREACH_DIRENT(de
, dir
, return -errno
) {
48 if (!dirent_is_file_with_suffix(de
, suffix
)) {
49 log_debug("Ignoring %s/%s, because it's not a regular file with suffix %s.", dirpath
, de
->d_name
, strna(suffix
));
53 if (flags
& CONF_FILES_EXECUTABLE
) {
56 /* As requested: check if the file is marked exectuable. Note that we don't check access(X_OK)
57 * here, as we care about whether the file is marked executable at all, and not whether it is
58 * executable for us, because if such errors are stuff we should log about. */
60 if (fstatat(dirfd(dir
), de
->d_name
, &st
, 0) < 0) {
61 log_debug_errno(errno
, "Failed to stat %s/%s, ignoring: %m", dirpath
, de
->d_name
);
65 if (!null_or_empty(&st
)) {
66 /* A mask is a symlink to /dev/null or an empty file. It does not even
67 * have to be executable. Other entries must be regular executable files
68 * or symlinks to them. */
69 if (S_ISREG(st
.st_mode
)) {
70 if ((st
.st_mode
& 0111) == 0) { /* not executable */
71 log_debug("Ignoring %s/%s, as it is not marked executable.",
76 log_debug("Ignoring %s/%s, as it is neither a regular file nor a mask.",
83 p
= strjoin(dirpath
, "/", de
->d_name
);
87 r
= hashmap_put(h
, basename(p
), p
);
89 log_debug("Skipping overridden file: %s.", p
);
95 log_debug("Duplicate file %s", p
);
103 static int base_cmp(const void *a
, const void *b
) {
106 s1
= *(char * const *)a
;
107 s2
= *(char * const *)b
;
108 return strcmp(basename(s1
), basename(s2
));
111 static int conf_files_list_strv_internal(char ***strv
, const char *suffix
, const char *root
, unsigned flags
, char **dirs
) {
112 _cleanup_hashmap_free_ Hashmap
*fh
= NULL
;
118 /* This alters the dirs string array */
119 if (!path_strv_resolve_uniq(dirs
, root
))
122 fh
= hashmap_new(&string_hash_ops
);
126 STRV_FOREACH(p
, dirs
) {
127 r
= files_add(fh
, suffix
, root
, flags
, *p
);
131 log_debug_errno(r
, "Failed to search for files in %s, ignoring: %m", *p
);
134 files
= hashmap_get_strv(fh
);
138 qsort_safe(files
, hashmap_size(fh
), sizeof(char *), base_cmp
);
144 int conf_files_insert(char ***strv
, const char *root
, char **dirs
, const char *path
) {
145 /* Insert a path into strv, at the place honouring the usual sorting rules:
146 * - we first compare by the basename
147 * - and then we compare by dirname, allowing just one file with the given
149 * This means that we will
150 * - add a new entry if basename(path) was not on the list,
151 * - do nothing if an entry with higher priority was already present,
152 * - do nothing if our new entry matches the existing entry,
153 * - replace the existing entry if our new entry has higher priority.
159 for (i
= 0; i
< strv_length(*strv
); i
++) {
162 c
= base_cmp(*strv
+ i
, &path
);
166 /* Oh, we found our spot and it already contains something. */
167 STRV_FOREACH(dir
, dirs
) {
170 p1
= path_startswith((*strv
)[i
], root
);
172 /* Skip "/" in *dir, because p1 is without "/" too */
173 p1
= path_startswith(p1
, *dir
+ 1);
175 /* Existing entry with higher priority
176 * or same priority, no need to do anything. */
179 p2
= path_startswith(path
, *dir
);
181 /* Our new entry has higher priority */
182 t
= path_join(root
, path
, NULL
);
186 return free_and_replace((*strv
)[i
], t
);
191 /* Following files have lower priority, let's go insert our
195 /* … we are not there yet, let's continue */
198 t
= path_join(root
, path
, NULL
);
202 r
= strv_insert(strv
, i
, t
);
208 int conf_files_insert_nulstr(char ***strv
, const char *root
, const char *dirs
, const char *path
) {
209 _cleanup_strv_free_
char **d
= NULL
;
213 d
= strv_split_nulstr(dirs
);
217 return conf_files_insert(strv
, root
, d
, path
);
220 int conf_files_list_strv(char ***strv
, const char *suffix
, const char *root
, unsigned flags
, const char* const* dirs
) {
221 _cleanup_strv_free_
char **copy
= NULL
;
225 copy
= strv_copy((char**) dirs
);
229 return conf_files_list_strv_internal(strv
, suffix
, root
, flags
, copy
);
232 int conf_files_list(char ***strv
, const char *suffix
, const char *root
, unsigned flags
, const char *dir
, ...) {
233 _cleanup_strv_free_
char **dirs
= NULL
;
239 dirs
= strv_new_ap(dir
, ap
);
245 return conf_files_list_strv_internal(strv
, suffix
, root
, flags
, dirs
);
248 int conf_files_list_nulstr(char ***strv
, const char *suffix
, const char *root
, unsigned flags
, const char *dirs
) {
249 _cleanup_strv_free_
char **d
= NULL
;
253 d
= strv_split_nulstr(dirs
);
257 return conf_files_list_strv_internal(strv
, suffix
, root
, flags
, d
);