install: unify checking whether operations may be applied to a unit file in a new...
authorLennart Poettering <lennart@poettering.net>
Mon, 7 Mar 2016 18:15:51 +0000 (19:15 +0100)
committerLennart Poettering <lennart@poettering.net>
Tue, 12 Apr 2016 11:43:31 +0000 (13:43 +0200)
Let's replace repeated code by a single implementation in a single function.

src/shared/install.c

index c19c85a..f78cc81 100644 (file)
@@ -704,6 +704,23 @@ static UnitFileInstallInfo *install_info_find(InstallContext *c, const char *nam
         return ordered_hashmap_get(c->will_process, name);
 }
 
+static int install_info_may_process(UnitFileInstallInfo *i, const LookupPaths *paths) {
+        assert(i);
+        assert(paths);
+
+        /* Checks whether the loaded unit file is one we should process, or is masked, transient or generated and thus
+         * not subject to enable/disable operations. */
+
+        if (i->type == UNIT_FILE_TYPE_MASKED)
+                return -ESHUTDOWN;
+        if (path_is_generator(paths, i->path))
+                return -EADDRNOTAVAIL;
+        if (path_is_transient(paths, i->path))
+                return -EADDRNOTAVAIL;
+
+        return 0;
+}
+
 static int install_info_add(
                 InstallContext *c,
                 const char *name,
@@ -1719,12 +1736,9 @@ int unit_file_add_dependency(
         r = install_info_discover(scope, &c, &paths, target, SEARCH_FOLLOW_CONFIG_SYMLINKS, &target_info);
         if (r < 0)
                 return r;
-        if (target_info->type == UNIT_FILE_TYPE_MASKED)
-                return -ESHUTDOWN;
-        if (path_is_generator(&paths, target_info->path))
-                return -EADDRNOTAVAIL;
-        if (path_is_transient(&paths, target_info->path))
-                return -EADDRNOTAVAIL;
+        r = install_info_may_process(target_info, &paths);
+        if (r < 0)
+                return r;
 
         assert(target_info->type == UNIT_FILE_TYPE_REGULAR);
 
@@ -1734,12 +1748,9 @@ int unit_file_add_dependency(
                 r = install_info_discover(scope, &c, &paths, *f, SEARCH_FOLLOW_CONFIG_SYMLINKS, &i);
                 if (r < 0)
                         return r;
-                if (i->type == UNIT_FILE_TYPE_MASKED)
-                        return -ESHUTDOWN;
-                if (path_is_generator(&paths, i->path))
-                        return -EADDRNOTAVAIL;
-                if (path_is_transient(&paths, i->path))
-                        return -EADDRNOTAVAIL;
+                r = install_info_may_process(i, &paths);
+                if (r < 0)
+                        return r;
 
                 assert(i->type == UNIT_FILE_TYPE_REGULAR);
 
@@ -1790,12 +1801,9 @@ int unit_file_enable(
                 r = install_info_discover(scope, &c, &paths, *f, SEARCH_LOAD, &i);
                 if (r < 0)
                         return r;
-                if (i->type == UNIT_FILE_TYPE_MASKED)
-                        return -ESHUTDOWN;
-                if (path_is_generator(&paths, i->path))
-                        return -EADDRNOTAVAIL;
-                if (path_is_transient(&paths, i->path))
-                        return -EADDRNOTAVAIL;
+                r = install_info_may_process(i, &paths);
+                if (r < 0)
+                        return r;
 
                 assert(i->type == UNIT_FILE_TYPE_REGULAR);
         }
@@ -1906,12 +1914,9 @@ int unit_file_set_default(
         r = install_info_discover(scope, &c, &paths, name, 0, &i);
         if (r < 0)
                 return r;
-        if (i->type == UNIT_FILE_TYPE_MASKED)
-                return -ESHUTDOWN;
-        if (path_is_generator(&paths, i->path))
-                return -EADDRNOTAVAIL;
-        if (path_is_transient(&paths, i->path))
-                return -EADDRNOTAVAIL;
+        r = install_info_may_process(i, &paths);
+        if (r < 0)
+                return r;
 
         old_path = skip_root(&paths, i->path);
         new_path = strjoina(paths.persistent_config, "/" SPECIAL_DEFAULT_TARGET);
@@ -1941,8 +1946,9 @@ int unit_file_get_default(
         r = install_info_discover(scope, &c, &paths, SPECIAL_DEFAULT_TARGET, SEARCH_FOLLOW_CONFIG_SYMLINKS, &i);
         if (r < 0)
                 return r;
-        if (i->type == UNIT_FILE_TYPE_MASKED)
-                return -ESHUTDOWN;
+        r = install_info_may_process(i, &paths);
+        if (r < 0)
+                return r;
 
         n = strdup(i->name);
         if (!n)
@@ -2201,12 +2207,9 @@ static int preset_prepare_one(
                 if (r < 0)
                         return r;
 
-                if (i->type == UNIT_FILE_TYPE_MASKED)
-                        return -ESHUTDOWN;
-                if (path_is_generator(paths, i->path))
-                        return -EADDRNOTAVAIL;
-                if (path_is_transient(paths, i->path))
-                        return -EADDRNOTAVAIL;
+                r = install_info_may_process(i, paths);
+                if (r < 0)
+                        return r;
         } else
                 r = install_info_discover(scope, minus, paths, name, SEARCH_FOLLOW_CONFIG_SYMLINKS, &i);