5509c00
From acefb01e351be2f1dbd05078fdf4522a9dadaa4d Mon Sep 17 00:00:00 2001
5509c00
From: Lennart Poettering <lennart@poettering.net>
5509c00
Date: Tue, 17 Jun 2014 00:13:48 +0200
5509c00
Subject: [PATCH] install: when looking for a unit file for enabling, search
5509c00
 for templates only after traversing all search directories
5509c00
5509c00
Let's always make sure to look in all search directories for the full
5509c00
unit names first, before looking for templates for them.
5509c00
5509c00
(cherry picked from commit e50bd775163cd96be1888943a8785a436be710e8)
5509c00
---
5509c00
 src/shared/install.c | 74 +++++++++++++++++++++++++++-------------------------
5509c00
 1 file changed, 38 insertions(+), 36 deletions(-)
5509c00
5509c00
diff --git a/src/shared/install.c b/src/shared/install.c
5509c00
index f0d3d1b7..e16de4d6 100644
5509c00
--- a/src/shared/install.c
5509c00
+++ b/src/shared/install.c
5509c00
@@ -1035,67 +1035,69 @@ static int unit_file_search(
5509c00
         assert(paths);
5509c00
 
5509c00
         if (info->path) {
5509c00
-                char *full_path = NULL;
5509c00
+                const char *path;
5509c00
 
5509c00
-                if (!isempty(root_dir))
5509c00
-                        full_path = strappenda(root_dir, info->path);
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, full_path ?: info->path, allow_symlink);
5509c00
+                return unit_file_load(c, info, path, allow_symlink);
5509c00
         }
5509c00
 
5509c00
         assert(info->name);
5509c00
 
5509c00
         STRV_FOREACH(p, paths->unit_path) {
5509c00
-                _cleanup_free_ char *path = NULL, *full_path = NULL;
5509c00
+                _cleanup_free_ char *path = NULL;
5509c00
 
5509c00
-                path = strjoin(*p, "/", info->name, NULL);
5509c00
+                if (isempty(root_dir))
5509c00
+                        path = strjoin(*p, "/", info->name, NULL);
5509c00
+                else
5509c00
+                        path = strjoin(root_dir, "/", *p, "/", info->name, NULL);
5509c00
                 if (!path)
5509c00
                         return -ENOMEM;
5509c00
 
5509c00
-                if (!isempty(root_dir)) {
5509c00
-                        full_path = strappend(root_dir, path);
5509c00
-                        if (!full_path)
5509c00
-                                return -ENOMEM;
5509c00
-                }
5509c00
-
5509c00
-                r = unit_file_load(c, info, full_path ?: path, allow_symlink);
5509c00
+                r = unit_file_load(c, info, path, allow_symlink);
5509c00
                 if (r >= 0) {
5509c00
                         info->path = path;
5509c00
                         path = NULL;
5509c00
-                } else if (r == -ENOENT && unit_name_is_instance(info->name)) {
5509c00
-                        /* Unit file doesn't exist, however instance enablement was requested.
5509c00
-                         * We will check if it is possible to load template unit file. */
5509c00
-                        _cleanup_free_ char *template = NULL, *template_dir = NULL;
5509c00
+                        return r;
5509c00
+                }
5509c00
+                if (r != -ENOENT && r != -ELOOP)
5509c00
+                        return r;
5509c00
+        }
5509c00
 
5509c00
-                        template = unit_name_template(info->name);
5509c00
-                        if (!template)
5509c00
-                                return -ENOMEM;
5509c00
+        if (unit_name_is_instance(info->name)) {
5509c00
+
5509c00
+                /* Unit file doesn't exist, however instance
5509c00
+                 * enablement was requested.  We will check if it is
5509c00
+                 * possible to load template unit file. */
5509c00
 
5509c00
-                        /* We will reuse path variable since we don't need it anymore. */
5509c00
-                        template_dir = path;
5509c00
-                        *(strrchr(template_dir, '/') + 1) = '\0';
5509c00
+                _cleanup_free_ char *template = NULL, *template_dir = NULL;
5509c00
+
5509c00
+                template = unit_name_template(info->name);
5509c00
+                if (!template)
5509c00
+                        return -ENOMEM;
5509c00
 
5509c00
-                        path = strappend(template_dir, template);
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
                         if (!path)
5509c00
                                 return -ENOMEM;
5509c00
 
5509c00
-                        if (!isempty(root_dir)) {
5509c00
-                                free(full_path);
5509c00
-                                full_path = strappend(root_dir, path);
5509c00
-                                if (!full_path)
5509c00
-                                        return -ENOMEM;
5509c00
-                        }
5509c00
-
5509c00
-                        /* Let's try to load template unit. */
5509c00
-                        r = unit_file_load(c, info, full_path ?: path, allow_symlink);
5509c00
+                        r = unit_file_load(c, info, path, allow_symlink);
5509c00
                         if (r >= 0) {
5509c00
                                 info->path = path;
5509c00
                                 path = NULL;
5509c00
+                                return r;
5509c00
                         }
5509c00
+                        if (r != -ENOENT && r != -ELOOP)
5509c00
+                                return r;
5509c00
                 }
5509c00
-
5509c00
-                if (r != -ENOENT && r != -ELOOP)
5509c00
-                        return r;
5509c00
         }
5509c00
 
5509c00
         return -ENOENT;