_cleanup_free_ char *rdir = NULL;
char *p1, *p2;
- rdir = prefix_root(root, *dir);
+ rdir = path_join(root, *dir);
if (!rdir)
return -ENOMEM;
if (p2) {
/* Our new entry has higher priority */
- t = prefix_root(root, path);
+ t = path_join(root, path);
if (!t)
return log_oom();
}
/* The new file has lower priority than all the existing entries */
- t = prefix_root(root, path);
+ t = path_join(root, path);
if (!t)
return -ENOMEM;
if (r < 0)
return log_error_errno(r, "Failed to extend config file list: %m");
- p = prefix_root(root, replacement);
+ p = path_join(root, replacement);
if (!p)
return log_oom();
}
if (root) {
orig = *s;
- t = prefix_root(root, orig);
+ t = path_join(root, orig);
if (!t) {
enomem = true;
continue;
return binary_is_good(mkfs);
}
-char *prefix_root(const char *root, const char *path) {
- char *n, *p;
- size_t l;
-
- /* If root is passed, prefixes path with it. Otherwise returns
- * it as is. */
-
- assert(path);
-
- /* First, drop duplicate prefixing slashes from the path */
- while (path[0] == '/' && path[1] == '/')
- path++;
-
- if (empty_or_root(root))
- return strdup(path);
-
- l = strlen(root) + 1 + strlen(path) + 1;
-
- n = new(char, l);
- if (!n)
- return NULL;
-
- p = stpcpy(n, root);
-
- while (p > n && p[-1] == '/')
- p--;
-
- if (path[0] != '/')
- *(p++) = '/';
-
- strcpy(p, path);
- return n;
-}
-
int parse_path_argument_and_warn(const char *path, bool suppress_root, char **arg) {
char *p;
int r;
_cleanup_free_ char *path = NULL;
char *c, **name;
- path = prefix_root(root, pattern);
+ path = path_join(root, pattern);
if (!path)
return -ENOMEM;
_slash && ((*_slash = 0), true); \
_slash = strrchr((prefix), '/'))
-char *prefix_root(const char *root, const char *path);
-
-/* Similar to prefix_root(), but returns an alloca() buffer, or
- * possibly a const pointer into the path parameter */
+/* Similar to path_join(), but only works for two components, and only the first one may be NULL and returns
+ * an alloca() buffer, or possibly a const pointer into the path parameter */
#define prefix_roota(root, path) \
({ \
const char* _path = (path), *_root = (root), *_ret; \
if (m[i].has_prefix)
continue;
- s = prefix_root(root_directory, mount_entry_path(m+i));
+ s = path_join(root_directory, mount_entry_path(m+i));
if (!s)
return -ENOMEM;
if (r < 0)
return log_error_errno(r, "Failed to generate keydev mount unit: %m");
- p = prefix_root(keydev_mount, password_escaped);
+ p = path_join(keydev_mount, password_escaped);
if (!p)
return log_oom();
if (streq(controller, tok))
break;
- target = prefix_root("/sys/fs/cgroup/", tok);
+ target = path_join("/sys/fs/cgroup/", tok);
if (!target)
return log_oom();
if (!controller)
break;
- origin = prefix_root("/sys/fs/cgroup/", controller);
+ origin = path_join("/sys/fs/cgroup/", controller);
if (!origin)
return log_oom();
else {
_cleanup_free_ char *target = NULL;
- target = prefix_root(dest, origin);
+ target = path_join(dest, origin);
if (!target)
return log_oom();
return NULL;
if (source[0] == '+')
- return prefix_root(dest, source + 1);
+ return path_join(dest, source + 1);
return strdup(source);
}
FOREACH_STRING(x, "block", "bus", "class", "dev", "devices", "kernel") {
_cleanup_free_ char *from = NULL, *to = NULL;
- from = prefix_root(full, x);
+ from = path_join(full, x);
if (!from)
return log_oom();
- to = prefix_root(top, x);
+ to = path_join(top, x);
if (!to)
return log_oom();
* Requires all file systems at directory and below to be mounted
* MS_PRIVATE or MS_SLAVE so they can be moved.
*/
- directory_pivot_root_new = prefix_root(directory, pivot_root_new);
+ directory_pivot_root_new = path_join(directory, pivot_root_new);
+ if (!directory_pivot_root_new)
+ return log_oom();
/* Remount directory_pivot_root_new to make it movable. */
r = mount_verbose(LOG_ERR, directory_pivot_root_new, directory_pivot_root_new, NULL, MS_BIND, NULL);
}
remove_pivot_tmp = true;
- pivot_tmp_pivot_root_old = prefix_root(pivot_tmp, pivot_root_old);
+ pivot_tmp_pivot_root_old = path_join(pivot_tmp, pivot_root_old);
+ if (!pivot_tmp_pivot_root_old) {
+ r = log_oom();
+ goto done;
+ }
r = mount_verbose(LOG_ERR, directory_pivot_root_new, pivot_tmp, NULL, MS_MOVE, NULL);
if (r < 0)
_cleanup_free_ char *from = NULL, *to = NULL;
struct stat st;
- from = strappend("/dev/", d);
+ from = path_join("/dev/", d);
if (!from)
return log_oom();
- to = prefix_root(dest, from);
+ to = path_join(dest, from);
if (!to)
return log_oom();
if (asprintf(&sl, "%s/%u:%u", dn, major(st.st_rdev), minor(st.st_rdev)) < 0)
return log_oom();
- prefixed = prefix_root(dest, sl);
+ prefixed = path_join(dest, sl);
if (!prefixed)
return log_oom();
_cleanup_free_ char *path = NULL;
DeviceNode *n = arg_extra_nodes + i;
- path = prefix_root(dest, n->path);
+ path = path_join(dest, n->path);
if (!path)
return log_oom();
}
if (prefix) {
- link_name = prefix_root(prefix, k);
+ link_name = path_join(prefix, k);
if (!link_name)
return -ENOMEM;
for (i = 0; i < ELEMENTSOF(table); i++) {
_cleanup_free_ char *path = NULL;
- path = prefix_root(root, table[i].name);
+ path = path_join(root, table[i].name);
if (!path)
return log_oom();
if (!path_is_absolute(dest)) {
char *x;
- x = prefix_root(root_dir, dest);
+ x = path_join(root_dir, dest);
if (!x)
return -ENOMEM;
if (path_is_absolute(target))
/* This is an absolute path, prefix the root so that we always deal with fully qualified paths */
- info->symlink_target = prefix_root(root_dir, target);
+ info->symlink_target = path_join(root_dir, target);
else
/* This is a relative path, take it relative to the dir the symlink is located in. */
info->symlink_target = file_in_same_dir(path, target);
if (!unit_name_is_valid(fn, UNIT_NAME_ANY))
return -EINVAL;
- full = prefix_root(paths.root_dir, *i);
+ full = path_join(paths.root_dir, *i);
if (!full)
return -ENOMEM;
if (!*p)
return 0;
- c = prefix_root(root_dir, *p);
+ c = path_join(root_dir, *p);
if (!c)
return -ENOMEM;
- free(*p);
- *p = c;
-
+ free_and_replace(*p, c);
return 0;
}
char **dirs = STRV_MAKE("/dir1", "/dir2", "/dir3");
_cleanup_free_ const char
- *foo1 = prefix_root(root, "/dir1/foo.conf"),
- *foo2 = prefix_root(root, "/dir2/foo.conf"),
- *bar2 = prefix_root(root, "/dir2/bar.conf"),
- *zzz3 = prefix_root(root, "/dir3/zzz.conf"),
- *whatever = prefix_root(root, "/whatever.conf");
+ *foo1 = path_join(root, "/dir1/foo.conf"),
+ *foo2 = path_join(root, "/dir2/foo.conf"),
+ *bar2 = path_join(root, "/dir2/bar.conf"),
+ *zzz3 = path_join(root, "/dir3/zzz.conf"),
+ *whatever = path_join(root, "/whatever.conf");
assert_se(conf_files_insert(&s, root, dirs, "/dir2/foo.conf") == 0);
assert_se(strv_equal(s, STRV_MAKE(foo2)));
_cleanup_free_ char *s = NULL;
const char *t;
- assert_se(s = prefix_root(r, p));
- assert_se(streq_ptr(s, expected));
+ assert_se(s = path_join(r, p));
+ assert_se(path_equal_ptr(s, expected));
t = prefix_roota(r, p);
assert_se(t);
- assert_se(streq_ptr(t, expected));
+ assert_se(path_equal_ptr(t, expected));
}
static void test_prefix_root(void) {
if (arg_root) {
char *p;
- p = prefix_root(arg_root, i.path);
+ p = path_join(arg_root, i.path);
if (!p)
return log_oom();