From: Lennart Poettering <lennart@poettering.net>
Date: Tue, 17 Jun 2014 00:53:49 +0200
Subject: install: make sure that --root= mode doesn't make us consider all
 units outside of search path

(cherry picked from commit 8f294b45cbb627d31342f6a79444be59ce7e2274)
(cherry picked from commit 9139b554df671481a7e03e928d661420bffb8774)
---
 src/shared/install.c | 41 +++++++++++++++++++++++++++++++++++++----
 src/shared/util.c    | 16 ----------------
 src/shared/util.h    |  1 -
 3 files changed, 37 insertions(+), 21 deletions(-)

diff --git a/src/shared/install.c b/src/shared/install.c
index e772c45..5d93a2e 100644
--- a/src/shared/install.c
+++ b/src/shared/install.c
@@ -47,6 +47,37 @@ typedef struct {
 #define _cleanup_lookup_paths_free_ _cleanup_(lookup_paths_free)
 #define _cleanup_install_context_done_ _cleanup_(install_context_done)
 
+static int in_search_path(const char *path, char **search, const char *root_dir) {
+        _cleanup_free_ char *parent = NULL;
+        char **i;
+        int r;
+
+        assert(path);
+
+        r = path_get_parent(path, &parent);
+        if (r < 0)
+                return r;
+
+        STRV_FOREACH(i, search) {
+                _cleanup_free_ char *buf = NULL;
+                const char *p;
+
+                if (root_dir) {
+                        buf = strjoin(root_dir, "/", *i, NULL);
+                        if (!buf)
+                                return -ENOMEM;
+
+                        p = buf;
+                } else
+                        p = *i;
+
+                if (path_equal(parent, p))
+                        return 1;
+        }
+
+        return 0;
+}
+
 static int lookup_paths_init_from_scope(LookupPaths *paths,
                                         UnitFileScope scope,
                                         const char *root_dir) {
@@ -759,7 +790,7 @@ int unit_file_link(
                         continue;
                 }
 
-                q = in_search_path(*i, paths.unit_path);
+                q = in_search_path(*i, paths.unit_path, root_dir);
                 if (q < 0)
                         return q;
 
@@ -1315,6 +1346,7 @@ static int install_info_symlink_link(
                 InstallInfo *i,
                 LookupPaths *paths,
                 const char *config_path,
+                const char *root_dir,
                 bool force,
                 UnitFileChange **changes,
                 unsigned *n_changes) {
@@ -1327,7 +1359,7 @@ static int install_info_symlink_link(
         assert(config_path);
         assert(i->path);
 
-        r = in_search_path(i->path, paths->unit_path);
+        r = in_search_path(i->path, paths->unit_path, root_dir);
         if (r != 0)
                 return r;
 
@@ -1342,6 +1374,7 @@ static int install_info_apply(
                 InstallInfo *i,
                 LookupPaths *paths,
                 const char *config_path,
+                const char *root_dir,
                 bool force,
                 UnitFileChange **changes,
                 unsigned *n_changes) {
@@ -1362,7 +1395,7 @@ static int install_info_apply(
         if (r == 0)
                 r = q;
 
-        q = install_info_symlink_link(i, paths, config_path, force, changes, n_changes);
+        q = install_info_symlink_link(i, paths, config_path, root_dir, force, changes, n_changes);
         if (r == 0)
                 r = q;
 
@@ -1402,7 +1435,7 @@ static int install_context_apply(
                 } else if (r >= 0)
                         r += q;
 
-                q = install_info_apply(i, paths, config_path, force, changes, n_changes);
+                q = install_info_apply(i, paths, config_path, root_dir, force, changes, n_changes);
                 if (r >= 0 && q < 0)
                         r = q;
         }
diff --git a/src/shared/util.c b/src/shared/util.c
index 1f83eb7..f034134 100644
--- a/src/shared/util.c
+++ b/src/shared/util.c
@@ -4520,22 +4520,6 @@ int dirent_ensure_type(DIR *d, struct dirent *de) {
         return 0;
 }
 
-int in_search_path(const char *path, char **search) {
-        char **i;
-        _cleanup_free_ char *parent = NULL;
-        int r;
-
-        r = path_get_parent(path, &parent);
-        if (r < 0)
-                return r;
-
-        STRV_FOREACH(i, search)
-                if (path_equal(parent, *i))
-                        return 1;
-
-        return 0;
-}
-
 int get_files_in_directory(const char *path, char ***list) {
         _cleanup_closedir_ DIR *d = NULL;
         size_t bufsize = 0, n = 0;
diff --git a/src/shared/util.h b/src/shared/util.h
index 3f97663..f358362 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -477,7 +477,6 @@ int glob_extend(char ***strv, const char *path);
 
 int dirent_ensure_type(DIR *d, struct dirent *de);
 
-int in_search_path(const char *path, char **search);
 int get_files_in_directory(const char *path, char ***list);
 
 char *strjoin(const char *x, ...) _sentinel_;
