8d419f
From caaea62c2c32e6aedb24288d5f51e6c35187e14c Mon Sep 17 00:00:00 2001
8d419f
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
8d419f
Date: Mon, 14 Mar 2022 12:09:31 +0100
8d419f
Subject: [PATCH] shared/install: split unit_file_{disable,enable}() so
8d419f
 _reenable doesn't do setup twice
8d419f
8d419f
It was pretty ugly that we were creating LookupPaths twice.
8d419f
8d419f
(cherry picked from commit ec7eaff3c2abf3048f3fba98bfbe08a0c7c898b0)
8d419f
8d419f
Related: #2082131
8d419f
---
8d419f
 src/shared/install.c | 105 +++++++++++++++++++++++++++++--------------
8d419f
 1 file changed, 72 insertions(+), 33 deletions(-)
8d419f
8d419f
diff --git a/src/shared/install.c b/src/shared/install.c
8d419f
index fadd2be248..1018e4fbf3 100644
8d419f
--- a/src/shared/install.c
8d419f
+++ b/src/shared/install.c
8d419f
@@ -2596,33 +2596,21 @@ int unit_file_add_dependency(
8d419f
                                      SEARCH_FOLLOW_CONFIG_SYMLINKS, changes, n_changes);
8d419f
 }
8d419f
 
