]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
2c21044f | 2 | |
2c21044f | 3 | #include <errno.h> |
11c3a366 | 4 | #include <stdarg.h> |
2c21044f | 5 | #include <stdio.h> |
07630cea | 6 | #include <stdlib.h> |
2c21044f | 7 | |
3ffd4af2 | 8 | #include "conf-files.h" |
854a42fb | 9 | #include "def.h" |
a0956174 | 10 | #include "dirent-util.h" |
3ffd4af2 | 11 | #include "fd-util.h" |
07630cea LP |
12 | #include "hashmap.h" |
13 | #include "log.h" | |
2c21044f | 14 | #include "macro.h" |
9eb977db | 15 | #include "path-util.h" |
3e36211b | 16 | #include "set.h" |
760877e9 | 17 | #include "sort-util.h" |
b5084605 | 18 | #include "stat-util.h" |
07630cea LP |
19 | #include "string-util.h" |
20 | #include "strv.h" | |
854a42fb | 21 | #include "terminal-util.h" |
2c21044f | 22 | |
3e36211b LP |
23 | static int files_add( |
24 | Hashmap *h, | |
25 | Set *masked, | |
26 | const char *suffix, | |
27 | const char *root, | |
28 | unsigned flags, | |
29 | const char *path) { | |
30 | ||
fabe5c0e | 31 | _cleanup_closedir_ DIR *dir = NULL; |
1d13f648 LP |
32 | const char *dirpath; |
33 | int r; | |
e02caf30 | 34 | |
3e36211b LP |
35 | assert(h); |
36 | assert((flags & CONF_FILES_FILTER_MASKED) == 0 || masked); | |
cba2ef02 | 37 | assert(path); |
cebed500 | 38 | |
1d13f648 | 39 | dirpath = prefix_roota(root, path); |
cebed500 | 40 | |
cba2ef02 | 41 | dir = opendir(dirpath); |
2c21044f KS |
42 | if (!dir) { |
43 | if (errno == ENOENT) | |
44 | return 0; | |
3e36211b LP |
45 | |
46 | return log_debug_errno(errno, "Failed to open directory '%s': %m", dirpath); | |
2c21044f KS |
47 | } |
48 | ||
31d5192d | 49 | FOREACH_DIRENT(de, dir, return -errno) { |
3e36211b LP |
50 | struct stat st; |
51 | char *p, *key; | |
52 | ||
53 | /* Does this match the suffix? */ | |
54 | if (suffix && !endswith(de->d_name, suffix)) | |
55 | continue; | |
2c21044f | 56 | |
3e36211b LP |
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); | |
2c21044f | 60 | continue; |
e34aa8ed | 61 | } |
2c21044f | 62 | |
3e36211b LP |
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); | |
66 | continue; | |
67 | } | |
68 | ||
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); | |
73 | continue; | |
74 | } | |
75 | ||
76 | /* Is this a masking entry? */ | |
77 | if ((flags & CONF_FILES_FILTER_MASKED)) | |
78 | if (null_or_empty(&st)) { | |
be327321 ZJS |
79 | assert(masked); |
80 | ||
3e36211b | 81 | /* Mark this one as masked */ |
be327321 | 82 | r = set_put_strdup(&masked, de->d_name); |
3e36211b LP |
83 | if (r < 0) |
84 | return r; | |
85 | ||
86 | log_debug("File '%s/%s' is a mask.", dirpath, de->d_name); | |
87 | continue; | |
88 | } | |
89 | ||
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))) { | |
387f6955 | 94 | log_debug("Ignoring '%s/%s', as it does not have the right type.", dirpath, de->d_name); |
3e36211b LP |
95 | continue; |
96 | } | |
b5084605 | 97 | |
3e36211b LP |
98 | /* Does this node have the executable bit set? */ |
99 | if (flags & CONF_FILES_EXECUTABLE) | |
5238e957 | 100 | /* As requested: check if the file is marked executable. Note that we don't check access(X_OK) |
b5084605 | 101 | * here, as we care about whether the file is marked executable at all, and not whether it is |
3e36211b | 102 | * executable for us, because if so, such errors are stuff we should log about. */ |
b5084605 | 103 | |
3e36211b LP |
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); | |
b5084605 LP |
106 | continue; |
107 | } | |
108 | ||
3e36211b LP |
109 | if (flags & CONF_FILES_BASENAME) { |
110 | p = strdup(de->d_name); | |
111 | if (!p) | |
112 | return -ENOMEM; | |
b5084605 | 113 | |
3e36211b LP |
114 | key = p; |
115 | } else { | |
657ee2d8 | 116 | p = path_join(dirpath, de->d_name); |
3e36211b LP |
117 | if (!p) |
118 | return -ENOMEM; | |
2c21044f | 119 | |
3e36211b LP |
120 | key = basename(p); |
121 | } | |
122 | ||
123 | r = hashmap_put(h, key, p); | |
124 | if (r < 0) { | |
2c21044f | 125 | free(p); |
3e36211b | 126 | return log_debug_errno(r, "Failed to add item to hashmap: %m"); |
2c21044f | 127 | } |
3e36211b LP |
128 | |
129 | assert(r > 0); | |
2c21044f KS |
130 | } |
131 | ||
e02caf30 | 132 | return 0; |
2c21044f KS |
133 | } |
134 | ||
93bab288 YW |
135 | static int base_cmp(char * const *a, char * const *b) { |
136 | return strcmp(basename(*a), basename(*b)); | |
2c21044f KS |
137 | } |
138 | ||
94916255 LP |
139 | static int conf_files_list_strv_internal( |
140 | char ***ret, | |
141 | const char *suffix, | |
142 | const char *root, | |
143 | unsigned flags, | |
144 | char **dirs) { | |
145 | ||
e1d75803 | 146 | _cleanup_hashmap_free_ Hashmap *fh = NULL; |
3e36211b | 147 | _cleanup_set_free_free_ Set *masked = NULL; |
fabe5c0e | 148 | char **files, **p; |
578ac060 | 149 | int r; |
2c21044f | 150 | |
94916255 | 151 | assert(ret); |
fabe5c0e LP |
152 | |
153 | /* This alters the dirs string array */ | |
7d8da2c9 | 154 | if (!path_strv_resolve_uniq(dirs, root)) |
fabe5c0e | 155 | return -ENOMEM; |
2c21044f | 156 | |
3e36211b | 157 | fh = hashmap_new(&path_hash_ops); |
fabe5c0e LP |
158 | if (!fh) |
159 | return -ENOMEM; | |
2c21044f | 160 | |
3e36211b LP |
161 | if (flags & CONF_FILES_FILTER_MASKED) { |
162 | masked = set_new(&path_hash_ops); | |
163 | if (!masked) | |
164 | return -ENOMEM; | |
165 | } | |
166 | ||
2c21044f | 167 | STRV_FOREACH(p, dirs) { |
3e36211b | 168 | r = files_add(fh, masked, suffix, root, flags, *p); |
31d5192d | 169 | if (r == -ENOMEM) |
fabe5c0e | 170 | return r; |
31d5192d LP |
171 | if (r < 0) |
172 | log_debug_errno(r, "Failed to search for files in %s, ignoring: %m", *p); | |
2c21044f KS |
173 | } |
174 | ||
175 | files = hashmap_get_strv(fh); | |
31d5192d | 176 | if (!files) |
fabe5c0e | 177 | return -ENOMEM; |
fabe5c0e | 178 | |
93bab288 | 179 | typesafe_qsort(files, hashmap_size(fh), base_cmp); |
94916255 | 180 | *ret = files; |
2c21044f | 181 | |
fabe5c0e LP |
182 | return 0; |
183 | } | |
184 | ||
a6d8474f | 185 | int conf_files_insert(char ***strv, const char *root, char **dirs, const char *path) { |
d16a1c1b ZJS |
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 | |
189 | * basename. | |
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. | |
195 | */ | |
243dd6ae | 196 | size_t i, n; |
d16a1c1b | 197 | char *t; |
d16a1c1b ZJS |
198 | int r; |
199 | ||
243dd6ae LP |
200 | n = strv_length(*strv); |
201 | for (i = 0; i < n; i++) { | |
d16a1c1b ZJS |
202 | int c; |
203 | ||
93bab288 | 204 | c = base_cmp((char* const*) *strv + i, (char* const*) &path); |
d16a1c1b | 205 | if (c == 0) { |
a6d8474f | 206 | char **dir; |
d16a1c1b | 207 | |
082bb1c5 ZJS |
208 | /* Oh, there already is an entry with a matching name (the last component). */ |
209 | ||
a6d8474f | 210 | STRV_FOREACH(dir, dirs) { |
082bb1c5 | 211 | _cleanup_free_ char *rdir = NULL; |
d16a1c1b ZJS |
212 | char *p1, *p2; |
213 | ||
c6134d3e | 214 | rdir = path_join(root, *dir); |
082bb1c5 ZJS |
215 | if (!rdir) |
216 | return -ENOMEM; | |
217 | ||
218 | p1 = path_startswith((*strv)[i], rdir); | |
d16a1c1b ZJS |
219 | if (p1) |
220 | /* Existing entry with higher priority | |
221 | * or same priority, no need to do anything. */ | |
222 | return 0; | |
223 | ||
a6d8474f | 224 | p2 = path_startswith(path, *dir); |
d16a1c1b ZJS |
225 | if (p2) { |
226 | /* Our new entry has higher priority */ | |
082bb1c5 | 227 | |
c6134d3e | 228 | t = path_join(root, path); |
d16a1c1b ZJS |
229 | if (!t) |
230 | return log_oom(); | |
231 | ||
232 | return free_and_replace((*strv)[i], t); | |
233 | } | |
234 | } | |
235 | ||
236 | } else if (c > 0) | |
237 | /* Following files have lower priority, let's go insert our | |
238 | * new entry. */ | |
239 | break; | |
240 | ||
241 | /* … we are not there yet, let's continue */ | |
242 | } | |
243 | ||
082bb1c5 | 244 | /* The new file has lower priority than all the existing entries */ |
c6134d3e | 245 | t = path_join(root, path); |
d16a1c1b | 246 | if (!t) |
a7181c67 | 247 | return -ENOMEM; |
d16a1c1b ZJS |
248 | |
249 | r = strv_insert(strv, i, t); | |
250 | if (r < 0) | |
251 | free(t); | |
d16a1c1b | 252 | |
380b82d6 | 253 | return r; |
a6d8474f ZJS |
254 | } |
255 | ||
94916255 | 256 | int conf_files_list_strv(char ***ret, const char *suffix, const char *root, unsigned flags, const char* const* dirs) { |
fabe5c0e LP |
257 | _cleanup_strv_free_ char **copy = NULL; |
258 | ||
94916255 | 259 | assert(ret); |
fabe5c0e LP |
260 | |
261 | copy = strv_copy((char**) dirs); | |
262 | if (!copy) | |
263 | return -ENOMEM; | |
264 | ||
94916255 | 265 | return conf_files_list_strv_internal(ret, suffix, root, flags, copy); |
2c21044f KS |
266 | } |
267 | ||
94916255 | 268 | int conf_files_list(char ***ret, const char *suffix, const char *root, unsigned flags, const char *dir) { |
8201ad81 | 269 | _cleanup_strv_free_ char **dirs = NULL; |
fabe5c0e | 270 | |
94916255 | 271 | assert(ret); |
2c21044f | 272 | |
36b12282 | 273 | dirs = strv_new(dir); |
8201ad81 LP |
274 | if (!dirs) |
275 | return -ENOMEM; | |
fabe5c0e | 276 | |
94916255 | 277 | return conf_files_list_strv_internal(ret, suffix, root, flags, dirs); |
fabe5c0e | 278 | } |
2c21044f | 279 | |
94916255 | 280 | int conf_files_list_nulstr(char ***ret, const char *suffix, const char *root, unsigned flags, const char *dirs) { |
a6d8474f | 281 | _cleanup_strv_free_ char **d = NULL; |
fabe5c0e | 282 | |
94916255 | 283 | assert(ret); |
fabe5c0e | 284 | |
a6d8474f ZJS |
285 | d = strv_split_nulstr(dirs); |
286 | if (!d) | |
fabe5c0e | 287 | return -ENOMEM; |
2c21044f | 288 | |
94916255 | 289 | return conf_files_list_strv_internal(ret, suffix, root, flags, d); |
2c21044f | 290 | } |
854a42fb | 291 | |
ceaaeb9b ZJS |
292 | int conf_files_list_with_replacement( |
293 | const char *root, | |
294 | char **config_dirs, | |
295 | const char *replacement, | |
94916255 LP |
296 | char ***ret_files, |
297 | char **ret_replace_file) { | |
ceaaeb9b ZJS |
298 | |
299 | _cleanup_strv_free_ char **f = NULL; | |
300 | _cleanup_free_ char *p = NULL; | |
301 | int r; | |
302 | ||
303 | assert(config_dirs); | |
94916255 LP |
304 | assert(ret_files); |
305 | assert(ret_replace_file || !replacement); | |
ceaaeb9b ZJS |
306 | |
307 | r = conf_files_list_strv(&f, ".conf", root, 0, (const char* const*) config_dirs); | |
308 | if (r < 0) | |
309 | return log_error_errno(r, "Failed to enumerate config files: %m"); | |
310 | ||
311 | if (replacement) { | |
312 | r = conf_files_insert(&f, root, config_dirs, replacement); | |
313 | if (r < 0) | |
314 | return log_error_errno(r, "Failed to extend config file list: %m"); | |
315 | ||
c6134d3e | 316 | p = path_join(root, replacement); |
ceaaeb9b ZJS |
317 | if (!p) |
318 | return log_oom(); | |
319 | } | |
320 | ||
94916255 LP |
321 | *ret_files = TAKE_PTR(f); |
322 | if (ret_replace_file) | |
323 | *ret_replace_file = TAKE_PTR(p); | |
324 | ||
ceaaeb9b ZJS |
325 | return 0; |
326 | } |