| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include "basic-forward.h" |
| 5 |
|
| 6 |
#define PATH_SPLIT_BIN(x) x "sbin:" x "bin" |
| 7 |
#define PATH_SPLIT_BIN_NULSTR(x) x "sbin\0" x "bin\0" |
| 8 |
|
| 9 |
#define PATH_MERGED_BIN(x) x "bin" |
| 10 |
#define PATH_MERGED_BIN_NULSTR(x) x "bin\0" |
| 11 |
|
| 12 |
#define DEFAULT_PATH_WITH_FULL_SBIN PATH_SPLIT_BIN("/usr/local/") ":" PATH_SPLIT_BIN("/usr/") |
| 13 |
#define DEFAULT_PATH_WITH_LOCAL_SBIN PATH_SPLIT_BIN("/usr/local/") ":" PATH_MERGED_BIN("/usr/") |
| 14 |
#define DEFAULT_PATH_WITHOUT_SBIN PATH_MERGED_BIN("/usr/local/") ":" PATH_MERGED_BIN("/usr/") |
| 15 |
|
| 16 |
#define DEFAULT_PATH_COMPAT DEFAULT_PATH_WITH_FULL_SBIN ":" PATH_SPLIT_BIN("/") |
| 17 |
|
| 18 |
const char* default_PATH(void); |
| 19 |
|
| 20 |
static inline const char* default_user_PATH(void) { |
| 21 |
#ifdef DEFAULT_USER_PATH |
| 22 |
return DEFAULT_USER_PATH; |
| 23 |
#else |
| 24 |
return default_PATH(); |
| 25 |
#endif |
| 26 |
} |
| 27 |
|
| 28 |
bool is_path(const char *p) _pure_; |
| 29 |
|
| 30 |
static inline bool path_is_absolute(const char *p) { |
| 31 |
if (!p) /* A NULL pointer is definitely not an absolute path */ |
| 32 |
return false; |
| 33 |
|
| 34 |
return p[0] == '/'; |
| 35 |
} |
| 36 |
|
| 37 |
int path_split_and_make_absolute(const char *p, char ***ret); |
| 38 |
char* path_make_absolute(const char *p, const char *prefix); |
| 39 |
int safe_getcwd(char **ret); |
| 40 |
int path_make_absolute_cwd(const char *p, char **ret); |
| 41 |
int path_make_relative(const char *from, const char *to, char **ret); |
| 42 |
int path_make_relative_parent(const char *from_child, const char *to, char **ret); |
| 43 |
|
| 44 |
typedef enum PathStartWithFlags { |
| 45 |
PATH_STARTSWITH_REFUSE_DOT_DOT = 1U << 0, |
| 46 |
PATH_STARTSWITH_RETURN_LEADING_SLASH = 1U << 1, |
| 47 |
} PathStartWithFlags; |
| 48 |
|
| 49 |
char* path_startswith_full(const char *path, const char *prefix, PathStartWithFlags flags) _pure_; |
| 50 |
static inline char* path_startswith(const char *path, const char *prefix) { |
| 51 |
return path_startswith_full(path, prefix, 0); |
| 52 |
} |
| 53 |
|
| 54 |
int path_compare(const char *a, const char *b) _pure_; |
| 55 |
static inline bool path_equal(const char *a, const char *b) { |
| 56 |
return path_compare(a, b) == 0; |
| 57 |
} |
| 58 |
|
| 59 |
int path_compare_filename(const char *a, const char *b); |
| 60 |
static inline bool path_equal_filename(const char *a, const char *b) { |
| 61 |
return path_compare_filename(a, b) == 0; |
| 62 |
} |
| 63 |
|
| 64 |
int path_equal_or_inode_same_full(const char *a, const char *b, int flags); |
| 65 |
static inline bool path_equal_or_inode_same(const char *a, const char *b, int flags) { |
| 66 |
return path_equal_or_inode_same_full(a, b, flags) > 0; |
| 67 |
} |
| 68 |
|
| 69 |
char* path_extend_internal(char **x, ...); |
| 70 |
#define path_extend(x, ...) path_extend_internal(x, __VA_ARGS__, POINTER_MAX) |
| 71 |
#define path_join(...) path_extend_internal(NULL, __VA_ARGS__, POINTER_MAX) |
| 72 |
|
| 73 |
char* skip_leading_slash(const char *p) _pure_; |
| 74 |
|
| 75 |
typedef enum PathSimplifyFlags { |
| 76 |
PATH_SIMPLIFY_KEEP_TRAILING_SLASH = 1 << 0, |
| 77 |
} PathSimplifyFlags; |
| 78 |
|
| 79 |
char* path_simplify_full(char *path, PathSimplifyFlags flags); |
| 80 |
static inline char* path_simplify(char *path) { |
| 81 |
return path_simplify_full(path, 0); |
| 82 |
} |
| 83 |
|
| 84 |
int path_simplify_alloc(const char *path, char **ret); |
| 85 |
|
| 86 |
/* Note: the search terminates on the first NULL item. */ |
| 87 |
#define PATH_IN_SET(p, ...) path_strv_contains(STRV_MAKE(__VA_ARGS__), p) |
| 88 |
|
| 89 |
char* path_startswith_strv(const char *p, char * const *strv); |
| 90 |
#define PATH_STARTSWITH_SET(p, ...) path_startswith_strv(p, STRV_MAKE(__VA_ARGS__)) |
| 91 |
|
| 92 |
int path_strv_make_absolute_cwd(char **l); |
| 93 |
char** path_strv_resolve(char **l, const char *root); |
| 94 |
char** path_strv_resolve_uniq(char **l, const char *root); |
| 95 |
|
| 96 |
int open_and_check_executable(const char *name, const char *root, char **ret_path, int *ret_fd); |
| 97 |
int find_executable_full( |
| 98 |
const char *name, |
| 99 |
const char *root, |
| 100 |
char * const *exec_search_path, |
| 101 |
bool use_path_envvar, |
| 102 |
char **ret_filename, |
| 103 |
int *ret_fd); |
| 104 |
static inline int find_executable(const char *name, char **ret_filename) { |
| 105 |
return find_executable_full(name, /* root= */ NULL, NULL, true, ret_filename, NULL); |
| 106 |
} |
| 107 |
|
| 108 |
int fsck_exists(void); |
| 109 |
int fsck_exists_for_fstype(const char *fstype); |
| 110 |
|
| 111 |
/* Iterates through the path prefixes of the specified path, going up |
| 112 |
* the tree, to root. Also returns "" (and not "/"!) for the root |
| 113 |
* directory. Excludes the specified directory itself */ |
| 114 |
#define PATH_FOREACH_PREFIX(prefix, path) \ |
| 115 |
for (char *_slash = ({ \ |
| 116 |
path_simplify(strcpy(prefix, path)); \ |
| 117 |
streq(prefix, "/") ? NULL : strrchr(prefix, '/'); \ |
| 118 |
}); \ |
| 119 |
_slash && ((*_slash = 0), true); \ |
| 120 |
_slash = strrchr((prefix), '/')) |
| 121 |
|
| 122 |
/* Same as PATH_FOREACH_PREFIX but also includes the specified path itself */ |
| 123 |
#define PATH_FOREACH_PREFIX_MORE(prefix, path) \ |
| 124 |
for (char *_slash = ({ \ |
| 125 |
path_simplify(strcpy(prefix, path)); \ |
| 126 |
if (streq(prefix, "/")) \ |
| 127 |
prefix[0] = 0; \ |
| 128 |
strrchr(prefix, 0); \ |
| 129 |
}); \ |
| 130 |
_slash && ((*_slash = 0), true); \ |
| 131 |
_slash = strrchr((prefix), '/')) |
| 132 |
|
| 133 |
int path_find_first_component(const char **p, bool accept_dot_dot, const char **ret); |
| 134 |
int path_find_last_component(const char *path, bool accept_dot_dot, const char **next, const char **ret); |
| 135 |
const char* last_path_component(const char *path); |
| 136 |
|
| 137 |
int path_split_prefix_filename(const char *path, char **ret_dir, char **ret_filename); |
| 138 |
static inline int path_extract_filename(const char *path, char **ret) { |
| 139 |
return path_split_prefix_filename(path, NULL, ret); |
| 140 |
} |
| 141 |
static inline int path_extract_directory(const char *path, char **ret) { |
| 142 |
int r = path_split_prefix_filename(path, ret, NULL); |
| 143 |
return r < 0 ? r : 0; /* suppress O_DIRECTORY */ |
| 144 |
} |
| 145 |
|
| 146 |
bool filename_part_is_valid(const char *p) _pure_; |
| 147 |
bool filename_is_valid(const char *p) _pure_; |
| 148 |
bool path_is_valid_full(const char *p, bool accept_dot_dot) _pure_; |
| 149 |
static inline bool path_is_valid(const char *p) { |
| 150 |
return path_is_valid_full(p, /* accept_dot_dot= */ true); |
| 151 |
} |
| 152 |
static inline bool path_is_safe(const char *p) { |
| 153 |
return path_is_valid_full(p, /* accept_dot_dot= */ false); |
| 154 |
} |
| 155 |
bool path_is_normalized(const char *p) _pure_; |
| 156 |
static inline bool filename_or_absolute_path_is_valid(const char *p) { |
| 157 |
if (path_is_absolute(p)) |
| 158 |
return path_is_valid(p); |
| 159 |
|
| 160 |
return filename_is_valid(p); |
| 161 |
} |
| 162 |
|
| 163 |
int file_in_same_dir(const char *path, const char *filename, char **ret); |
| 164 |
|
| 165 |
bool hidden_or_backup_file(const char *filename) _pure_; |
| 166 |
|
| 167 |
bool is_device_path(const char *path) _pure_; |
| 168 |
|
| 169 |
bool valid_device_node_path(const char *path) _pure_; |
| 170 |
bool valid_device_allow_pattern(const char *path) _pure_; |
| 171 |
|
| 172 |
bool dot_or_dot_dot(const char *path) _pure_; |
| 173 |
|
| 174 |
bool path_implies_directory(const char *path) _pure_; |
| 175 |
|
| 176 |
static inline const char* skip_dev_prefix(const char *p) { |
| 177 |
const char *e; |
| 178 |
|
| 179 |
/* Drop any /dev prefix if there is any */ |
| 180 |
|
| 181 |
e = path_startswith(p, "/dev/"); |
| 182 |
|
| 183 |
return e ?: p; |
| 184 |
} |
| 185 |
|
| 186 |
bool empty_or_root(const char *path) _pure_; |
| 187 |
const char* empty_to_root(const char *path) _pure_; |
| 188 |
int empty_or_root_harder_to_null(const char **path); |
| 189 |
|
| 190 |
bool path_strv_contains(char * const *l, const char *path); |
| 191 |
bool prefixed_path_strv_contains(char * const *l, const char *path); |
| 192 |
|
| 193 |
int path_glob_can_match(const char *pattern, const char *prefix, char **ret); |