8d419f
-int unit_file_enable(
8d419f
+static int do_unit_file_enable(
8d419f
+                const LookupPaths *lp,
8d419f
                 UnitFileScope scope,
8d419f
-                UnitFileFlags file_flags,
8d419f
-                const char *root_dir,
8d419f
+                UnitFileFlags flags,
8d419f
+                const char *config_path,
8d419f
                 char **files,
8d419f
                 UnitFileChange **changes,
8d419f
                 size_t *n_changes) {
8d419f
 
8d419f
-        _cleanup_(lookup_paths_free) LookupPaths lp = {};
8d419f
         _cleanup_(install_context_done) InstallContext ctx = { .scope = scope };
8d419f
-        const char *config_path;
8d419f
         UnitFileInstallInfo *info;
8d419f
         int r;
8d419f
 
8d419f
-        assert(scope >= 0);
8d419f
-        assert(scope < _UNIT_FILE_SCOPE_MAX);
8d419f
-
8d419f
-        r = lookup_paths_init_or_warn(&lp, scope, 0, root_dir);
8d419f
-        if (r < 0)
8d419f
-                return r;
8d419f
-
8d419f
-        config_path = config_path_from_flags(&lp, file_flags);
8d419f
-        if (!config_path)
8d419f
-                return -ENXIO;
8d419f
-
8d419f
         STRV_FOREACH(f, files) {
8d419f
-                r = install_info_discover_and_check(&ctx, &lp, *f, SEARCH_LOAD|SEARCH_FOLLOW_CONFIG_SYMLINKS,
8d419f
+                r = install_info_discover_and_check(&ctx, lp, *f, SEARCH_LOAD|SEARCH_FOLLOW_CONFIG_SYMLINKS,
8d419f
                                                     &info, changes, n_changes);
8d419f
                 if (r < 0)
8d419f
                         return r;
8d419f
@@ -2635,11 +2623,11 @@ int unit_file_enable(
8d419f
            is useful to determine whether the passed files had any
8d419f
            installation data at all. */
8d419f
 
8d419f
-        return install_context_apply(&ctx, &lp, file_flags, config_path,
8d419f
+        return install_context_apply(&ctx, lp, flags, config_path,
8d419f
                                      SEARCH_LOAD, changes, n_changes);
8d419f
 }
8d419f
 
8d419f
-int unit_file_disable(
8d419f
+int unit_file_enable(
8d419f
                 UnitFileScope scope,
8d419f
                 UnitFileFlags flags,
8d419f
                 const char *root_dir,
8d419f
@@ -2648,9 +2636,6 @@ int unit_file_disable(
8d419f
                 size_t *n_changes) {
8d419f
 
8d419f
         _cleanup_(lookup_paths_free) LookupPaths lp = {};
8d419f
-        _cleanup_(install_context_done) InstallContext ctx = { .scope = scope };
8d419f
-        _cleanup_set_free_free_ Set *remove_symlinks_to = NULL;
8d419f
-        const char *config_path;
8d419f
         int r;
8d419f
 
8d419f
         assert(scope >= 0);
8d419f
@@ -2660,27 +2645,44 @@ int unit_file_disable(
8d419f
         if (r < 0)
8d419f
                 return r;
8d419f
 
8d419f
-        config_path = config_path_from_flags(&lp, flags);
8d419f
+        const char *config_path = config_path_from_flags(&lp, flags);
8d419f
         if (!config_path)
8d419f
                 return -ENXIO;
8d419f
 
8d419f
+        return do_unit_file_enable(&lp, scope, flags, config_path, files, changes, n_changes);
8d419f
+}
8d419f
+
8d419f
+static int do_unit_file_disable(
8d419f
+                const LookupPaths *lp,
8d419f
+                UnitFileScope scope,
8d419f
+                UnitFileFlags flags,
8d419f
+                const char *config_path,
8d419f
+                char **files,
8d419f
+                UnitFileChange **changes,
8d419f
+                size_t *n_changes) {
8d419f
+
8d419f
+        _cleanup_(install_context_done) InstallContext ctx = { .scope = scope };
8d419f
+        _cleanup_set_free_free_ Set *remove_symlinks_to = NULL;
8d419f
+        int r;
8d419f
+
8d419f
         STRV_FOREACH(i, files) {
8d419f
                 if (!unit_name_is_valid(*i, UNIT_NAME_ANY))
8d419f
                         return -EINVAL;
8d419f
 
8d419f
-                r = install_info_add(&ctx, *i, NULL, lp.root_dir, /* auxiliary= */ false, NULL);
8d419f
+                r = install_info_add(&ctx, *i, NULL, lp->root_dir, /* auxiliary= */ false, NULL);
8d419f
                 if (r < 0)
8d419f
                         return r;
8d419f
         }
8d419f
 
8d419f
-        r = install_context_mark_for_removal(&ctx, &lp, &remove_symlinks_to, config_path, changes, n_changes);
8d419f
+        r = install_context_mark_for_removal(&ctx, lp, &remove_symlinks_to, config_path, changes, n_changes);
8d419f
         if (r < 0)
8d419f
                 return r;
8d419f
 
8d419f
-        return remove_marked_symlinks(remove_symlinks_to, config_path, &lp, flags & UNIT_FILE_DRY_RUN, changes, n_changes);
8d419f
+        return remove_marked_symlinks(remove_symlinks_to, config_path, lp, flags & UNIT_FILE_DRY_RUN, changes, n_changes);
8d419f
 }
8d419f
 
8d419f
-int unit_file_reenable(
8d419f
+
8d419f
+int unit_file_disable(
8d419f
                 UnitFileScope scope,
8d419f
                 UnitFileFlags flags,
8d419f
                 const char *root_dir,
8d419f
@@ -2688,23 +2690,60 @@ int unit_file_reenable(
8d419f
                 UnitFileChange **changes,
8d419f
                 size_t *n_changes) {
8d419f
 
8d419f
-        char **n;
8d419f
+        _cleanup_(lookup_paths_free) LookupPaths lp = {};
8d419f
         int r;
8d419f
+
8d419f
+        assert(scope >= 0);
8d419f
+        assert(scope < _UNIT_FILE_SCOPE_MAX);
8d419f
+
8d419f
+        r = lookup_paths_init(&lp, scope, 0, root_dir);
8d419f
+        if (r < 0)
8d419f
+                return r;
8d419f
+
8d419f
+        const char *config_path = config_path_from_flags(&lp, flags);
8d419f
+        if (!config_path)
8d419f
+                return -ENXIO;
8d419f
+
8d419f
+        return do_unit_file_disable(&lp, scope, flags, config_path, files, changes, n_changes);
8d419f
+}
8d419f
+
8d419f
+int unit_file_reenable(
8d419f
+                UnitFileScope scope,
8d419f
+                UnitFileFlags flags,
8d419f
+                const char *root_dir,
8d419f
+                char **files,
8d419f
+                UnitFileChange **changes,
8d419f
+                size_t *n_changes) {
8d419f
+
8d419f
+        _cleanup_(lookup_paths_free) LookupPaths lp = {};
8d419f
         size_t l, i;
8d419f
+        char **names;
8d419f
+        int r;
8d419f
+
8d419f
+        assert(scope >= 0);
8d419f
+        assert(scope < _UNIT_FILE_SCOPE_MAX);
8d419f
+
8d419f
+        r = lookup_paths_init(&lp, scope, 0, root_dir);
8d419f
+        if (r < 0)
8d419f
+                return r;
8d419f
+
8d419f
+        const char *config_path = config_path_from_flags(&lp, flags);
8d419f
+        if (!config_path)
8d419f
+                return -ENXIO;
8d419f
 
8d419f
         /* First, we invoke the disable command with only the basename... */
8d419f
         l = strv_length(files);
8d419f
-        n = newa(char*, l+1);
8d419f
+        names = newa(char*, l+1);
8d419f
         for (i = 0; i < l; i++)
8d419f
-                n[i] = basename(files[i]);
8d419f
-        n[i] = NULL;
8d419f
+                names[i] = basename(files[i]);
8d419f
+        names[i] = NULL;
8d419f
 
8d419f
-        r = unit_file_disable(scope, flags, root_dir, n, changes, n_changes);
8d419f
+        r = do_unit_file_disable(&lp, scope, flags, config_path, names, changes, n_changes);
8d419f
         if (r < 0)
8d419f
                 return r;
8d419f
 
8d419f
         /* But the enable command with the full name */
8d419f
-        return unit_file_enable(scope, flags, root_dir, files, changes, n_changes);
8d419f
+        return do_unit_file_enable(&lp, scope, flags, config_path, files, changes, n_changes);
8d419f
 }
8d419f
 
8d419f
 int unit_file_set_default(