5509c00
From 7e1db29dde294155be972acf6d3e2db4d27266de Mon Sep 17 00:00:00 2001
5509c00
From: Lennart Poettering <lennart@poettering.net>
5509c00
Date: Tue, 17 Jun 2014 01:37:54 +0200
5509c00
Subject: [PATCH] install: simplify symlink --root= logic
5509c00
5509c00
(cherry picked from commit 278fa5758c8e30f03c8c50f15873d55edfc4cbaf)
5509c00
---
5509c00
 src/shared/install.c | 59 +++++++++++++---------------------------------------
5509c00
 1 file changed, 15 insertions(+), 44 deletions(-)
5509c00
5509c00
diff --git a/src/shared/install.c b/src/shared/install.c
5509c00
index 509ae933..8322970a 100644
5509c00
--- a/src/shared/install.c
5509c00
+++ b/src/shared/install.c
5509c00
@@ -47,9 +47,8 @@ typedef struct {
5509c00
 
5509c00
 #define _cleanup_install_context_done_ _cleanup_(install_context_done)
5509c00
 
5509c00
-static int in_search_path(const char *path, char **search, const char *root_dir) {
5509c00
+static int in_search_path(const char *path, char **search) {
5509c00
         _cleanup_free_ char *parent = NULL;
5509c00
-        char **i;
5509c00
         int r;
5509c00
 
5509c00
         assert(path);
5509c00
@@ -58,24 +57,7 @@ static int in_search_path(const char *path, char **search, const char *root_dir)
5509c00
         if (r < 0)
5509c00
                 return r;
5509c00
 
5509c00
-        STRV_FOREACH(i, search) {
5509c00
-                _cleanup_free_ char *buf = NULL;
5509c00
-                const char *p;
5509c00
-
5509c00
-                if (root_dir) {
5509c00
-                        buf = strjoin(root_dir, "/", *i, NULL);
5509c00
-                        if (!buf)
5509c00
-                                return -ENOMEM;
5509c00
-
5509c00
-                        p = buf;
5509c00
-                } else
5509c00
-                        p = *i;
5509c00
-
5509c00
-                if (path_equal(parent, p))
5509c00
-                        return 1;
5509c00
-        }
5509c00
-
5509c00
-        return 0;
5509c00
+        return strv_contains(search, parent);
5509c00
 }
5509c00
 
5509c00
 static int lookup_paths_init_from_scope(LookupPaths *paths,
5509c00
@@ -777,7 +759,7 @@ int unit_file_link(
5509c00
                         continue;
5509c00
                 }
5509c00
 
5509c00
-                q = in_search_path(*i, paths.unit_path, root_dir);
5509c00
+                q = in_search_path(*i, paths.unit_path);
5509c00
                 if (q < 0)
5509c00
                         return q;
5509c00
 
5509c00
@@ -1021,6 +1003,7 @@ static int unit_file_load(
5509c00
                 InstallContext *c,
5509c00
                 InstallInfo *info,
5509c00
                 const char *path,
5509c00
+                const char *root_dir,
5509c00
                 bool allow_symlink) {
5509c00
 
5509c00
         const ConfigTableItem items[] = {
5509c00
@@ -1032,14 +1015,16 @@ static int unit_file_load(
5509c00
                 { NULL, NULL, NULL, 0, NULL }
5509c00
         };
5509c00
 
5509c00
-        int fd;
5509c00
         _cleanup_fclose_ FILE *f = NULL;
5509c00
-        int r;
5509c00
+        int fd, r;
5509c00
 
5509c00
         assert(c);
5509c00
         assert(info);
5509c00
         assert(path);
5509c00
 
5509c00
+        if (!isempty(root_dir))
5509c00
+                path = strappenda3(root_dir, "/", path);
5509c00
+
5509c00
         fd = open(path, O_RDONLY|O_CLOEXEC|O_NOCTTY|(allow_symlink ? 0 : O_NOFOLLOW));
5509c00
         if (fd < 0)
5509c00
                 return -errno;
5509c00
@@ -1075,30 +1060,19 @@ static int unit_file_search(
5509c00
         assert(info);
5509c00
         assert(paths);
5509c00
 
5509c00
-        if (info->path) {
5509c00
-                const char *path;
5509c00
-
5509c00
-                if (isempty(root_dir))
5509c00
-                        path = info->path;
5509c00
-                else
5509c00
-                        path = strappenda(root_dir, info->path);
5509c00
-
5509c00
-                return unit_file_load(c, info, path, allow_symlink);
5509c00
-        }
5509c00
+        if (info->path)
5509c00
+                return unit_file_load(c, info, info->path, root_dir, allow_symlink);
5509c00
 
5509c00
         assert(info->name);
5509c00
 
5509c00
         STRV_FOREACH(p, paths->unit_path) {
5509c00
                 _cleanup_free_ char *path = NULL;
5509c00
 
5509c00
-                if (isempty(root_dir))
5509c00
-                        path = strjoin(*p, "/", info->name, NULL);
5509c00
-                else
5509c00
-                        path = strjoin(root_dir, "/", *p, "/", info->name, NULL);
5509c00
+                path = strjoin(*p, "/", info->name, NULL);
5509c00
                 if (!path)
5509c00
                         return -ENOMEM;
5509c00
 
5509c00
-                r = unit_file_load(c, info, path, allow_symlink);
5509c00
+                r = unit_file_load(c, info, path, root_dir, allow_symlink);
5509c00
                 if (r >= 0) {
5509c00
                         info->path = path;
5509c00
                         path = NULL;
5509c00
@@ -1123,14 +1097,11 @@ static int unit_file_search(
5509c00
                 STRV_FOREACH(p, paths->unit_path) {
5509c00
                         _cleanup_free_ char *path = NULL;
5509c00
 
5509c00
-                        if (isempty(root_dir))
5509c00
-                                path = strjoin(*p, "/", template, NULL);
5509c00
-                        else
5509c00
-                                path = strjoin(root_dir, "/", *p, "/", template, NULL);
5509c00
+                        path = strjoin(*p, "/", template, NULL);
5509c00
                         if (!path)
5509c00
                                 return -ENOMEM;
5509c00
 
5509c00
-                        r = unit_file_load(c, info, path, allow_symlink);
5509c00
+                        r = unit_file_load(c, info, path, root_dir, allow_symlink);
5509c00
                         if (r >= 0) {
5509c00
                                 info->path = path;
5509c00
                                 path = NULL;
5509c00
@@ -1340,7 +1311,7 @@ static int install_info_symlink_link(
5509c00
         assert(config_path);
5509c00
         assert(i->path);
5509c00
 
5509c00
-        r = in_search_path(i->path, paths->unit_path, root_dir);
5509c00
+        r = in_search_path(i->path, paths->unit_path);
5509c00
         if (r != 0)
5509c00
                 return r;
5509c00