1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
10 #include "alloc-util.h"
12 #include "extract-word.h"
15 #include "glob-util.h"
18 #include "path-util.h"
19 #include "stat-util.h"
20 #include "string-util.h"
22 #include "time-util.h"
24 int path_split_and_make_absolute(const char *p
, char ***ret
) {
25 _cleanup_strv_free_
char **l
= NULL
;
31 l
= strv_split(p
, ":");
35 r
= path_strv_make_absolute_cwd(l
);
43 char* path_make_absolute(const char *p
, const char *prefix
) {
46 /* Makes every item in the list an absolute path by prepending
47 * the prefix, if specified and necessary */
49 if (path_is_absolute(p
) || isempty(prefix
))
52 return path_join(prefix
, p
);
55 int safe_getcwd(char **ret
) {
56 _cleanup_free_
char *cwd
= NULL
;
58 cwd
= get_current_dir_name();
60 return negative_errno();
62 /* Let's make sure the directory is really absolute, to protect us from the logic behind
73 int path_make_absolute_cwd(const char *p
, char **ret
) {
80 /* Similar to path_make_absolute(), but prefixes with the
81 * current working directory. */
83 if (path_is_absolute(p
))
86 _cleanup_free_
char *cwd
= NULL
;
88 r
= safe_getcwd(&cwd
);
92 c
= path_join(cwd
, p
);
101 int path_make_relative(const char *from
, const char *to
, char **ret
) {
102 _cleanup_free_
char *result
= NULL
;
112 /* Strips the common part, and adds ".." elements as necessary. */
114 if (!path_is_absolute(from
) || !path_is_absolute(to
))
118 r
= path_find_first_component(&from
, true, &f
);
122 k
= path_find_first_component(&to
, true, &t
);
129 /* from and to are equivalent. */
130 result
= strdup(".");
134 /* 'to' is inside of 'from'. */
135 r
= path_simplify_alloc(t
, &result
);
139 if (!path_is_valid(result
))
143 *ret
= TAKE_PTR(result
);
147 if (r
!= k
|| !strneq(f
, t
, r
))
151 /* If we're here, then "from_dir" has one or more elements that need to
152 * be replaced with "..". */
154 for (n_parents
= 1;; n_parents
++) {
155 /* If this includes ".." we can't do a simple series of "..". */
156 r
= path_find_first_component(&from
, false, &f
);
163 if (isempty(t
) && n_parents
* 3 > PATH_MAX
)
164 /* PATH_MAX is counted *with* the trailing NUL byte */
167 result
= new(char, n_parents
* 3 + !isempty(t
) + strlen_ptr(t
));
171 for (p
= result
; n_parents
> 0; n_parents
--)
172 p
= mempcpy(p
, "../", 3);
175 /* Remove trailing slash and terminate string. */
177 *ret
= TAKE_PTR(result
);
183 path_simplify(result
);
185 if (!path_is_valid(result
))
188 *ret
= TAKE_PTR(result
);
192 int path_make_relative_parent(const char *from_child
, const char *to
, char **ret
) {
193 _cleanup_free_
char *from
= NULL
;
200 /* Similar to path_make_relative(), but provides the relative path from the parent directory of
201 * 'from_child'. This may be useful when creating relative symlink.
204 * - from = "/path/to/aaa", to = "/path/to/bbb"
205 * path_make_relative(from, to) = "../bbb"
206 * path_make_relative_parent(from, to) = "bbb"
208 * - from = "/path/to/aaa/bbb", to = "/path/to/ccc/ddd"
209 * path_make_relative(from, to) = "../../ccc/ddd"
210 * path_make_relative_parent(from, to) = "../ccc/ddd"
213 r
= path_extract_directory(from_child
, &from
);
217 return path_make_relative(from
, to
, ret
);
220 char* path_startswith_strv(const char *p
, char **set
) {
221 STRV_FOREACH(s
, set
) {
224 t
= path_startswith(p
, *s
);
232 int path_strv_make_absolute_cwd(char **l
) {
235 /* Goes through every item in the string list and makes it
236 * absolute. This works in place and won't rollback any
237 * changes on failure. */
242 r
= path_make_absolute_cwd(*s
, &t
);
247 free_and_replace(*s
, t
);
253 char** path_strv_resolve(char **l
, const char *root
) {
261 /* Goes through every item in the string list and canonicalize
262 * the path. This works in place and won't rollback any
263 * changes on failure. */
266 _cleanup_free_
char *orig
= NULL
;
269 if (!path_is_absolute(*s
)) {
276 t
= path_join(root
, orig
);
284 r
= chase(t
, root
, 0, &u
, NULL
);
302 x
= path_startswith(u
, root
);
304 /* restore the slash if it was lost */
305 if (!startswith(x
, "/"))
316 /* canonicalized path goes outside of
317 * prefix, keep the original path instead */
318 free_and_replace(u
, orig
);
334 char** path_strv_resolve_uniq(char **l
, const char *root
) {
339 if (!path_strv_resolve(l
, root
))
345 char* path_simplify_full(char *path
, PathSimplifyFlags flags
) {
346 bool add_slash
= false, keep_trailing_slash
, absolute
, beginning
= true;
350 /* Removes redundant inner and trailing slashes. Also removes unnecessary dots.
351 * Modifies the passed string in-place.
353 * ///foo//./bar/. becomes /foo/bar
354 * .//./foo//./bar/. becomes foo/bar
355 * /../foo/bar becomes /foo/bar
356 * /../foo/bar/.. becomes /foo/bar/..
362 keep_trailing_slash
= FLAGS_SET(flags
, PATH_SIMPLIFY_KEEP_TRAILING_SLASH
) && endswith(path
, "/");
364 absolute
= path_is_absolute(path
);
365 f
+= absolute
; /* Keep leading /, if present. */
367 for (const char *p
= f
;;) {
370 r
= path_find_first_component(&p
, true, &e
);
374 if (r
> 0 && absolute
&& beginning
&& path_startswith(e
, ".."))
375 /* If we're at the beginning of an absolute path, we can safely skip ".." */
384 /* if path is invalid, then refuse to simplify the remaining part. */
385 memmove(f
, p
, strlen(p
) + 1);
395 /* Special rule, if we stripped everything, we need a "." for the current directory. */
399 if (*(f
-1) != '/' && keep_trailing_slash
)
406 char* path_startswith_full(const char *path
, const char *prefix
, bool accept_dot_dot
) {
410 /* Returns a pointer to the start of the first component after the parts matched by
412 * - both paths are absolute or both paths are relative,
414 * - each component in prefix in turn matches a component in path at the same position.
415 * An empty string will be returned when the prefix and path are equivalent.
417 * Returns NULL otherwise.
420 if ((path
[0] == '/') != (prefix
[0] == '/'))
427 r
= path_find_first_component(&path
, accept_dot_dot
, &p
);
431 k
= path_find_first_component(&prefix
, accept_dot_dot
, &q
);
436 return (char*) (p
?: path
);
441 if (!strneq(p
, q
, r
))
446 int path_compare(const char *a
, const char *b
) {
449 /* Order NULL before non-NULL */
454 /* A relative path and an absolute path must not compare as equal.
455 * Which one is sorted before the other does not really matter.
456 * Here a relative path is ordered before an absolute path. */
457 r
= CMP(path_is_absolute(a
), path_is_absolute(b
));
465 j
= path_find_first_component(&a
, true, &aa
);
466 k
= path_find_first_component(&b
, true, &bb
);
468 if (j
< 0 || k
< 0) {
469 /* When one of paths is invalid, order invalid path after valid one. */
470 r
= CMP(j
< 0, k
< 0);
474 /* fallback to use strcmp() if both paths are invalid. */
478 /* Order prefixes first: "/foo" before "/foo/bar" */
487 /* Alphabetical sort: "/foo/aaa" before "/foo/b" */
488 r
= memcmp(aa
, bb
, MIN(j
, k
));
492 /* Sort "/foo/a" before "/foo/aaa" */
499 int path_compare_filename(const char *a
, const char *b
) {
500 _cleanup_free_
char *fa
= NULL
, *fb
= NULL
;
503 /* Order NULL before non-NULL */
508 j
= path_extract_filename(a
, &fa
);
509 k
= path_extract_filename(b
, &fb
);
511 /* When one of paths is "." or root, then order it earlier. */
512 r
= CMP(j
!= -EADDRNOTAVAIL
, k
!= -EADDRNOTAVAIL
);
516 /* When one of paths is invalid (or we get OOM), order invalid path after valid one. */
517 r
= CMP(j
< 0, k
< 0);
521 /* fallback to use strcmp() if both paths are invalid. */
525 return strcmp(fa
, fb
);
528 char* path_extend_internal(char **x
, ...) {
535 /* Joins all listed strings until the sentinel and places a "/" between them unless the strings
536 * end/begin already with one so that it is unnecessary. Note that slashes which are already
537 * duplicate won't be removed. The string returned is hence always equal to or longer than the sum of
538 * the lengths of the individual strings.
540 * The first argument may be an already allocated string that is extended via realloc() if
541 * non-NULL. path_extend() and path_join() are macro wrappers around this function, making use of the
542 * first parameter to distinguish the two operations.
544 * Note: any listed empty string is simply skipped. This can be useful for concatenating strings of
545 * which some are optional.
549 * path_join("foo", "bar") → "foo/bar"
550 * path_join("foo/", "bar") → "foo/bar"
551 * path_join("", "foo", "", "bar", "") → "foo/bar" */
553 sz
= old_sz
= x
? strlen_ptr(*x
) : 0;
555 while ((p
= va_arg(ap
, char*)) != POINTER_MAX
) {
562 if (sz
> SIZE_MAX
- add
) { /* overflow check */
571 nx
= realloc(x
? *x
: NULL
, GREEDY_ALLOC_ROUND_UP(sz
+1));
578 slash
= nx
[old_sz
-1] == '/';
581 slash
= true; /* no need to generate a slash anymore */
587 while ((p
= va_arg(ap
, char*)) != POINTER_MAX
) {
591 if (!slash
&& p
[0] != '/')
595 slash
= endswith(p
, "/");
602 static int check_x_access(const char *path
, int *ret_fd
) {
603 _cleanup_close_
int fd
= -EBADF
;
606 /* We need to use O_PATH because there may be executables for which we have only exec
607 * permissions, but not read (usually suid executables). */
608 fd
= open(path
, O_PATH
|O_CLOEXEC
);
612 r
= fd_verify_regular(fd
);
616 r
= access_fd(fd
, X_OK
);
618 /* /proc is not mounted. Fallback to access(). */
619 if (access(path
, X_OK
) < 0)
625 *ret_fd
= TAKE_FD(fd
);
630 static int find_executable_impl(const char *name
, const char *root
, char **ret_filename
, int *ret_fd
) {
631 _cleanup_close_
int fd
= -EBADF
;
632 _cleanup_free_
char *path_name
= NULL
;
637 /* Function chase() is invoked only when root is not NULL, as using it regardless of
638 * root value would alter the behavior of existing callers for example: /bin/sleep would become
639 * /usr/bin/sleep when find_executables is called. Hence, this function should be invoked when
640 * needed to avoid unforeseen regression or other complicated changes. */
642 /* prefix root to name in case full paths are not specified */
643 r
= chase(name
, root
, CHASE_PREFIX_ROOT
, &path_name
, /* ret_fd= */ NULL
);
650 r
= check_x_access(name
, ret_fd
? &fd
: NULL
);
655 r
= path_make_absolute_cwd(name
, ret_filename
);
661 *ret_fd
= TAKE_FD(fd
);
666 int find_executable_full(
669 char **exec_search_path
,
670 bool use_path_envvar
,
674 int last_error
= -ENOENT
, r
= 0;
675 const char *p
= NULL
;
680 return find_executable_impl(name
, root
, ret_filename
, ret_fd
);
683 /* Plain getenv, not secure_getenv, because we want to actually allow the user to pick the
689 if (exec_search_path
) {
690 STRV_FOREACH(element
, exec_search_path
) {
691 _cleanup_free_
char *full_path
= NULL
;
693 if (!path_is_absolute(*element
))
696 full_path
= path_join(*element
, name
);
700 r
= find_executable_impl(full_path
, root
, ret_filename
, ret_fd
);
711 /* Resolve a single-component name to a full path */
713 _cleanup_free_
char *element
= NULL
;
715 r
= extract_first_word(&p
, &element
, ":", EXTRACT_RELAX
|EXTRACT_DONT_COALESCE_SEPARATORS
);
721 if (!path_is_absolute(element
))
724 if (!path_extend(&element
, name
))
727 r
= find_executable_impl(element
, root
, ret_filename
, ret_fd
);
729 /* PATH entries which we don't have access to are ignored, as per tradition. */
742 bool paths_check_timestamp(const char* const* paths
, usec_t
*timestamp
, bool update
) {
743 bool changed
= false, originally_unset
;
750 originally_unset
= *timestamp
== 0;
752 STRV_FOREACH(i
, paths
) {
756 if (stat(*i
, &stats
) < 0)
759 u
= timespec_load(&stats
.st_mtim
);
765 log_debug(originally_unset
? "Loaded timestamp for '%s'." : "Timestamp of '%s' changed.", *i
);
767 /* update timestamp */
778 static int executable_is_good(const char *executable
) {
779 _cleanup_free_
char *p
= NULL
, *d
= NULL
;
782 r
= find_executable(executable
, &p
);
788 /* An fsck that is linked to /bin/true is a non-existent fsck */
790 r
= readlink_malloc(p
, &d
);
791 if (r
== -EINVAL
) /* not a symlink */
796 return !PATH_IN_SET(d
, "true"
802 int fsck_exists(void) {
803 return executable_is_good("fsck");
806 int fsck_exists_for_fstype(const char *fstype
) {
812 if (streq(fstype
, "auto"))
819 checker
= strjoina("fsck.", fstype
);
820 return executable_is_good(checker
);
823 static const char* skip_slash_or_dot(const char *p
) {
824 for (; !isempty(p
); p
++) {
827 if (startswith(p
, "./")) {
836 int path_find_first_component(const char **p
, bool accept_dot_dot
, const char **ret
) {
837 const char *q
, *first
, *end_first
, *next
;
842 /* When a path is input, then returns the pointer to the first component and its length, and
843 * move the input pointer to the next component or nul. This skips both over any '/'
844 * immediately *before* and *after* the first component before returning.
847 * Input: p: "//.//aaa///bbbbb/cc"
848 * Output: p: "bbbbb///cc"
849 * ret: "aaa///bbbbb/cc"
850 * return value: 3 (== strlen("aaa"))
853 * Output: p: (pointer to NUL)
855 * return value: 3 (== strlen("aaa"))
857 * Input: p: "/", ".", ""
858 * Output: p: (pointer to NUL)
867 * Input: p: "(too long component)"
868 * Output: return value: -EINVAL
870 * (when accept_dot_dot is false)
871 * Input: p: "//..//aaa///bbbbb/cc"
872 * Output: return value: -EINVAL
877 first
= skip_slash_or_dot(q
);
878 if (isempty(first
)) {
884 if (streq(first
, ".")) {
891 end_first
= strchrnul(first
, '/');
892 len
= end_first
- first
;
896 if (!accept_dot_dot
&& len
== 2 && first
[0] == '.' && first
[1] == '.')
899 next
= skip_slash_or_dot(end_first
);
901 *p
= next
+ streq(next
, ".");
907 static const char* skip_slash_or_dot_backward(const char *path
, const char *q
) {
909 assert(!q
|| q
>= path
);
911 for (; q
; q
= PTR_SUB1(q
, path
)) {
914 if (q
> path
&& strneq(q
- 1, "/.", 2))
916 if (q
== path
&& *q
== '.')
923 int path_find_last_component(const char *path
, bool accept_dot_dot
, const char **next
, const char **ret
) {
924 const char *q
, *last_end
, *last_begin
;
927 /* Similar to path_find_first_component(), but search components from the end.
930 * Input: path: "//.//aaa///bbbbb/cc//././"
932 * Output: next: "/cc//././"
934 * return value: 2 (== strlen("cc"))
936 * Input: path: "//.//aaa///bbbbb/cc//././"
938 * Output: next: "///bbbbb/cc//././"
939 * ret: "bbbbb/cc//././"
940 * return value: 5 (== strlen("bbbbb"))
942 * Input: path: "//.//aaa///bbbbb/cc//././"
943 * next: "///bbbbb/cc//././"
944 * Output: next: "//.//aaa///bbbbb/cc//././" (next == path)
945 * ret: "aaa///bbbbb/cc//././"
946 * return value: 3 (== strlen("aaa"))
948 * Input: path: "/", ".", "", or NULL
949 * Output: next: equivalent to path
953 * Input: path: "(too long component)"
954 * Output: return value: -EINVAL
956 * (when accept_dot_dot is false)
957 * Input: path: "//..//aaa///bbbbb/cc/..//"
958 * Output: return value: -EINVAL
970 if (*next
< path
|| *next
> path
+ strlen(path
))
977 if (!IN_SET(**next
, '\0', '/'))
981 q
= path
+ strlen(path
) - 1;
983 q
= skip_slash_or_dot_backward(path
, q
);
984 if (!q
|| /* the root directory */
985 (q
== path
&& *q
== '.')) { /* path is "." or "./" */
995 while (q
&& *q
!= '/')
996 q
= PTR_SUB1(q
, path
);
998 last_begin
= q
? q
+ 1 : path
;
999 len
= last_end
- last_begin
;
1003 if (!accept_dot_dot
&& len
== 2 && strneq(last_begin
, "..", 2))
1007 q
= skip_slash_or_dot_backward(path
, q
);
1008 *next
= q
? q
+ 1 : path
;
1016 const char* last_path_component(const char *path
) {
1018 /* Finds the last component of the path, preserving the optional trailing slash that signifies a directory.
1031 * Also, the empty string is mapped to itself.
1033 * This is different than basename(), which returns "" when a trailing slash is present.
1035 * This always succeeds (except if you pass NULL in which case it returns NULL, too).
1043 l
= k
= strlen(path
);
1044 if (l
== 0) /* special case — an empty string */
1047 while (k
> 0 && path
[k
-1] == '/')
1050 if (k
== 0) /* the root directory */
1051 return path
+ l
- 1;
1053 while (k
> 0 && path
[k
-1] != '/')
1059 int path_extract_filename(const char *path
, char **ret
) {
1060 _cleanup_free_
char *a
= NULL
;
1061 const char *c
, *next
= NULL
;
1064 /* Extracts the filename part (i.e. right-most component) from a path, i.e. string that passes
1065 * filename_is_valid(). A wrapper around last_path_component(), but eats up trailing
1068 * -EINVAL → if the path is not valid
1069 * -EADDRNOTAVAIL → if only a directory was specified, but no filename, i.e. the root dir
1070 * itself or "." is specified
1071 * -ENOMEM → no memory
1073 * Returns >= 0 on success. If the input path has a trailing slash, returns O_DIRECTORY, to
1074 * indicate the referenced file must be a directory.
1076 * This function guarantees to return a fully valid filename, i.e. one that passes
1077 * filename_is_valid() – this means "." and ".." are not accepted. */
1079 if (!path_is_valid(path
))
1082 r
= path_find_last_component(path
, false, &next
, &c
);
1085 if (r
== 0) /* root directory */
1086 return -EADDRNOTAVAIL
;
1093 return strlen(c
) > (size_t) r
? O_DIRECTORY
: 0;
1096 int path_extract_directory(const char *path
, char **ret
) {
1097 _cleanup_free_
char *a
= NULL
;
1098 const char *c
, *next
= NULL
;
1101 /* The inverse of path_extract_filename(), i.e. returns the directory path prefix. Returns:
1103 * -EINVAL → if the path is not valid
1104 * -EDESTADDRREQ → if no directory was specified in the passed in path, i.e. only a filename was passed
1105 * -EADDRNOTAVAIL → if the passed in parameter had no filename but did have a directory, i.e.
1106 * the root dir itself or "." was specified
1107 * -ENOMEM → no memory (surprise!)
1109 * This function guarantees to return a fully valid path, i.e. one that passes path_is_valid().
1112 r
= path_find_last_component(path
, false, &next
, &c
);
1115 if (r
== 0) /* empty or root */
1116 return isempty(path
) ? -EINVAL
: -EADDRNOTAVAIL
;
1118 if (*path
!= '/') /* filename only */
1119 return -EDESTADDRREQ
;
1128 a
= strndup(path
, next
- path
);
1134 if (!path_is_valid(a
))
1143 bool filename_part_is_valid(const char *p
) {
1146 /* Checks f the specified string is OK to be *part* of a filename. This is different from
1147 * filename_is_valid() as "." and ".." and "" are OK by this call, but not by filename_is_valid(). */
1152 e
= strchrnul(p
, '/');
1156 if (e
- p
> NAME_MAX
) /* NAME_MAX is counted *without* the trailing NUL byte */
1162 bool filename_is_valid(const char *p
) {
1167 if (dot_or_dot_dot(p
)) /* Yes, in this context we consider "." and ".." invalid */
1170 return filename_part_is_valid(p
);
1173 bool path_is_valid_full(const char *p
, bool accept_dot_dot
) {
1177 for (const char *e
= p
;;) {
1180 r
= path_find_first_component(&e
, accept_dot_dot
, NULL
);
1184 if (e
- p
>= PATH_MAX
) /* Already reached the maximum length for a path? (PATH_MAX is counted
1185 * *with* the trailing NUL byte) */
1187 if (*e
== 0) /* End of string? Yay! */
1192 bool path_is_normalized(const char *p
) {
1193 if (!path_is_safe(p
))
1196 if (streq(p
, ".") || startswith(p
, "./") || endswith(p
, "/.") || strstr(p
, "/./"))
1199 if (strstr(p
, "//"))
1205 int file_in_same_dir(const char *path
, const char *filename
, char **ret
) {
1206 _cleanup_free_
char *b
= NULL
;
1213 /* This removes the last component of path and appends filename, unless the latter is absolute anyway
1214 * or the former isn't */
1216 if (path_is_absolute(filename
))
1217 b
= strdup(filename
);
1219 _cleanup_free_
char *dn
= NULL
;
1221 r
= path_extract_directory(path
, &dn
);
1222 if (r
== -EDESTADDRREQ
) /* no path prefix */
1223 b
= strdup(filename
);
1227 b
= path_join(dn
, filename
);
1236 bool hidden_or_backup_file(const char *filename
) {
1239 if (filename
[0] == '.' ||
1240 STR_IN_SET(filename
,
1244 endswith(filename
, "~"))
1247 const char *dot
= strrchr(filename
, '.');
1251 /* Please, let's not add more entries to the list below. If external projects think it's a good idea
1252 * to come up with always new suffixes and that everybody else should just adjust to that, then it
1253 * really should be on them. Hence, in future, let's not add any more entries. Instead, let's ask
1254 * those packages to instead adopt one of the generic suffixes/prefixes for hidden files or backups,
1255 * possibly augmented with an additional string. Specifically: there's now:
1257 * The generic suffixes "~" and ".bak" for backup files
1258 * The generic prefix "." for hidden files
1260 * Thus, if a new package manager "foopkg" wants its own set of ".foopkg-new", ".foopkg-old",
1261 * ".foopkg-dist" or so registered, let's refuse that and ask them to use ".foopkg.new",
1262 * ".foopkg.old" or ".foopkg~" instead.
1265 return STR_IN_SET(dot
+ 1,
1285 bool is_device_path(const char *path
) {
1287 /* Returns true for paths that likely refer to a device, either by path in sysfs or to something in
1288 * /dev. This accepts any path that starts with /dev/ or /sys/ and has something after that prefix.
1289 * It does not actually resolve the path.
1292 * /dev/sda, /dev/sda/foo, /sys/class, /dev/.., /sys/.., /./dev/foo → yes.
1293 * /../dev/sda, /dev, /sys, /usr/path, /usr/../dev/sda → no.
1296 const char *p
= PATH_STARTSWITH_SET(ASSERT_PTR(path
), "/dev/", "/sys/");
1300 bool valid_device_node_path(const char *path
) {
1302 /* Some superficial checks whether the specified path is a valid device node path, all without
1303 * looking at the actual device node. */
1305 if (!PATH_STARTSWITH_SET(path
, "/dev/", "/run/systemd/inaccessible/"))
1308 if (endswith(path
, "/")) /* can't be a device node if it ends in a slash */
1311 return path_is_normalized(path
);
1314 bool valid_device_allow_pattern(const char *path
) {
1317 /* Like valid_device_node_path(), but also allows full-subsystem expressions like those accepted by
1318 * DeviceAllow= and DeviceDeny=. */
1320 if (STARTSWITH_SET(path
, "block-", "char-"))
1323 return valid_device_node_path(path
);
1326 bool dot_or_dot_dot(const char *path
) {
1336 return path
[2] == 0;
1339 bool empty_or_root(const char *path
) {
1341 /* For operations relative to some root directory, returns true if the specified root directory is
1342 * redundant, i.e. either / or NULL or the empty string or any equivalent. */
1347 return path_equal(path
, "/");
1350 bool path_strv_contains(char **l
, const char *path
) {
1352 if (path_equal(*i
, path
))
1358 bool prefixed_path_strv_contains(char **l
, const char *path
) {
1359 STRV_FOREACH(i
, l
) {
1366 if (path_equal(j
, path
))
1373 int path_glob_can_match(const char *pattern
, const char *prefix
, char **ret
) {
1377 for (const char *a
= pattern
, *b
= prefix
;;) {
1378 _cleanup_free_
char *g
= NULL
, *h
= NULL
;
1382 r
= path_find_first_component(&a
, /* accept_dot_dot = */ false, &p
);
1386 s
= path_find_first_component(&b
, /* accept_dot_dot = */ false, &q
);
1391 /* The pattern matches the prefix. */
1395 t
= path_join(prefix
, p
);
1407 if (r
== s
&& strneq(p
, q
, r
))
1408 continue; /* common component. Check next. */
1414 if (!string_is_glob(g
))
1417 /* We found a glob component. Check if the glob pattern matches the prefix component. */
1423 r
= fnmatch(g
, h
, 0);
1424 if (r
== FNM_NOMATCH
)
1426 if (r
!= 0) /* Failure to process pattern? */
1430 /* The pattern does not match the prefix. */