tree-wide: add path_simplify_alloc() and use it

path_simplify_full()/path_simplify() are changed to allow a NULL path, for
which a NULL is returned. Generally, callers have already asserted before that
the argument is nonnull. This way path_simplify_full()/path_simplify() and
path_simplify_alloc() behave consistently.

In sd-device.c, logging in device_set_syspath() is intentionally dropped: other
branches don't log.

In mount-tool.c, logging in parse_argv() is changed to log the user-specified
value, not the simplified string. In an error message, we should show the
actual argument we got, not some transformed version.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2023-09-22 08:13:34 +02:00
parent 7902df1263
commit 660087dc9c
13 changed files with 84 additions and 124 deletions
+6 -15
View File
@@ -947,6 +947,7 @@ int cg_is_empty_recursive(const char *controller, const char *path) {
int cg_split_spec(const char *spec, char **ret_controller, char **ret_path) {
_cleanup_free_ char *controller = NULL, *path = NULL;
int r;
assert(spec);
@@ -955,11 +956,9 @@ int cg_split_spec(const char *spec, char **ret_controller, char **ret_path) {
return -EINVAL;
if (ret_path) {
path = strdup(spec);
if (!path)
return -ENOMEM;
path_simplify(path);
r = path_simplify_alloc(spec, &path);
if (r < 0)
return r;
}
} else {
@@ -1006,22 +1005,14 @@ int cg_split_spec(const char *spec, char **ret_controller, char **ret_path) {
int cg_mangle_path(const char *path, char **result) {
_cleanup_free_ char *c = NULL, *p = NULL;
char *t;
int r;
assert(path);
assert(result);
/* First, check if it already is a filesystem path */
if (path_startswith(path, "/sys/fs/cgroup")) {
t = strdup(path);
if (!t)
return -ENOMEM;
*result = path_simplify(t);
return 0;
}
if (path_startswith(path, "/sys/fs/cgroup"))
return path_simplify_alloc(path, result);
/* Otherwise, treat it as cg spec */
r = cg_split_spec(path, &c, &p);
+8 -14
View File
@@ -1110,6 +1110,7 @@ int search_and_fopen(
char **ret_path) {
_cleanup_strv_free_ char **copy = NULL;
int r;
assert(filename);
assert(mode);
@@ -1123,13 +1124,9 @@ int search_and_fopen(
return -errno;
if (ret_path) {
char *p;
p = strdup(filename);
if (!p)
return -ENOMEM;
*ret_path = path_simplify(p);
r = path_simplify_alloc(filename, ret_path);
if (r < 0)
return r;
}
*ret = TAKE_PTR(f);
@@ -1152,6 +1149,7 @@ int search_and_fopen_nulstr(
char **ret_path) {
_cleanup_strv_free_ char **s = NULL;
int r;
if (path_is_absolute(filename)) {
_cleanup_fclose_ FILE *f = NULL;
@@ -1161,13 +1159,9 @@ int search_and_fopen_nulstr(
return -errno;
if (ret_path) {
char *p;
p = strdup(filename);
if (!p)
return -ENOMEM;
*ret_path = path_simplify(p);
r = path_simplify_alloc(filename, ret_path);
if (r < 0)
return r;
}
*ret = TAKE_PTR(f);
+4 -6
View File
@@ -132,11 +132,9 @@ int path_make_relative(const char *from, const char *to, char **ret) {
return -ENOMEM;
} else {
/* 'to' is inside of 'from'. */
result = strdup(t);
if (!result)
return -ENOMEM;
path_simplify(result);
r = path_simplify_alloc(t, &result);
if (r < 0)
return r;
if (!path_is_valid(result))
return -EINVAL;
@@ -346,7 +344,7 @@ char** path_strv_resolve_uniq(char **l, const char *root) {
char* path_simplify_full(char *path, PathSimplifyFlags flags) {
bool add_slash = false, keep_trailing_slash, absolute, beginning = true;
char *f = ASSERT_PTR(path);
char *f = path;
int r;
/* Removes redundant inner and trailing slashes. Also removes unnecessary dots.
+16
View File
@@ -85,6 +85,22 @@ static inline char* path_simplify(char *path) {
return path_simplify_full(path, 0);
}
static inline int path_simplify_alloc(const char *path, char **ret) {
assert(ret);
if (!path) {
*ret = NULL;
return 0;
}
char *t = strdup(path);
if (!t)
return -ENOMEM;
*ret = path_simplify(t);
return 0;
}
static inline bool path_equal_ptr(const char *a, const char *b) {
return !!a == !!b && (!a || path_equal(a, b));
}
+4 -5
View File
@@ -389,15 +389,14 @@ int unit_name_unescape(const char *f, char **ret) {
int unit_name_path_escape(const char *f, char **ret) {
_cleanup_free_ char *p = NULL;
char *s;
int r;
assert(f);
assert(ret);
p = strdup(f);
if (!p)
return -ENOMEM;
path_simplify(p);
r = path_simplify_alloc(f, &p);
if (r < 0)
return r;
if (empty_or_root(p))
s = strdup("-");
+2 -14
View File
@@ -589,7 +589,6 @@ int getgroups_alloc(gid_t** gids) {
int get_home_dir(char **ret) {
struct passwd *p;
const char *e;
char *h;
uid_t u;
assert(ret);
@@ -622,18 +621,12 @@ int get_home_dir(char **ret) {
return -EINVAL;
found:
h = strdup(e);
if (!h)
return -ENOMEM;
*ret = path_simplify(h);
return 0;
return path_simplify_alloc(e, ret);
}
int get_shell(char **ret) {
struct passwd *p;
const char *e;
char *s;
uid_t u;
assert(ret);
@@ -665,12 +658,7 @@ int get_shell(char **ret) {
return -EINVAL;
found:
s = strdup(e);
if (!s)
return -ENOMEM;
*ret = path_simplify(s);
return 0;
return path_simplify_alloc(e, ret);
}
int reset_uid_gid(void) {