Add utility function to append root to path

This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2014-07-26 14:47:31 -04:00
parent 60731f32f1
commit 0c6ea3a4e2
6 changed files with 57 additions and 62 deletions

View file

@ -1614,17 +1614,13 @@ int main(int argc, char *argv[]) {
arg_action == ACTION_LIST_CATALOG || arg_action == ACTION_LIST_CATALOG ||
arg_action == ACTION_DUMP_CATALOG) { arg_action == ACTION_DUMP_CATALOG) {
const char* database = CATALOG_DATABASE; _cleanup_free_ char *database;
_cleanup_free_ char *copy = NULL;
if (arg_root) { database = path_join(arg_root, CATALOG_DATABASE, NULL);
copy = strjoin(arg_root, "/", CATALOG_DATABASE, NULL); if (!database) {
if (!copy) {
r = log_oom(); r = log_oom();
goto finish; goto finish;
} }
path_kill_slashes(copy);
database = copy;
}
if (arg_action == ACTION_UPDATE_CATALOG) { if (arg_action == ACTION_UPDATE_CATALOG) {
r = catalog_update(database, arg_root, catalog_file_dirs); r = catalog_update(database, arg_root, catalog_file_dirs);

View file

@ -88,18 +88,10 @@ static int get_config_path(UnitFileScope scope, bool runtime, const char *root_d
case UNIT_FILE_SYSTEM: case UNIT_FILE_SYSTEM:
if (root_dir && runtime) { if (runtime)
if (asprintf(&p, "%s/run/systemd/system", root_dir) < 0) p = path_join(root_dir, "/run/systemd/system", NULL);
return -ENOMEM; else
} else if (runtime) p = path_join(root_dir, SYSTEM_CONFIG_UNIT_PATH, NULL);
p = strdup("/run/systemd/system");
else if (root_dir) {
if (asprintf(&p, "%s/%s", root_dir,
SYSTEM_CONFIG_UNIT_PATH) < 0)
return -ENOMEM;
} else
p = strdup(SYSTEM_CONFIG_UNIT_PATH);
break; break;
case UNIT_FILE_GLOBAL: case UNIT_FILE_GLOBAL:
@ -1664,11 +1656,7 @@ int unit_file_get_default(
_cleanup_free_ char *path = NULL, *tmp = NULL; _cleanup_free_ char *path = NULL, *tmp = NULL;
char *n; char *n;
if (isempty(root_dir)) path = path_join(root_dir, *p, SPECIAL_DEFAULT_TARGET);
path = strappend(*p, "/" SPECIAL_DEFAULT_TARGET);
else
path = strjoin(root_dir, "/", *p, "/" SPECIAL_DEFAULT_TARGET, NULL);
if (!path) if (!path)
return -ENOMEM; return -ENOMEM;
@ -1725,15 +1713,12 @@ UnitFileState unit_file_get_state(
free(path); free(path);
path = NULL; path = NULL;
if (root_dir) path = path_join(root_dir, *i, name);
asprintf(&path, "%s/%s/%s", root_dir, *i, name);
else
asprintf(&path, "%s/%s", *i, name);
if (!path) if (!path)
return -ENOMEM; return -ENOMEM;
if (root_dir) if (root_dir)
partial = path + strlen(root_dir) + 1; partial = path + strlen(root_dir);
else else
partial = path; partial = path;
@ -1960,18 +1945,12 @@ int unit_file_preset_all(
STRV_FOREACH(i, paths.unit_path) { STRV_FOREACH(i, paths.unit_path) {
_cleanup_closedir_ DIR *d = NULL; _cleanup_closedir_ DIR *d = NULL;
_cleanup_free_ char *buf = NULL; _cleanup_free_ char *units_dir;
const char *units_dir;
if (!isempty(root_dir)) { units_dir = path_join(root_dir, *i, NULL);
buf = strjoin(root_dir, "/", *i, NULL); if (!units_dir)
if (!buf)
return -ENOMEM; return -ENOMEM;
units_dir = buf;
} else
units_dir = *i;
d = opendir(units_dir); d = opendir(units_dir);
if (!d) { if (!d) {
if (errno == ENOENT) if (errno == ENOENT)
@ -2069,18 +2048,12 @@ int unit_file_get_list(
STRV_FOREACH(i, paths.unit_path) { STRV_FOREACH(i, paths.unit_path) {
_cleanup_closedir_ DIR *d = NULL; _cleanup_closedir_ DIR *d = NULL;
_cleanup_free_ char *buf = NULL; _cleanup_free_ char *units_dir;
const char *units_dir;
if (!isempty(root_dir)) { units_dir = path_join(root_dir, *i, NULL);
buf = strjoin(root_dir, "/", *i, NULL); if (!units_dir)
if (!buf)
return -ENOMEM; return -ENOMEM;
units_dir = buf;
} else
units_dir = *i;
d = opendir(units_dir); d = opendir(units_dir);
if (!d) { if (!d) {
if (errno == ENOENT) if (errno == ENOENT)

View file

@ -435,6 +435,22 @@ bool path_equal(const char *a, const char *b) {
} }
} }
char* path_join(const char *root, const char *path, const char *rest) {
assert(path);
if (!isempty(root))
return strjoin(root, "/",
path[0] == '/' ? path+1 : path,
rest ? "/" : NULL,
rest && rest[0] == '/' ? rest+1 : rest,
NULL);
else
return strjoin(path,
rest ? "/" : NULL,
rest && rest[0] == '/' ? rest+1 : rest,
NULL);
}
int path_is_mount_point(const char *t, bool allow_symlink) { int path_is_mount_point(const char *t, bool allow_symlink) {
union file_handle_union h = { union file_handle_union h = {

View file

@ -45,6 +45,7 @@ int path_make_relative(const char *from_dir, const char *to_path, char **_r);
char* path_kill_slashes(char *path); char* path_kill_slashes(char *path);
char* path_startswith(const char *path, const char *prefix) _pure_; char* path_startswith(const char *path, const char *prefix) _pure_;
bool path_equal(const char *a, const char *b) _pure_; bool path_equal(const char *a, const char *b) _pure_;
char* path_join(const char *root, const char *path, const char *rest);
char** path_strv_make_absolute_cwd(char **l); char** path_strv_make_absolute_cwd(char **l);
char** path_strv_resolve(char **l, const char *prefix); char** path_strv_resolve(char **l, const char *prefix);

View file

@ -4998,11 +4998,8 @@ static int enable_sysv_units(const char *verb, char **args) {
STRV_FOREACH(k, paths.unit_path) { STRV_FOREACH(k, paths.unit_path) {
_cleanup_free_ char *path = NULL; _cleanup_free_ char *path = NULL;
if (!isempty(arg_root)) path = path_join(arg_root, *k, name);
j = asprintf(&path, "%s/%s/%s", arg_root, *k, name); if (!path)
else
j = asprintf(&path, "%s/%s", *k, name);
if (j < 0)
return log_oom(); return log_oom();
found_native = access(path, F_OK) >= 0; found_native = access(path, F_OK) >= 0;
@ -5013,11 +5010,8 @@ static int enable_sysv_units(const char *verb, char **args) {
if (found_native) if (found_native)
continue; continue;
if (!isempty(arg_root)) p = path_join(arg_root, SYSTEM_SYSVINIT_PATH, name);
j = asprintf(&p, "%s/" SYSTEM_SYSVINIT_PATH "/%s", arg_root, name); if (!p)
else
j = asprintf(&p, SYSTEM_SYSVINIT_PATH "/%s", name);
if (j < 0)
return log_oom(); return log_oom();
p[strlen(p) - strlen(".service")] = 0; p[strlen(p) - strlen(".service")] = 0;

View file

@ -162,6 +162,20 @@ static void test_prefixes(void) {
} }
} }
static void test_path_join(void) {
assert_se(streq(path_join("/root", "/a/b", "/c"), "/root/a/b/c"));
assert_se(streq(path_join("/root", "a/b", "c"), "/root/a/b/c"));
assert_se(streq(path_join("/root", "/a/b", "c"), "/root/a/b/c"));
assert_se(streq(path_join("/root", "/", "c"), "/root//c"));
assert_se(streq(path_join("/root", "/", NULL), "/root/"));
assert_se(streq(path_join(NULL, "/a/b", "/c"), "/a/b/c"));
assert_se(streq(path_join(NULL, "a/b", "c"), "a/b/c"));
assert_se(streq(path_join(NULL, "/a/b", "c"), "/a/b/c"));
assert_se(streq(path_join(NULL, "/", "c"), "//c"));
assert_se(streq(path_join(NULL, "/", NULL), "/"));
}
static void test_fsck_exists(void) { static void test_fsck_exists(void) {
/* Ensure we use a sane default for PATH. */ /* Ensure we use a sane default for PATH. */
unsetenv("PATH"); unsetenv("PATH");
@ -225,6 +239,7 @@ int main(int argc, char **argv) {
test_path(); test_path();
test_find_binary(argv[0]); test_find_binary(argv[0]);
test_prefixes(); test_prefixes();
test_path_join();
test_fsck_exists(); test_fsck_exists();
test_make_relative(); test_make_relative();
test_strv_resolve(); test_strv_resolve();