int cg_split_spec(const char *spec, char **ret_controller, char **ret_path) {
_cleanup_free_ char *controller = NULL, *path = NULL;
+ int r;
assert(spec);
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 {
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);
char **ret_path) {
_cleanup_strv_free_ char **copy = NULL;
+ int r;
assert(filename);
assert(mode);
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);
char **ret_path) {
_cleanup_strv_free_ char **s = NULL;
+ int r;
if (path_is_absolute(filename)) {
_cleanup_fclose_ FILE *f = NULL;
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);
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;
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.
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));
}
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("-");
int get_home_dir(char **ret) {
struct passwd *p;
const char *e;
- char *h;
uid_t u;
assert(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);
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) {
if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
_cleanup_free_ char *k = NULL;
- k = strdup(path);
- if (!k)
- return -ENOMEM;
-
- path_simplify(k);
+ r = path_simplify_alloc(path, &k);
+ if (r < 0)
+ return r;
PathSpec *s = new(PathSpec, 1);
if (!s)
if (!path_is_absolute(a) || !path_is_valid(a))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Invalid socket path: %s", a);
- p->path = strdup(a);
- if (!p->path)
- return log_oom();
-
- path_simplify(p->path);
+ r = path_simplify_alloc(a, &p->path);
+ if (r < 0)
+ return r;
} else if (streq(t, "Netlink")) {
r = socket_address_parse_netlink(&p->address, a);
"sd-device: Syspath '%s' is not a subdirectory of /sys",
_syspath);
- syspath = strdup(_syspath);
- if (!syspath)
- return log_oom_debug();
-
- path_simplify(syspath);
+ r = path_simplify_alloc(_syspath, &syspath);
+ if (r < 0)
+ return r;
}
assert_se(devpath = startswith(syspath, "/sys"));
STATIC_DESTRUCTOR_REGISTER(arg_automount_property, strv_freep);
static int parse_where(const char *input, char **ret_where) {
- _cleanup_free_ char *where = NULL;
int r;
assert(input);
assert(ret_where);
if (arg_transport == BUS_TRANSPORT_LOCAL) {
- r = chase(input, NULL, CHASE_NONEXISTENT, &where, NULL);
+ r = chase(input, NULL, CHASE_NONEXISTENT, ret_where, NULL);
if (r < 0)
return log_error_errno(r, "Failed to make path %s absolute: %m", input);
} else {
- where = strdup(input);
- if (!where)
- return log_oom();
-
- path_simplify(where);
-
- if (!path_is_absolute(where))
+ if (!path_is_absolute(input))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Path must be absolute when operating remotely: %s",
- where);
+ input);
+
+ r = path_simplify_alloc(input, ret_where);
+ if (r < 0)
+ return log_error_errno(r, "Failed to simplify path %s: %m", input);
}
- *ret_where = TAKE_PTR(where);
return 0;
}
r = chase(u, NULL, 0, &arg_mount_what, NULL);
if (r < 0)
return log_error_errno(r, "Failed to make path %s absolute: %m", u);
- } else {
- arg_mount_what = strdup(argv[optind]);
- if (!arg_mount_what)
- return log_oom();
- path_simplify(arg_mount_what);
-
- if (!path_is_absolute(arg_mount_what))
+ } else {
+ if (!path_is_absolute(argv[optind]))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Path must be absolute when operating remotely: %s",
- arg_mount_what);
+ argv[optind]);
+
+ r = path_simplify_alloc(argv[optind], &arg_mount_what);
+ if (r < 0)
+ return log_error_errno(r, "Failed to simplify path: %m");
}
}
for (int i = optind; i < argc; i++) {
_cleanup_free_ char *p = NULL;
- p = strdup(argv[i]);
- if (!p)
- return log_oom();
-
- path_simplify(p);
+ r = path_simplify_alloc(argv[i], &p);
+ if (r < 0)
+ return r;
r = stop_mounts(bus, p);
if (r < 0)
_cleanup_free_ char *p = NULL, *s = NULL;
PortableChange *c;
+ int r;
assert(path);
assert(!changes == !n_changes);
return -ENOMEM;
*changes = c;
- p = strdup(path);
- if (!p)
- return -ENOMEM;
-
- path_simplify(p);
-
- if (source) {
- s = strdup(source);
- if (!s)
- return -ENOMEM;
+ r = path_simplify_alloc(path, &p);
+ if (r < 0)
+ return r;
- path_simplify(s);
- }
+ r = path_simplify_alloc(source, &s);
+ if (r < 0)
+ return r;
c[(*n_changes)++] = (PortableChange) {
.type_or_errno = type_or_errno,
_cleanup_free_ char *p = NULL, *s = NULL;
InstallChange *c;
+ int r;
assert(!changes == !n_changes);
assert(INSTALL_CHANGE_TYPE_VALID(type));
return -ENOMEM;
*changes = c;
- if (path) {
- p = strdup(path);
- if (!p)
- return -ENOMEM;
-
- path_simplify(p);
- }
-
- if (source) {
- s = strdup(source);
- if (!s)
- return -ENOMEM;
+ r = path_simplify_alloc(path, &p);
+ if (r < 0)
+ return r;
- path_simplify(s);
- }
+ r = path_simplify_alloc(source, &s);
+ if (r < 0)
+ return r;
c[(*n_changes)++] = (InstallChange) {
.type = type,
_cleanup_free_ char *s = NULL, *dirname = NULL;
char name_enc[NAME_MAX+1];
const char *name;
+ int r;
assert(slink);
assert(ret);
- s = strdup(slink);
- if (!s)
- return -ENOMEM;
-
- path_simplify(s);
+ r = path_simplify_alloc(slink, &s);
+ if (r < 0)
+ return r;
if (!path_is_normalized(s))
return -EINVAL;