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 * const *strv
) {
223 STRV_FOREACH(s
, strv
) {
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
, absolute
, beginning
= true;
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
357 * /../foo/bar becomes /foo/bar
358 * /../foo/bar/.. becomes /foo/bar/..
364 keep_trailing_slash
= FLAGS_SET(flags
, PATH_SIMPLIFY_KEEP_TRAILING_SLASH
) && endswith(path
, "/");
366 absolute
= path_is_absolute(path
);
367 f
+= absolute
; /* Keep leading /, if present. */
369 for (const char *p
= f
;;) {
372 r
= path_find_first_component(&p
, true, &e
);
376 if (r
> 0 && absolute
&& beginning
&& path_startswith(e
, ".."))
377 /* If we're at the beginning of an absolute path, we can safely skip ".." */
386 /* if path is invalid, then refuse to simplify the remaining part. */
387 memmove(f
, p
, strlen(p
) + 1);
397 /* Special rule, if we stripped everything, we need a "." for the current directory. */
401 if (*(f
-1) != '/' && keep_trailing_slash
)
408 char* path_startswith_full(const char *path
, const char *prefix
, bool accept_dot_dot
) {
412 /* Returns a pointer to the start of the first component after the parts matched by
414 * - both paths are absolute or both paths are relative,
416 * - each component in prefix in turn matches a component in path at the same position.
417 * An empty string will be returned when the prefix and path are equivalent.
419 * Returns NULL otherwise.
422 if ((path
[0] == '/') != (prefix
[0] == '/'))
429 r
= path_find_first_component(&path
, accept_dot_dot
, &p
);
433 k
= path_find_first_component(&prefix
, accept_dot_dot
, &q
);
438 return (char*) (p
?: path
);
443 if (!strneq(p
, q
, r
))
448 int path_compare(const char *a
, const char *b
) {
451 /* Order NULL before non-NULL */
456 /* A relative path and an absolute path must not compare as equal.
457 * Which one is sorted before the other does not really matter.
458 * Here a relative path is ordered before an absolute path. */
459 r
= CMP(path_is_absolute(a
), path_is_absolute(b
));
467 j
= path_find_first_component(&a
, true, &aa
);
468 k
= path_find_first_component(&b
, true, &bb
);
470 if (j
< 0 || k
< 0) {
471 /* When one of paths is invalid, order invalid path after valid one. */
472 r
= CMP(j
< 0, k
< 0);
476 /* fallback to use strcmp() if both paths are invalid. */
480 /* Order prefixes first: "/foo" before "/foo/bar" */
489 /* Alphabetical sort: "/foo/aaa" before "/foo/b" */
490 r
= memcmp(aa
, bb
, MIN(j
, k
));
494 /* Sort "/foo/a" before "/foo/aaa" */
501 int path_compare_filename(const char *a
, const char *b
) {
502 _cleanup_free_
char *fa
= NULL
, *fb
= NULL
;
505 /* Order NULL before non-NULL */
510 j
= path_extract_filename(a
, &fa
);
511 k
= path_extract_filename(b
, &fb
);
513 /* When one of paths is "." or root, then order it earlier. */
514 r
= CMP(j
!= -EADDRNOTAVAIL
, k
!= -EADDRNOTAVAIL
);
518 /* When one of paths is invalid (or we get OOM), order invalid path after valid one. */
519 r
= CMP(j
< 0, k
< 0);
523 /* fallback to use strcmp() if both paths are invalid. */
527 return strcmp(fa
, fb
);
530 int path_equal_or_inode_same_full(const char *a
, const char *b
, int flags
) {
531 /* Returns true if paths are of the same entry, false if not, <0 on error. */
533 if (path_equal(a
, b
))
539 return inode_same(a
, b
, flags
);
542 char* path_extend_internal(char **x
, ...) {
549 /* Joins all listed strings until the sentinel and places a "/" between them unless the strings
550 * end/begin already with one so that it is unnecessary. Note that slashes which are already
551 * duplicate won't be removed. The string returned is hence always equal to or longer than the sum of
552 * the lengths of the individual strings.
554 * The first argument may be an already allocated string that is extended via realloc() if
555 * non-NULL. path_extend() and path_join() are macro wrappers around this function, making use of the
556 * first parameter to distinguish the two operations.
558 * Note: any listed empty string is simply skipped. This can be useful for concatenating strings of
559 * which some are optional.
563 * path_join("foo", "bar") → "foo/bar"
564 * path_join("foo/", "bar") → "foo/bar"
565 * path_join("", "foo", "", "bar", "") → "foo/bar" */
567 sz
= old_sz
= x
? strlen_ptr(*x
) : 0;
569 while ((p
= va_arg(ap
, char*)) != POINTER_MAX
) {
576 if (sz
> SIZE_MAX
- add
) { /* overflow check */
585 nx
= realloc(x
? *x
: NULL
, GREEDY_ALLOC_ROUND_UP(sz
+1));
592 slash
= nx
[old_sz
-1] == '/';
595 slash
= true; /* no need to generate a slash anymore */
601 while ((p
= va_arg(ap
, char*)) != POINTER_MAX
) {
605 if (!slash
&& p
[0] != '/')
609 slash
= endswith(p
, "/");
616 static int check_x_access(const char *path
, int *ret_fd
) {
617 _cleanup_close_
int fd
= -EBADF
;
620 /* We need to use O_PATH because there may be executables for which we have only exec
621 * permissions, but not read (usually suid executables). */
622 fd
= open(path
, O_PATH
|O_CLOEXEC
);
626 r
= fd_verify_regular(fd
);
630 r
= access_fd(fd
, X_OK
);
632 /* /proc is not mounted. Fallback to access(). */
633 if (access(path
, X_OK
) < 0)
639 *ret_fd
= TAKE_FD(fd
);
644 static int find_executable_impl(const char *name
, const char *root
, char **ret_filename
, int *ret_fd
) {
645 _cleanup_close_
int fd
= -EBADF
;
646 _cleanup_free_
char *path_name
= NULL
;
651 /* Function chase() is invoked only when root is not NULL, as using it regardless of
652 * root value would alter the behavior of existing callers for example: /bin/sleep would become
653 * /usr/bin/sleep when find_executables is called. Hence, this function should be invoked when
654 * needed to avoid unforeseen regression or other complicated changes. */
656 /* prefix root to name in case full paths are not specified */
657 r
= chase(name
, root
, CHASE_PREFIX_ROOT
, &path_name
, /* ret_fd= */ NULL
);
664 r
= check_x_access(name
, ret_fd
? &fd
: NULL
);
669 r
= path_make_absolute_cwd(name
, ret_filename
);
675 *ret_fd
= TAKE_FD(fd
);
680 int find_executable_full(
683 char **exec_search_path
,
684 bool use_path_envvar
,
688 int last_error
= -ENOENT
, r
= 0;
689 const char *p
= NULL
;
694 return find_executable_impl(name
, root
, ret_filename
, ret_fd
);
697 /* Plain getenv, not secure_getenv, because we want to actually allow the user to pick the
703 if (exec_search_path
) {
704 STRV_FOREACH(element
, exec_search_path
) {
705 _cleanup_free_
char *full_path
= NULL
;
707 if (!path_is_absolute(*element
))
710 full_path
= path_join(*element
, name
);
714 r
= find_executable_impl(full_path
, root
, ret_filename
, ret_fd
);
725 /* Resolve a single-component name to a full path */
727 _cleanup_free_
char *element
= NULL
;
729 r
= extract_first_word(&p
, &element
, ":", EXTRACT_RELAX
|EXTRACT_DONT_COALESCE_SEPARATORS
);
735 if (!path_is_absolute(element
))
738 if (!path_extend(&element
, name
))
741 r
= find_executable_impl(element
, root
, ret_filename
, ret_fd
);
743 /* PATH entries which we don't have access to are ignored, as per tradition. */
756 bool paths_check_timestamp(const char* const* paths
, usec_t
*timestamp
, bool update
) {
757 bool changed
= false, originally_unset
;
764 originally_unset
= *timestamp
== 0;
766 STRV_FOREACH(i
, paths
) {
770 if (stat(*i
, &stats
) < 0)
773 u
= timespec_load(&stats
.st_mtim
);
779 log_debug(originally_unset
? "Loaded timestamp for '%s'." : "Timestamp of '%s' changed.", *i
);
781 /* update timestamp */
792 static int executable_is_good(const char *executable
) {
793 _cleanup_free_
char *p
= NULL
, *d
= NULL
;
796 r
= find_executable(executable
, &p
);
802 /* An fsck that is linked to /bin/true is a non-existent fsck */
804 r
= readlink_malloc(p
, &d
);
805 if (r
== -EINVAL
) /* not a symlink */
810 return !PATH_IN_SET(d
, "true"
816 int fsck_exists(void) {
817 return executable_is_good("fsck");
820 int fsck_exists_for_fstype(const char *fstype
) {
826 if (streq(fstype
, "auto"))
833 checker
= strjoina("fsck.", fstype
);
834 return executable_is_good(checker
);
837 static const char* skip_slash_or_dot(const char *p
) {
838 for (; !isempty(p
); p
++) {
841 if (startswith(p
, "./")) {
850 int path_find_first_component(const char **p
, bool accept_dot_dot
, const char **ret
) {
851 const char *q
, *first
, *end_first
, *next
;
856 /* When a path is input, then returns the pointer to the first component and its length, and
857 * move the input pointer to the next component or nul. This skips both over any '/'
858 * immediately *before* and *after* the first component before returning.
861 * Input: p: "//.//aaa///bbbbb/cc"
862 * Output: p: "bbbbb///cc"
863 * ret: "aaa///bbbbb/cc"
864 * return value: 3 (== strlen("aaa"))
867 * Output: p: (pointer to NUL)
869 * return value: 3 (== strlen("aaa"))
871 * Input: p: "/", ".", ""
872 * Output: p: (pointer to NUL)
881 * Input: p: "(too long component)"
882 * Output: return value: -EINVAL
884 * (when accept_dot_dot is false)
885 * Input: p: "//..//aaa///bbbbb/cc"
886 * Output: return value: -EINVAL
891 first
= skip_slash_or_dot(q
);
892 if (isempty(first
)) {
898 if (streq(first
, ".")) {
905 end_first
= strchrnul(first
, '/');
906 len
= end_first
- first
;
910 if (!accept_dot_dot
&& len
== 2 && first
[0] == '.' && first
[1] == '.')
913 next
= skip_slash_or_dot(end_first
);
915 *p
= next
+ streq(next
, ".");
921 static const char* skip_slash_or_dot_backward(const char *path
, const char *q
) {
923 assert(!q
|| q
>= path
);
925 for (; q
; q
= PTR_SUB1(q
, path
)) {
928 if (q
> path
&& strneq(q
- 1, "/.", 2))
930 if (q
== path
&& *q
== '.')
937 int path_find_last_component(const char *path
, bool accept_dot_dot
, const char **next
, const char **ret
) {
938 const char *q
, *last_end
, *last_begin
;
941 /* Similar to path_find_first_component(), but search components from the end.
944 * Input: path: "//.//aaa///bbbbb/cc//././"
946 * Output: next: "/cc//././"
948 * return value: 2 (== strlen("cc"))
950 * Input: path: "//.//aaa///bbbbb/cc//././"
952 * Output: next: "///bbbbb/cc//././"
953 * ret: "bbbbb/cc//././"
954 * return value: 5 (== strlen("bbbbb"))
956 * Input: path: "//.//aaa///bbbbb/cc//././"
957 * next: "///bbbbb/cc//././"
958 * Output: next: "//.//aaa///bbbbb/cc//././" (next == path)
959 * ret: "aaa///bbbbb/cc//././"
960 * return value: 3 (== strlen("aaa"))
962 * Input: path: "/", ".", "", or NULL
963 * Output: next: equivalent to path
967 * Input: path: "(too long component)"
968 * Output: return value: -EINVAL
970 * (when accept_dot_dot is false)
971 * Input: path: "//..//aaa///bbbbb/cc/..//"
972 * Output: return value: -EINVAL
984 if (*next
< path
|| *next
> path
+ strlen(path
))
991 if (!IN_SET(**next
, '\0', '/'))
995 q
= path
+ strlen(path
) - 1;
997 q
= skip_slash_or_dot_backward(path
, q
);
998 if (!q
|| /* the root directory */
999 (q
== path
&& *q
== '.')) { /* path is "." or "./" */
1009 while (q
&& *q
!= '/')
1010 q
= PTR_SUB1(q
, path
);
1012 last_begin
= q
? q
+ 1 : path
;
1013 len
= last_end
- last_begin
;
1017 if (!accept_dot_dot
&& len
== 2 && strneq(last_begin
, "..", 2))
1021 q
= skip_slash_or_dot_backward(path
, q
);
1022 *next
= q
? q
+ 1 : path
;
1030 const char* last_path_component(const char *path
) {
1032 /* Finds the last component of the path, preserving the optional trailing slash that signifies a directory.
1045 * Also, the empty string is mapped to itself.
1047 * This is different than basename(), which returns "" when a trailing slash is present.
1049 * This always succeeds (except if you pass NULL in which case it returns NULL, too).
1057 l
= k
= strlen(path
);
1058 if (l
== 0) /* special case — an empty string */
1061 while (k
> 0 && path
[k
-1] == '/')
1064 if (k
== 0) /* the root directory */
1065 return path
+ l
- 1;
1067 while (k
> 0 && path
[k
-1] != '/')
1073 int path_extract_filename(const char *path
, char **ret
) {
1074 _cleanup_free_
char *a
= NULL
;
1075 const char *c
, *next
= NULL
;
1078 /* Extracts the filename part (i.e. right-most component) from a path, i.e. string that passes
1079 * filename_is_valid(). A wrapper around last_path_component(), but eats up trailing
1082 * -EINVAL → if the path is not valid
1083 * -EADDRNOTAVAIL → if only a directory was specified, but no filename, i.e. the root dir
1084 * itself or "." is specified
1085 * -ENOMEM → no memory
1087 * Returns >= 0 on success. If the input path has a trailing slash, returns O_DIRECTORY, to
1088 * indicate the referenced file must be a directory.
1090 * This function guarantees to return a fully valid filename, i.e. one that passes
1091 * filename_is_valid() – this means "." and ".." are not accepted. */
1093 if (!path_is_valid(path
))
1096 r
= path_find_last_component(path
, false, &next
, &c
);
1099 if (r
== 0) /* root directory */
1100 return -EADDRNOTAVAIL
;
1107 return strlen(c
) > (size_t) r
? O_DIRECTORY
: 0;
1110 int path_extract_directory(const char *path
, char **ret
) {
1111 const char *c
, *next
= NULL
;
1114 /* The inverse of path_extract_filename(), i.e. returns the directory path prefix. Returns:
1116 * -EINVAL → if the path is not valid
1117 * -EDESTADDRREQ → if no directory was specified in the passed in path, i.e. only a filename was passed
1118 * -EADDRNOTAVAIL → if the passed in parameter had no filename but did have a directory, i.e.
1119 * the root dir itself or "." was specified
1120 * -ENOMEM → no memory (surprise!)
1122 * This function guarantees to return a fully valid path, i.e. one that passes path_is_valid().
1125 r
= path_find_last_component(path
, false, &next
, &c
);
1128 if (r
== 0) /* empty or root */
1129 return isempty(path
) ? -EINVAL
: -EADDRNOTAVAIL
;
1131 if (*path
!= '/') /* filename only */
1132 return -EDESTADDRREQ
;
1134 return strdup_to(ret
, "/");
1137 _cleanup_free_
char *a
= strndup(path
, next
- path
);
1143 if (!path_is_valid(a
))
1152 bool filename_part_is_valid(const char *p
) {
1155 /* Checks f the specified string is OK to be *part* of a filename. This is different from
1156 * filename_is_valid() as "." and ".." and "" are OK by this call, but not by filename_is_valid(). */
1161 e
= strchrnul(p
, '/');
1165 if (e
- p
> NAME_MAX
) /* NAME_MAX is counted *without* the trailing NUL byte */
1171 bool filename_is_valid(const char *p
) {
1176 if (dot_or_dot_dot(p
)) /* Yes, in this context we consider "." and ".." invalid */
1179 return filename_part_is_valid(p
);
1182 bool path_is_valid_full(const char *p
, bool accept_dot_dot
) {
1186 for (const char *e
= p
;;) {
1189 r
= path_find_first_component(&e
, accept_dot_dot
, NULL
);
1193 if (e
- p
>= PATH_MAX
) /* Already reached the maximum length for a path? (PATH_MAX is counted
1194 * *with* the trailing NUL byte) */
1196 if (*e
== 0) /* End of string? Yay! */
1201 bool path_is_normalized(const char *p
) {
1202 if (!path_is_safe(p
))
1205 if (streq(p
, ".") || startswith(p
, "./") || endswith(p
, "/.") || strstr(p
, "/./"))
1208 if (strstr(p
, "//"))
1214 int file_in_same_dir(const char *path
, const char *filename
, char **ret
) {
1215 _cleanup_free_
char *b
= NULL
;
1222 /* This removes the last component of path and appends filename, unless the latter is absolute anyway
1223 * or the former isn't */
1225 if (path_is_absolute(filename
))
1226 b
= strdup(filename
);
1228 _cleanup_free_
char *dn
= NULL
;
1230 r
= path_extract_directory(path
, &dn
);
1231 if (r
== -EDESTADDRREQ
) /* no path prefix */
1232 b
= strdup(filename
);
1236 b
= path_join(dn
, filename
);
1245 bool hidden_or_backup_file(const char *filename
) {
1248 if (filename
[0] == '.' ||
1249 STR_IN_SET(filename
,
1253 endswith(filename
, "~"))
1256 const char *dot
= strrchr(filename
, '.');
1260 /* Please, let's not add more entries to the list below. If external projects think it's a good idea
1261 * to come up with always new suffixes and that everybody else should just adjust to that, then it
1262 * really should be on them. Hence, in future, let's not add any more entries. Instead, let's ask
1263 * those packages to instead adopt one of the generic suffixes/prefixes for hidden files or backups,
1264 * possibly augmented with an additional string. Specifically: there's now:
1266 * The generic suffixes "~" and ".bak" for backup files
1267 * The generic prefix "." for hidden files
1269 * Thus, if a new package manager "foopkg" wants its own set of ".foopkg-new", ".foopkg-old",
1270 * ".foopkg-dist" or so registered, let's refuse that and ask them to use ".foopkg.new",
1271 * ".foopkg.old" or ".foopkg~" instead.
1274 return STR_IN_SET(dot
+ 1,
1294 bool is_device_path(const char *path
) {
1296 /* Returns true for paths that likely refer to a device, either by path in sysfs or to something in
1297 * /dev. This accepts any path that starts with /dev/ or /sys/ and has something after that prefix.
1298 * It does not actually resolve the path.
1301 * /dev/sda, /dev/sda/foo, /sys/class, /dev/.., /sys/.., /./dev/foo → yes.
1302 * /../dev/sda, /dev, /sys, /usr/path, /usr/../dev/sda → no.
1305 const char *p
= PATH_STARTSWITH_SET(ASSERT_PTR(path
), "/dev/", "/sys/");
1309 bool valid_device_node_path(const char *path
) {
1311 /* Some superficial checks whether the specified path is a valid device node path, all without
1312 * looking at the actual device node. */
1314 if (!PATH_STARTSWITH_SET(path
, "/dev/", "/run/systemd/inaccessible/"))
1317 if (endswith(path
, "/")) /* can't be a device node if it ends in a slash */
1320 return path_is_normalized(path
);
1323 bool valid_device_allow_pattern(const char *path
) {
1326 /* Like valid_device_node_path(), but also allows full-subsystem expressions like those accepted by
1327 * DeviceAllow= and DeviceDeny=. */
1329 if (STARTSWITH_SET(path
, "block-", "char-"))
1332 return valid_device_node_path(path
);
1335 bool dot_or_dot_dot(const char *path
) {
1345 return path
[2] == 0;
1348 bool path_implies_directory(const char *path
) {
1350 /* Sometimes, if we look at a path we already know it must refer to a directory, because it is
1351 * suffixed with a slash, or its last component is "." or ".." */
1356 if (dot_or_dot_dot(path
))
1359 return ENDSWITH_SET(path
, "/", "/.", "/..");
1362 bool empty_or_root(const char *path
) {
1364 /* For operations relative to some root directory, returns true if the specified root directory is
1365 * redundant, i.e. either / or NULL or the empty string or any equivalent. */
1370 return path_equal(path
, "/");
1373 bool path_strv_contains(char * const *l
, const char *path
) {
1377 if (path_equal(*i
, path
))
1383 bool prefixed_path_strv_contains(char * const *l
, const char *path
) {
1386 STRV_FOREACH(i
, l
) {
1394 if (path_equal(j
, path
))
1401 int path_glob_can_match(const char *pattern
, const char *prefix
, char **ret
) {
1405 for (const char *a
= pattern
, *b
= prefix
;;) {
1406 _cleanup_free_
char *g
= NULL
, *h
= NULL
;
1410 r
= path_find_first_component(&a
, /* accept_dot_dot = */ false, &p
);
1414 s
= path_find_first_component(&b
, /* accept_dot_dot = */ false, &q
);
1419 /* The pattern matches the prefix. */
1423 t
= path_join(prefix
, p
);
1435 if (r
== s
&& strneq(p
, q
, r
))
1436 continue; /* common component. Check next. */
1442 if (!string_is_glob(g
))
1445 /* We found a glob component. Check if the glob pattern matches the prefix component. */
1451 r
= fnmatch(g
, h
, 0);
1452 if (r
== FNM_NOMATCH
)
1454 if (r
!= 0) /* Failure to process pattern? */
1458 /* The pattern does not match the prefix. */
1464 const char* default_PATH(void) {
1466 static int split
= -1;
1469 /* Check whether /usr/sbin is not a symlink and return the appropriate $PATH.
1470 * On error fall back to the safe value with both directories as configured… */
1473 STRV_FOREACH_PAIR(bin
, sbin
, STRV_MAKE("/usr/bin", "/usr/sbin",
1474 "/usr/local/bin", "/usr/local/sbin")) {
1475 r
= inode_same(*bin
, *sbin
, AT_NO_AUTOMOUNT
);
1476 if (r
> 0 || r
== -ENOENT
)
1479 log_debug_errno(r
, "Failed to compare \"%s\" and \"%s\", using compat $PATH: %m",
1487 return DEFAULT_PATH_WITH_SBIN
;
1489 return DEFAULT_PATH_WITHOUT_SBIN
;