]>
Commit | Line | Data |
---|---|---|
a032b68d | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
663996b3 MS |
2 | #pragma once |
3 | ||
4c89c718 | 4 | #include <alloca.h> |
663996b3 | 5 | #include <stdbool.h> |
4c89c718 | 6 | #include <stddef.h> |
663996b3 MS |
7 | |
8 | #include "macro.h" | |
2897b343 | 9 | #include "string-util.h" |
6e866b33 | 10 | #include "strv.h" |
60f067b4 JS |
11 | #include "time-util.h" |
12 | ||
b012e921 MB |
13 | #define PATH_SPLIT_SBIN_BIN(x) x "sbin:" x "bin" |
14 | #define PATH_SPLIT_SBIN_BIN_NULSTR(x) x "sbin\0" x "bin\0" | |
15 | ||
16 | #define PATH_NORMAL_SBIN_BIN(x) x "bin" | |
17 | #define PATH_NORMAL_SBIN_BIN_NULSTR(x) x "bin\0" | |
18 | ||
98393f85 | 19 | #if HAVE_SPLIT_BIN |
b012e921 MB |
20 | # define PATH_SBIN_BIN(x) PATH_SPLIT_SBIN_BIN(x) |
21 | # define PATH_SBIN_BIN_NULSTR(x) PATH_SPLIT_SBIN_BIN_NULSTR(x) | |
98393f85 | 22 | #else |
b012e921 MB |
23 | # define PATH_SBIN_BIN(x) PATH_NORMAL_SBIN_BIN(x) |
24 | # define PATH_SBIN_BIN_NULSTR(x) PATH_NORMAL_SBIN_BIN_NULSTR(x) | |
98393f85 MB |
25 | #endif |
26 | ||
27 | #define DEFAULT_PATH_NORMAL PATH_SBIN_BIN("/usr/local/") ":" PATH_SBIN_BIN("/usr/") | |
b012e921 | 28 | #define DEFAULT_PATH_NORMAL_NULSTR PATH_SBIN_BIN_NULSTR("/usr/local/") PATH_SBIN_BIN_NULSTR("/usr/") |
98393f85 | 29 | #define DEFAULT_PATH_SPLIT_USR DEFAULT_PATH_NORMAL ":" PATH_SBIN_BIN("/") |
b012e921 MB |
30 | #define DEFAULT_PATH_SPLIT_USR_NULSTR DEFAULT_PATH_NORMAL_NULSTR PATH_SBIN_BIN_NULSTR("/") |
31 | #define DEFAULT_PATH_COMPAT PATH_SPLIT_SBIN_BIN("/usr/local/") ":" PATH_SPLIT_SBIN_BIN("/usr/") ":" PATH_SPLIT_SBIN_BIN("/") | |
663996b3 | 32 | |
f5e65279 | 33 | #if HAVE_SPLIT_USR |
60f067b4 | 34 | # define DEFAULT_PATH DEFAULT_PATH_SPLIT_USR |
b012e921 | 35 | # define DEFAULT_PATH_NULSTR DEFAULT_PATH_SPLIT_USR_NULSTR |
14228c0d | 36 | #else |
60f067b4 | 37 | # define DEFAULT_PATH DEFAULT_PATH_NORMAL |
b012e921 | 38 | # define DEFAULT_PATH_NULSTR DEFAULT_PATH_NORMAL_NULSTR |
14228c0d MB |
39 | #endif |
40 | ||
e1f67bc7 MB |
41 | #ifndef DEFAULT_USER_PATH |
42 | # define DEFAULT_USER_PATH DEFAULT_PATH | |
43 | #endif | |
44 | ||
a032b68d MB |
45 | static inline bool is_path(const char *p) { |
46 | assert(p); | |
47 | return strchr(p, '/'); | |
48 | } | |
49 | ||
50 | static inline bool path_is_absolute(const char *p) { | |
51 | assert(p); | |
52 | return p[0] == '/'; | |
53 | } | |
54 | ||
db2df898 | 55 | int path_split_and_make_absolute(const char *p, char ***ret); |
663996b3 | 56 | char* path_make_absolute(const char *p, const char *prefix); |
1d42b86d | 57 | int safe_getcwd(char **ret); |
db2df898 | 58 | int path_make_absolute_cwd(const char *p, char **ret); |
8b3d4ff0 MB |
59 | int path_make_relative(const char *from, const char *to, char **ret); |
60 | char *path_startswith_full(const char *path, const char *prefix, bool accept_dot_dot) _pure_; | |
61 | static inline char* path_startswith(const char *path, const char *prefix) { | |
62 | return path_startswith_full(path, prefix, true); | |
63 | } | |
e3bff60a | 64 | int path_compare(const char *a, const char *b) _pure_; |
663996b3 | 65 | bool path_equal(const char *a, const char *b) _pure_; |
81c58355 | 66 | bool path_equal_or_files_same(const char *a, const char *b, int flags); |
8b3d4ff0 MB |
67 | /* Compares only the last portion of the input paths, ie: the filenames */ |
68 | bool path_equal_filename(const char *a, const char *b); | |
69 | ||
70 | char* path_extend_internal(char **x, ...); | |
71 | #define path_extend(x, ...) path_extend_internal(x, __VA_ARGS__, POINTER_MAX) | |
72 | #define path_join(...) path_extend_internal(NULL, __VA_ARGS__, POINTER_MAX) | |
6e866b33 | 73 | |
8b3d4ff0 | 74 | char* path_simplify(char *path); |
663996b3 | 75 | |
f2dec872 BR |
76 | enum { |
77 | PATH_CHECK_FATAL = 1 << 0, /* If not set, then error message is appended with 'ignoring'. */ | |
78 | PATH_CHECK_ABSOLUTE = 1 << 1, | |
79 | PATH_CHECK_RELATIVE = 1 << 2, | |
80 | }; | |
81 | ||
82 | int path_simplify_and_warn(char *path, unsigned flag, const char *unit, const char *filename, unsigned line, const char *lvalue); | |
83 | ||
aa27b158 MP |
84 | static inline bool path_equal_ptr(const char *a, const char *b) { |
85 | return !!a == !!b && (!a || path_equal(a, b)); | |
86 | } | |
87 | ||
88 | /* Note: the search terminates on the first NULL item. */ | |
46cdbd49 | 89 | #define PATH_IN_SET(p, ...) path_strv_contains(STRV_MAKE(__VA_ARGS__), p) |
aa27b158 | 90 | |
f2dec872 BR |
91 | char* path_startswith_strv(const char *p, char **set); |
92 | #define PATH_STARTSWITH_SET(p, ...) path_startswith_strv(p, STRV_MAKE(__VA_ARGS__)) | |
2897b343 | 93 | |
db2df898 | 94 | int path_strv_make_absolute_cwd(char **l); |
2897b343 MP |
95 | char** path_strv_resolve(char **l, const char *root); |
96 | char** path_strv_resolve_uniq(char **l, const char *root); | |
663996b3 | 97 | |
3a6ce677 BR |
98 | int find_executable_full(const char *name, bool use_path_envvar, char **ret_filename, int *ret_fd); |
99 | static inline int find_executable(const char *name, char **ret_filename) { | |
100 | return find_executable_full(name, true, ret_filename, NULL); | |
a032b68d | 101 | } |
14228c0d | 102 | |
60f067b4 JS |
103 | bool paths_check_timestamp(const char* const* paths, usec_t *paths_ts_usec, bool update); |
104 | ||
105 | int fsck_exists(const char *fstype); | |
106 | ||
14228c0d MB |
107 | /* Iterates through the path prefixes of the specified path, going up |
108 | * the tree, to root. Also returns "" (and not "/"!) for the root | |
109 | * directory. Excludes the specified directory itself */ | |
6e866b33 MB |
110 | #define PATH_FOREACH_PREFIX(prefix, path) \ |
111 | for (char *_slash = ({ \ | |
8b3d4ff0 | 112 | path_simplify(strcpy(prefix, path)); \ |
6e866b33 MB |
113 | streq(prefix, "/") ? NULL : strrchr(prefix, '/'); \ |
114 | }); \ | |
115 | _slash && ((*_slash = 0), true); \ | |
116 | _slash = strrchr((prefix), '/')) | |
14228c0d MB |
117 | |
118 | /* Same as PATH_FOREACH_PREFIX but also includes the specified path itself */ | |
6e866b33 MB |
119 | #define PATH_FOREACH_PREFIX_MORE(prefix, path) \ |
120 | for (char *_slash = ({ \ | |
8b3d4ff0 | 121 | path_simplify(strcpy(prefix, path)); \ |
6e866b33 MB |
122 | if (streq(prefix, "/")) \ |
123 | prefix[0] = 0; \ | |
124 | strrchr(prefix, 0); \ | |
125 | }); \ | |
126 | _slash && ((*_slash = 0), true); \ | |
127 | _slash = strrchr((prefix), '/')) | |
e3bff60a | 128 | |
f2dec872 BR |
129 | /* Similar to path_join(), but only works for two components, and only the first one may be NULL and returns |
130 | * an alloca() buffer, or possibly a const pointer into the path parameter. */ | |
e3bff60a MP |
131 | #define prefix_roota(root, path) \ |
132 | ({ \ | |
133 | const char* _path = (path), *_root = (root), *_ret; \ | |
134 | char *_p, *_n; \ | |
135 | size_t _l; \ | |
136 | while (_path[0] == '/' && _path[1] == '/') \ | |
137 | _path ++; \ | |
f2dec872 | 138 | if (isempty(_root)) \ |
e3bff60a MP |
139 | _ret = _path; \ |
140 | else { \ | |
141 | _l = strlen(_root) + 1 + strlen(_path) + 1; \ | |
7c20daf6 | 142 | _n = newa(char, _l); \ |
e3bff60a MP |
143 | _p = stpcpy(_n, _root); \ |
144 | while (_p > _n && _p[-1] == '/') \ | |
145 | _p--; \ | |
146 | if (_path[0] != '/') \ | |
147 | *(_p++) = '/'; \ | |
148 | strcpy(_p, _path); \ | |
149 | _ret = _n; \ | |
150 | } \ | |
151 | _ret; \ | |
152 | }) | |
db2df898 | 153 | |
db2df898 | 154 | char* dirname_malloc(const char *path); |
8b3d4ff0 MB |
155 | int path_find_first_component(const char **p, bool accept_dot_dot, const char **ret); |
156 | int path_find_last_component(const char *path, bool accept_dot_dot, const char **next, const char **ret); | |
52ad194e | 157 | const char *last_path_component(const char *path); |
8b3d4ff0 MB |
158 | int path_extract_filename(const char *path, char **ret); |
159 | int path_extract_directory(const char *path, char **ret); | |
db2df898 MP |
160 | |
161 | bool filename_is_valid(const char *p) _pure_; | |
8b3d4ff0 MB |
162 | bool path_is_valid_full(const char *p, bool accept_dot_dot) _pure_; |
163 | static inline bool path_is_valid(const char *p) { | |
164 | return path_is_valid_full(p, true); | |
165 | } | |
166 | static inline bool path_is_safe(const char *p) { | |
167 | return path_is_valid_full(p, false); | |
168 | } | |
52ad194e | 169 | bool path_is_normalized(const char *p) _pure_; |
db2df898 MP |
170 | |
171 | char *file_in_same_dir(const char *path, const char *filename); | |
172 | ||
aa27b158 | 173 | bool hidden_or_backup_file(const char *filename) _pure_; |
db2df898 MP |
174 | |
175 | bool is_device_path(const char *path); | |
b012e921 MB |
176 | |
177 | bool valid_device_node_path(const char *path); | |
178 | bool valid_device_allow_pattern(const char *path); | |
8a584da2 MP |
179 | |
180 | int systemd_installation_has_version(const char *root, unsigned minimal_version); | |
2897b343 MP |
181 | |
182 | bool dot_or_dot_dot(const char *path); | |
f5e65279 MB |
183 | |
184 | static inline const char *skip_dev_prefix(const char *p) { | |
185 | const char *e; | |
186 | ||
187 | /* Drop any /dev prefix if there is any */ | |
188 | ||
189 | e = path_startswith(p, "/dev/"); | |
190 | ||
191 | return e ?: p; | |
192 | } | |
b012e921 | 193 | |
8b3d4ff0 | 194 | bool empty_or_root(const char *path); |
b012e921 MB |
195 | static inline const char *empty_to_root(const char *path) { |
196 | return isempty(path) ? "/" : path; | |
197 | } | |
46cdbd49 BR |
198 | |
199 | bool path_strv_contains(char **l, const char *path); | |
cb695f0e | 200 | bool prefixed_path_strv_contains(char **l, const char *path); |