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