]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
udev-builtin-path_id: split out add_id_tag()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 11 May 2023 07:20:57 +0000 (16:20 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 20 May 2023 05:27:07 +0000 (14:27 +0900)
No functional change, just refactoring.

src/udev/udev-builtin-path_id.c

index 404196f409e76b21572cbed8584c11b59b231224..2bdeb40d50de921129f03c05a69a1440aaf3fab7 100644 (file)
@@ -14,6 +14,7 @@
 #include <unistd.h>
 
 #include "alloc-util.h"
+#include "device-util.h"
 #include "dirent-util.h"
 #include "fd-util.h"
 #include "parse-util.h"
@@ -589,6 +590,40 @@ static int find_real_nvme_parent(sd_device *dev, sd_device **ret) {
         return 0;
 }
 
+static void add_id_tag(sd_device *dev, bool test, const char *path) {
+        char tag[UDEV_NAME_SIZE];
+        size_t i = 0;
+        int r;
+
+        /* compose valid udev tag name */
+        for (const char *p = path; *p; p++) {
+                if (ascii_isdigit(*p) ||
+                    ascii_isalpha(*p) ||
+                    *p == '-') {
+                        tag[i++] = *p;
+                        continue;
+                }
+
+                /* skip all leading '_' */
+                if (i == 0)
+                        continue;
+
+                /* avoid second '_' */
+                if (tag[i-1] == '_')
+                        continue;
+
+                tag[i++] = '_';
+        }
+        /* strip trailing '_' */
+        while (i > 0 && tag[i-1] == '_')
+                i--;
+        tag[i] = '\0';
+
+        r = udev_builtin_add_property(dev, test, "ID_PATH_TAG", tag);
+        if (r < 0)
+                log_device_debug_errno(dev, r, "Failed to add ID_PATH_TAG property, ignoring: %m");
+}
+
 static int builtin_path_id(UdevEvent *event, int argc, char *argv[], bool test) {
         sd_device *dev = ASSERT_PTR(ASSERT_PTR(event)->dev);
         _cleanup_(sd_device_unrefp) sd_device *dev_other_branch = NULL;
@@ -748,37 +783,11 @@ static int builtin_path_id(UdevEvent *event, int argc, char *argv[], bool test)
             !supported_transport)
                 return -ENOENT;
 
-        {
-                char tag[UDEV_NAME_SIZE];
-                size_t i = 0;
-
-                /* compose valid udev tag name */
-                for (const char *p = path; *p; p++) {
-                        if (ascii_isdigit(*p) ||
-                            ascii_isalpha(*p) ||
-                            *p == '-') {
-                                tag[i++] = *p;
-                                continue;
-                        }
-
-                        /* skip all leading '_' */
-                        if (i == 0)
-                                continue;
-
-                        /* avoid second '_' */
-                        if (tag[i-1] == '_')
-                                continue;
-
-                        tag[i++] = '_';
-                }
-                /* strip trailing '_' */
-                while (i > 0 && tag[i-1] == '_')
-                        i--;
-                tag[i] = '\0';
+        r = udev_builtin_add_property(dev, test, "ID_PATH", path);
+        if (r < 0)
+                log_device_debug_errno(dev, r, "Failed to add ID_PATH property, ignoring: %m");
 
-                udev_builtin_add_property(dev, test, "ID_PATH", path);
-                udev_builtin_add_property(dev, test, "ID_PATH_TAG", tag);
-        }
+        add_id_tag(dev, test, path);
 
         /*
          * Compatible link generation for ATA devices