dcavalca / rpms / systemd

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