]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
shared/install: modernize find_symlinks and friends
authorMike Yuan <me@yhndnzj.com>
Wed, 1 May 2024 07:32:21 +0000 (15:32 +0800)
committerLuca Boccassi <bluca@debian.org>
Tue, 11 Jun 2024 22:17:21 +0000 (23:17 +0100)
src/shared/install.c

index b760f8c67e3c867d906deff14966e1bc90df8d05..b8ee82afcd28779a664686054bb2600b47b31324 100644 (file)
@@ -830,6 +830,9 @@ static int remove_marked_symlinks(
 static int is_symlink_with_known_name(const InstallInfo *i, const char *name) {
         int r;
 
+        assert(i);
+        assert(name);
+
         if (streq(name, i->name))
                 return true;
 
@@ -863,11 +866,17 @@ static int find_symlinks_in_directory(
                 const char *config_path,
                 bool *same_name_link) {
 
-        int r = 0;
+        int r, ret = 0;
+
+        assert(dir);
+        assert(dir_path);
+        assert(info);
+        assert(unit_name_is_valid(info->name, UNIT_NAME_ANY));
+        assert(config_path);
+        assert(same_name_link);
 
         FOREACH_DIRENT(de, dir, return -errno) {
                 bool found_path = false, found_dest = false, b = false;
-                int q;
 
                 if (de->d_type != DT_LNK)
                         continue;
@@ -876,32 +885,17 @@ static int find_symlinks_in_directory(
                         _cleanup_free_ char *dest = NULL;
 
                         /* Acquire symlink destination */
-                        q = readlinkat_malloc(dirfd(dir), de->d_name, &dest);
-                        if (q == -ENOENT)
-                                continue;
-                        if (q < 0) {
-                                if (r == 0)
-                                        r = q;
+                        r = readlinkat_malloc(dirfd(dir), de->d_name, &dest);
+                        if (r < 0) {
+                                if (r != -ENOENT)
+                                        RET_GATHER(ret, r);
                                 continue;
                         }
 
-                        /* Make absolute */
-                        if (!path_is_absolute(dest)) {
-                                char *x;
-
-                                x = path_join(dir_path, dest);
-                                if (!x)
-                                        return -ENOMEM;
-
-                                free_and_replace(dest, x);
-                        }
-
                         /* Check if what the symlink points to matches what we are looking for */
-                        found_dest = streq(basename(dest), info->name);
+                        found_dest = path_equal_filename(dest, info->name);
                 }
 
-                assert(unit_name_is_valid(info->name, UNIT_NAME_ANY));
-
                 /* Check if the symlink itself matches what we are looking for.
                  *
                  * If ignore_destination is specified, we only look at the source name.
@@ -916,10 +910,10 @@ static int find_symlinks_in_directory(
                 if (!found_path && ignore_destination) {
                         _cleanup_free_ char *template = NULL;
 
-                        q = unit_name_template(de->d_name, &template);
-                        if (q < 0 && q != -EINVAL)
-                                return q;
-                        if (q >= 0)
+                        r = unit_name_template(de->d_name, &template);
+                        if (r < 0 && r != -EINVAL)
+                                return r;
+                        if (r >= 0)
                                 found_dest = streq(template, info->name);
                 }
 
@@ -929,7 +923,6 @@ static int find_symlinks_in_directory(
                         /* Filter out same name links in the main config path */
                         p = path_make_absolute(de->d_name, dir_path);
                         t = path_make_absolute(info->name, config_path);
-
                         if (!p || !t)
                                 return -ENOMEM;
 
@@ -943,15 +936,13 @@ static int find_symlinks_in_directory(
                                 return 1;
 
                         /* Check if symlink name is in the set of names used by [Install] */
-                        q = is_symlink_with_known_name(info, de->d_name);
-                        if (q < 0)
-                                return q;
-                        if (q > 0)
-                                return 1;
+                        r = is_symlink_with_known_name(info, de->d_name);
+                        if (r != 0)
+                                return r;
                 }
         }
 
-        return r;
+        return ret;
 }
 
 static int find_symlinks(
@@ -963,7 +954,7 @@ static int find_symlinks(
                 bool *same_name_link) {
 
         _cleanup_closedir_ DIR *config_dir = NULL;
-        int r = 0;
+        int r;
 
         assert(i);
         assert(config_path);
@@ -1006,7 +997,7 @@ static int find_symlinks(
                                                same_name_link);
                 if (r > 0)
                         return 1;
-                else if (r < 0)
+                if (r < 0)
                         log_debug_errno(r, "Failed to look up symlinks in \"%s\": %m", path);
         }