summaryrefslogtreecommitdiff
path: root/src/shared
diff options
context:
space:
mode:
Diffstat (limited to 'src/shared')
-rw-r--r--src/shared/install.c57
-rw-r--r--src/shared/path-util.c16
-rw-r--r--src/shared/path-util.h1
3 files changed, 32 insertions, 42 deletions
diff --git a/src/shared/install.c b/src/shared/install.c
index e957c33344..cc61c01e20 100644
--- a/src/shared/install.c
+++ b/src/shared/install.c
@@ -88,18 +88,10 @@ static int get_config_path(UnitFileScope scope, bool runtime, const char *root_d
case UNIT_FILE_SYSTEM:
- if (root_dir && runtime) {
- if (asprintf(&p, "%s/run/systemd/system", root_dir) < 0)
- return -ENOMEM;
- } else if (runtime)
- 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);
-
+ if (runtime)
+ p = path_join(root_dir, "/run/systemd/system", NULL);
+ else
+ p = path_join(root_dir, SYSTEM_CONFIG_UNIT_PATH, NULL);
break;
case UNIT_FILE_GLOBAL:
@@ -1664,11 +1656,7 @@ int unit_file_get_default(
_cleanup_free_ char *path = NULL, *tmp = NULL;
char *n;
- if (isempty(root_dir))
- path = strappend(*p, "/" SPECIAL_DEFAULT_TARGET);
- else
- path = strjoin(root_dir, "/", *p, "/" SPECIAL_DEFAULT_TARGET, NULL);
-
+ path = path_join(root_dir, *p, SPECIAL_DEFAULT_TARGET);
if (!path)
return -ENOMEM;
@@ -1725,15 +1713,12 @@ UnitFileState unit_file_get_state(
free(path);
path = NULL;
- if (root_dir)
- asprintf(&path, "%s/%s/%s", root_dir, *i, name);
- else
- asprintf(&path, "%s/%s", *i, name);
+ path = path_join(root_dir, *i, name);
if (!path)
return -ENOMEM;
if (root_dir)
- partial = path + strlen(root_dir) + 1;
+ partial = path + strlen(root_dir);
else
partial = path;
@@ -1960,17 +1945,11 @@ int unit_file_preset_all(
STRV_FOREACH(i, paths.unit_path) {
_cleanup_closedir_ DIR *d = NULL;
- _cleanup_free_ char *buf = NULL;
- const char *units_dir;
-
- if (!isempty(root_dir)) {
- buf = strjoin(root_dir, "/", *i, NULL);
- if (!buf)
- return -ENOMEM;
+ _cleanup_free_ char *units_dir;
- units_dir = buf;
- } else
- units_dir = *i;
+ units_dir = path_join(root_dir, *i, NULL);
+ if (!units_dir)
+ return -ENOMEM;
d = opendir(units_dir);
if (!d) {
@@ -2069,17 +2048,11 @@ int unit_file_get_list(
STRV_FOREACH(i, paths.unit_path) {
_cleanup_closedir_ DIR *d = NULL;
- _cleanup_free_ char *buf = NULL;
- const char *units_dir;
+ _cleanup_free_ char *units_dir;
- if (!isempty(root_dir)) {
- buf = strjoin(root_dir, "/", *i, NULL);
- if (!buf)
- return -ENOMEM;
-
- units_dir = buf;
- } else
- units_dir = *i;
+ units_dir = path_join(root_dir, *i, NULL);
+ if (!units_dir)
+ return -ENOMEM;
d = opendir(units_dir);
if (!d) {
diff --git a/src/shared/path-util.c b/src/shared/path-util.c
index 6b13f0254e..5bc5012fe8 100644
--- a/src/shared/path-util.c
+++ b/src/shared/path-util.c
@@ -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) {
union file_handle_union h = {
diff --git a/src/shared/path-util.h b/src/shared/path-util.h
index 976d2b26d0..d85291bb44 100644
--- a/src/shared/path-util.h
+++ b/src/shared/path-util.h
@@ -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_startswith(const char *path, const char *prefix) _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_resolve(char **l, const char *prefix);