anitazha / rpms / systemd

Forked from rpms/systemd 3 years ago
Clone
9fc0f6
From 6dadf44d7062c38c3f4f782da3cd88114dceb959 Mon Sep 17 00:00:00 2001
9fc0f6
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
9fc0f6
Date: Mon, 21 Apr 2014 19:17:40 -0400
9fc0f6
Subject: [PATCH] shared/install: do not prefix created symlink with root path
9fc0f6
9fc0f6
Before: /var/tmp/inst1//etc/systemd/system/default.target -> /var/tmp/inst1//usr/lib/systemd/system/graphical.target
9fc0f6
After: /var/tmp/inst1/etc/systemd/system/default.target -> /usr/lib/systemd/system/graphical.target
9fc0f6
9fc0f6
(cherry picked from commit 62b002337727093c21d020c730bd65971f7783a7)
9fc0f6
9fc0f6
Related: #1111199
9fc0f6
---
9fc0f6
 src/shared/install.c | 89 +++++++++++++++++++++++++---------------------------
9fc0f6
 1 file changed, 43 insertions(+), 46 deletions(-)
9fc0f6
9fc0f6
diff --git a/src/shared/install.c b/src/shared/install.c
9fc0f6
index 7dad66d..955a9bc 100644
9fc0f6
--- a/src/shared/install.c
9fc0f6
+++ b/src/shared/install.c
9fc0f6
@@ -1073,67 +1073,64 @@ static int unit_file_search(
9fc0f6
         assert(info);
9fc0f6
         assert(paths);
9fc0f6
 
9fc0f6
-        if (info->path)
9fc0f6
-                return unit_file_load(c, info, info->path, allow_symlink);
9fc0f6
+        if (info->path) {
9fc0f6
+                char *full_path = NULL;
9fc0f6
+
9fc0f6
+                if (!isempty(root_dir))
9fc0f6
+                        full_path = strappenda(root_dir, info->path);
9fc0f6
+
9fc0f6
+                return unit_file_load(c, info, full_path ?: info->path, allow_symlink);
9fc0f6
+        }
9fc0f6
 
9fc0f6
         assert(info->name);
9fc0f6
 
9fc0f6
         STRV_FOREACH(p, paths->unit_path) {
9fc0f6
-                char *path = NULL;
9fc0f6
-
9fc0f6
-                if (isempty(root_dir))
9fc0f6
-                        asprintf(&path, "%s/%s", *p, info->name);
9fc0f6
-                else
9fc0f6
-                        asprintf(&path, "%s/%s/%s", root_dir, *p, info->name);
9fc0f6
+                _cleanup_free_ char *path = NULL, *full_path = NULL;
9fc0f6
 
9fc0f6
+                path = strjoin(*p, "/", info->name, NULL);
9fc0f6
                 if (!path)
9fc0f6
                         return -ENOMEM;
9fc0f6
 
9fc0f6
-                r = unit_file_load(c, info, path, allow_symlink);
9fc0f6
+                if (!isempty(root_dir)) {
9fc0f6
+                        full_path = strappend(root_dir, path);
9fc0f6
+                        if (!full_path)
9fc0f6
+                                return -ENOMEM;
9fc0f6
+                }
9fc0f6
 
9fc0f6
-                if (r >= 0)
9fc0f6
+                r = unit_file_load(c, info, full_path ?: path, allow_symlink);
9fc0f6
+                if (r >= 0) {
9fc0f6
                         info->path = path;
9fc0f6
-                else {
9fc0f6
-                        if (r == -ENOENT && unit_name_is_instance(info->name)) {
9fc0f6
-                                /* Unit file doesn't exist, however instance enablement was requested.
9fc0f6
-                                 * We will check if it is possible to load template unit file. */
9fc0f6
-                                char *template = NULL,
9fc0f6
-                                     *template_path = NULL,
9fc0f6
-                                     *template_dir = NULL;
9fc0f6
-
9fc0f6
-                                template = unit_name_template(info->name);
9fc0f6
-                                if (!template) {
9fc0f6
-                                        free(path);
9fc0f6
-                                        return -ENOMEM;
9fc0f6
-                                }
9fc0f6
+                        path = NULL;
9fc0f6
+                } else if (r == -ENOENT && unit_name_is_instance(info->name)) {
9fc0f6
+                        /* Unit file doesn't exist, however instance enablement was requested.
9fc0f6
+                         * We will check if it is possible to load template unit file. */
9fc0f6
+                        _cleanup_free_ char *template = NULL, *template_dir = NULL;
9fc0f6
+
9fc0f6
+                        template = unit_name_template(info->name);
9fc0f6
+                        if (!template)
9fc0f6
+                                return -ENOMEM;
9fc0f6
 
9fc0f6
-                                /* We will reuse path variable since we don't need it anymore. */
9fc0f6
-                                template_dir = path;
9fc0f6
-                                *(strrchr(path, '/') + 1) = '\0';
9fc0f6
+                        /* We will reuse path variable since we don't need it anymore. */
9fc0f6
+                        template_dir = path;
9fc0f6
+                        *(strrchr(template_dir, '/') + 1) = '\0';
9fc0f6
 
9fc0f6
-                                template_path = strjoin(template_dir, template, NULL);
9fc0f6
-                                if (!template_path) {
9fc0f6
-                                        free(path);
9fc0f6
-                                        free(template);
9fc0f6
-                                        return -ENOMEM;
9fc0f6
-                                }
9fc0f6
+                        path = strappend(template_dir, template);
9fc0f6
+                        if (!path)
9fc0f6
+                                return -ENOMEM;
9fc0f6
 
9fc0f6
-                                /* Let's try to load template unit. */
9fc0f6
-                                r = unit_file_load(c, info, template_path, allow_symlink);
9fc0f6
-                                if (r >= 0) {
9fc0f6
-                                        info->path = strdup(template_path);
9fc0f6
-                                        if (!info->path) {
9fc0f6
-                                                free(path);
9fc0f6
-                                                free(template);
9fc0f6
-                                                free(template_path);
9fc0f6
-                                                return -ENOMEM;
9fc0f6
-                                        }
9fc0f6
-                                }
9fc0f6
+                        if (!isempty(root_dir)) {
9fc0f6
+                                free(full_path);
9fc0f6
+                                full_path = strappend(root_dir, path);
9fc0f6
+                                if (!full_path)
9fc0f6
+                                        return -ENOMEM;
9fc0f6
+                        }
9fc0f6
 
9fc0f6
-                                free(template);
9fc0f6
-                                free(template_path);
9fc0f6
+                        /* Let's try to load template unit. */
9fc0f6
+                        r = unit_file_load(c, info, full_path ?: path, allow_symlink);
9fc0f6
+                        if (r >= 0) {
9fc0f6
+                                info->path = path;
9fc0f6
+                                path = NULL;
9fc0f6
                         }
9fc0f6
-                        free(path);
9fc0f6
                 }
9fc0f6
 
9fc0f6
                 if (r != -ENOENT && r != -ELOOP)