]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/path-util.c
986dfe94a4b719b16e13c50b94bf6d041887c42d
[thirdparty/systemd.git] / src / basic / path-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <limits.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8
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
15 #include "alloc-util.h"
16 #include "extract-word.h"
17 #include "fs-util.h"
18 #include "glob-util.h"
19 #include "log.h"
20 #include "macro.h"
21 #include "nulstr-util.h"
22 #include "parse-util.h"
23 #include "path-util.h"
24 #include "stat-util.h"
25 #include "string-util.h"
26 #include "strv.h"
27 #include "time-util.h"
28 #include "utf8.h"
29
30 bool path_is_absolute(const char *p) {
31 return p[0] == '/';
32 }
33
34 bool is_path(const char *p) {
35 return !!strchr(p, '/');
36 }
37
38 int path_split_and_make_absolute(const char *p, char ***ret) {
39 char **l;
40 int r;
41
42 assert(p);
43 assert(ret);
44
45 l = strv_split(p, ":");
46 if (!l)
47 return -ENOMEM;
48
49 r = path_strv_make_absolute_cwd(l);
50 if (r < 0) {
51 strv_free(l);
52 return r;
53 }
54
55 *ret = l;
56 return r;
57 }
58
59 char *path_make_absolute(const char *p, const char *prefix) {
60 assert(p);
61
62 /* Makes every item in the list an absolute path by prepending
63 * the prefix, if specified and necessary */
64
65 if (path_is_absolute(p) || isempty(prefix))
66 return strdup(p);
67
68 return path_join(prefix, p);
69 }
70
71 int safe_getcwd(char **ret) {
72 char *cwd;
73
74 cwd = get_current_dir_name();
75 if (!cwd)
76 return negative_errno();
77
78 /* Let's make sure the directory is really absolute, to protect us from the logic behind
79 * CVE-2018-1000001 */
80 if (cwd[0] != '/') {
81 free(cwd);
82 return -ENOMEDIUM;
83 }
84
85 *ret = cwd;
86 return 0;
87 }
88
89 int path_make_absolute_cwd(const char *p, char **ret) {
90 char *c;
91 int r;
92
93 assert(p);
94 assert(ret);
95
96 /* Similar to path_make_absolute(), but prefixes with the
97 * current working directory. */
98
99 if (path_is_absolute(p))
100 c = strdup(p);
101 else {
102 _cleanup_free_ char *cwd = NULL;
103
104 r = safe_getcwd(&cwd);
105 if (r < 0)
106 return r;
107
108 c = path_join(cwd, p);
109 }
110 if (!c)
111 return -ENOMEM;
112
113 *ret = c;
114 return 0;
115 }
116
117 int path_make_relative(const char *from_dir, const char *to_path, char **_r) {
118 char *f, *t, *r, *p;
119 unsigned n_parents = 0;
120
121 assert(from_dir);
122 assert(to_path);
123 assert(_r);
124
125 /* Strips the common part, and adds ".." elements as necessary. */
126
127 if (!path_is_absolute(from_dir) || !path_is_absolute(to_path))
128 return -EINVAL;
129
130 f = strdupa(from_dir);
131 t = strdupa(to_path);
132
133 path_simplify(f, true);
134 path_simplify(t, true);
135
136 /* Skip the common part. */
137 for (;;) {
138 size_t a, b;
139
140 f += *f == '/';
141 t += *t == '/';
142
143 if (!*f) {
144 if (!*t)
145 /* from_dir equals to_path. */
146 r = strdup(".");
147 else
148 /* from_dir is a parent directory of to_path. */
149 r = strdup(t);
150 if (!r)
151 return -ENOMEM;
152
153 *_r = r;
154 return 0;
155 }
156
157 if (!*t)
158 break;
159
160 a = strcspn(f, "/");
161 b = strcspn(t, "/");
162
163 if (a != b || memcmp(f, t, a) != 0)
164 break;
165
166 f += a;
167 t += b;
168 }
169
170 /* If we're here, then "from_dir" has one or more elements that need to
171 * be replaced with "..". */
172
173 /* Count the number of necessary ".." elements. */
174 for (; *f;) {
175 size_t w;
176
177 w = strcspn(f, "/");
178
179 /* If this includes ".." we can't do a simple series of "..", refuse */
180 if (w == 2 && f[0] == '.' && f[1] == '.')
181 return -EINVAL;
182
183 /* Count number of elements */
184 n_parents++;
185
186 f += w;
187 f += *f == '/';
188 }
189
190 r = new(char, n_parents * 3 + strlen(t) + 1);
191 if (!r)
192 return -ENOMEM;
193
194 for (p = r; n_parents > 0; n_parents--)
195 p = mempcpy(p, "../", 3);
196
197 if (*t)
198 strcpy(p, t);
199 else
200 /* Remove trailing slash */
201 *(--p) = 0;
202
203 *_r = r;
204 return 0;
205 }
206
207 char* path_startswith_strv(const char *p, char **set) {
208 char **s, *t;
209
210 STRV_FOREACH(s, set) {
211 t = path_startswith(p, *s);
212 if (t)
213 return t;
214 }
215
216 return NULL;
217 }
218
219 int path_strv_make_absolute_cwd(char **l) {
220 char **s;
221 int r;
222
223 /* Goes through every item in the string list and makes it
224 * absolute. This works in place and won't rollback any
225 * changes on failure. */
226
227 STRV_FOREACH(s, l) {
228 char *t;
229
230 r = path_make_absolute_cwd(*s, &t);
231 if (r < 0)
232 return r;
233
234 path_simplify(t, false);
235 free_and_replace(*s, t);
236 }
237
238 return 0;
239 }
240
241 char **path_strv_resolve(char **l, const char *root) {
242 char **s;
243 unsigned k = 0;
244 bool enomem = false;
245 int r;
246
247 if (strv_isempty(l))
248 return l;
249
250 /* Goes through every item in the string list and canonicalize
251 * the path. This works in place and won't rollback any
252 * changes on failure. */
253
254 STRV_FOREACH(s, l) {
255 _cleanup_free_ char *orig = NULL;
256 char *t, *u;
257
258 if (!path_is_absolute(*s)) {
259 free(*s);
260 continue;
261 }
262
263 if (root) {
264 orig = *s;
265 t = path_join(root, orig);
266 if (!t) {
267 enomem = true;
268 continue;
269 }
270 } else
271 t = *s;
272
273 r = chase_symlinks(t, root, 0, &u, NULL);
274 if (r == -ENOENT) {
275 if (root) {
276 u = TAKE_PTR(orig);
277 free(t);
278 } else
279 u = t;
280 } else if (r < 0) {
281 free(t);
282
283 if (r == -ENOMEM)
284 enomem = true;
285
286 continue;
287 } else if (root) {
288 char *x;
289
290 free(t);
291 x = path_startswith(u, root);
292 if (x) {
293 /* restore the slash if it was lost */
294 if (!startswith(x, "/"))
295 *(--x) = '/';
296
297 t = strdup(x);
298 free(u);
299 if (!t) {
300 enomem = true;
301 continue;
302 }
303 u = t;
304 } else {
305 /* canonicalized path goes outside of
306 * prefix, keep the original path instead */
307 free_and_replace(u, orig);
308 }
309 } else
310 free(t);
311
312 l[k++] = u;
313 }
314
315 l[k] = NULL;
316
317 if (enomem)
318 return NULL;
319
320 return l;
321 }
322
323 char **path_strv_resolve_uniq(char **l, const char *root) {
324
325 if (strv_isempty(l))
326 return l;
327
328 if (!path_strv_resolve(l, root))
329 return NULL;
330
331 return strv_uniq(l);
332 }
333
334 char *path_simplify(char *path, bool kill_dots) {
335 char *f, *t;
336 bool slash = false, ignore_slash = false, absolute;
337
338 assert(path);
339
340 /* Removes redundant inner and trailing slashes. Also removes unnecessary dots
341 * if kill_dots is true. Modifies the passed string in-place.
342 *
343 * ///foo//./bar/. becomes /foo/./bar/. (if kill_dots is false)
344 * ///foo//./bar/. becomes /foo/bar (if kill_dots is true)
345 * .//./foo//./bar/. becomes ././foo/./bar/. (if kill_dots is false)
346 * .//./foo//./bar/. becomes foo/bar (if kill_dots is true)
347 */
348
349 if (isempty(path))
350 return path;
351
352 absolute = path_is_absolute(path);
353
354 f = path;
355 if (kill_dots && *f == '.' && IN_SET(f[1], 0, '/')) {
356 ignore_slash = true;
357 f++;
358 }
359
360 for (t = path; *f; f++) {
361
362 if (*f == '/') {
363 slash = true;
364 continue;
365 }
366
367 if (slash) {
368 if (kill_dots && *f == '.' && IN_SET(f[1], 0, '/'))
369 continue;
370
371 slash = false;
372 if (ignore_slash)
373 ignore_slash = false;
374 else
375 *(t++) = '/';
376 }
377
378 *(t++) = *f;
379 }
380
381 /* Special rule, if we stripped everything, we either need a "/" (for the root directory)
382 * or "." for the current directory */
383 if (t == path) {
384 if (absolute)
385 *(t++) = '/';
386 else
387 *(t++) = '.';
388 }
389
390 *t = 0;
391 return path;
392 }
393
394 int path_simplify_and_warn(
395 char *path,
396 unsigned flag,
397 const char *unit,
398 const char *filename,
399 unsigned line,
400 const char *lvalue) {
401
402 bool fatal = flag & PATH_CHECK_FATAL;
403
404 assert(!FLAGS_SET(flag, PATH_CHECK_ABSOLUTE | PATH_CHECK_RELATIVE));
405
406 if (!utf8_is_valid(path))
407 return log_syntax_invalid_utf8(unit, LOG_ERR, filename, line, path);
408
409 if (flag & (PATH_CHECK_ABSOLUTE | PATH_CHECK_RELATIVE)) {
410 bool absolute;
411
412 absolute = path_is_absolute(path);
413
414 if (!absolute && (flag & PATH_CHECK_ABSOLUTE))
415 return log_syntax(unit, LOG_ERR, filename, line, SYNTHETIC_ERRNO(EINVAL),
416 "%s= path is not absolute%s: %s",
417 lvalue, fatal ? "" : ", ignoring", path);
418
419 if (absolute && (flag & PATH_CHECK_RELATIVE))
420 return log_syntax(unit, LOG_ERR, filename, line, SYNTHETIC_ERRNO(EINVAL),
421 "%s= path is absolute%s: %s",
422 lvalue, fatal ? "" : ", ignoring", path);
423 }
424
425 path_simplify(path, true);
426
427 if (!path_is_valid(path))
428 return log_syntax(unit, LOG_ERR, filename, line, SYNTHETIC_ERRNO(EINVAL),
429 "%s= path has invalid length (%zu bytes)%s.",
430 lvalue, strlen(path), fatal ? "" : ", ignoring");
431
432 if (!path_is_normalized(path))
433 return log_syntax(unit, LOG_ERR, filename, line, SYNTHETIC_ERRNO(EINVAL),
434 "%s= path is not normalized%s: %s",
435 lvalue, fatal ? "" : ", ignoring", path);
436
437 return 0;
438 }
439
440 char* path_startswith(const char *path, const char *prefix) {
441 assert(path);
442 assert(prefix);
443
444 /* Returns a pointer to the start of the first component after the parts matched by
445 * the prefix, iff
446 * - both paths are absolute or both paths are relative,
447 * and
448 * - each component in prefix in turn matches a component in path at the same position.
449 * An empty string will be returned when the prefix and path are equivalent.
450 *
451 * Returns NULL otherwise.
452 */
453
454 if ((path[0] == '/') != (prefix[0] == '/'))
455 return NULL;
456
457 for (;;) {
458 size_t a, b;
459
460 path += strspn(path, "/");
461 prefix += strspn(prefix, "/");
462
463 if (*prefix == 0)
464 return (char*) path;
465
466 if (*path == 0)
467 return NULL;
468
469 a = strcspn(path, "/");
470 b = strcspn(prefix, "/");
471
472 if (a != b)
473 return NULL;
474
475 if (memcmp(path, prefix, a) != 0)
476 return NULL;
477
478 path += a;
479 prefix += b;
480 }
481 }
482
483 int path_compare(const char *a, const char *b) {
484 int d;
485
486 assert(a);
487 assert(b);
488
489 /* A relative path and an absolute path must not compare as equal.
490 * Which one is sorted before the other does not really matter.
491 * Here a relative path is ordered before an absolute path. */
492 d = (a[0] == '/') - (b[0] == '/');
493 if (d != 0)
494 return d;
495
496 for (;;) {
497 size_t j, k;
498
499 a += strspn(a, "/");
500 b += strspn(b, "/");
501
502 if (*a == 0 && *b == 0)
503 return 0;
504
505 /* Order prefixes first: "/foo" before "/foo/bar" */
506 if (*a == 0)
507 return -1;
508 if (*b == 0)
509 return 1;
510
511 j = strcspn(a, "/");
512 k = strcspn(b, "/");
513
514 /* Alphabetical sort: "/foo/aaa" before "/foo/b" */
515 d = memcmp(a, b, MIN(j, k));
516 if (d != 0)
517 return (d > 0) - (d < 0); /* sign of d */
518
519 /* Sort "/foo/a" before "/foo/aaa" */
520 d = (j > k) - (j < k); /* sign of (j - k) */
521 if (d != 0)
522 return d;
523
524 a += j;
525 b += k;
526 }
527 }
528
529 bool path_equal(const char *a, const char *b) {
530 return path_compare(a, b) == 0;
531 }
532
533 bool path_equal_or_files_same(const char *a, const char *b, int flags) {
534 return path_equal(a, b) || files_same(a, b, flags) > 0;
535 }
536
537 char* path_join_internal(const char *first, ...) {
538 char *joined, *q;
539 const char *p;
540 va_list ap;
541 bool slash;
542 size_t sz;
543
544 /* Joins all listed strings until the sentinel and places a "/" between them unless the strings end/begin
545 * already with one so that it is unnecessary. Note that slashes which are already duplicate won't be
546 * removed. The string returned is hence always equal to or longer than the sum of the lengths of each
547 * individual string.
548 *
549 * Note: any listed empty string is simply skipped. This can be useful for concatenating strings of which some
550 * are optional.
551 *
552 * Examples:
553 *
554 * path_join("foo", "bar") → "foo/bar"
555 * path_join("foo/", "bar") → "foo/bar"
556 * path_join("", "foo", "", "bar", "") → "foo/bar" */
557
558 sz = strlen_ptr(first);
559 va_start(ap, first);
560 while ((p = va_arg(ap, char*)) != (const char*) -1)
561 if (!isempty(p))
562 sz += 1 + strlen(p);
563 va_end(ap);
564
565 joined = new(char, sz + 1);
566 if (!joined)
567 return NULL;
568
569 if (!isempty(first)) {
570 q = stpcpy(joined, first);
571 slash = endswith(first, "/");
572 } else {
573 /* Skip empty items */
574 joined[0] = 0;
575 q = joined;
576 slash = true; /* no need to generate a slash anymore */
577 }
578
579 va_start(ap, first);
580 while ((p = va_arg(ap, char*)) != (const char*) -1) {
581 if (isempty(p))
582 continue;
583
584 if (!slash && p[0] != '/')
585 *(q++) = '/';
586
587 q = stpcpy(q, p);
588 slash = endswith(p, "/");
589 }
590 va_end(ap);
591
592 return joined;
593 }
594
595 int find_binary(const char *name, char **ret) {
596 int last_error, r;
597 const char *p;
598
599 assert(name);
600
601 if (is_path(name)) {
602 if (access(name, X_OK) < 0)
603 return -errno;
604
605 if (ret) {
606 r = path_make_absolute_cwd(name, ret);
607 if (r < 0)
608 return r;
609 }
610
611 return 0;
612 }
613
614 /**
615 * Plain getenv, not secure_getenv, because we want
616 * to actually allow the user to pick the binary.
617 */
618 p = getenv("PATH");
619 if (!p)
620 p = DEFAULT_PATH;
621
622 last_error = -ENOENT;
623
624 for (;;) {
625 _cleanup_free_ char *j = NULL, *element = NULL;
626
627 r = extract_first_word(&p, &element, ":", EXTRACT_RELAX|EXTRACT_DONT_COALESCE_SEPARATORS);
628 if (r < 0)
629 return r;
630 if (r == 0)
631 break;
632
633 if (!path_is_absolute(element))
634 continue;
635
636 j = path_join(element, name);
637 if (!j)
638 return -ENOMEM;
639
640 if (access(j, X_OK) >= 0) {
641 /* Found it! */
642
643 if (ret) {
644 *ret = path_simplify(j, false);
645 j = NULL;
646 }
647
648 return 0;
649 }
650
651 /* PATH entries which we don't have access to are ignored, as per tradition. */
652 if (errno != EACCES)
653 last_error = -errno;
654 }
655
656 return last_error;
657 }
658
659 bool paths_check_timestamp(const char* const* paths, usec_t *timestamp, bool update) {
660 bool changed = false;
661 const char* const* i;
662
663 assert(timestamp);
664
665 if (!paths)
666 return false;
667
668 STRV_FOREACH(i, paths) {
669 struct stat stats;
670 usec_t u;
671
672 if (stat(*i, &stats) < 0)
673 continue;
674
675 u = timespec_load(&stats.st_mtim);
676
677 /* first check */
678 if (*timestamp >= u)
679 continue;
680
681 log_debug("timestamp of '%s' changed", *i);
682
683 /* update timestamp */
684 if (update) {
685 *timestamp = u;
686 changed = true;
687 } else
688 return true;
689 }
690
691 return changed;
692 }
693
694 static int binary_is_good(const char *binary) {
695 _cleanup_free_ char *p = NULL, *d = NULL;
696 int r;
697
698 r = find_binary(binary, &p);
699 if (r == -ENOENT)
700 return 0;
701 if (r < 0)
702 return r;
703
704 /* An fsck that is linked to /bin/true is a non-existent
705 * fsck */
706
707 r = readlink_malloc(p, &d);
708 if (r == -EINVAL) /* not a symlink */
709 return 1;
710 if (r < 0)
711 return r;
712
713 return !PATH_IN_SET(d, "true"
714 "/bin/true",
715 "/usr/bin/true",
716 "/dev/null");
717 }
718
719 int fsck_exists(const char *fstype) {
720 const char *checker;
721
722 assert(fstype);
723
724 if (streq(fstype, "auto"))
725 return -EINVAL;
726
727 checker = strjoina("fsck.", fstype);
728 return binary_is_good(checker);
729 }
730
731 int mkfs_exists(const char *fstype) {
732 const char *mkfs;
733
734 assert(fstype);
735
736 if (streq(fstype, "auto"))
737 return -EINVAL;
738
739 mkfs = strjoina("mkfs.", fstype);
740 return binary_is_good(mkfs);
741 }
742
743 int parse_path_argument_and_warn(const char *path, bool suppress_root, char **arg) {
744 char *p;
745 int r;
746
747 /*
748 * This function is intended to be used in command line
749 * parsers, to handle paths that are passed in. It makes the
750 * path absolute, and reduces it to NULL if omitted or
751 * root (the latter optionally).
752 *
753 * NOTE THAT THIS WILL FREE THE PREVIOUS ARGUMENT POINTER ON
754 * SUCCESS! Hence, do not pass in uninitialized pointers.
755 */
756
757 if (isempty(path)) {
758 *arg = mfree(*arg);
759 return 0;
760 }
761
762 r = path_make_absolute_cwd(path, &p);
763 if (r < 0)
764 return log_error_errno(r, "Failed to parse path \"%s\" and make it absolute: %m", path);
765
766 path_simplify(p, false);
767 if (suppress_root && empty_or_root(p))
768 p = mfree(p);
769
770 free_and_replace(*arg, p);
771
772 return 0;
773 }
774
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 }
795
796 const char *last_path_component(const char *path) {
797
798 /* Finds the last component of the path, preserving the optional trailing slash that signifies a directory.
799 *
800 * a/b/c → c
801 * a/b/c/ → c/
802 * x → x
803 * x/ → x/
804 * /y → y
805 * /y/ → y/
806 * / → /
807 * // → /
808 * /foo/a → a
809 * /foo/a/ → a/
810 *
811 * Also, the empty string is mapped to itself.
812 *
813 * This is different than basename(), which returns "" when a trailing slash is present.
814 */
815
816 unsigned l, k;
817
818 if (!path)
819 return NULL;
820
821 l = k = strlen(path);
822 if (l == 0) /* special case — an empty string */
823 return path;
824
825 while (k > 0 && path[k-1] == '/')
826 k--;
827
828 if (k == 0) /* the root directory */
829 return path + l - 1;
830
831 while (k > 0 && path[k-1] != '/')
832 k--;
833
834 return path + k;
835 }
836
837 int path_extract_filename(const char *p, char **ret) {
838 _cleanup_free_ char *a = NULL;
839 const char *c, *e = NULL, *q;
840
841 /* Extracts the filename part (i.e. right-most component) from a path, i.e. string that passes
842 * filename_is_valid(). A wrapper around last_path_component(), but eats up trailing slashes. */
843
844 if (!p)
845 return -EINVAL;
846
847 c = last_path_component(p);
848
849 for (q = c; *q != 0; q++)
850 if (*q != '/')
851 e = q + 1;
852
853 if (!e) /* no valid character? */
854 return -EINVAL;
855
856 a = strndup(c, e - c);
857 if (!a)
858 return -ENOMEM;
859
860 if (!filename_is_valid(a))
861 return -EINVAL;
862
863 *ret = TAKE_PTR(a);
864
865 return 0;
866 }
867
868 bool filename_is_valid(const char *p) {
869 const char *e;
870
871 if (isempty(p))
872 return false;
873
874 if (dot_or_dot_dot(p))
875 return false;
876
877 e = strchrnul(p, '/');
878 if (*e != 0)
879 return false;
880
881 if (e - p > FILENAME_MAX) /* FILENAME_MAX is counted *without* the trailing NUL byte */
882 return false;
883
884 return true;
885 }
886
887 bool path_is_valid(const char *p) {
888
889 if (isempty(p))
890 return false;
891
892 if (strlen(p) >= PATH_MAX) /* PATH_MAX is counted *with* the trailing NUL byte */
893 return false;
894
895 return true;
896 }
897
898 bool path_is_normalized(const char *p) {
899
900 if (!path_is_valid(p))
901 return false;
902
903 if (dot_or_dot_dot(p))
904 return false;
905
906 if (startswith(p, "../") || endswith(p, "/..") || strstr(p, "/../"))
907 return false;
908
909 if (startswith(p, "./") || endswith(p, "/.") || strstr(p, "/./"))
910 return false;
911
912 if (strstr(p, "//"))
913 return false;
914
915 return true;
916 }
917
918 char *file_in_same_dir(const char *path, const char *filename) {
919 char *e, *ret;
920 size_t k;
921
922 assert(path);
923 assert(filename);
924
925 /* This removes the last component of path and appends
926 * filename, unless the latter is absolute anyway or the
927 * former isn't */
928
929 if (path_is_absolute(filename))
930 return strdup(filename);
931
932 e = strrchr(path, '/');
933 if (!e)
934 return strdup(filename);
935
936 k = strlen(filename);
937 ret = new(char, (e + 1 - path) + k + 1);
938 if (!ret)
939 return NULL;
940
941 memcpy(mempcpy(ret, path, e + 1 - path), filename, k + 1);
942 return ret;
943 }
944
945 bool hidden_or_backup_file(const char *filename) {
946 const char *p;
947
948 assert(filename);
949
950 if (filename[0] == '.' ||
951 streq(filename, "lost+found") ||
952 streq(filename, "aquota.user") ||
953 streq(filename, "aquota.group") ||
954 endswith(filename, "~"))
955 return true;
956
957 p = strrchr(filename, '.');
958 if (!p)
959 return false;
960
961 /* Please, let's not add more entries to the list below. If external projects think it's a good idea to come up
962 * with always new suffixes and that everybody else should just adjust to that, then it really should be on
963 * them. Hence, in future, let's not add any more entries. Instead, let's ask those packages to instead adopt
964 * one of the generic suffixes/prefixes for hidden files or backups, possibly augmented with an additional
965 * string. Specifically: there's now:
966 *
967 * The generic suffixes "~" and ".bak" for backup files
968 * The generic prefix "." for hidden files
969 *
970 * Thus, if a new package manager "foopkg" wants its own set of ".foopkg-new", ".foopkg-old", ".foopkg-dist"
971 * or so registered, let's refuse that and ask them to use ".foopkg.new", ".foopkg.old" or ".foopkg~" instead.
972 */
973
974 return STR_IN_SET(p + 1,
975 "rpmnew",
976 "rpmsave",
977 "rpmorig",
978 "dpkg-old",
979 "dpkg-new",
980 "dpkg-tmp",
981 "dpkg-dist",
982 "dpkg-bak",
983 "dpkg-backup",
984 "dpkg-remove",
985 "ucf-new",
986 "ucf-old",
987 "ucf-dist",
988 "swp",
989 "bak",
990 "old",
991 "new");
992 }
993
994 bool is_device_path(const char *path) {
995
996 /* Returns true on paths that likely refer to a device, either by path in sysfs or to something in /dev */
997
998 return PATH_STARTSWITH_SET(path, "/dev/", "/sys/");
999 }
1000
1001 bool valid_device_node_path(const char *path) {
1002
1003 /* Some superficial checks whether the specified path is a valid device node path, all without looking at the
1004 * actual device node. */
1005
1006 if (!PATH_STARTSWITH_SET(path, "/dev/", "/run/systemd/inaccessible/"))
1007 return false;
1008
1009 if (endswith(path, "/")) /* can't be a device node if it ends in a slash */
1010 return false;
1011
1012 return path_is_normalized(path);
1013 }
1014
1015 bool valid_device_allow_pattern(const char *path) {
1016 assert(path);
1017
1018 /* Like valid_device_node_path(), but also allows full-subsystem expressions, like DeviceAllow= and DeviceDeny=
1019 * accept it */
1020
1021 if (STARTSWITH_SET(path, "block-", "char-"))
1022 return true;
1023
1024 return valid_device_node_path(path);
1025 }
1026
1027 int systemd_installation_has_version(const char *root, unsigned minimal_version) {
1028 const char *pattern;
1029 int r;
1030
1031 /* Try to guess if systemd installation is later than the specified version. This
1032 * is hacky and likely to yield false negatives, particularly if the installation
1033 * is non-standard. False positives should be relatively rare.
1034 */
1035
1036 NULSTR_FOREACH(pattern,
1037 /* /lib works for systems without usr-merge, and for systems with a sane
1038 * usr-merge, where /lib is a symlink to /usr/lib. /usr/lib is necessary
1039 * for Gentoo which does a merge without making /lib a symlink.
1040 */
1041 "lib/systemd/libsystemd-shared-*.so\0"
1042 "lib64/systemd/libsystemd-shared-*.so\0"
1043 "usr/lib/systemd/libsystemd-shared-*.so\0"
1044 "usr/lib64/systemd/libsystemd-shared-*.so\0") {
1045
1046 _cleanup_strv_free_ char **names = NULL;
1047 _cleanup_free_ char *path = NULL;
1048 char *c, **name;
1049
1050 path = path_join(root, pattern);
1051 if (!path)
1052 return -ENOMEM;
1053
1054 r = glob_extend(&names, path, 0);
1055 if (r == -ENOENT)
1056 continue;
1057 if (r < 0)
1058 return r;
1059
1060 assert_se(c = endswith(path, "*.so"));
1061 *c = '\0'; /* truncate the glob part */
1062
1063 STRV_FOREACH(name, names) {
1064 /* This is most likely to run only once, hence let's not optimize anything. */
1065 char *t, *t2;
1066 unsigned version;
1067
1068 t = startswith(*name, path);
1069 if (!t)
1070 continue;
1071
1072 t2 = endswith(t, ".so");
1073 if (!t2)
1074 continue;
1075
1076 t2[0] = '\0'; /* truncate the suffix */
1077
1078 r = safe_atou(t, &version);
1079 if (r < 0) {
1080 log_debug_errno(r, "Found libsystemd shared at \"%s.so\", but failed to parse version: %m", *name);
1081 continue;
1082 }
1083
1084 log_debug("Found libsystemd shared at \"%s.so\", version %u (%s).",
1085 *name, version,
1086 version >= minimal_version ? "OK" : "too old");
1087 if (version >= minimal_version)
1088 return true;
1089 }
1090 }
1091
1092 return false;
1093 }
1094
1095 bool dot_or_dot_dot(const char *path) {
1096 if (!path)
1097 return false;
1098 if (path[0] != '.')
1099 return false;
1100 if (path[1] == 0)
1101 return true;
1102 if (path[1] != '.')
1103 return false;
1104
1105 return path[2] == 0;
1106 }
1107
1108 bool empty_or_root(const char *root) {
1109
1110 /* For operations relative to some root directory, returns true if the specified root directory is redundant,
1111 * i.e. either / or NULL or the empty string or any equivalent. */
1112
1113 if (!root)
1114 return true;
1115
1116 return root[strspn(root, "/")] == 0;
1117 }
1118
1119 bool path_strv_contains(char **l, const char *path) {
1120 char **i;
1121
1122 STRV_FOREACH(i, l)
1123 if (path_equal(*i, path))
1124 return true;
1125
1126 return false;
1127 }
1128
1129 bool prefixed_path_strv_contains(char **l, const char *path) {
1130 char **i, *j;
1131
1132 STRV_FOREACH(i, l) {
1133 j = *i;
1134 if (*j == '-')
1135 j++;
1136 if (*j == '+')
1137 j++;
1138 if (path_equal(j, path))
1139 return true;
1140 }
1141
1142 return false;
1143 }