]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
udev: simplify a bit stack_directory_find_prioritized_devnode() 26047/head
authorFranck Bui <fbui@suse.com>
Wed, 4 Jan 2023 13:59:00 +0000 (14:59 +0100)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 13 Jan 2023 03:06:00 +0000 (12:06 +0900)
And make the new format the one we expect as it should replace the old one
pretty quickly.

src/udev/udev-node.c

index e1e1695050988f58fe50929dbde2862825bb0ec7..1596497da893a11675d2f2258536b283eddfde0f 100644 (file)
@@ -104,7 +104,8 @@ static int node_symlink(sd_device *dev, const char *devnode, const char *slink)
         return 0;
 }
 
-static int stack_directory_read_one(int dirfd, const char *id, bool is_symlink, char **devnode, int *priority) {
+static int stack_directory_read_one(int dirfd, const char *id, char **devnode, int *priority) {
+        _cleanup_free_ char *buf = NULL;
         int tmp_prio, r;
 
         assert(dirfd >= 0);
@@ -112,15 +113,14 @@ static int stack_directory_read_one(int dirfd, const char *id, bool is_symlink,
         assert(devnode);
         assert(priority);
 
-        if (is_symlink) {
-                _cleanup_free_ char *buf = NULL;
-                char *colon;
+        /* First, let's try to read the entry with the new format, which should replace the old format pretty
+         * quickly. */
 
-                /* New format. The devnode and priority can be obtained from symlink. */
+        r = readlinkat_malloc(dirfd, id, &buf);
+        if (r >= 0) {
+                char *colon;
 
-                r = readlinkat_malloc(dirfd, id, &buf);
-                if (r < 0)
-                        return r;
+                /* With the new format, the devnode and priority can be obtained from symlink itself. */
 
                 colon = strchr(buf, ':');
                 if (!colon || colon == buf)
@@ -146,7 +146,7 @@ static int stack_directory_read_one(int dirfd, const char *id, bool is_symlink,
                 if (r < 0)
                         return r;
 
-        } else {
+        } else if (r == -EINVAL) { /* Not a symlink ? try the old format */
                 _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
                 const char *val;
 
@@ -170,7 +170,9 @@ static int stack_directory_read_one(int dirfd, const char *id, bool is_symlink,
                 r = free_and_strdup(devnode, val);
                 if (r < 0)
                         return r;
-        }
+
+        } else
+                return r == -ENOENT ? -ENODEV : r;
 
         *priority = tmp_prio;
         return 1; /* Updated */
@@ -213,22 +215,15 @@ static int stack_directory_find_prioritized_devnode(sd_device *dev, int dirfd, b
         if (r < 0)
                 return r;
 
-        FOREACH_DIRENT_ALL(de, dir, break) {
-                if (de->d_name[0] == '.')
-                        continue;
+        FOREACH_DIRENT(de, dir, break) {
 
                 /* skip ourself */
                 if (streq(de->d_name, id))
                         continue;
 
-                if (!IN_SET(de->d_type, DT_LNK, DT_REG))
-                        continue;
-
-                r = stack_directory_read_one(dirfd, de->d_name, /* is_symlink = */ de->d_type == DT_LNK, &devnode, &priority);
-                if (r < 0) {
+                r = stack_directory_read_one(dirfd, de->d_name, &devnode, &priority);
+                if (r < 0 && r != -ENODEV)
                         log_debug_errno(r, "Failed to read '%s', ignoring: %m", de->d_name);
-                        continue;
-                }
         }
 
         *ret = TAKE_PTR(devnode);