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'. */
139 path_simplify(result
);
141 if (!path_is_valid(result
))
145 *ret
= TAKE_PTR(result
);
149 if (r
!= k
|| !strneq(f
, t
, r
))
153 /* If we're here, then "from_dir" has one or more elements that need to
154 * be replaced with "..". */
156 for (n_parents
= 1;; n_parents
++) {
157 /* If this includes ".." we can't do a simple series of "..". */
158 r
= path_find_first_component(&from
, false, &f
);
165 if (isempty(t
) && n_parents
* 3 > PATH_MAX
)
166 /* PATH_MAX is counted *with* the trailing NUL byte */
169 result
= new(char, n_parents
* 3 + !isempty(t
) + strlen_ptr(t
));
173 for (p
= result
; n_parents
> 0; n_parents
--)
174 p
= mempcpy(p
, "../", 3);
177 /* Remove trailing slash and terminate string. */
179 *ret
= TAKE_PTR(result
);
185 path_simplify(result
);
187 if (!path_is_valid(result
))
190 *ret
= TAKE_PTR(result
);
194 int path_make_relative_parent(const char *from_child
, const char *to
, char **ret
) {
195 _cleanup_free_
char *from
= NULL
;
202 /* Similar to path_make_relative(), but provides the relative path from the parent directory of
203 * 'from_child'. This may be useful when creating relative symlink.
206 * - from = "/path/to/aaa", to = "/path/to/bbb"
207 * path_make_relative(from, to) = "../bbb"
208 * path_make_relative_parent(from, to) = "bbb"
210 * - from = "/path/to/aaa/bbb", to = "/path/to/ccc/ddd"
211 * path_make_relative(from, to) = "../../ccc/ddd"
212 * path_make_relative_parent(from, to) = "../ccc/ddd"
215 r
= path_extract_directory(from_child
, &from
);
219 return path_make_relative(from
, to
, ret
);
222 char* path_startswith_strv(const char *p
, char **set
) {
223 STRV_FOREACH(s
, set
) {
226 t
= path_startswith(p
, *s
);
234 int path_strv_make_absolute_cwd(char **l
) {
237 /* Goes through every item in the string list and makes it
238 * absolute. This works in place and won't rollback any
239 * changes on failure. */
244 r
= path_make_absolute_cwd(*s
, &t
);
249 free_and_replace(*s
, t
);
255 char **path_strv_resolve(char **l
, const char *root
) {
263 /* Goes through every item in the string list and canonicalize
264 * the path. This works in place and won't rollback any
265 * changes on failure. */
268 _cleanup_free_
char *orig
= NULL
;
271 if (!path_is_absolute(*s
)) {
278 t
= path_join(root
, orig
);
286 r
= chase(t
, root
, 0, &u
, NULL
);
304 x
= path_startswith(u
, root
);
306 /* restore the slash if it was lost */
307 if (!startswith(x
, "/"))
318 /* canonicalized path goes outside of
319 * prefix, keep the original path instead */
320 free_and_replace(u
, orig
);
336 char **path_strv_resolve_uniq(char **l
, const char *root
) {
341 if (!path_strv_resolve(l
, root
))
347 char *path_simplify_full(char *path
, PathSimplifyFlags flags
) {
348 bool add_slash
= false, keep_trailing_slash
;
349 char *f
= ASSERT_PTR(path
);
352 /* Removes redundant inner and trailing slashes. Also removes unnecessary dots.
353 * Modifies the passed string in-place.
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 if (path_is_absolute(path
))
367 for (const char *p
= f
;;) {
370 r
= path_find_first_component(&p
, true, &e
);
378 /* if path is invalid, then refuse to simplify remaining part. */
379 memmove(f
, p
, strlen(p
) + 1);
389 /* Special rule, if we stripped everything, we need a "." for the current directory. */
393 if (*(f
-1) != '/' && keep_trailing_slash
)
400 char *path_startswith_full(const char *path
, const char *prefix
, bool accept_dot_dot
) {
404 /* Returns a pointer to the start of the first component after the parts matched by
406 * - both paths are absolute or both paths are relative,
408 * - each component in prefix in turn matches a component in path at the same position.
409 * An empty string will be returned when the prefix and path are equivalent.
411 * Returns NULL otherwise.
414 if ((path
[0] == '/') != (prefix
[0] == '/'))
421 r
= path_find_first_component(&path
, accept_dot_dot
, &p
);
425 k
= path_find_first_component(&prefix
, accept_dot_dot
, &q
);
430 return (char*) (p
?: path
);
435 if (!strneq(p
, q
, r
))
440 int path_compare(const char *a
, const char *b
) {
443 /* Order NULL before non-NULL */
448 /* A relative path and an absolute path must not compare as equal.
449 * Which one is sorted before the other does not really matter.
450 * Here a relative path is ordered before an absolute path. */
451 r
= CMP(path_is_absolute(a
), path_is_absolute(b
));
459 j
= path_find_first_component(&a
, true, &aa
);
460 k
= path_find_first_component(&b
, true, &bb
);
462 if (j
< 0 || k
< 0) {
463 /* When one of paths is invalid, order invalid path after valid one. */
464 r
= CMP(j
< 0, k
< 0);
468 /* fallback to use strcmp() if both paths are invalid. */
472 /* Order prefixes first: "/foo" before "/foo/bar" */
481 /* Alphabetical sort: "/foo/aaa" before "/foo/b" */
482 r
= memcmp(aa
, bb
, MIN(j
, k
));
486 /* Sort "/foo/a" before "/foo/aaa" */
493 int path_compare_filename(const char *a
, const char *b
) {
494 _cleanup_free_
char *fa
= NULL
, *fb
= NULL
;
497 /* Order NULL before non-NULL */
502 j
= path_extract_filename(a
, &fa
);
503 k
= path_extract_filename(b
, &fb
);
505 /* When one of paths is "." or root, then order it earlier. */
506 r
= CMP(j
!= -EADDRNOTAVAIL
, k
!= -EADDRNOTAVAIL
);
510 /* When one of paths is invalid (or we get OOM), order invalid path after valid one. */
511 r
= CMP(j
< 0, k
< 0);
515 /* fallback to use strcmp() if both paths are invalid. */
519 return strcmp(fa
, fb
);
522 char* path_extend_internal(char **x
, ...) {
529 /* Joins all listed strings until the sentinel and places a "/" between them unless the strings
530 * end/begin already with one so that it is unnecessary. Note that slashes which are already
531 * duplicate won't be removed. The string returned is hence always equal to or longer than the sum of
532 * the lengths of the individual strings.
534 * The first argument may be an already allocated string that is extended via realloc() if
535 * non-NULL. path_extend() and path_join() are macro wrappers around this function, making use of the
536 * first parameter to distinguish the two operations.
538 * Note: any listed empty string is simply skipped. This can be useful for concatenating strings of
539 * which some are optional.
543 * path_join("foo", "bar") → "foo/bar"
544 * path_join("foo/", "bar") → "foo/bar"
545 * path_join("", "foo", "", "bar", "") → "foo/bar" */
547 sz
= old_sz
= x
? strlen_ptr(*x
) : 0;
549 while ((p
= va_arg(ap
, char*)) != POINTER_MAX
) {
556 if (sz
> SIZE_MAX
- add
) { /* overflow check */
565 nx
= realloc(x
? *x
: NULL
, GREEDY_ALLOC_ROUND_UP(sz
+1));
572 slash
= nx
[old_sz
-1] == '/';
575 slash
= true; /* no need to generate a slash anymore */
581 while ((p
= va_arg(ap
, char*)) != POINTER_MAX
) {
585 if (!slash
&& p
[0] != '/')
589 slash
= endswith(p
, "/");
596 static int check_x_access(const char *path
, int *ret_fd
) {
597 _cleanup_close_
int fd
= -EBADF
;
600 /* We need to use O_PATH because there may be executables for which we have only exec
601 * permissions, but not read (usually suid executables). */
602 fd
= open(path
, O_PATH
|O_CLOEXEC
);
606 r
= fd_verify_regular(fd
);
610 r
= access_fd(fd
, X_OK
);
612 /* /proc is not mounted. Fallback to access(). */
613 if (access(path
, X_OK
) < 0)
619 *ret_fd
= TAKE_FD(fd
);
624 static int find_executable_impl(const char *name
, const char *root
, char **ret_filename
, int *ret_fd
) {
625 _cleanup_close_
int fd
= -EBADF
;
626 _cleanup_free_
char *path_name
= NULL
;
631 /* Function chase() is invoked only when root is not NULL, as using it regardless of
632 * root value would alter the behavior of existing callers for example: /bin/sleep would become
633 * /usr/bin/sleep when find_executables is called. Hence, this function should be invoked when
634 * needed to avoid unforeseen regression or other complicated changes. */
636 /* prefix root to name in case full paths are not specified */
637 r
= chase(name
, root
, CHASE_PREFIX_ROOT
, &path_name
, /* ret_fd= */ NULL
);
644 r
= check_x_access(name
, ret_fd
? &fd
: NULL
);
649 r
= path_make_absolute_cwd(name
, ret_filename
);
655 *ret_fd
= TAKE_FD(fd
);
660 int find_executable_full(const char *name
, const char *root
, char **exec_search_path
, bool use_path_envvar
, char **ret_filename
, int *ret_fd
) {
661 int last_error
= -ENOENT
, r
= 0;
662 const char *p
= NULL
;
667 return find_executable_impl(name
, root
, ret_filename
, ret_fd
);
670 /* Plain getenv, not secure_getenv, because we want to actually allow the user to pick the
676 if (exec_search_path
) {
677 STRV_FOREACH(element
, exec_search_path
) {
678 _cleanup_free_
char *full_path
= NULL
;
680 if (!path_is_absolute(*element
))
683 full_path
= path_join(*element
, name
);
687 r
= find_executable_impl(full_path
, root
, ret_filename
, ret_fd
);
698 /* Resolve a single-component name to a full path */
700 _cleanup_free_
char *element
= NULL
;
702 r
= extract_first_word(&p
, &element
, ":", EXTRACT_RELAX
|EXTRACT_DONT_COALESCE_SEPARATORS
);
708 if (!path_is_absolute(element
))
711 if (!path_extend(&element
, name
))
714 r
= find_executable_impl(element
, root
, ret_filename
, ret_fd
);
716 /* PATH entries which we don't have access to are ignored, as per tradition. */
729 bool paths_check_timestamp(const char* const* paths
, usec_t
*timestamp
, bool update
) {
730 bool changed
= false, originally_unset
;
737 originally_unset
= *timestamp
== 0;
739 STRV_FOREACH(i
, paths
) {
743 if (stat(*i
, &stats
) < 0)
746 u
= timespec_load(&stats
.st_mtim
);
752 log_debug(originally_unset
? "Loaded timestamp for '%s'." : "Timestamp of '%s' changed.", *i
);
754 /* update timestamp */
765 static int executable_is_good(const char *executable
) {
766 _cleanup_free_
char *p
= NULL
, *d
= NULL
;
769 r
= find_executable(executable
, &p
);
775 /* An fsck that is linked to /bin/true is a non-existent fsck */
777 r
= readlink_malloc(p
, &d
);
778 if (r
== -EINVAL
) /* not a symlink */
783 return !PATH_IN_SET(d
, "true"
789 int fsck_exists(void) {
790 return executable_is_good("fsck");
793 int fsck_exists_for_fstype(const char *fstype
) {
799 if (streq(fstype
, "auto"))
806 checker
= strjoina("fsck.", fstype
);
807 return executable_is_good(checker
);
810 static const char *skip_slash_or_dot(const char *p
) {
811 for (; !isempty(p
); p
++) {
814 if (startswith(p
, "./")) {
823 int path_find_first_component(const char **p
, bool accept_dot_dot
, const char **ret
) {
824 const char *q
, *first
, *end_first
, *next
;
829 /* When a path is input, then returns the pointer to the first component and its length, and
830 * move the input pointer to the next component or nul. This skips both over any '/'
831 * immediately *before* and *after* the first component before returning.
834 * Input: p: "//.//aaa///bbbbb/cc"
835 * Output: p: "bbbbb///cc"
836 * ret: "aaa///bbbbb/cc"
837 * return value: 3 (== strlen("aaa"))
840 * Output: p: (pointer to NUL)
842 * return value: 3 (== strlen("aaa"))
844 * Input: p: "/", ".", ""
845 * Output: p: (pointer to NUL)
854 * Input: p: "(too long component)"
855 * Output: return value: -EINVAL
857 * (when accept_dot_dot is false)
858 * Input: p: "//..//aaa///bbbbb/cc"
859 * Output: return value: -EINVAL
864 first
= skip_slash_or_dot(q
);
865 if (isempty(first
)) {
871 if (streq(first
, ".")) {
878 end_first
= strchrnul(first
, '/');
879 len
= end_first
- first
;
883 if (!accept_dot_dot
&& len
== 2 && first
[0] == '.' && first
[1] == '.')
886 next
= skip_slash_or_dot(end_first
);
888 *p
= next
+ streq(next
, ".");
894 static const char *skip_slash_or_dot_backward(const char *path
, const char *q
) {
896 assert(!q
|| q
>= path
);
898 for (; q
; q
= PTR_SUB1(q
, path
)) {
901 if (q
> path
&& strneq(q
- 1, "/.", 2))
903 if (q
== path
&& *q
== '.')
910 int path_find_last_component(const char *path
, bool accept_dot_dot
, const char **next
, const char **ret
) {
911 const char *q
, *last_end
, *last_begin
;
914 /* Similar to path_find_first_component(), but search components from the end.
917 * Input: path: "//.//aaa///bbbbb/cc//././"
919 * Output: next: "/cc//././"
921 * return value: 2 (== strlen("cc"))
923 * Input: path: "//.//aaa///bbbbb/cc//././"
925 * Output: next: "///bbbbb/cc//././"
926 * ret: "bbbbb/cc//././"
927 * return value: 5 (== strlen("bbbbb"))
929 * Input: path: "//.//aaa///bbbbb/cc//././"
930 * next: "///bbbbb/cc//././"
931 * Output: next: "//.//aaa///bbbbb/cc//././" (next == path)
932 * ret: "aaa///bbbbb/cc//././"
933 * return value: 3 (== strlen("aaa"))
935 * Input: path: "/", ".", "", or NULL
936 * Output: next: equivalent to path
940 * Input: path: "(too long component)"
941 * Output: return value: -EINVAL
943 * (when accept_dot_dot is false)
944 * Input: path: "//..//aaa///bbbbb/cc/..//"
945 * Output: return value: -EINVAL
957 if (*next
< path
|| *next
> path
+ strlen(path
))
964 if (!IN_SET(**next
, '\0', '/'))
968 q
= path
+ strlen(path
) - 1;
970 q
= skip_slash_or_dot_backward(path
, q
);
971 if (!q
|| /* the root directory */
972 (q
== path
&& *q
== '.')) { /* path is "." or "./" */
982 while (q
&& *q
!= '/')
983 q
= PTR_SUB1(q
, path
);
985 last_begin
= q
? q
+ 1 : path
;
986 len
= last_end
- last_begin
;
990 if (!accept_dot_dot
&& len
== 2 && strneq(last_begin
, "..", 2))
994 q
= skip_slash_or_dot_backward(path
, q
);
995 *next
= q
? q
+ 1 : path
;
1003 const char *last_path_component(const char *path
) {
1005 /* Finds the last component of the path, preserving the optional trailing slash that signifies a directory.
1018 * Also, the empty string is mapped to itself.
1020 * This is different than basename(), which returns "" when a trailing slash is present.
1022 * This always succeeds (except if you pass NULL in which case it returns NULL, too).
1030 l
= k
= strlen(path
);
1031 if (l
== 0) /* special case — an empty string */
1034 while (k
> 0 && path
[k
-1] == '/')
1037 if (k
== 0) /* the root directory */
1038 return path
+ l
- 1;
1040 while (k
> 0 && path
[k
-1] != '/')
1046 int path_extract_filename(const char *path
, char **ret
) {
1047 _cleanup_free_
char *a
= NULL
;
1048 const char *c
, *next
= NULL
;
1051 /* Extracts the filename part (i.e. right-most component) from a path, i.e. string that passes
1052 * filename_is_valid(). A wrapper around last_path_component(), but eats up trailing
1055 * -EINVAL → if the path is not valid
1056 * -EADDRNOTAVAIL → if only a directory was specified, but no filename, i.e. the root dir
1057 * itself or "." is specified
1058 * -ENOMEM → no memory
1060 * Returns >= 0 on success. If the input path has a trailing slash, returns O_DIRECTORY, to
1061 * indicate the referenced file must be a directory.
1063 * This function guarantees to return a fully valid filename, i.e. one that passes
1064 * filename_is_valid() – this means "." and ".." are not accepted. */
1066 if (!path_is_valid(path
))
1069 r
= path_find_last_component(path
, false, &next
, &c
);
1072 if (r
== 0) /* root directory */
1073 return -EADDRNOTAVAIL
;
1080 return strlen(c
) > (size_t) r
? O_DIRECTORY
: 0;
1083 int path_extract_directory(const char *path
, char **ret
) {
1084 _cleanup_free_
char *a
= NULL
;
1085 const char *c
, *next
= NULL
;
1088 /* The inverse of path_extract_filename(), i.e. returns the directory path prefix. Returns:
1090 * -EINVAL → if the path is not valid
1091 * -EDESTADDRREQ → if no directory was specified in the passed in path, i.e. only a filename was passed
1092 * -EADDRNOTAVAIL → if the passed in parameter had no filename but did have a directory, i.e.
1093 * the root dir itself or "." was specified
1094 * -ENOMEM → no memory (surprise!)
1096 * This function guarantees to return a fully valid path, i.e. one that passes path_is_valid().
1099 r
= path_find_last_component(path
, false, &next
, &c
);
1102 if (r
== 0) /* empty or root */
1103 return isempty(path
) ? -EINVAL
: -EADDRNOTAVAIL
;
1105 if (*path
!= '/') /* filename only */
1106 return -EDESTADDRREQ
;
1115 a
= strndup(path
, next
- path
);
1121 if (!path_is_valid(a
))
1130 bool filename_part_is_valid(const char *p
) {
1133 /* Checks f the specified string is OK to be *part* of a filename. This is different from
1134 * filename_is_valid() as "." and ".." and "" are OK by this call, but not by filename_is_valid(). */
1139 e
= strchrnul(p
, '/');
1143 if (e
- p
> NAME_MAX
) /* NAME_MAX is counted *without* the trailing NUL byte */
1149 bool filename_is_valid(const char *p
) {
1154 if (dot_or_dot_dot(p
)) /* Yes, in this context we consider "." and ".." invalid */
1157 return filename_part_is_valid(p
);
1160 bool path_is_valid_full(const char *p
, bool accept_dot_dot
) {
1164 for (const char *e
= p
;;) {
1167 r
= path_find_first_component(&e
, accept_dot_dot
, NULL
);
1171 if (e
- p
>= PATH_MAX
) /* Already reached the maximum length for a path? (PATH_MAX is counted
1172 * *with* the trailing NUL byte) */
1174 if (*e
== 0) /* End of string? Yay! */
1179 bool path_is_normalized(const char *p
) {
1180 if (!path_is_safe(p
))
1183 if (streq(p
, ".") || startswith(p
, "./") || endswith(p
, "/.") || strstr(p
, "/./"))
1186 if (strstr(p
, "//"))
1192 int file_in_same_dir(const char *path
, const char *filename
, char **ret
) {
1193 _cleanup_free_
char *b
= NULL
;
1200 /* This removes the last component of path and appends filename, unless the latter is absolute anyway
1201 * or the former isn't */
1203 if (path_is_absolute(filename
))
1204 b
= strdup(filename
);
1206 _cleanup_free_
char *dn
= NULL
;
1208 r
= path_extract_directory(path
, &dn
);
1209 if (r
== -EDESTADDRREQ
) /* no path prefix */
1210 b
= strdup(filename
);
1214 b
= path_join(dn
, filename
);
1223 bool hidden_or_backup_file(const char *filename
) {
1226 if (filename
[0] == '.' ||
1227 STR_IN_SET(filename
,
1231 endswith(filename
, "~"))
1234 const char *dot
= strrchr(filename
, '.');
1238 /* Please, let's not add more entries to the list below. If external projects think it's a good idea
1239 * to come up with always new suffixes and that everybody else should just adjust to that, then it
1240 * really should be on them. Hence, in future, let's not add any more entries. Instead, let's ask
1241 * those packages to instead adopt one of the generic suffixes/prefixes for hidden files or backups,
1242 * possibly augmented with an additional string. Specifically: there's now:
1244 * The generic suffixes "~" and ".bak" for backup files
1245 * The generic prefix "." for hidden files
1247 * Thus, if a new package manager "foopkg" wants its own set of ".foopkg-new", ".foopkg-old",
1248 * ".foopkg-dist" or so registered, let's refuse that and ask them to use ".foopkg.new",
1249 * ".foopkg.old" or ".foopkg~" instead.
1252 return STR_IN_SET(dot
+ 1,
1272 bool is_device_path(const char *path
) {
1274 /* Returns true for paths that likely refer to a device, either by path in sysfs or to something in
1275 * /dev. This accepts any path that starts with /dev/ or /sys/ and has something after that prefix.
1276 * It does not actually resolve the path.
1279 * /dev/sda, /dev/sda/foo, /sys/class, /dev/.., /sys/.., /./dev/foo → yes.
1280 * /../dev/sda, /dev, /sys, /usr/path, /usr/../dev/sda → no.
1283 const char *p
= PATH_STARTSWITH_SET(ASSERT_PTR(path
), "/dev/", "/sys/");
1287 bool valid_device_node_path(const char *path
) {
1289 /* Some superficial checks whether the specified path is a valid device node path, all without
1290 * looking at the actual device node. */
1292 if (!PATH_STARTSWITH_SET(path
, "/dev/", "/run/systemd/inaccessible/"))
1295 if (endswith(path
, "/")) /* can't be a device node if it ends in a slash */
1298 return path_is_normalized(path
);
1301 bool valid_device_allow_pattern(const char *path
) {
1304 /* Like valid_device_node_path(), but also allows full-subsystem expressions like those accepted by
1305 * DeviceAllow= and DeviceDeny=. */
1307 if (STARTSWITH_SET(path
, "block-", "char-"))
1310 return valid_device_node_path(path
);
1313 bool dot_or_dot_dot(const char *path
) {
1323 return path
[2] == 0;
1326 bool empty_or_root(const char *path
) {
1328 /* For operations relative to some root directory, returns true if the specified root directory is
1329 * redundant, i.e. either / or NULL or the empty string or any equivalent. */
1334 return path_equal(path
, "/");
1337 bool path_strv_contains(char **l
, const char *path
) {
1339 if (path_equal(*i
, path
))
1345 bool prefixed_path_strv_contains(char **l
, const char *path
) {
1346 STRV_FOREACH(i
, l
) {
1353 if (path_equal(j
, path
))
1360 int path_glob_can_match(const char *pattern
, const char *prefix
, char **ret
) {
1364 for (const char *a
= pattern
, *b
= prefix
;;) {
1365 _cleanup_free_
char *g
= NULL
, *h
= NULL
;
1369 r
= path_find_first_component(&a
, /* accept_dot_dot = */ false, &p
);
1373 s
= path_find_first_component(&b
, /* accept_dot_dot = */ false, &q
);
1378 /* The pattern matches the prefix. */
1382 t
= path_join(prefix
, p
);
1394 if (r
== s
&& strneq(p
, q
, r
))
1395 continue; /* common component. Check next. */
1401 if (!string_is_glob(g
))
1404 /* We found a glob component. Check if the glob pattern matches the prefix component. */
1410 r
= fnmatch(g
, h
, 0);
1411 if (r
== FNM_NOMATCH
)
1413 if (r
!= 0) /* Failure to process pattern? */
1417 /* The pattern does not match the prefix. */