chiark / gitweb /
Add utility function to append root to path
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sat, 26 Jul 2014 18:47:31 +0000 (14:47 -0400)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sat, 26 Jul 2014 19:08:42 +0000 (15:08 -0400)
src/journal/journalctl.c
src/shared/install.c
src/shared/path-util.c
src/shared/path-util.h
src/systemctl/systemctl.c
src/test/test-path-util.c

index 5a59a3a..78495f8 100644 (file)
@@ -1614,16 +1614,12 @@ int main(int argc, char *argv[]) {
             arg_action == ACTION_LIST_CATALOG ||
             arg_action == ACTION_DUMP_CATALOG) {
 
             arg_action == ACTION_LIST_CATALOG ||
             arg_action == ACTION_DUMP_CATALOG) {
 
-                const char* database = CATALOG_DATABASE;
-                _cleanup_free_ char *copy = NULL;
-                if (arg_root) {
-                        copy = strjoin(arg_root, "/", CATALOG_DATABASE, NULL);
-                        if (!copy) {
-                                r = log_oom();
-                                goto finish;
-                        }
-                        path_kill_slashes(copy);
-                        database = copy;
+                _cleanup_free_ char *database;
+
+                database = path_join(arg_root, CATALOG_DATABASE, NULL);
+                if (!database) {
+                        r = log_oom();
+                        goto finish;
                 }
 
                 if (arg_action == ACTION_UPDATE_CATALOG) {
                 }
 
                 if (arg_action == ACTION_UPDATE_CATALOG) {
index e957c33..cc61c01 100644 (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 (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:
                 break;
 
         case UNIT_FILE_GLOBAL:
@@ -1664,11 +1656,7 @@ int unit_file_get_default(
                 _cleanup_free_ char *path = NULL, *tmp = NULL;
                 char *n;
 
                 _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;
 
                 if (!path)
                         return -ENOMEM;
 
@@ -1725,15 +1713,12 @@ UnitFileState unit_file_get_state(
                 free(path);
                 path = NULL;
 
                 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)
                 if (!path)
                         return -ENOMEM;
 
                 if (root_dir)
-                        partial = path + strlen(root_dir) + 1;
+                        partial = path + strlen(root_dir);
                 else
                         partial = path;
 
                 else
                         partial = path;
 
@@ -1960,17 +1945,11 @@ int unit_file_preset_all(
 
         STRV_FOREACH(i, paths.unit_path) {
                 _cleanup_closedir_ DIR *d = NULL;
 
         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) {
 
                 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;
 
         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) {
 
                 d = opendir(units_dir);
                 if (!d) {
index 6b13f02..5bc5012 100644 (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) {
 
         union file_handle_union h = {
 int path_is_mount_point(const char *t, bool allow_symlink) {
 
         union file_handle_union h = {
index 976d2b2..d85291b 100644 (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_startswith(const char *path, const char *prefix) _pure_;
 bool path_equal(const char *a, const char *b) _pure_;
 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);
 
 char** path_strv_make_absolute_cwd(char **l);
 char** path_strv_resolve(char **l, const char *prefix);
index 0631190..599d590 100644 (file)
@@ -4998,11 +4998,8 @@ static int enable_sysv_units(const char *verb, char **args) {
                 STRV_FOREACH(k, paths.unit_path) {
                         _cleanup_free_ char *path = NULL;
 
                 STRV_FOREACH(k, paths.unit_path) {
                         _cleanup_free_ char *path = NULL;
 
-                        if (!isempty(arg_root))
-                                j = asprintf(&path, "%s/%s/%s", arg_root, *k, name);
-                        else
-                                j = asprintf(&path, "%s/%s", *k, name);
-                        if (j < 0)
+                        path = path_join(arg_root, *k, name);
+                        if (!path)
                                 return log_oom();
 
                         found_native = access(path, F_OK) >= 0;
                                 return log_oom();
 
                         found_native = access(path, F_OK) >= 0;
@@ -5013,11 +5010,8 @@ static int enable_sysv_units(const char *verb, char **args) {
                 if (found_native)
                         continue;
 
                 if (found_native)
                         continue;
 
-                if (!isempty(arg_root))
-                        j = asprintf(&p, "%s/" SYSTEM_SYSVINIT_PATH "/%s", arg_root, name);
-                else
-                        j = asprintf(&p, SYSTEM_SYSVINIT_PATH "/%s", name);
-                if (j < 0)
+                p = path_join(arg_root, SYSTEM_SYSVINIT_PATH, name);
+                if (!p)
                         return log_oom();
 
                 p[strlen(p) - strlen(".service")] = 0;
                         return log_oom();
 
                 p[strlen(p) - strlen(".service")] = 0;
index 19462c3..c8dcd85 100644 (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) {
         /* Ensure we use a sane default for PATH. */
         unsetenv("PATH");
 static void test_fsck_exists(void) {
         /* Ensure we use a sane default for PATH. */
         unsetenv("PATH");
@@ -225,6 +239,7 @@ int main(int argc, char **argv) {
         test_path();
         test_find_binary(argv[0]);
         test_prefixes();
         test_path();
         test_find_binary(argv[0]);
         test_prefixes();
+        test_path_join();
         test_fsck_exists();
         test_make_relative();
         test_strv_resolve();
         test_fsck_exists();
         test_make_relative();
         test_strv_resolve();