]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/path-util.c
path-util: make use of TAKE_PTR() where we can
[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(TAKE_PTR(j), false);
645
646 return 0;
647 }
648
649 /* PATH entries which we don't have access to are ignored, as per tradition. */
650 if (errno != EACCES)
651 last_error = -errno;
652 }
653
654 return last_error;
655 }
656
657 bool paths_check_timestamp(const char* const* paths, usec_t *timestamp, bool update) {
658 bool changed = false;
659 const char* const* i;
660
661 assert(timestamp);
662
663 if (!paths)
664 return false;
665
666 STRV_FOREACH(i, paths) {
667 struct stat stats;
668 usec_t u;
669
670 if (stat(*i, &stats) < 0)
671 continue;
672
673 u = timespec_load(&stats.st_mtim);
674
675 /* first check */
676 if (*timestamp >= u)
677 continue;
678
679 log_debug("timestamp of '%s' changed", *i);
680
681 /* update timestamp */
682 if (update) {
683 *timestamp = u;
684 changed = true;
685 } else
686 return true;
687 }
688
689 return changed;
690 }
691
692 static int binary_is_good(const char *binary) {
693 _cleanup_free_ char *p = NULL, *d = NULL;
694 int r;
695
696 r = find_binary(binary, &p);
697 if (r == -ENOENT)
698 return 0;
699 if (r < 0)
700 return r;
701
702 /* An fsck that is linked to /bin/true is a non-existent
703 * fsck */
704
705 r = readlink_malloc(p, &d);
706 if (r == -EINVAL) /* not a symlink */
707 return 1;
708 if (r < 0)
709 return r;
710
711 return !PATH_IN_SET(d, "true"
712 "/bin/true",
713 "/usr/bin/true",
714 "/dev/null");
715 }
716
717 int fsck_exists(const char *fstype) {
718 const char *checker;
719
720 assert(fstype);
721
722 if (streq(fstype, "auto"))
723 return -EINVAL;
724
725 checker = strjoina("fsck.", fstype);
726 return binary_is_good(checker);
727 }
728
729 int mkfs_exists(const char *fstype) {
730 const char *mkfs;
731
732 assert(fstype);
733
734 if (streq(fstype, "auto"))
735 return -EINVAL;
736
737 mkfs = strjoina("mkfs.", fstype);
738 return binary_is_good(mkfs);
739 }
740
741 int parse_path_argument_and_warn(const char *path, bool suppress_root, char **arg) {
742 char *p;
743 int r;
744
745 /*
746 * This function is intended to be used in command line
747 * parsers, to handle paths that are passed in. It makes the
748 * path absolute, and reduces it to NULL if omitted or
749 * root (the latter optionally).
750 *
751 * NOTE THAT THIS WILL FREE THE PREVIOUS ARGUMENT POINTER ON
752 * SUCCESS! Hence, do not pass in uninitialized pointers.
753 */
754
755 if (isempty(path)) {
756 *arg = mfree(*arg);
757 return 0;
758 }
759
760 r = path_make_absolute_cwd(path, &p);
761 if (r < 0)
762 return log_error_errno(r, "Failed to parse path \"%s\" and make it absolute: %m", path);
763
764 path_simplify(p, false);
765 if (suppress_root && empty_or_root(p))
766 p = mfree(p);
767
768 free_and_replace(*arg, p);
769
770 return 0;
771 }
772
773 char* dirname_malloc(const char *path) {
774 char *d, *dir, *dir2;
775
776 assert(path);
777
778 d = strdup(path);
779 if (!d)
780 return NULL;
781
782 dir = dirname(d);
783 assert(dir);
784
785 if (dir == d)
786 return d;
787
788 dir2 = strdup(dir);
789 free(d);
790
791 return dir2;
792 }
793
794 const char *last_path_component(const char *path) {
795
796 /* Finds the last component of the path, preserving the optional trailing slash that signifies a directory.
797 *
798 * a/b/c → c
799 * a/b/c/ → c/
800 * x → x
801 * x/ → x/
802 * /y → y
803 * /y/ → y/
804 * / → /
805 * // → /
806 * /foo/a → a
807 * /foo/a/ → a/
808 *
809 * Also, the empty string is mapped to itself.
810 *
811 * This is different than basename(), which returns "" when a trailing slash is present.
812 */
813
814 unsigned l, k;
815
816 if (!path)
817 return NULL;
818
819 l = k = strlen(path);
820 if (l == 0) /* special case — an empty string */
821 return path;
822
823 while (k > 0 && path[k-1] == '/')
824 k--;
825
826 if (k == 0) /* the root directory */
827 return path + l - 1;
828
829 while (k > 0 && path[k-1] != '/')
830 k--;
831
832 return path + k;
833 }
834
835 int path_extract_filename(const char *p, char **ret) {
836 _cleanup_free_ char *a = NULL;
837 const char *c, *e = NULL, *q;
838
839 /* Extracts the filename part (i.e. right-most component) from a path, i.e. string that passes
840 * filename_is_valid(). A wrapper around last_path_component(), but eats up trailing slashes. */
841
842 if (!p)
843 return -EINVAL;
844
845 c = last_path_component(p);
846
847 for (q = c; *q != 0; q++)
848 if (*q != '/')
849 e = q + 1;
850
851 if (!e) /* no valid character? */
852 return -EINVAL;
853
854 a = strndup(c, e - c);
855 if (!a)
856 return -ENOMEM;
857
858 if (!filename_is_valid(a))
859 return -EINVAL;
860
861 *ret = TAKE_PTR(a);
862
863 return 0;
864 }
865
866 bool filename_is_valid(const char *p) {
867 const char *e;
868
869 if (isempty(p))
870 return false;
871
872 if (dot_or_dot_dot(p))
873 return false;
874
875 e = strchrnul(p, '/');
876 if (*e != 0)
877 return false;
878
879 if (e - p > FILENAME_MAX) /* FILENAME_MAX is counted *without* the trailing NUL byte */
880 return false;
881
882 return true;
883 }
884
885 bool path_is_valid(const char *p) {
886
887 if (isempty(p))
888 return false;
889
890 if (strlen(p) >= PATH_MAX) /* PATH_MAX is counted *with* the trailing NUL byte */
891 return false;
892
893 return true;
894 }
895
896 bool path_is_normalized(const char *p) {
897
898 if (!path_is_valid(p))
899 return false;
900
901 if (dot_or_dot_dot(p))
902 return false;
903
904 if (startswith(p, "../") || endswith(p, "/..") || strstr(p, "/../"))
905 return false;
906
907 if (startswith(p, "./") || endswith(p, "/.") || strstr(p, "/./"))
908 return false;
909
910 if (strstr(p, "//"))
911 return false;
912
913 return true;
914 }
915
916 char *file_in_same_dir(const char *path, const char *filename) {
917 char *e, *ret;
918 size_t k;
919
920 assert(path);
921 assert(filename);
922
923 /* This removes the last component of path and appends
924 * filename, unless the latter is absolute anyway or the
925 * former isn't */
926
927 if (path_is_absolute(filename))
928 return strdup(filename);
929
930 e = strrchr(path, '/');
931 if (!e)
932 return strdup(filename);
933
934 k = strlen(filename);
935 ret = new(char, (e + 1 - path) + k + 1);
936 if (!ret)
937 return NULL;
938
939 memcpy(mempcpy(ret, path, e + 1 - path), filename, k + 1);
940 return ret;
941 }
942
943 bool hidden_or_backup_file(const char *filename) {
944 const char *p;
945
946 assert(filename);
947
948 if (filename[0] == '.' ||
949 streq(filename, "lost+found") ||
950 streq(filename, "aquota.user") ||
951 streq(filename, "aquota.group") ||
952 endswith(filename, "~"))
953 return true;
954
955 p = strrchr(filename, '.');
956 if (!p)
957 return false;
958
959 /* Please, let's not add more entries to the list below. If external projects think it's a good idea to come up
960 * with always new suffixes and that everybody else should just adjust to that, then it really should be on
961 * them. Hence, in future, let's not add any more entries. Instead, let's ask those packages to instead adopt
962 * one of the generic suffixes/prefixes for hidden files or backups, possibly augmented with an additional
963 * string. Specifically: there's now:
964 *
965 * The generic suffixes "~" and ".bak" for backup files
966 * The generic prefix "." for hidden files
967 *
968 * Thus, if a new package manager "foopkg" wants its own set of ".foopkg-new", ".foopkg-old", ".foopkg-dist"
969 * or so registered, let's refuse that and ask them to use ".foopkg.new", ".foopkg.old" or ".foopkg~" instead.
970 */
971
972 return STR_IN_SET(p + 1,
973 "rpmnew",
974 "rpmsave",
975 "rpmorig",
976 "dpkg-old",
977 "dpkg-new",
978 "dpkg-tmp",
979 "dpkg-dist",
980 "dpkg-bak",
981 "dpkg-backup",
982 "dpkg-remove",
983 "ucf-new",
984 "ucf-old",
985 "ucf-dist",
986 "swp",
987 "bak",
988 "old",
989 "new");
990 }
991
992 bool is_device_path(const char *path) {
993
994 /* Returns true on paths that likely refer to a device, either by path in sysfs or to something in /dev */
995
996 return PATH_STARTSWITH_SET(path, "/dev/", "/sys/");
997 }
998
999 bool valid_device_node_path(const char *path) {
1000
1001 /* Some superficial checks whether the specified path is a valid device node path, all without looking at the
1002 * actual device node. */
1003
1004 if (!PATH_STARTSWITH_SET(path, "/dev/", "/run/systemd/inaccessible/"))
1005 return false;
1006
1007 if (endswith(path, "/")) /* can't be a device node if it ends in a slash */
1008 return false;
1009
1010 return path_is_normalized(path);
1011 }
1012
1013 bool valid_device_allow_pattern(const char *path) {
1014 assert(path);
1015
1016 /* Like valid_device_node_path(), but also allows full-subsystem expressions, like DeviceAllow= and DeviceDeny=
1017 * accept it */
1018
1019 if (STARTSWITH_SET(path, "block-", "char-"))
1020 return true;
1021
1022 return valid_device_node_path(path);
1023 }
1024
1025 int systemd_installation_has_version(const char *root, unsigned minimal_version) {
1026 const char *pattern;
1027 int r;
1028
1029 /* Try to guess if systemd installation is later than the specified version. This
1030 * is hacky and likely to yield false negatives, particularly if the installation
1031 * is non-standard. False positives should be relatively rare.
1032 */
1033
1034 NULSTR_FOREACH(pattern,
1035 /* /lib works for systems without usr-merge, and for systems with a sane
1036 * usr-merge, where /lib is a symlink to /usr/lib. /usr/lib is necessary
1037 * for Gentoo which does a merge without making /lib a symlink.
1038 */
1039 "lib/systemd/libsystemd-shared-*.so\0"
1040 "lib64/systemd/libsystemd-shared-*.so\0"
1041 "usr/lib/systemd/libsystemd-shared-*.so\0"
1042 "usr/lib64/systemd/libsystemd-shared-*.so\0") {
1043
1044 _cleanup_strv_free_ char **names = NULL;
1045 _cleanup_free_ char *path = NULL;
1046 char *c, **name;
1047
1048 path = path_join(root, pattern);
1049 if (!path)
1050 return -ENOMEM;
1051
1052 r = glob_extend(&names, path, 0);
1053 if (r == -ENOENT)
1054 continue;
1055 if (r < 0)
1056 return r;
1057
1058 assert_se(c = endswith(path, "*.so"));
1059 *c = '\0'; /* truncate the glob part */
1060
1061 STRV_FOREACH(name, names) {
1062 /* This is most likely to run only once, hence let's not optimize anything. */
1063 char *t, *t2;
1064 unsigned version;
1065
1066 t = startswith(*name, path);
1067 if (!t)
1068 continue;
1069
1070 t2 = endswith(t, ".so");
1071 if (!t2)
1072 continue;
1073
1074 t2[0] = '\0'; /* truncate the suffix */
1075
1076 r = safe_atou(t, &version);
1077 if (r < 0) {
1078 log_debug_errno(r, "Found libsystemd shared at \"%s.so\", but failed to parse version: %m", *name);
1079 continue;
1080 }
1081
1082 log_debug("Found libsystemd shared at \"%s.so\", version %u (%s).",
1083 *name, version,
1084 version >= minimal_version ? "OK" : "too old");
1085 if (version >= minimal_version)
1086 return true;
1087 }
1088 }
1089
1090 return false;
1091 }
1092
1093 bool dot_or_dot_dot(const char *path) {
1094 if (!path)
1095 return false;
1096 if (path[0] != '.')
1097 return false;
1098 if (path[1] == 0)
1099 return true;
1100 if (path[1] != '.')
1101 return false;
1102
1103 return path[2] == 0;
1104 }
1105
1106 bool empty_or_root(const char *root) {
1107
1108 /* For operations relative to some root directory, returns true if the specified root directory is redundant,
1109 * i.e. either / or NULL or the empty string or any equivalent. */
1110
1111 if (!root)
1112 return true;
1113
1114 return root[strspn(root, "/")] == 0;
1115 }
1116
1117 bool path_strv_contains(char **l, const char *path) {
1118 char **i;
1119
1120 STRV_FOREACH(i, l)
1121 if (path_equal(*i, path))
1122 return true;
1123
1124 return false;
1125 }
1126
1127 bool prefixed_path_strv_contains(char **l, const char *path) {
1128 char **i, *j;
1129
1130 STRV_FOREACH(i, l) {
1131 j = *i;
1132 if (*j == '-')
1133 j++;
1134 if (*j == '+')
1135 j++;
1136 if (path_equal(j, path))
1137 return true;
1138 }
1139
1140 return false;
1141 }