chiark / gitweb /
install: various modernizations
[elogind.git] / src / shared / install.c
index 7e0e20c603aef5c1e8c6e37c6a8b950340cd64ae..a6a9f19e75b38b773dd8b7b25f614399665e8243 100644 (file)
@@ -287,30 +287,31 @@ static int remove_marked_symlinks_fd(
                                 set_get(remove_symlinks_to, dest) ||
                                 set_get(remove_symlinks_to, basename(dest));
 
-                        if (found) {
+                        if (!found)
+                                continue;
 
-                                if (unlink(p) < 0 && errno != ENOENT) {
 
-                                        if (r == 0)
-                                                r = -errno;
-                                        continue;
-                                }
+                        if (unlink(p) < 0 && errno != ENOENT) {
 
-                                rmdir_parents(p, config_path);
+                                if (r == 0)
+                                        r = -errno;
+                                continue;
+                        }
 
-                                path_kill_slashes(p);
+                        rmdir_parents(p, config_path);
 
-                                add_file_change(changes, n_changes, UNIT_FILE_UNLINK, p, NULL);
+                        path_kill_slashes(p);
 
-                                if (!set_get(remove_symlinks_to, p)) {
+                        add_file_change(changes, n_changes, UNIT_FILE_UNLINK, p, NULL);
 
-                                        q = mark_symlink_for_removal(&remove_symlinks_to, p);
-                                        if (q < 0) {
-                                                if (r == 0)
-                                                        r = q;
-                                        } else
-                                                *deleted = true;
-                                }
+                        if (!set_get(remove_symlinks_to, p)) {
+
+                                q = mark_symlink_for_removal(&remove_symlinks_to, p);
+                                if (q < 0) {
+                                        if (r == 0)
+                                                r = q;
+                                } else
+                                        *deleted = true;
                         }
                 }
         }
@@ -417,10 +418,8 @@ static int find_symlinks_fd(
 
                         /* This will close nfd, regardless whether it succeeds or not */
                         q = find_symlinks_fd(name, nfd, p, config_path, same_name_link);
-
                         if (q > 0)
                                 return 1;
-
                         if (r == 0)
                                 r = q;
 
@@ -595,7 +594,6 @@ int unit_file_mask(
 
                 if (symlink("/dev/null", path) >= 0) {
                         add_file_change(changes, n_changes, UNIT_FILE_SYMLINK, path, "/dev/null");
-
                         continue;
                 }
 
@@ -762,7 +760,6 @@ int unit_file_link(
                         _cleanup_free_ char *dest = NULL;
 
                         q = readlink_and_make_absolute(path, &dest);
-
                         if (q < 0 && errno != ENOENT) {
                                 if (r == 0)
                                         r = q;
@@ -1302,8 +1299,8 @@ static int install_info_symlink_link(
                 UnitFileChange **changes,
                 unsigned *n_changes) {
 
-        int r;
         _cleanup_free_ char *path = NULL;
+        int r;
 
         assert(i);
         assert(paths);
@@ -1314,11 +1311,11 @@ static int install_info_symlink_link(
         if (r != 0)
                 return r;
 
-        if (asprintf(&path, "%s/%s", config_path, i->name) < 0)
+        path = strjoin(config_path, "/", i->name, NULL);
+        if (!path)
                 return -ENOMEM;
 
-        r = create_symlink(i->path, path, force, changes, n_changes);
-        return r;
+        return create_symlink(i->path, path, force, changes, n_changes);
 }
 
 static int install_info_apply(
@@ -1749,7 +1746,7 @@ UnitFileState unit_file_get_state(
         return r < 0 ? r : state;
 }
 
-int unit_file_query_preset(UnitFileScope scope, const char *name) {
+int unit_file_query_preset(UnitFileScope scope, const char *root_dir, const char *name) {
         _cleanup_strv_free_ char **files = NULL;
         char **i;
         int r;
@@ -1759,7 +1756,7 @@ int unit_file_query_preset(UnitFileScope scope, const char *name) {
         assert(name);
 
         if (scope == UNIT_FILE_SYSTEM)
-                r = conf_files_list(&files, ".preset", NULL,
+                r = conf_files_list(&files, ".preset", root_dir,
                                     "/etc/systemd/system-preset",
                                     "/usr/local/lib/systemd/system-preset",
                                     "/usr/lib/systemd/system-preset",
@@ -1768,7 +1765,7 @@ int unit_file_query_preset(UnitFileScope scope, const char *name) {
 #endif
                                     NULL);
         else if (scope == UNIT_FILE_GLOBAL)
-                r = conf_files_list(&files, ".preset", NULL,
+                r = conf_files_list(&files, ".preset", root_dir,
                                     "/etc/systemd/user-preset",
                                     "/usr/local/lib/systemd/user-preset",
                                     "/usr/lib/systemd/user-preset",
@@ -1780,9 +1777,16 @@ int unit_file_query_preset(UnitFileScope scope, const char *name) {
                 return r;
 
         STRV_FOREACH(i, files) {
+                _cleanup_free_ char *buf = NULL;
                 _cleanup_fclose_ FILE *f;
+                const char *p;
+
+                if (root_dir)
+                        p = buf = strjoin(root_dir, "/", *i, NULL);
+                else
+                        p = *i;
 
-                f = fopen(*i, "re");
+                f = fopen(p, "re");
                 if (!f) {
                         if (errno == ENOENT)
                                 continue;
@@ -1807,15 +1811,19 @@ int unit_file_query_preset(UnitFileScope scope, const char *name) {
                                 l += 6;
                                 l += strspn(l, WHITESPACE);
 
-                                if (fnmatch(l, name, FNM_NOESCAPE) == 0)
+                                if (fnmatch(l, name, FNM_NOESCAPE) == 0) {
+                                        log_debug("Preset file says enable %s.", name);
                                         return 1;
+                                }
 
                         } else if (first_word(l, "disable")) {
                                 l += 7;
                                 l += strspn(l, WHITESPACE);
 
-                                if (fnmatch(l, name, FNM_NOESCAPE) == 0)
+                                if (fnmatch(l, name, FNM_NOESCAPE) == 0) {
+                                        log_debug("Preset file says disable %s.", name);
                                         return 0;
+                                }
 
                         } else
                                 log_debug("Couldn't parse line '%s'", l);
@@ -1823,6 +1831,7 @@ int unit_file_query_preset(UnitFileScope scope, const char *name) {
         }
 
         /* Default is "enable" */
+        log_debug("Preset file doesn't say anything about %s, enabling.", name);
         return 1;
 }
 
@@ -1859,7 +1868,7 @@ int unit_file_preset(
                 if (!unit_name_is_valid(*i, TEMPLATE_VALID))
                         return -EINVAL;
 
-                r = unit_file_query_preset(scope, *i);
+                r = unit_file_query_preset(scope, root_dir, *i);
                 if (r < 0)
                         return r;
 
@@ -1966,7 +1975,7 @@ int unit_file_preset_all(
                         if (de->d_type != DT_REG)
                                 continue;
 
-                        r = unit_file_query_preset(scope, de->d_name);
+                        r = unit_file_query_preset(scope, root_dir, de->d_name);
                         if (r < 0)
                                 return r;