From 5a54a01634fffb1d20db98ea7de9b06528d05ca4 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Thu, 11 May 2023 16:20:57 +0900 Subject: [PATCH] udev-builtin-path_id: split out add_id_tag() No functional change, just refactoring. --- src/udev/udev-builtin-path_id.c | 69 +++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 30 deletions(-) diff --git a/src/udev/udev-builtin-path_id.c b/src/udev/udev-builtin-path_id.c index 404196f409e..2bdeb40d50d 100644 --- a/src/udev/udev-builtin-path_id.c +++ b/src/udev/udev-builtin-path_id.c @@ -14,6 +14,7 @@ #include #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 -- 2.47.3