]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
9eb977db | 2 | |
9eb977db | 3 | #include <errno.h> |
11c3a366 | 4 | #include <limits.h> |
07630cea LP |
5 | #include <stdio.h> |
6 | #include <stdlib.h> | |
07630cea | 7 | #include <unistd.h> |
9eb977db | 8 | |
5f311f8c LP |
9 | /* When we include libgen.h because we need dirname() we immediately |
10 | * undefine basename() since libgen.h defines it as a macro to the | |
11 | * POSIX version which is really broken. We prefer GNU basename(). */ | |
12 | #include <libgen.h> | |
13 | #undef basename | |
14 | ||
b5efdb8a | 15 | #include "alloc-util.h" |
f4351959 | 16 | #include "chase-symlinks.h" |
93cc7779 | 17 | #include "extract-word.h" |
bb0c0d6f | 18 | #include "fd-util.h" |
f4f15635 | 19 | #include "fs-util.h" |
9eb977db | 20 | #include "log.h" |
07630cea | 21 | #include "macro.h" |
3ffd4af2 | 22 | #include "path-util.h" |
8fcde012 | 23 | #include "stat-util.h" |
07630cea | 24 | #include "string-util.h" |
9eb977db | 25 | #include "strv.h" |
93cc7779 | 26 | #include "time-util.h" |
9eb977db | 27 | |
0f474365 | 28 | int path_split_and_make_absolute(const char *p, char ***ret) { |
9eb977db | 29 | char **l; |
0f474365 LP |
30 | int r; |
31 | ||
9eb977db | 32 | assert(p); |
0f474365 | 33 | assert(ret); |
9eb977db | 34 | |
116cc028 ZJS |
35 | l = strv_split(p, ":"); |
36 | if (!l) | |
ce9d6bcf | 37 | return -ENOMEM; |
9eb977db | 38 | |
0f474365 LP |
39 | r = path_strv_make_absolute_cwd(l); |
40 | if (r < 0) { | |
9eb977db | 41 | strv_free(l); |
0f474365 | 42 | return r; |
9eb977db KS |
43 | } |
44 | ||
0f474365 LP |
45 | *ret = l; |
46 | return r; | |
9eb977db KS |
47 | } |
48 | ||
49 | char *path_make_absolute(const char *p, const char *prefix) { | |
50 | assert(p); | |
51 | ||
52 | /* Makes every item in the list an absolute path by prepending | |
53 | * the prefix, if specified and necessary */ | |
54 | ||
81cce8de | 55 | if (path_is_absolute(p) || isempty(prefix)) |
9eb977db KS |
56 | return strdup(p); |
57 | ||
657ee2d8 | 58 | return path_join(prefix, p); |
9eb977db KS |
59 | } |
60 | ||
a2556d25 | 61 | int safe_getcwd(char **ret) { |
aed3c5ec | 62 | _cleanup_free_ char *cwd = NULL; |
a2556d25 LP |
63 | |
64 | cwd = get_current_dir_name(); | |
65 | if (!cwd) | |
66 | return negative_errno(); | |
67 | ||
68 | /* Let's make sure the directory is really absolute, to protect us from the logic behind | |
69 | * CVE-2018-1000001 */ | |
aed3c5ec | 70 | if (cwd[0] != '/') |
a2556d25 | 71 | return -ENOMEDIUM; |
a2556d25 | 72 | |
aed3c5ec LP |
73 | if (ret) |
74 | *ret = TAKE_PTR(cwd); | |
75 | ||
a2556d25 LP |
76 | return 0; |
77 | } | |
78 | ||
0f474365 LP |
79 | int path_make_absolute_cwd(const char *p, char **ret) { |
80 | char *c; | |
d7249575 | 81 | int r; |
9eb977db KS |
82 | |
83 | assert(p); | |
0f474365 | 84 | assert(ret); |
9eb977db KS |
85 | |
86 | /* Similar to path_make_absolute(), but prefixes with the | |
87 | * current working directory. */ | |
88 | ||
89 | if (path_is_absolute(p)) | |
0f474365 LP |
90 | c = strdup(p); |
91 | else { | |
92 | _cleanup_free_ char *cwd = NULL; | |
9eb977db | 93 | |
d7249575 LP |
94 | r = safe_getcwd(&cwd); |
95 | if (r < 0) | |
96 | return r; | |
0f474365 | 97 | |
62a85ee0 | 98 | c = path_join(cwd, p); |
0f474365 LP |
99 | } |
100 | if (!c) | |
101 | return -ENOMEM; | |
9eb977db | 102 | |
0f474365 LP |
103 | *ret = c; |
104 | return 0; | |
9eb977db KS |
105 | } |
106 | ||
fe69c41e YW |
107 | int path_make_relative(const char *from, const char *to, char **ret) { |
108 | _cleanup_free_ char *result = NULL; | |
109 | unsigned n_parents; | |
110 | const char *f, *t; | |
111 | int r, k; | |
112 | char *p; | |
113 | ||
114 | assert(from); | |
115 | assert(to); | |
116 | assert(ret); | |
7cb9c51c TK |
117 | |
118 | /* Strips the common part, and adds ".." elements as necessary. */ | |
119 | ||
fe69c41e | 120 | if (!path_is_absolute(from) || !path_is_absolute(to)) |
7cb9c51c TK |
121 | return -EINVAL; |
122 | ||
7cb9c51c | 123 | for (;;) { |
fe69c41e YW |
124 | r = path_find_first_component(&from, true, &f); |
125 | if (r < 0) | |
126 | return r; | |
127 | ||
128 | k = path_find_first_component(&to, true, &t); | |
129 | if (k < 0) | |
130 | return k; | |
131 | ||
132 | if (r == 0) { | |
133 | /* end of 'from' */ | |
134 | if (k == 0) { | |
135 | /* from and to are equivalent. */ | |
136 | result = strdup("."); | |
137 | if (!result) | |
138 | return -ENOMEM; | |
139 | } else { | |
140 | /* 'to' is inside of 'from'. */ | |
141 | result = strdup(t); | |
142 | if (!result) | |
143 | return -ENOMEM; | |
144 | ||
4ff361cc | 145 | path_simplify(result); |
fe69c41e YW |
146 | |
147 | if (!path_is_valid(result)) | |
148 | return -EINVAL; | |
149 | } | |
150 | ||
151 | *ret = TAKE_PTR(result); | |
7cb9c51c TK |
152 | return 0; |
153 | } | |
154 | ||
fe69c41e | 155 | if (r != k || !strneq(f, t, r)) |
7cb9c51c | 156 | break; |
7cb9c51c TK |
157 | } |
158 | ||
159 | /* If we're here, then "from_dir" has one or more elements that need to | |
160 | * be replaced with "..". */ | |
161 | ||
fe69c41e YW |
162 | for (n_parents = 1;; n_parents++) { |
163 | /* If this includes ".." we can't do a simple series of "..". */ | |
164 | r = path_find_first_component(&from, false, &f); | |
165 | if (r < 0) | |
166 | return r; | |
167 | if (r == 0) | |
168 | break; | |
169 | } | |
2a5beb66 | 170 | |
fe69c41e YW |
171 | if (isempty(t) && n_parents * 3 > PATH_MAX) |
172 | /* PATH_MAX is counted *with* the trailing NUL byte */ | |
173 | return -EINVAL; | |
2a5beb66 | 174 | |
fe69c41e YW |
175 | result = new(char, n_parents * 3 + !isempty(t) + strlen_ptr(t)); |
176 | if (!result) | |
177 | return -ENOMEM; | |
2a5beb66 | 178 | |
fe69c41e YW |
179 | for (p = result; n_parents > 0; n_parents--) |
180 | p = mempcpy(p, "../", 3); | |
2a5beb66 | 181 | |
fe69c41e YW |
182 | if (isempty(t)) { |
183 | /* Remove trailing slash and terminate string. */ | |
184 | *(--p) = '\0'; | |
185 | *ret = TAKE_PTR(result); | |
186 | return 0; | |
7cb9c51c TK |
187 | } |
188 | ||
fe69c41e | 189 | strcpy(p, t); |
7cb9c51c | 190 | |
4ff361cc | 191 | path_simplify(result); |
7cb9c51c | 192 | |
fe69c41e YW |
193 | if (!path_is_valid(result)) |
194 | return -EINVAL; | |
7cb9c51c | 195 | |
fe69c41e | 196 | *ret = TAKE_PTR(result); |
7cb9c51c TK |
197 | return 0; |
198 | } | |
199 | ||
cc4d7d81 | 200 | char* path_startswith_strv(const char *p, char **set) { |
cc4d7d81 | 201 | STRV_FOREACH(s, set) { |
de010b0b YW |
202 | char *t; |
203 | ||
cc4d7d81 ZJS |
204 | t = path_startswith(p, *s); |
205 | if (t) | |
206 | return t; | |
207 | } | |
208 | ||
209 | return NULL; | |
210 | } | |
211 | ||
0f474365 | 212 | int path_strv_make_absolute_cwd(char **l) { |
0f474365 | 213 | int r; |
9eb977db KS |
214 | |
215 | /* Goes through every item in the string list and makes it | |
216 | * absolute. This works in place and won't rollback any | |
217 | * changes on failure. */ | |
218 | ||
219 | STRV_FOREACH(s, l) { | |
220 | char *t; | |
221 | ||
0f474365 LP |
222 | r = path_make_absolute_cwd(*s, &t); |
223 | if (r < 0) | |
224 | return r; | |
9eb977db | 225 | |
4ff361cc | 226 | path_simplify(t); |
32a8f700 | 227 | free_and_replace(*s, t); |
9eb977db KS |
228 | } |
229 | ||
0f474365 | 230 | return 0; |
9eb977db KS |
231 | } |
232 | ||
e1873695 | 233 | char **path_strv_resolve(char **l, const char *root) { |
9eb977db KS |
234 | unsigned k = 0; |
235 | bool enomem = false; | |
e1873695 | 236 | int r; |
9eb977db KS |
237 | |
238 | if (strv_isempty(l)) | |
239 | return l; | |
240 | ||
241 | /* Goes through every item in the string list and canonicalize | |
242 | * the path. This works in place and won't rollback any | |
243 | * changes on failure. */ | |
244 | ||
245 | STRV_FOREACH(s, l) { | |
12ed81d9 | 246 | _cleanup_free_ char *orig = NULL; |
e1873695 | 247 | char *t, *u; |
9eb977db | 248 | |
12ed81d9 ZJS |
249 | if (!path_is_absolute(*s)) { |
250 | free(*s); | |
9eb977db | 251 | continue; |
12ed81d9 | 252 | } |
112cfb18 | 253 | |
e1873695 | 254 | if (root) { |
12ed81d9 | 255 | orig = *s; |
c6134d3e | 256 | t = path_join(root, orig); |
112cfb18 MM |
257 | if (!t) { |
258 | enomem = true; | |
259 | continue; | |
260 | } | |
12ed81d9 | 261 | } else |
112cfb18 | 262 | t = *s; |
9eb977db | 263 | |
a5648b80 | 264 | r = chase_symlinks(t, root, 0, &u, NULL); |
e1873695 LP |
265 | if (r == -ENOENT) { |
266 | if (root) { | |
ae2a15bc | 267 | u = TAKE_PTR(orig); |
874310b7 | 268 | free(t); |
e1873695 LP |
269 | } else |
270 | u = t; | |
271 | } else if (r < 0) { | |
272 | free(t); | |
874310b7 | 273 | |
e1873695 LP |
274 | if (r == -ENOMEM) |
275 | enomem = true; | |
276 | ||
277 | continue; | |
278 | } else if (root) { | |
12ed81d9 ZJS |
279 | char *x; |
280 | ||
281 | free(t); | |
e1873695 | 282 | x = path_startswith(u, root); |
12ed81d9 ZJS |
283 | if (x) { |
284 | /* restore the slash if it was lost */ | |
285 | if (!startswith(x, "/")) | |
286 | *(--x) = '/'; | |
287 | ||
288 | t = strdup(x); | |
289 | free(u); | |
290 | if (!t) { | |
291 | enomem = true; | |
292 | continue; | |
293 | } | |
294 | u = t; | |
295 | } else { | |
296 | /* canonicalized path goes outside of | |
297 | * prefix, keep the original path instead */ | |
3b319885 | 298 | free_and_replace(u, orig); |
12ed81d9 | 299 | } |
91a6489d LP |
300 | } else |
301 | free(t); | |
9eb977db KS |
302 | |
303 | l[k++] = u; | |
304 | } | |
305 | ||
306 | l[k] = NULL; | |
307 | ||
308 | if (enomem) | |
309 | return NULL; | |
310 | ||
311 | return l; | |
312 | } | |
313 | ||
e1873695 | 314 | char **path_strv_resolve_uniq(char **l, const char *root) { |
112cfb18 | 315 | |
fabe5c0e LP |
316 | if (strv_isempty(l)) |
317 | return l; | |
318 | ||
e1873695 | 319 | if (!path_strv_resolve(l, root)) |
fabe5c0e LP |
320 | return NULL; |
321 | ||
322 | return strv_uniq(l); | |
323 | } | |
324 | ||
4ff361cc | 325 | char *path_simplify(char *path) { |
cb71ed91 YW |
326 | bool add_slash = false; |
327 | char *f = path; | |
328 | int r; | |
9eb977db | 329 | |
858d36c1 YW |
330 | assert(path); |
331 | ||
cb71ed91 YW |
332 | /* Removes redundant inner and trailing slashes. Also removes unnecessary dots. |
333 | * Modifies the passed string in-place. | |
9eb977db | 334 | * |
cb71ed91 YW |
335 | * ///foo//./bar/. becomes /foo/bar |
336 | * .//./foo//./bar/. becomes foo/bar | |
9eb977db KS |
337 | */ |
338 | ||
afbae3e9 TH |
339 | if (isempty(path)) |
340 | return path; | |
341 | ||
cb71ed91 | 342 | if (path_is_absolute(path)) |
858d36c1 | 343 | f++; |
858d36c1 | 344 | |
cb71ed91 YW |
345 | for (const char *p = f;;) { |
346 | const char *e; | |
9eb977db | 347 | |
cb71ed91 YW |
348 | r = path_find_first_component(&p, true, &e); |
349 | if (r == 0) | |
350 | break; | |
9eb977db | 351 | |
cb71ed91 YW |
352 | if (add_slash) |
353 | *f++ = '/'; | |
858d36c1 | 354 | |
cb71ed91 YW |
355 | if (r < 0) { |
356 | /* if path is invalid, then refuse to simplify remaining part. */ | |
357 | memmove(f, p, strlen(p) + 1); | |
358 | return path; | |
9eb977db KS |
359 | } |
360 | ||
cb71ed91 YW |
361 | memmove(f, e, r); |
362 | f += r; | |
9eb977db | 363 | |
cb71ed91 | 364 | add_slash = true; |
afbae3e9 | 365 | } |
9eb977db | 366 | |
cb71ed91 YW |
367 | /* Special rule, if we stripped everything, we need a "." for the current directory. */ |
368 | if (f == path) | |
369 | *f++ = '.'; | |
370 | ||
371 | *f = '\0'; | |
9eb977db KS |
372 | return path; |
373 | } | |
374 | ||
63f11e35 | 375 | char *path_startswith_full(const char *path, const char *prefix, bool accept_dot_dot) { |
9eb977db KS |
376 | assert(path); |
377 | assert(prefix); | |
378 | ||
0470289b ZJS |
379 | /* Returns a pointer to the start of the first component after the parts matched by |
380 | * the prefix, iff | |
381 | * - both paths are absolute or both paths are relative, | |
382 | * and | |
383 | * - each component in prefix in turn matches a component in path at the same position. | |
384 | * An empty string will be returned when the prefix and path are equivalent. | |
385 | * | |
386 | * Returns NULL otherwise. | |
387 | */ | |
388 | ||
9eb977db | 389 | if ((path[0] == '/') != (prefix[0] == '/')) |
424a19f8 | 390 | return NULL; |
9eb977db KS |
391 | |
392 | for (;;) { | |
63f11e35 YW |
393 | const char *p, *q; |
394 | int r, k; | |
9eb977db | 395 | |
63f11e35 YW |
396 | r = path_find_first_component(&path, accept_dot_dot, &p); |
397 | if (r < 0) | |
398 | return NULL; | |
9eb977db | 399 | |
63f11e35 YW |
400 | k = path_find_first_component(&prefix, accept_dot_dot, &q); |
401 | if (k < 0) | |
424a19f8 | 402 | return NULL; |
9eb977db | 403 | |
63f11e35 YW |
404 | if (k == 0) |
405 | return (char*) (p ?: path); | |
9eb977db | 406 | |
63f11e35 | 407 | if (r != k) |
424a19f8 | 408 | return NULL; |
9eb977db | 409 | |
63f11e35 | 410 | if (!strneq(p, q, r)) |
424a19f8 | 411 | return NULL; |
9eb977db KS |
412 | } |
413 | } | |
414 | ||
2230852b | 415 | int path_compare(const char *a, const char *b) { |
353df443 | 416 | int r; |
2230852b | 417 | |
10539677 LP |
418 | /* Order NULL before non-NULL */ |
419 | r = CMP(!!a, !!b); | |
420 | if (r != 0) | |
421 | return r; | |
9eb977db | 422 | |
f21f31b2 | 423 | /* A relative path and an absolute path must not compare as equal. |
2230852b MS |
424 | * Which one is sorted before the other does not really matter. |
425 | * Here a relative path is ordered before an absolute path. */ | |
353df443 YW |
426 | r = CMP(path_is_absolute(a), path_is_absolute(b)); |
427 | if (r != 0) | |
428 | return r; | |
9eb977db KS |
429 | |
430 | for (;;) { | |
353df443 YW |
431 | const char *aa, *bb; |
432 | int j, k; | |
9eb977db | 433 | |
353df443 YW |
434 | j = path_find_first_component(&a, true, &aa); |
435 | k = path_find_first_component(&b, true, &bb); | |
9eb977db | 436 | |
353df443 YW |
437 | if (j < 0 || k < 0) { |
438 | /* When one of paths is invalid, order invalid path after valid one. */ | |
439 | r = CMP(j < 0, k < 0); | |
440 | if (r != 0) | |
441 | return r; | |
442 | ||
443 | /* fallback to use strcmp() if both paths are invalid. */ | |
444 | return strcmp(a, b); | |
445 | } | |
9eb977db | 446 | |
2230852b | 447 | /* Order prefixes first: "/foo" before "/foo/bar" */ |
353df443 YW |
448 | if (j == 0) { |
449 | if (k == 0) | |
450 | return 0; | |
2230852b | 451 | return -1; |
353df443 YW |
452 | } |
453 | if (k == 0) | |
2230852b | 454 | return 1; |
9eb977db | 455 | |
2230852b | 456 | /* Alphabetical sort: "/foo/aaa" before "/foo/b" */ |
353df443 YW |
457 | r = memcmp(aa, bb, MIN(j, k)); |
458 | if (r != 0) | |
459 | return r; | |
9eb977db | 460 | |
2230852b | 461 | /* Sort "/foo/a" before "/foo/aaa" */ |
353df443 YW |
462 | r = CMP(j, k); |
463 | if (r != 0) | |
464 | return r; | |
9eb977db KS |
465 | } |
466 | } | |
467 | ||
e3f791a2 ZJS |
468 | bool path_equal_or_files_same(const char *a, const char *b, int flags) { |
469 | return path_equal(a, b) || files_same(a, b, flags) > 0; | |
c78e47a6 MS |
470 | } |
471 | ||
727e63e3 LB |
472 | bool path_equal_filename(const char *a, const char *b) { |
473 | _cleanup_free_ char *a_basename = NULL, *b_basename = NULL; | |
474 | int r; | |
475 | ||
476 | assert(a); | |
477 | assert(b); | |
478 | ||
479 | r = path_extract_filename(a, &a_basename); | |
480 | if (r < 0) { | |
481 | log_debug_errno(r, "Failed to parse basename of %s: %m", a); | |
482 | return false; | |
483 | } | |
484 | r = path_extract_filename(b, &b_basename); | |
485 | if (r < 0) { | |
486 | log_debug_errno(r, "Failed to parse basename of %s: %m", b); | |
487 | return false; | |
488 | } | |
489 | ||
490 | return path_equal(a_basename, b_basename); | |
491 | } | |
492 | ||
7ae27680 LP |
493 | char* path_extend_internal(char **x, ...) { |
494 | size_t sz, old_sz; | |
495 | char *q, *nx; | |
cd8194a3 LP |
496 | const char *p; |
497 | va_list ap; | |
498 | bool slash; | |
cd8194a3 | 499 | |
652ef298 ZJS |
500 | /* Joins all listed strings until the sentinel and places a "/" between them unless the strings end/begin |
501 | * already with one so that it is unnecessary. Note that slashes which are already duplicate won't be | |
502 | * removed. The string returned is hence always equal to or longer than the sum of the lengths of each | |
503 | * individual string. | |
cd8194a3 | 504 | * |
7ae27680 LP |
505 | * The first argument may be an already allocated string that is extended via realloc() if |
506 | * non-NULL. path_extend() and path_join() are macro wrappers around this function, making use of the | |
507 | * first parameter to distinguish the two operations. | |
508 | * | |
cd8194a3 LP |
509 | * Note: any listed empty string is simply skipped. This can be useful for concatenating strings of which some |
510 | * are optional. | |
511 | * | |
512 | * Examples: | |
513 | * | |
62a85ee0 ZJS |
514 | * path_join("foo", "bar") → "foo/bar" |
515 | * path_join("foo/", "bar") → "foo/bar" | |
516 | * path_join("", "foo", "", "bar", "") → "foo/bar" */ | |
cd8194a3 | 517 | |
7ae27680 LP |
518 | sz = old_sz = x ? strlen_ptr(*x) : 0; |
519 | va_start(ap, x); | |
520 | while ((p = va_arg(ap, char*)) != POINTER_MAX) { | |
521 | size_t add; | |
522 | ||
523 | if (isempty(p)) | |
524 | continue; | |
525 | ||
526 | add = 1 + strlen(p); | |
50fd596e YW |
527 | if (sz > SIZE_MAX - add) { /* overflow check */ |
528 | va_end(ap); | |
7ae27680 | 529 | return NULL; |
50fd596e | 530 | } |
7ae27680 LP |
531 | |
532 | sz += add; | |
533 | } | |
cd8194a3 LP |
534 | va_end(ap); |
535 | ||
7ae27680 LP |
536 | nx = realloc(x ? *x : NULL, GREEDY_ALLOC_ROUND_UP(sz+1)); |
537 | if (!nx) | |
cd8194a3 | 538 | return NULL; |
7ae27680 LP |
539 | if (x) |
540 | *x = nx; | |
cd8194a3 | 541 | |
7ae27680 | 542 | if (old_sz > 0) |
340cd6b6 | 543 | slash = nx[old_sz-1] == '/'; |
7ae27680 LP |
544 | else { |
545 | nx[old_sz] = 0; | |
cd8194a3 LP |
546 | slash = true; /* no need to generate a slash anymore */ |
547 | } | |
548 | ||
7ae27680 LP |
549 | q = nx + old_sz; |
550 | ||
551 | va_start(ap, x); | |
66032ef4 | 552 | while ((p = va_arg(ap, char*)) != POINTER_MAX) { |
652ef298 | 553 | if (isempty(p)) |
cd8194a3 LP |
554 | continue; |
555 | ||
556 | if (!slash && p[0] != '/') | |
557 | *(q++) = '/'; | |
558 | ||
559 | q = stpcpy(q, p); | |
560 | slash = endswith(p, "/"); | |
561 | } | |
562 | va_end(ap); | |
563 | ||
7ae27680 | 564 | return nx; |
cd8194a3 LP |
565 | } |
566 | ||
5ca9139a | 567 | static int check_x_access(const char *path, int *ret_fd) { |
ece852c8 | 568 | _cleanup_close_ int fd = -1; |
ece852c8 | 569 | int r; |
5ca9139a | 570 | |
888f65ac YW |
571 | /* We need to use O_PATH because there may be executables for which we have only exec |
572 | * permissions, but not read (usually suid executables). */ | |
573 | fd = open(path, O_PATH|O_CLOEXEC); | |
574 | if (fd < 0) | |
575 | return -errno; | |
ece852c8 | 576 | |
888f65ac YW |
577 | r = fd_verify_regular(fd); |
578 | if (r < 0) | |
579 | return r; | |
ece852c8 | 580 | |
888f65ac | 581 | r = access_fd(fd, X_OK); |
93413acd YW |
582 | if (r == -ENOSYS) { |
583 | /* /proc is not mounted. Fallback to access(). */ | |
584 | if (access(path, X_OK) < 0) | |
585 | return -errno; | |
586 | } else if (r < 0) | |
888f65ac | 587 | return r; |
ece852c8 YW |
588 | |
589 | if (ret_fd) | |
590 | *ret_fd = TAKE_FD(fd); | |
591 | ||
5ca9139a ZJS |
592 | return 0; |
593 | } | |
594 | ||
ded8039a YW |
595 | static int find_executable_impl(const char *name, const char *root, char **ret_filename, int *ret_fd) { |
596 | _cleanup_close_ int fd = -1; | |
597 | _cleanup_free_ char *path_name = NULL; | |
598 | int r; | |
85eca92e | 599 | |
c9d954b2 | 600 | assert(name); |
4087cb9e | 601 | |
ded8039a YW |
602 | /* Function chase_symlinks() is invoked only when root is not NULL, as using it regardless of |
603 | * root value would alter the behavior of existing callers for example: /bin/sleep would become | |
604 | * /usr/bin/sleep when find_executables is called. Hence, this function should be invoked when | |
605 | * needed to avoid unforeseen regression or other complicated changes. */ | |
606 | if (root) { | |
607 | r = chase_symlinks(name, | |
608 | root, | |
609 | CHASE_PREFIX_ROOT, | |
610 | &path_name, | |
611 | /* ret_fd= */ NULL); /* prefix root to name in case full paths are not specified */ | |
612 | if (r < 0) | |
613 | return r; | |
36f4af05 | 614 | |
ded8039a YW |
615 | name = path_name; |
616 | } | |
36f4af05 | 617 | |
ded8039a YW |
618 | r = check_x_access(name, ret_fd ? &fd : NULL); |
619 | if (r < 0) | |
620 | return r; | |
b972115c | 621 | |
ded8039a YW |
622 | if (ret_filename) { |
623 | r = path_make_absolute_cwd(name, ret_filename); | |
5ca9139a ZJS |
624 | if (r < 0) |
625 | return r; | |
ded8039a | 626 | } |
5ca9139a | 627 | |
ded8039a YW |
628 | if (ret_fd) |
629 | *ret_fd = TAKE_FD(fd); | |
c9d954b2 | 630 | |
ded8039a YW |
631 | return 0; |
632 | } | |
5ca9139a | 633 | |
8c35c10d | 634 | int find_executable_full(const char *name, const char *root, char **exec_search_path, bool use_path_envvar, char **ret_filename, int *ret_fd) { |
635 | int last_error = -ENOENT, r = 0; | |
ded8039a YW |
636 | const char *p = NULL; |
637 | ||
638 | assert(name); | |
639 | ||
640 | if (is_path(name)) | |
641 | return find_executable_impl(name, root, ret_filename, ret_fd); | |
c9d954b2 | 642 | |
92673045 ZJS |
643 | if (use_path_envvar) |
644 | /* Plain getenv, not secure_getenv, because we want to actually allow the user to pick the | |
645 | * binary. */ | |
646 | p = getenv("PATH"); | |
85eca92e LP |
647 | if (!p) |
648 | p = DEFAULT_PATH; | |
649 | ||
8c35c10d | 650 | if (exec_search_path) { |
8c35c10d | 651 | STRV_FOREACH(element, exec_search_path) { |
652 | _cleanup_free_ char *full_path = NULL; | |
de010b0b | 653 | |
8c35c10d | 654 | if (!path_is_absolute(*element)) |
655 | continue; | |
de010b0b | 656 | |
8c35c10d | 657 | full_path = path_join(*element, name); |
658 | if (!full_path) | |
659 | return -ENOMEM; | |
660 | ||
661 | r = find_executable_impl(full_path, root, ret_filename, ret_fd); | |
662 | if (r < 0) { | |
663 | if (r != -EACCES) | |
664 | last_error = r; | |
665 | continue; | |
666 | } | |
667 | return 0; | |
668 | } | |
669 | return last_error; | |
670 | } | |
85eca92e | 671 | |
5ca9139a | 672 | /* Resolve a single-component name to a full path */ |
85eca92e | 673 | for (;;) { |
4ede9802 | 674 | _cleanup_free_ char *element = NULL; |
85eca92e LP |
675 | |
676 | r = extract_first_word(&p, &element, ":", EXTRACT_RELAX|EXTRACT_DONT_COALESCE_SEPARATORS); | |
677 | if (r < 0) | |
678 | return r; | |
679 | if (r == 0) | |
680 | break; | |
681 | ||
0f474365 LP |
682 | if (!path_is_absolute(element)) |
683 | continue; | |
684 | ||
4ede9802 | 685 | if (!path_extend(&element, name)) |
85eca92e LP |
686 | return -ENOMEM; |
687 | ||
ded8039a | 688 | r = find_executable_impl(element, root, ret_filename, ret_fd); |
ece852c8 YW |
689 | if (r < 0) { |
690 | /* PATH entries which we don't have access to are ignored, as per tradition. */ | |
691 | if (r != -EACCES) | |
692 | last_error = r; | |
693 | continue; | |
c9d954b2 ZJS |
694 | } |
695 | ||
ece852c8 | 696 | /* Found it! */ |
ece852c8 | 697 | return 0; |
c9d954b2 | 698 | } |
85eca92e LP |
699 | |
700 | return last_error; | |
c9d954b2 | 701 | } |
8e184852 | 702 | |
2ad8416d | 703 | bool paths_check_timestamp(const char* const* paths, usec_t *timestamp, bool update) { |
0d5a24be | 704 | bool changed = false, originally_unset; |
8e184852 | 705 | |
97f2d76d TG |
706 | assert(timestamp); |
707 | ||
234519ae | 708 | if (!paths) |
4087cb9e | 709 | return false; |
8e184852 | 710 | |
0d5a24be ZJS |
711 | originally_unset = *timestamp == 0; |
712 | ||
4087cb9e | 713 | STRV_FOREACH(i, paths) { |
8e184852 | 714 | struct stat stats; |
4087cb9e | 715 | usec_t u; |
8e184852 | 716 | |
4087cb9e | 717 | if (stat(*i, &stats) < 0) |
8e184852 TG |
718 | continue; |
719 | ||
4087cb9e LP |
720 | u = timespec_load(&stats.st_mtim); |
721 | ||
0d5a24be | 722 | /* check first */ |
4087cb9e | 723 | if (*timestamp >= u) |
8e184852 TG |
724 | continue; |
725 | ||
0d5a24be | 726 | log_debug(originally_unset ? "Loaded timestamp for '%s'." : "Timestamp of '%s' changed.", *i); |
8e184852 TG |
727 | |
728 | /* update timestamp */ | |
4087cb9e LP |
729 | if (update) { |
730 | *timestamp = u; | |
731 | changed = true; | |
732 | } else | |
733 | return true; | |
8e184852 | 734 | } |
4087cb9e | 735 | |
8e184852 TG |
736 | return changed; |
737 | } | |
eb66db55 | 738 | |
f7bc0c32 | 739 | static int executable_is_good(const char *executable) { |
571d0134 | 740 | _cleanup_free_ char *p = NULL, *d = NULL; |
571d0134 | 741 | int r; |
eb66db55 | 742 | |
f7bc0c32 | 743 | r = find_executable(executable, &p); |
85eca92e LP |
744 | if (r == -ENOENT) |
745 | return 0; | |
571d0134 LP |
746 | if (r < 0) |
747 | return r; | |
748 | ||
f7bc0c32 | 749 | /* An fsck that is linked to /bin/true is a non-existent fsck */ |
571d0134 LP |
750 | |
751 | r = readlink_malloc(p, &d); | |
85eca92e LP |
752 | if (r == -EINVAL) /* not a symlink */ |
753 | return 1; | |
754 | if (r < 0) | |
755 | return r; | |
571d0134 | 756 | |
3ae5990c ZJS |
757 | return !PATH_IN_SET(d, "true" |
758 | "/bin/true", | |
759 | "/usr/bin/true", | |
760 | "/dev/null"); | |
eb66db55 | 761 | } |
1d13f648 | 762 | |
5bcd08db LP |
763 | int fsck_exists(const char *fstype) { |
764 | const char *checker; | |
765 | ||
85eca92e | 766 | assert(fstype); |
5bcd08db | 767 | |
85eca92e LP |
768 | if (streq(fstype, "auto")) |
769 | return -EINVAL; | |
770 | ||
771 | checker = strjoina("fsck.", fstype); | |
f7bc0c32 | 772 | return executable_is_good(checker); |
5bcd08db LP |
773 | } |
774 | ||
5f311f8c LP |
775 | char* dirname_malloc(const char *path) { |
776 | char *d, *dir, *dir2; | |
777 | ||
778 | assert(path); | |
779 | ||
780 | d = strdup(path); | |
781 | if (!d) | |
782 | return NULL; | |
783 | ||
784 | dir = dirname(d); | |
785 | assert(dir); | |
786 | ||
787 | if (dir == d) | |
788 | return d; | |
789 | ||
790 | dir2 = strdup(dir); | |
791 | free(d); | |
792 | ||
793 | return dir2; | |
794 | } | |
bb15fafe | 795 | |
0ee54dd4 YW |
796 | static const char *skip_slash_or_dot(const char *p) { |
797 | for (; !isempty(p); p++) { | |
798 | if (*p == '/') | |
799 | continue; | |
800 | if (startswith(p, "./")) { | |
801 | p++; | |
802 | continue; | |
803 | } | |
804 | break; | |
805 | } | |
806 | return p; | |
807 | } | |
808 | ||
809 | int path_find_first_component(const char **p, bool accept_dot_dot, const char **ret) { | |
810 | const char *q, *first, *end_first, *next; | |
811 | size_t len; | |
812 | ||
813 | assert(p); | |
814 | ||
815 | /* When a path is input, then returns the pointer to the first component and its length, and | |
816 | * move the input pointer to the next component or nul. This skips both over any '/' | |
817 | * immediately *before* and *after* the first component before returning. | |
818 | * | |
819 | * Examples | |
820 | * Input: p: "//.//aaa///bbbbb/cc" | |
821 | * Output: p: "bbbbb///cc" | |
822 | * ret: "aaa///bbbbb/cc" | |
823 | * return value: 3 (== strlen("aaa")) | |
824 | * | |
825 | * Input: p: "aaa//" | |
826 | * Output: p: (pointer to NUL) | |
827 | * ret: "aaa//" | |
828 | * return value: 3 (== strlen("aaa")) | |
829 | * | |
830 | * Input: p: "/", ".", "" | |
831 | * Output: p: (pointer to NUL) | |
832 | * ret: NULL | |
833 | * return value: 0 | |
834 | * | |
835 | * Input: p: NULL | |
836 | * Output: p: NULL | |
837 | * ret: NULL | |
838 | * return value: 0 | |
839 | * | |
840 | * Input: p: "(too long component)" | |
841 | * Output: return value: -EINVAL | |
842 | * | |
843 | * (when accept_dot_dot is false) | |
844 | * Input: p: "//..//aaa///bbbbb/cc" | |
845 | * Output: return value: -EINVAL | |
846 | */ | |
847 | ||
848 | q = *p; | |
849 | ||
850 | first = skip_slash_or_dot(q); | |
851 | if (isempty(first)) { | |
852 | *p = first; | |
853 | if (ret) | |
854 | *ret = NULL; | |
855 | return 0; | |
856 | } | |
857 | if (streq(first, ".")) { | |
858 | *p = first + 1; | |
859 | if (ret) | |
860 | *ret = NULL; | |
861 | return 0; | |
862 | } | |
863 | ||
864 | end_first = strchrnul(first, '/'); | |
865 | len = end_first - first; | |
866 | ||
867 | if (len > NAME_MAX) | |
868 | return -EINVAL; | |
869 | if (!accept_dot_dot && len == 2 && first[0] == '.' && first[1] == '.') | |
870 | return -EINVAL; | |
871 | ||
872 | next = skip_slash_or_dot(end_first); | |
873 | ||
874 | *p = next + streq(next, "."); | |
875 | if (ret) | |
876 | *ret = first; | |
877 | return len; | |
878 | } | |
879 | ||
484cd43c YW |
880 | static const char *skip_slash_or_dot_backward(const char *path, const char *q) { |
881 | assert(path); | |
3066293d | 882 | assert(!q || q >= path); |
484cd43c | 883 | |
3066293d | 884 | for (; q; q = PTR_SUB1(q, path)) { |
484cd43c YW |
885 | if (*q == '/') |
886 | continue; | |
887 | if (q > path && strneq(q - 1, "/.", 2)) | |
888 | continue; | |
889 | break; | |
890 | } | |
891 | return q; | |
892 | } | |
893 | ||
894 | int path_find_last_component(const char *path, bool accept_dot_dot, const char **next, const char **ret) { | |
895 | const char *q, *last_end, *last_begin; | |
896 | size_t len; | |
897 | ||
898 | /* Similar to path_find_first_component(), but search components from the end. | |
899 | * | |
900 | * Examples | |
901 | * Input: path: "//.//aaa///bbbbb/cc//././" | |
902 | * next: NULL | |
903 | * Output: next: "/cc//././" | |
904 | * ret: "cc//././" | |
905 | * return value: 2 (== strlen("cc")) | |
906 | * | |
907 | * Input: path: "//.//aaa///bbbbb/cc//././" | |
908 | * next: "/cc//././" | |
909 | * Output: next: "///bbbbb/cc//././" | |
910 | * ret: "bbbbb/cc//././" | |
911 | * return value: 5 (== strlen("bbbbb")) | |
912 | * | |
913 | * Input: path: "/", ".", "", or NULL | |
914 | * Output: next: equivalent to path | |
915 | * ret: NULL | |
916 | * return value: 0 | |
917 | * | |
918 | * Input: path: "(too long component)" | |
919 | * Output: return value: -EINVAL | |
920 | * | |
921 | * (when accept_dot_dot is false) | |
922 | * Input: path: "//..//aaa///bbbbb/cc/..//" | |
923 | * Output: return value: -EINVAL | |
924 | */ | |
925 | ||
926 | if (isempty(path)) { | |
927 | if (next) | |
928 | *next = path; | |
929 | if (ret) | |
930 | *ret = NULL; | |
931 | return 0; | |
932 | } | |
933 | ||
934 | if (next && *next) { | |
935 | if (*next < path || *next > path + strlen(path)) | |
936 | return -EINVAL; | |
937 | if (*next == path) { | |
938 | if (ret) | |
939 | *ret = NULL; | |
940 | return 0; | |
941 | } | |
942 | if (!IN_SET(**next, '\0', '/')) | |
943 | return -EINVAL; | |
944 | q = *next - 1; | |
945 | } else | |
946 | q = path + strlen(path) - 1; | |
947 | ||
948 | q = skip_slash_or_dot_backward(path, q); | |
3066293d | 949 | if (!q || /* the root directory */ |
484cd43c YW |
950 | (q == path && *q == '.')) { /* path is "." or "./" */ |
951 | if (next) | |
952 | *next = path; | |
953 | if (ret) | |
954 | *ret = NULL; | |
955 | return 0; | |
956 | } | |
957 | ||
958 | last_end = q + 1; | |
959 | ||
3066293d YW |
960 | while (q && *q != '/') |
961 | q = PTR_SUB1(q, path); | |
484cd43c | 962 | |
3066293d | 963 | last_begin = q ? q + 1 : path; |
484cd43c YW |
964 | len = last_end - last_begin; |
965 | ||
966 | if (len > NAME_MAX) | |
967 | return -EINVAL; | |
968 | if (!accept_dot_dot && len == 2 && strneq(last_begin, "..", 2)) | |
969 | return -EINVAL; | |
970 | ||
971 | if (next) { | |
972 | q = skip_slash_or_dot_backward(path, q); | |
3066293d | 973 | *next = q ? q + 1 : path; |
484cd43c YW |
974 | } |
975 | ||
976 | if (ret) | |
977 | *ret = last_begin; | |
978 | return len; | |
979 | } | |
980 | ||
b12d25a8 | 981 | const char *last_path_component(const char *path) { |
8460289f LP |
982 | |
983 | /* Finds the last component of the path, preserving the optional trailing slash that signifies a directory. | |
984 | * | |
b12d25a8 ZJS |
985 | * a/b/c → c |
986 | * a/b/c/ → c/ | |
8460289f LP |
987 | * x → x |
988 | * x/ → x/ | |
989 | * /y → y | |
990 | * /y/ → y/ | |
b12d25a8 ZJS |
991 | * / → / |
992 | * // → / | |
993 | * /foo/a → a | |
994 | * /foo/a/ → a/ | |
8460289f LP |
995 | * |
996 | * Also, the empty string is mapped to itself. | |
997 | * | |
998 | * This is different than basename(), which returns "" when a trailing slash is present. | |
3cdcbdd3 LP |
999 | * |
1000 | * This always succeeds (except if you pass NULL in which case it returns NULL, too). | |
b12d25a8 ZJS |
1001 | */ |
1002 | ||
1003 | unsigned l, k; | |
1004 | ||
77e0a1b5 LP |
1005 | if (!path) |
1006 | return NULL; | |
1007 | ||
b12d25a8 | 1008 | l = k = strlen(path); |
69f9ccf1 ZJS |
1009 | if (l == 0) /* special case — an empty string */ |
1010 | return path; | |
1011 | ||
b12d25a8 ZJS |
1012 | while (k > 0 && path[k-1] == '/') |
1013 | k--; | |
1014 | ||
1015 | if (k == 0) /* the root directory */ | |
1016 | return path + l - 1; | |
1017 | ||
1018 | while (k > 0 && path[k-1] != '/') | |
1019 | k--; | |
1020 | ||
1021 | return path + k; | |
1022 | } | |
1023 | ||
01950464 | 1024 | int path_extract_filename(const char *path, char **ret) { |
a60c8eee | 1025 | _cleanup_free_ char *a = NULL; |
01950464 YW |
1026 | const char *c, *next = NULL; |
1027 | int r; | |
a60c8eee LP |
1028 | |
1029 | /* Extracts the filename part (i.e. right-most component) from a path, i.e. string that passes | |
ee277c6b LP |
1030 | * filename_is_valid(). A wrapper around last_path_component(), but eats up trailing |
1031 | * slashes. Returns: | |
1032 | * | |
01950464 YW |
1033 | * -EINVAL → if the path is not valid |
1034 | * -EADDRNOTAVAIL → if only a directory was specified, but no filename, i.e. the root dir | |
1035 | * itself or "." is specified | |
ee277c6b LP |
1036 | * -ENOMEM → no memory |
1037 | * | |
01950464 YW |
1038 | * Returns >= 0 on success. If the input path has a trailing slash, returns O_DIRECTORY, to |
1039 | * indicate the referenced file must be a directory. | |
ee277c6b LP |
1040 | * |
1041 | * This function guarantees to return a fully valid filename, i.e. one that passes | |
1042 | * filename_is_valid() – this means "." and ".." are not accepted. */ | |
a60c8eee | 1043 | |
01950464 | 1044 | if (!path_is_valid(path)) |
a60c8eee LP |
1045 | return -EINVAL; |
1046 | ||
01950464 YW |
1047 | r = path_find_last_component(path, false, &next, &c); |
1048 | if (r < 0) | |
1049 | return r; | |
1050 | if (r == 0) /* root directory */ | |
3cdcbdd3 | 1051 | return -EADDRNOTAVAIL; |
a60c8eee | 1052 | |
01950464 | 1053 | a = strndup(c, r); |
a60c8eee LP |
1054 | if (!a) |
1055 | return -ENOMEM; | |
1056 | ||
a60c8eee | 1057 | *ret = TAKE_PTR(a); |
01950464 | 1058 | return strlen(c) > (size_t)r ? O_DIRECTORY : 0; |
a60c8eee LP |
1059 | } |
1060 | ||
01950464 | 1061 | int path_extract_directory(const char *path, char **ret) { |
8dcb891c | 1062 | _cleanup_free_ char *a = NULL; |
01950464 YW |
1063 | const char *c, *next = NULL; |
1064 | int r; | |
8dcb891c LP |
1065 | |
1066 | /* The inverse of path_extract_filename(), i.e. returns the directory path prefix. Returns: | |
1067 | * | |
01950464 | 1068 | * -EINVAL → if the path is not valid |
8dcb891c | 1069 | * -EDESTADDRREQ → if no directory was specified in the passed in path, i.e. only a filename was passed |
01950464 YW |
1070 | * -EADDRNOTAVAIL → if the passed in parameter had no filename but did have a directory, i.e. |
1071 | * the root dir itself or "." was specified | |
8dcb891c LP |
1072 | * -ENOMEM → no memory (surprise!) |
1073 | * | |
1074 | * This function guarantees to return a fully valid path, i.e. one that passes path_is_valid(). | |
1075 | */ | |
1076 | ||
01950464 YW |
1077 | r = path_find_last_component(path, false, &next, &c); |
1078 | if (r < 0) | |
1079 | return r; | |
1080 | if (r == 0) /* empty or root */ | |
1081 | return isempty(path) ? -EINVAL : -EADDRNOTAVAIL; | |
1082 | if (next == path) { | |
1083 | if (*path != '/') /* filename only */ | |
1084 | return -EDESTADDRREQ; | |
1085 | ||
1086 | a = strdup("/"); | |
1087 | if (!a) | |
1088 | return -ENOMEM; | |
1089 | *ret = TAKE_PTR(a); | |
1090 | return 0; | |
1091 | } | |
8dcb891c | 1092 | |
01950464 | 1093 | a = strndup(path, next - path); |
8dcb891c LP |
1094 | if (!a) |
1095 | return -ENOMEM; | |
1096 | ||
4ff361cc | 1097 | path_simplify(a); |
01950464 | 1098 | |
8dcb891c LP |
1099 | if (!path_is_valid(a)) |
1100 | return -EINVAL; | |
1101 | ||
1102 | *ret = TAKE_PTR(a); | |
1103 | return 0; | |
1104 | } | |
1105 | ||
bb15fafe LP |
1106 | bool filename_is_valid(const char *p) { |
1107 | const char *e; | |
1108 | ||
1109 | if (isempty(p)) | |
1110 | return false; | |
1111 | ||
49bfc877 | 1112 | if (dot_or_dot_dot(p)) |
bb15fafe LP |
1113 | return false; |
1114 | ||
1115 | e = strchrnul(p, '/'); | |
1116 | if (*e != 0) | |
1117 | return false; | |
1118 | ||
2ef2376d | 1119 | if (e - p > NAME_MAX) /* NAME_MAX is counted *without* the trailing NUL byte */ |
bb15fafe LP |
1120 | return false; |
1121 | ||
1122 | return true; | |
1123 | } | |
1124 | ||
32df2e14 | 1125 | bool path_is_valid_full(const char *p, bool accept_dot_dot) { |
bb15fafe LP |
1126 | if (isempty(p)) |
1127 | return false; | |
1128 | ||
2ef2376d | 1129 | for (const char *e = p;;) { |
66368835 YW |
1130 | int r; |
1131 | ||
32df2e14 | 1132 | r = path_find_first_component(&e, accept_dot_dot, NULL); |
66368835 YW |
1133 | if (r < 0) |
1134 | return false; | |
49bfc877 | 1135 | |
2ef2376d LP |
1136 | if (e - p >= PATH_MAX) /* Already reached the maximum length for a path? (PATH_MAX is counted |
1137 | * *with* the trailing NUL byte) */ | |
1138 | return false; | |
1139 | if (*e == 0) /* End of string? Yay! */ | |
1140 | return true; | |
2ef2376d | 1141 | } |
656552eb LP |
1142 | } |
1143 | ||
1144 | bool path_is_normalized(const char *p) { | |
0b869625 | 1145 | if (!path_is_safe(p)) |
bb15fafe LP |
1146 | return false; |
1147 | ||
0b869625 | 1148 | if (streq(p, ".") || startswith(p, "./") || endswith(p, "/.") || strstr(p, "/./")) |
bb15fafe LP |
1149 | return false; |
1150 | ||
1151 | if (strstr(p, "//")) | |
1152 | return false; | |
1153 | ||
1154 | return true; | |
1155 | } | |
a0956174 LP |
1156 | |
1157 | char *file_in_same_dir(const char *path, const char *filename) { | |
1158 | char *e, *ret; | |
1159 | size_t k; | |
1160 | ||
1161 | assert(path); | |
1162 | assert(filename); | |
1163 | ||
1164 | /* This removes the last component of path and appends | |
1165 | * filename, unless the latter is absolute anyway or the | |
1166 | * former isn't */ | |
1167 | ||
1168 | if (path_is_absolute(filename)) | |
1169 | return strdup(filename); | |
1170 | ||
1171 | e = strrchr(path, '/'); | |
1172 | if (!e) | |
1173 | return strdup(filename); | |
1174 | ||
1175 | k = strlen(filename); | |
1176 | ret = new(char, (e + 1 - path) + k + 1); | |
1177 | if (!ret) | |
1178 | return NULL; | |
1179 | ||
1180 | memcpy(mempcpy(ret, path, e + 1 - path), filename, k + 1); | |
1181 | return ret; | |
1182 | } | |
1183 | ||
55cdd057 | 1184 | bool hidden_or_backup_file(const char *filename) { |
a0956174 LP |
1185 | assert(filename); |
1186 | ||
55cdd057 | 1187 | if (filename[0] == '.' || |
fdf02a4f LP |
1188 | STR_IN_SET(filename, |
1189 | "lost+found", | |
1190 | "aquota.user", | |
1191 | "aquota.group") || | |
55cdd057 | 1192 | endswith(filename, "~")) |
a0956174 LP |
1193 | return true; |
1194 | ||
3c7af1af ZJS |
1195 | const char *dot = strrchr(filename, '.'); |
1196 | if (!dot) | |
55cdd057 ZJS |
1197 | return false; |
1198 | ||
3c7af1af ZJS |
1199 | /* Please, let's not add more entries to the list below. If external projects think it's a good idea |
1200 | * to come up with always new suffixes and that everybody else should just adjust to that, then it | |
1201 | * really should be on them. Hence, in future, let's not add any more entries. Instead, let's ask | |
1202 | * those packages to instead adopt one of the generic suffixes/prefixes for hidden files or backups, | |
1203 | * possibly augmented with an additional string. Specifically: there's now: | |
941060bf LP |
1204 | * |
1205 | * The generic suffixes "~" and ".bak" for backup files | |
1206 | * The generic prefix "." for hidden files | |
1207 | * | |
3c7af1af ZJS |
1208 | * Thus, if a new package manager "foopkg" wants its own set of ".foopkg-new", ".foopkg-old", |
1209 | * ".foopkg-dist" or so registered, let's refuse that and ask them to use ".foopkg.new", | |
1210 | * ".foopkg.old" or ".foopkg~" instead. | |
941060bf LP |
1211 | */ |
1212 | ||
3c7af1af | 1213 | return STR_IN_SET(dot + 1, |
55cdd057 ZJS |
1214 | "rpmnew", |
1215 | "rpmsave", | |
1216 | "rpmorig", | |
1217 | "dpkg-old", | |
1218 | "dpkg-new", | |
1219 | "dpkg-tmp", | |
1220 | "dpkg-dist", | |
1221 | "dpkg-bak", | |
1222 | "dpkg-backup", | |
1223 | "dpkg-remove", | |
1224 | "ucf-new", | |
1225 | "ucf-old", | |
1226 | "ucf-dist", | |
941060bf | 1227 | "swp", |
94a0ef6e ZJS |
1228 | "bak", |
1229 | "old", | |
1230 | "new"); | |
a0956174 LP |
1231 | } |
1232 | ||
1233 | bool is_device_path(const char *path) { | |
1234 | ||
3c7af1af ZJS |
1235 | /* Returns true for paths that likely refer to a device, either by path in sysfs or to something in |
1236 | * /dev. */ | |
a0956174 | 1237 | |
bae47ba7 | 1238 | return PATH_STARTSWITH_SET(path, "/dev/", "/sys/"); |
3ccb8862 ZJS |
1239 | } |
1240 | ||
57e84e75 LP |
1241 | bool valid_device_node_path(const char *path) { |
1242 | ||
3c7af1af ZJS |
1243 | /* Some superficial checks whether the specified path is a valid device node path, all without |
1244 | * looking at the actual device node. */ | |
57e84e75 LP |
1245 | |
1246 | if (!PATH_STARTSWITH_SET(path, "/dev/", "/run/systemd/inaccessible/")) | |
1247 | return false; | |
1248 | ||
1249 | if (endswith(path, "/")) /* can't be a device node if it ends in a slash */ | |
1250 | return false; | |
1251 | ||
1252 | return path_is_normalized(path); | |
1253 | } | |
1254 | ||
1255 | bool valid_device_allow_pattern(const char *path) { | |
1256 | assert(path); | |
1257 | ||
3c7af1af ZJS |
1258 | /* Like valid_device_node_path(), but also allows full-subsystem expressions like those accepted by |
1259 | * DeviceAllow= and DeviceDeny=. */ | |
57e84e75 | 1260 | |
49fe5c09 | 1261 | if (STARTSWITH_SET(path, "block-", "char-")) |
57e84e75 LP |
1262 | return true; |
1263 | ||
1264 | return valid_device_node_path(path); | |
a0956174 | 1265 | } |
5a46d55f | 1266 | |
49bfc877 LP |
1267 | bool dot_or_dot_dot(const char *path) { |
1268 | if (!path) | |
1269 | return false; | |
1270 | if (path[0] != '.') | |
1271 | return false; | |
1272 | if (path[1] == 0) | |
1273 | return true; | |
1274 | if (path[1] != '.') | |
1275 | return false; | |
1276 | ||
1277 | return path[2] == 0; | |
1278 | } | |
57ea45e1 | 1279 | |
15bac3e8 | 1280 | bool empty_or_root(const char *path) { |
57ea45e1 | 1281 | |
3c7af1af ZJS |
1282 | /* For operations relative to some root directory, returns true if the specified root directory is |
1283 | * redundant, i.e. either / or NULL or the empty string or any equivalent. */ | |
57ea45e1 | 1284 | |
15bac3e8 | 1285 | if (isempty(path)) |
57ea45e1 LP |
1286 | return true; |
1287 | ||
15bac3e8 | 1288 | return path_equal(path, "/"); |
57ea45e1 | 1289 | } |
3841fee8 LP |
1290 | |
1291 | bool path_strv_contains(char **l, const char *path) { | |
3841fee8 LP |
1292 | STRV_FOREACH(i, l) |
1293 | if (path_equal(*i, path)) | |
1294 | return true; | |
1295 | ||
1296 | return false; | |
1297 | } | |
de46b2be TM |
1298 | |
1299 | bool prefixed_path_strv_contains(char **l, const char *path) { | |
de46b2be | 1300 | STRV_FOREACH(i, l) { |
de010b0b YW |
1301 | const char *j = *i; |
1302 | ||
de46b2be TM |
1303 | if (*j == '-') |
1304 | j++; | |
1305 | if (*j == '+') | |
1306 | j++; | |
1307 | if (path_equal(j, path)) | |
1308 | return true; | |
1309 | } | |
1310 | ||
1311 | return false; | |
1312 | } |