]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
udev/node: split out link_search_and_update() and reduce indentation
authorYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 31 Jul 2025 11:18:54 +0000 (20:18 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 31 Jul 2025 17:10:10 +0000 (02:10 +0900)
No functional change, just refactoring and preparation for later change.

src/udev/udev-node.c

index 75e8b09e89f32da0e9f09641b8dbd601a1056afd..1266b7afe5b8594ee9ff6dcb8c8999f9fa2ece77 100644 (file)
@@ -213,6 +213,24 @@ static int stack_directory_find_prioritized_devnode(sd_device *dev, int dirfd, b
         return !!*ret;
 }
 
+static int link_search_and_update(sd_device *dev, const char *slink, int dirfd, bool add) {
+        int r;
+
+        assert(dev);
+        assert(slink);
+        assert(dirfd >= 0);
+
+        _cleanup_free_ char *devnode = NULL;
+        r = stack_directory_find_prioritized_devnode(dev, dirfd, add, &devnode);
+        if (r < 0)
+                return log_device_debug_errno(dev, r, "Failed to determine device node with the highest priority for '%s': %m", slink);
+        if (r > 0)
+                return node_create_symlink(dev, devnode, slink);
+
+        log_device_debug(dev, "No reference left for '%s', removing", slink);
+        return node_remove_symlink(dev, slink);
+}
+
 static int stack_directory_update(sd_device *dev, int fd, bool add) {
         const char *id;
         int r;
@@ -420,7 +438,7 @@ static int link_update(sd_device *dev, const char *slink, bool add) {
         _cleanup_(release_lock_file) LockFile lockfile = LOCK_FILE_INIT; /* #3 */
         _cleanup_(rmdir_and_freep) char *dirpath = NULL; /* #2 */
         _cleanup_close_ int dirfd = -EBADF; /* #1 */
-        _cleanup_free_ char *current_id = NULL, *devnode = NULL;
+        _cleanup_free_ char *current_id = NULL;
         int r, current_prio;
 
         assert(dev);
@@ -438,64 +456,52 @@ static int link_update(sd_device *dev, const char *slink, bool add) {
         if (r < 0)
                 return log_device_debug_errno(dev, r, "Failed to update stack directory for '%s': %m", slink);
 
-        if (current_id) {
-                const char *id;
+        if (!current_id)
+                /* The requested devlink does not exist, or the target device does not exist and the devlink
+                 * points to a non-existing device. Let's search the device that has the highest priority,
+                 * and update the devlink. */
+                return link_search_and_update(dev, slink, dirfd, add);
 
-                r = sd_device_get_device_id(dev, &id);
-                if (r < 0)
-                        return log_device_debug_errno(dev, r, "Failed to get device id: %m");
+        const char *id;
+        r = sd_device_get_device_id(dev, &id);
+        if (r < 0)
+                return log_device_debug_errno(dev, r, "Failed to get device id: %m");
 
-                if (add) {
-                        int prio;
+        if (!add) {
+                if (!streq(current_id, id))
+                        /* The devlink already exists and is owned by another device. Hence, it is
+                         * not necessary to recreate it. */
+                        return 0;
 
-                        r = device_get_devlink_priority(dev, &prio);
-                        if (r < 0)
-                                return log_device_debug_errno(dev, r, "Failed to get devlink priority: %m");
-
-                        if (streq(current_id, id)) {
-                                if (current_prio <= prio)
-                                        /* The devlink is ours and already exists, and the new priority is
-                                         * equal or higher than the previous. Hence, it is not necessary to
-                                         * recreate it. */
-                                        return 0;
-
-                                /* The devlink priority is downgraded. Another device may have a higher
-                                 * priority now. Let's find the device node with the highest priority. */
-                        } else {
-                                if (current_prio > prio)
-                                        /* The devlink with a higher priority already exists and is owned by
-                                         * another device. Hence, it is not necessary to recreate it. */
-                                        return 0;
-
-                                /* This device has the equal or a higher priority than the current. Let's
-                                 * create the devlink to our device node. */
-                                return node_create_symlink(dev, /* devnode = */ NULL, slink);
-                        }
+                /* The current devlink is ours, and the target device will be removed. Hence, we need
+                 * to search the device that has the highest priority. and update the devlink. */
+                return link_search_and_update(dev, slink, dirfd, add);
+        }
 
-                } else {
-                        if (!streq(current_id, id))
-                                /* The devlink already exists and is owned by another device. Hence, it is
-                                 * not necessary to recreate it. */
-                                return 0;
+        int prio;
+        r = device_get_devlink_priority(dev, &prio);
+        if (r < 0)
+                return log_device_debug_errno(dev, r, "Failed to get devlink priority: %m");
 
-                        /* The current devlink is ours, and the target device will be removed. Hence, we need
-                         * to search the device that has the highest priority. and update the devlink. */
-                }
-        } else {
-                /* The requested devlink does not exist, or the target device does not exist and the devlink
-                 * points to a non-existing device. Let's search the device that has the highest priority,
-                 * and update the devlink. */
-                ;
+        if (streq(current_id, id)) {
+                if (current_prio <= prio)
+                        /* The devlink is ours and already exists, and the new priority is equal or higher
+                         * than the previous. Hence, it is not necessary to recreate it. */
+                        return 0;
+
+                /* The devlink priority is downgraded. Another device may have a higher priority now. Let's
+                 * find the device node with the highest priority. */
+                return link_search_and_update(dev, slink, dirfd, add);
         }
 
-        r = stack_directory_find_prioritized_devnode(dev, dirfd, add, &devnode);
-        if (r < 0)
-                return log_device_debug_errno(dev, r, "Failed to determine device node with the highest priority for '%s': %m", slink);
-        if (r > 0)
-                return node_create_symlink(dev, devnode, slink);
+        if (current_prio > prio)
+                /* The devlink with a higher priority already exists and is owned by another device. Hence,
+                 * it is not necessary to recreate it. */
+                return 0;
 
-        log_device_debug(dev, "No reference left for '%s', removing", slink);
-        return node_remove_symlink(dev, slink);
+        /* This device has the equal or a higher priority than the current. Let's create the devlink to our
+         * device node. */
+        return node_create_symlink(dev, /* devnode = */ NULL, slink);
 }
 
 static int device_get_devpath_by_devnum(sd_device *dev, char **ret) {