1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
8 #include "alloc-util.h"
10 #include "errno-util.h"
11 #include "extract-word.h"
14 #include "glob-util.h"
16 #include "path-util.h"
17 #include "stat-util.h"
18 #include "string-util.h"
20 #include "time-util.h"
22 bool is_path(const char *p
) {
23 if (!p
) /* A NULL pointer is definitely not a path */
26 return strchr(p
, '/');
29 int path_split_and_make_absolute(const char *p
, char ***ret
) {
30 _cleanup_strv_free_
char **l
= NULL
;
36 l
= strv_split(p
, ":");
40 r
= path_strv_make_absolute_cwd(l
);
48 char* path_make_absolute(const char *p
, const char *prefix
) {
51 /* Makes every item in the list an absolute path by prepending
52 * the prefix, if specified and necessary */
54 if (path_is_absolute(p
) || isempty(prefix
))
57 return path_join(prefix
, p
);
60 int safe_getcwd(char **ret
) {
61 _cleanup_free_
char *cwd
= NULL
;
63 cwd
= get_current_dir_name();
65 return negative_errno();
67 /* Let's make sure the directory is really absolute, to protect us from the logic behind
78 int path_make_absolute_cwd(const char *p
, char **ret
) {
85 /* Similar to path_make_absolute(), but prefixes with the
86 * current working directory. */
88 if (path_is_absolute(p
))
91 _cleanup_free_
char *cwd
= NULL
;
93 r
= safe_getcwd(&cwd
);
97 c
= path_join(cwd
, p
);
106 int path_make_relative(const char *from
, const char *to
, char **ret
) {
107 _cleanup_free_
char *result
= NULL
;
117 /* Strips the common part, and adds ".." elements as necessary. */
119 if (!path_is_absolute(from
) || !path_is_absolute(to
))
123 r
= path_find_first_component(&from
, true, &f
);
127 k
= path_find_first_component(&to
, true, &t
);
134 /* from and to are equivalent. */
135 result
= strdup(".");
139 /* 'to' is inside of 'from'. */
140 r
= path_simplify_alloc(t
, &result
);
144 if (!path_is_valid(result
))
148 *ret
= TAKE_PTR(result
);
152 if (r
!= k
|| !strneq(f
, t
, r
))
156 /* If we're here, then "from_dir" has one or more elements that need to
157 * be replaced with "..". */
159 for (n_parents
= 1;; n_parents
++) {
160 /* If this includes ".." we can't do a simple series of "..". */
161 r
= path_find_first_component(&from
, false, &f
);
168 if (isempty(t
) && n_parents
* 3 > PATH_MAX
)
169 /* PATH_MAX is counted *with* the trailing NUL byte */
172 result
= new(char, n_parents
* 3 + !isempty(t
) + strlen_ptr(t
));
176 for (p
= result
; n_parents
> 0; n_parents
--)
177 p
= mempcpy(p
, "../", 3);
180 /* Remove trailing slash and terminate string. */
182 *ret
= TAKE_PTR(result
);
188 path_simplify(result
);
190 if (!path_is_valid(result
))
193 *ret
= TAKE_PTR(result
);
197 int path_make_relative_parent(const char *from_child
, const char *to
, char **ret
) {
198 _cleanup_free_
char *from
= NULL
;
205 /* Similar to path_make_relative(), but provides the relative path from the parent directory of
206 * 'from_child'. This may be useful when creating relative symlink.
209 * - from = "/path/to/aaa", to = "/path/to/bbb"
210 * path_make_relative(from, to) = "../bbb"
211 * path_make_relative_parent(from, to) = "bbb"
213 * - from = "/path/to/aaa/bbb", to = "/path/to/ccc/ddd"
214 * path_make_relative(from, to) = "../../ccc/ddd"
215 * path_make_relative_parent(from, to) = "../ccc/ddd"
218 r
= path_extract_directory(from_child
, &from
);
222 return path_make_relative(from
, to
, ret
);
225 char* path_startswith_strv(const char *p
, char * const *strv
) {
228 STRV_FOREACH(s
, strv
) {
231 t
= path_startswith(p
, *s
);
239 int path_strv_make_absolute_cwd(char **l
) {
242 /* Goes through every item in the string list and makes it
243 * absolute. This works in place and won't rollback any
244 * changes on failure. */
249 r
= path_make_absolute_cwd(*s
, &t
);
254 free_and_replace(*s
, t
);
260 char** path_strv_resolve(char **l
, const char *root
) {
268 /* Goes through every item in the string list and canonicalize
269 * the path. This works in place and won't rollback any
270 * changes on failure. */
273 _cleanup_free_
char *orig
= NULL
;
276 if (!path_is_absolute(*s
)) {
283 t
= path_join(root
, orig
);
291 r
= chase(t
, root
, 0, &u
, NULL
);
309 x
= path_startswith(u
, root
);
311 /* restore the slash if it was lost */
312 if (!startswith(x
, "/"))
323 /* canonicalized path goes outside of
324 * prefix, keep the original path instead */
325 free_and_replace(u
, orig
);
341 char** path_strv_resolve_uniq(char **l
, const char *root
) {
346 if (!path_strv_resolve(l
, root
))
352 char* skip_leading_slash(const char *p
) {
353 return skip_leading_chars(p
, "/");
356 char* path_simplify_full(char *path
, PathSimplifyFlags flags
) {
357 bool add_slash
= false, keep_trailing_slash
, absolute
, beginning
= true;
361 /* Removes redundant inner and trailing slashes. Also removes unnecessary dots.
362 * Modifies the passed string in-place.
364 * ///foo//./bar/. becomes /foo/bar
365 * .//./foo//./bar/. becomes foo/bar
366 * /../foo/bar becomes /foo/bar
367 * /../foo/bar/.. becomes /foo/bar/..
373 keep_trailing_slash
= FLAGS_SET(flags
, PATH_SIMPLIFY_KEEP_TRAILING_SLASH
) && endswith(path
, "/");
375 absolute
= path_is_absolute(path
);
376 f
+= absolute
; /* Keep leading /, if present. */
378 for (const char *p
= f
;;) {
381 r
= path_find_first_component(&p
, true, &e
);
385 if (r
> 0 && absolute
&& beginning
&& path_startswith(e
, ".."))
386 /* If we're at the beginning of an absolute path, we can safely skip ".." */
395 /* if path is invalid, then refuse to simplify the remaining part. */
396 memmove(f
, p
, strlen(p
) + 1);
406 /* Special rule, if we stripped everything, we need a "." for the current directory. */
410 if (*(f
-1) != '/' && keep_trailing_slash
)
417 int path_simplify_alloc(const char *path
, char **ret
) {
425 char *t
= strdup(path
);
429 *ret
= path_simplify(t
);
433 char* path_startswith_full(const char *original_path
, const char *prefix
, PathStartWithFlags flags
) {
434 assert(original_path
);
437 /* Returns a pointer to the start of the first component after the parts matched by
439 * - both paths are absolute or both paths are relative,
441 * - each component in prefix in turn matches a component in path at the same position.
442 * An empty string will be returned when the prefix and path are equivalent.
444 * Returns NULL otherwise.
447 const char *path
= original_path
;
449 if ((path
[0] == '/') != (prefix
[0] == '/'))
456 m
= path_find_first_component(&path
, !FLAGS_SET(flags
, PATH_STARTSWITH_REFUSE_DOT_DOT
), &p
);
460 n
= path_find_first_component(&prefix
, !FLAGS_SET(flags
, PATH_STARTSWITH_REFUSE_DOT_DOT
), &q
);
468 if (FLAGS_SET(flags
, PATH_STARTSWITH_RETURN_LEADING_SLASH
)) {
470 if (p
<= original_path
)
485 if (!strneq(p
, q
, m
))
490 int path_compare(const char *a
, const char *b
) {
493 /* Order NULL before non-NULL */
498 /* A relative path and an absolute path must not compare as equal.
499 * Which one is sorted before the other does not really matter.
500 * Here a relative path is ordered before an absolute path. */
501 r
= CMP(path_is_absolute(a
), path_is_absolute(b
));
509 j
= path_find_first_component(&a
, true, &aa
);
510 k
= path_find_first_component(&b
, true, &bb
);
512 if (j
< 0 || k
< 0) {
513 /* When one of paths is invalid, order invalid path after valid one. */
514 r
= CMP(j
< 0, k
< 0);
518 /* fallback to use strcmp() if both paths are invalid. */
522 /* Order prefixes first: "/foo" before "/foo/bar" */
531 /* Alphabetical sort: "/foo/aaa" before "/foo/b" */
532 r
= memcmp(aa
, bb
, MIN(j
, k
));
536 /* Sort "/foo/a" before "/foo/aaa" */
543 int path_compare_filename(const char *a
, const char *b
) {
544 _cleanup_free_
char *fa
= NULL
, *fb
= NULL
;
547 /* Order NULL before non-NULL */
552 j
= path_extract_filename(a
, &fa
);
553 k
= path_extract_filename(b
, &fb
);
555 /* When one of paths is "." or root, then order it earlier. */
556 r
= CMP(j
!= -EADDRNOTAVAIL
, k
!= -EADDRNOTAVAIL
);
560 /* When one of paths is invalid (or we get OOM), order invalid path after valid one. */
561 r
= CMP(j
< 0, k
< 0);
565 /* fallback to use strcmp() if both paths are invalid. */
569 return strcmp(fa
, fb
);
572 int path_equal_or_inode_same_full(const char *a
, const char *b
, int flags
) {
573 /* Returns true if paths are of the same entry, false if not, <0 on error. */
575 if (path_equal(a
, b
))
581 return inode_same(a
, b
, flags
);
584 char* path_extend_internal(char **x
, ...) {
591 /* Joins all listed strings until the sentinel and places a "/" between them unless the strings
592 * end/begin already with one so that it is unnecessary. Note that slashes which are already
593 * duplicate won't be removed. The string returned is hence always equal to or longer than the sum of
594 * the lengths of the individual strings.
596 * The first argument may be an already allocated string that is extended via realloc() if
597 * non-NULL. path_extend() and path_join() are macro wrappers around this function, making use of the
598 * first parameter to distinguish the two operations.
600 * Note: any listed empty string is simply skipped. This can be useful for concatenating strings of
601 * which some are optional.
605 * path_join("foo", "bar") → "foo/bar"
606 * path_join("foo/", "bar") → "foo/bar"
607 * path_join("", "foo", "", "bar", "") → "foo/bar" */
609 sz
= old_sz
= x
? strlen_ptr(*x
) : 0;
611 while ((p
= va_arg(ap
, char*)) != POINTER_MAX
) {
618 if (sz
> SIZE_MAX
- add
) { /* overflow check */
627 nx
= realloc(x
? *x
: NULL
, GREEDY_ALLOC_ROUND_UP(sz
+1));
634 slash
= nx
[old_sz
-1] == '/';
637 slash
= true; /* no need to generate a slash anymore */
643 while ((p
= va_arg(ap
, char*)) != POINTER_MAX
) {
647 if (!slash
&& p
[0] != '/')
651 slash
= endswith(p
, "/");
658 int open_and_check_executable(const char *name
, const char *root
, char **ret_path
, int *ret_fd
) {
659 _cleanup_close_
int fd
= -EBADF
;
660 _cleanup_free_
char *resolved
= NULL
;
665 /* Function chase() is invoked only when root is not NULL, as using it regardless of
666 * root value would alter the behavior of existing callers for example: /bin/sleep would become
667 * /usr/bin/sleep when find_executables is called. Hence, this function should be invoked when
668 * needed to avoid unforeseen regression or other complicated changes. */
670 /* prefix root to name in case full paths are not specified */
671 r
= chase(name
, root
, CHASE_PREFIX_ROOT
, &resolved
, &fd
);
677 /* We need to use O_PATH because there may be executables for which we have only exec permissions,
678 * but not read (usually suid executables). */
679 fd
= open(name
, O_PATH
|O_CLOEXEC
);
684 r
= fd_verify_regular(fd
);
688 r
= access_fd(fd
, X_OK
);
690 /* /proc/ is not mounted. Fall back to access(). */
691 r
= RET_NERRNO(access(name
, X_OK
));
697 *ret_path
= TAKE_PTR(resolved
);
699 r
= path_make_absolute_cwd(name
, ret_path
);
703 path_simplify(*ret_path
);
708 *ret_fd
= TAKE_FD(fd
);
713 int find_executable_full(
716 char * const *exec_search_path
,
717 bool use_path_envvar
,
721 int last_error
= -ENOENT
, r
= 0;
726 return open_and_check_executable(name
, root
, ret_filename
, ret_fd
);
728 if (exec_search_path
) {
729 STRV_FOREACH(element
, exec_search_path
) {
730 _cleanup_free_
char *full_path
= NULL
;
732 if (!path_is_absolute(*element
)) {
733 log_debug("Exec search path '%s' isn't absolute, ignoring.", *element
);
737 full_path
= path_join(*element
, name
);
741 r
= open_and_check_executable(full_path
, root
, ret_filename
, ret_fd
);
750 const char *p
= NULL
;
753 /* Plain getenv, not secure_getenv, because we want to actually allow the user to pick the
759 /* Resolve a single-component name to a full path */
761 _cleanup_free_
char *element
= NULL
;
763 r
= extract_first_word(&p
, &element
, ":", EXTRACT_RELAX
|EXTRACT_DONT_COALESCE_SEPARATORS
);
769 if (!path_is_absolute(element
)) {
770 log_debug("Exec search path '%s' isn't absolute, ignoring.", element
);
774 if (!path_extend(&element
, name
))
777 r
= open_and_check_executable(element
, root
, ret_filename
, ret_fd
);
778 if (r
>= 0) /* Found it! */
780 /* PATH entries which we don't have access to are ignored, as per tradition. */
788 bool paths_check_timestamp(const char* const* paths
, usec_t
*timestamp
, bool update
) {
789 bool changed
= false, originally_unset
;
796 originally_unset
= *timestamp
== 0;
798 STRV_FOREACH(i
, paths
) {
802 if (stat(*i
, &stats
) < 0)
805 u
= timespec_load(&stats
.st_mtim
);
811 log_debug(originally_unset
? "Loaded timestamp for '%s'." : "Timestamp of '%s' changed.", *i
);
813 /* update timestamp */
824 static int executable_is_good(const char *executable
) {
825 _cleanup_free_
char *p
= NULL
, *d
= NULL
;
828 r
= find_executable(executable
, &p
);
834 /* An fsck that is linked to /bin/true is a non-existent fsck */
836 r
= readlink_malloc(p
, &d
);
837 if (r
== -EINVAL
) /* not a symlink */
842 return !PATH_IN_SET(d
, "true"
848 int fsck_exists(void) {
849 return executable_is_good("fsck");
852 int fsck_exists_for_fstype(const char *fstype
) {
858 if (streq(fstype
, "auto"))
865 checker
= strjoina("fsck.", fstype
);
866 return executable_is_good(checker
);
869 static const char* skip_slash_or_dot(const char *p
) {
870 for (; !isempty(p
); p
++) {
873 if (startswith(p
, "./")) {
882 int path_find_first_component(const char **p
, bool accept_dot_dot
, const char **ret
) {
883 const char *q
, *first
, *end_first
, *next
;
888 /* When a path is input, then returns the pointer to the first component and its length, and
889 * move the input pointer to the next component or nul. This skips both over any '/'
890 * immediately *before* and *after* the first component before returning.
893 * Input: p: "//.//aaa///bbbbb/cc"
894 * Output: p: "bbbbb///cc"
895 * ret: "aaa///bbbbb/cc"
896 * return value: 3 (== strlen("aaa"))
899 * Output: p: (pointer to NUL)
901 * return value: 3 (== strlen("aaa"))
903 * Input: p: "/", ".", ""
904 * Output: p: (pointer to NUL)
913 * Input: p: "(too long component)"
914 * Output: return value: -EINVAL
916 * (when accept_dot_dot is false)
917 * Input: p: "//..//aaa///bbbbb/cc"
918 * Output: return value: -EINVAL
923 first
= skip_slash_or_dot(q
);
924 if (isempty(first
)) {
930 if (streq(first
, ".")) {
937 end_first
= strchrnul(first
, '/');
938 len
= end_first
- first
;
942 if (!accept_dot_dot
&& len
== 2 && first
[0] == '.' && first
[1] == '.')
945 next
= skip_slash_or_dot(end_first
);
947 *p
= next
+ streq(next
, ".");
953 static const char* skip_slash_or_dot_backward(const char *path
, const char *q
) {
955 assert(!q
|| q
>= path
);
957 for (; q
; q
= PTR_SUB1(q
, path
)) {
960 if (q
> path
&& strneq(q
- 1, "/.", 2))
962 if (q
== path
&& *q
== '.')
969 int path_find_last_component(const char *path
, bool accept_dot_dot
, const char **next
, const char **ret
) {
970 const char *q
, *last_end
, *last_begin
;
973 /* Similar to path_find_first_component(), but search components from the end.
976 * Input: path: "//.//aaa///bbbbb/cc//././"
978 * Output: next: "/cc//././"
980 * return value: 2 (== strlen("cc"))
982 * Input: path: "//.//aaa///bbbbb/cc//././"
984 * Output: next: "///bbbbb/cc//././"
985 * ret: "bbbbb/cc//././"
986 * return value: 5 (== strlen("bbbbb"))
988 * Input: path: "//.//aaa///bbbbb/cc//././"
989 * next: "///bbbbb/cc//././"
990 * Output: next: "//.//aaa///bbbbb/cc//././" (next == path)
991 * ret: "aaa///bbbbb/cc//././"
992 * return value: 3 (== strlen("aaa"))
994 * Input: path: "/", ".", "", or NULL
995 * Output: next: equivalent to path
999 * Input: path: "(too long component)"
1000 * Output: return value: -EINVAL
1002 * (when accept_dot_dot is false)
1003 * Input: path: "//..//aaa///bbbbb/cc/..//"
1004 * Output: return value: -EINVAL
1007 if (isempty(path
)) {
1015 if (next
&& *next
) {
1016 if (*next
< path
|| *next
> path
+ strlen(path
))
1018 if (*next
== path
) {
1023 if (!IN_SET(**next
, '\0', '/'))
1027 q
= path
+ strlen(path
) - 1;
1029 q
= skip_slash_or_dot_backward(path
, q
);
1030 if (!q
|| /* the root directory */
1031 (q
== path
&& *q
== '.')) { /* path is "." or "./" */
1041 while (q
&& *q
!= '/')
1042 q
= PTR_SUB1(q
, path
);
1044 last_begin
= q
? q
+ 1 : path
;
1045 len
= last_end
- last_begin
;
1049 if (!accept_dot_dot
&& len
== 2 && strneq(last_begin
, "..", 2))
1053 q
= skip_slash_or_dot_backward(path
, q
);
1054 *next
= q
? q
+ 1 : path
;
1062 const char* last_path_component(const char *path
) {
1064 /* Finds the last component of the path, preserving the optional trailing slash that signifies a directory.
1077 * Also, the empty string is mapped to itself.
1079 * This is different than basename(), which returns "" when a trailing slash is present.
1081 * This always succeeds (except if you pass NULL in which case it returns NULL, too).
1089 l
= k
= strlen(path
);
1090 if (l
== 0) /* special case — an empty string */
1093 while (k
> 0 && path
[k
-1] == '/')
1096 if (k
== 0) /* the root directory */
1097 return path
+ l
- 1;
1099 while (k
> 0 && path
[k
-1] != '/')
1105 int path_extract_filename(const char *path
, char **ret
) {
1106 _cleanup_free_
char *a
= NULL
;
1107 const char *c
, *next
= NULL
;
1110 /* Extracts the filename part (i.e. right-most component) from a path, i.e. string that passes
1111 * filename_is_valid(). A wrapper around last_path_component(), but eats up trailing
1114 * -EINVAL → if the path is not valid
1115 * -EADDRNOTAVAIL → if only a directory was specified, but no filename, i.e. the root dir
1116 * itself or "." is specified
1117 * -ENOMEM → no memory
1119 * Returns >= 0 on success. If the input path has a trailing slash, returns O_DIRECTORY, to
1120 * indicate the referenced file must be a directory.
1122 * This function guarantees to return a fully valid filename, i.e. one that passes
1123 * filename_is_valid() – this means "." and ".." are not accepted. */
1125 if (!path_is_valid(path
))
1128 r
= path_find_last_component(path
, false, &next
, &c
);
1131 if (r
== 0) /* root directory */
1132 return -EADDRNOTAVAIL
;
1139 return strlen(c
) > (size_t) r
? O_DIRECTORY
: 0;
1142 int path_extract_directory(const char *path
, char **ret
) {
1143 const char *c
, *next
= NULL
;
1146 /* The inverse of path_extract_filename(), i.e. returns the directory path prefix. Returns:
1148 * -EINVAL → if the path is not valid
1149 * -EDESTADDRREQ → if no directory was specified in the passed in path, i.e. only a filename was passed
1150 * -EADDRNOTAVAIL → if the passed in parameter had no filename but did have a directory, i.e.
1151 * the root dir itself or "." was specified
1152 * -ENOMEM → no memory (surprise!)
1154 * This function guarantees to return a fully valid path, i.e. one that passes path_is_valid().
1157 r
= path_find_last_component(path
, false, &next
, &c
);
1160 if (r
== 0) /* empty or root */
1161 return isempty(path
) ? -EINVAL
: -EADDRNOTAVAIL
;
1163 if (*path
!= '/') /* filename only */
1164 return -EDESTADDRREQ
;
1166 return strdup_to(ret
, "/");
1169 _cleanup_free_
char *a
= strndup(path
, next
- path
);
1175 if (!path_is_valid(a
))
1184 bool filename_part_is_valid(const char *p
) {
1187 /* Checks f the specified string is OK to be *part* of a filename. This is different from
1188 * filename_is_valid() as "." and ".." and "" are OK by this call, but not by filename_is_valid(). */
1193 e
= strchrnul(p
, '/');
1197 if (e
- p
> NAME_MAX
) /* NAME_MAX is counted *without* the trailing NUL byte */
1203 bool filename_is_valid(const char *p
) {
1208 if (dot_or_dot_dot(p
)) /* Yes, in this context we consider "." and ".." invalid */
1211 return filename_part_is_valid(p
);
1214 bool path_is_valid_full(const char *p
, bool accept_dot_dot
) {
1218 for (const char *e
= p
;;) {
1221 r
= path_find_first_component(&e
, accept_dot_dot
, NULL
);
1225 if (e
- p
>= PATH_MAX
) /* Already reached the maximum length for a path? (PATH_MAX is counted
1226 * *with* the trailing NUL byte) */
1228 if (*e
== 0) /* End of string? Yay! */
1233 bool path_is_normalized(const char *p
) {
1234 if (!path_is_safe(p
))
1237 if (streq(p
, ".") || startswith(p
, "./") || endswith(p
, "/.") || strstr(p
, "/./"))
1240 if (strstr(p
, "//"))
1246 int file_in_same_dir(const char *path
, const char *filename
, char **ret
) {
1247 _cleanup_free_
char *b
= NULL
;
1254 /* This removes the last component of path and appends filename, unless the latter is absolute anyway
1255 * or the former isn't */
1257 if (path_is_absolute(filename
))
1258 b
= strdup(filename
);
1260 _cleanup_free_
char *dn
= NULL
;
1262 r
= path_extract_directory(path
, &dn
);
1263 if (r
== -EDESTADDRREQ
) /* no path prefix */
1264 b
= strdup(filename
);
1268 b
= path_join(dn
, filename
);
1277 bool hidden_or_backup_file(const char *filename
) {
1280 if (filename
[0] == '.' ||
1281 STR_IN_SET(filename
,
1285 endswith(filename
, "~"))
1288 const char *dot
= strrchr(filename
, '.');
1292 /* Please, let's not add more entries to the list below. If external projects think it's a good idea
1293 * to come up with always new suffixes and that everybody else should just adjust to that, then it
1294 * really should be on them. Hence, in future, let's not add any more entries. Instead, let's ask
1295 * those packages to instead adopt one of the generic suffixes/prefixes for hidden files or backups,
1296 * possibly augmented with an additional string. Specifically: there's now:
1298 * The generic suffixes "~" and ".bak" for backup files
1299 * The generic prefix "." for hidden files
1301 * Thus, if a new package manager "foopkg" wants its own set of ".foopkg-new", ".foopkg-old",
1302 * ".foopkg-dist" or so registered, let's refuse that and ask them to use ".foopkg.new",
1303 * ".foopkg.old" or ".foopkg~" instead.
1306 return STR_IN_SET(dot
+ 1,
1326 bool is_device_path(const char *path
) {
1328 /* Returns true for paths that likely refer to a device, either by path in sysfs or to something in
1329 * /dev. This accepts any path that starts with /dev/ or /sys/ and has something after that prefix.
1330 * It does not actually resolve the path.
1333 * /dev/sda, /dev/sda/foo, /sys/class, /dev/.., /sys/.., /./dev/foo → yes.
1334 * /../dev/sda, /dev, /sys, /usr/path, /usr/../dev/sda → no.
1337 const char *p
= PATH_STARTSWITH_SET(ASSERT_PTR(path
), "/dev/", "/sys/");
1341 bool valid_device_node_path(const char *path
) {
1343 /* Some superficial checks whether the specified path is a valid device node path, all without
1344 * looking at the actual device node. */
1346 if (!PATH_STARTSWITH_SET(path
, "/dev/", "/run/systemd/inaccessible/"))
1349 if (endswith(path
, "/")) /* can't be a device node if it ends in a slash */
1352 return path_is_normalized(path
);
1355 bool valid_device_allow_pattern(const char *path
) {
1358 /* Like valid_device_node_path(), but also allows full-subsystem expressions like those accepted by
1359 * DeviceAllow= and DeviceDeny=. */
1361 if (STARTSWITH_SET(path
, "block-", "char-"))
1364 return valid_device_node_path(path
);
1367 bool dot_or_dot_dot(const char *path
) {
1377 return path
[2] == 0;
1380 bool path_implies_directory(const char *path
) {
1382 /* Sometimes, if we look at a path we already know it must refer to a directory, because it is
1383 * suffixed with a slash, or its last component is "." or ".." */
1388 if (dot_or_dot_dot(path
))
1391 return ENDSWITH_SET(path
, "/", "/.", "/..");
1394 bool empty_or_root(const char *path
) {
1396 /* For operations relative to some root directory, returns true if the specified root directory is
1397 * redundant, i.e. either / or NULL or the empty string or any equivalent. */
1402 return path_equal(path
, "/");
1405 const char* empty_to_root(const char *path
) {
1406 return isempty(path
) ? "/" : path
;
1409 int empty_or_root_harder_to_null(const char **path
) {
1414 /* This nullifies the input path when the path is empty or points to "/". */
1416 if (empty_or_root(*path
)) {
1421 r
= path_is_root(*path
);
1430 bool path_strv_contains(char * const *l
, const char *path
) {
1434 if (path_equal(*i
, path
))
1440 bool prefixed_path_strv_contains(char * const *l
, const char *path
) {
1443 STRV_FOREACH(i
, l
) {
1451 if (path_equal(j
, path
))
1458 int path_glob_can_match(const char *pattern
, const char *prefix
, char **ret
) {
1462 for (const char *a
= pattern
, *b
= prefix
;;) {
1463 _cleanup_free_
char *g
= NULL
, *h
= NULL
;
1467 r
= path_find_first_component(&a
, /* accept_dot_dot = */ false, &p
);
1471 s
= path_find_first_component(&b
, /* accept_dot_dot = */ false, &q
);
1476 /* The pattern matches the prefix. */
1480 t
= path_join(prefix
, p
);
1492 if (r
== s
&& strneq(p
, q
, r
))
1493 continue; /* common component. Check next. */
1499 if (!string_is_glob(g
))
1502 /* We found a glob component. Check if the glob pattern matches the prefix component. */
1508 r
= fnmatch(g
, h
, 0);
1509 if (r
== FNM_NOMATCH
)
1511 if (r
!= 0) /* Failure to process pattern? */
1515 /* The pattern does not match the prefix. */
1521 const char* default_PATH(void) {
1523 static int split
= -1;
1526 /* Check whether /usr/sbin is not a symlink and return the appropriate $PATH.
1527 * On error fall back to the safe value with both directories as configured… */
1530 STRV_FOREACH_PAIR(bin
, sbin
, STRV_MAKE("/usr/bin", "/usr/sbin",
1531 "/usr/local/bin", "/usr/local/sbin")) {
1532 r
= inode_same(*bin
, *sbin
, AT_NO_AUTOMOUNT
);
1533 if (r
> 0 || r
== -ENOENT
)
1536 log_debug_errno(r
, "Failed to compare \"%s\" and \"%s\", using compat $PATH: %m",
1544 return DEFAULT_PATH_WITH_SBIN
;
1546 return DEFAULT_PATH_WITHOUT_SBIN
;