]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-device: use path_extract_directory() at one more place
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 30 Mar 2022 19:11:30 +0000 (04:11 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 30 Mar 2022 22:25:24 +0000 (07:25 +0900)
src/libsystemd/sd-device/sd-device.c

index 545a0f5a5d763158645914dda64584e37ed34b94..d2fd322e2a14645366ae48d1601255c7a41f6d60 100644 (file)
@@ -735,7 +735,7 @@ _public_ int sd_device_get_syspath(sd_device *device, const char **ret) {
 
 static int device_new_from_child(sd_device **ret, sd_device *child) {
         _cleanup_free_ char *path = NULL;
-        const char *subdir, *syspath;
+        const char *syspath;
         int r;
 
         assert(ret);
@@ -745,25 +745,21 @@ static int device_new_from_child(sd_device **ret, sd_device *child) {
         if (r < 0)
                 return r;
 
-        path = strdup(syspath);
-        if (!path)
-                return -ENOMEM;
-        subdir = path + STRLEN("/sys");
-
         for (;;) {
-                char *pos;
+                _cleanup_free_ char *p = NULL;
 
-                pos = strrchr(subdir, '/');
-                if (!pos || pos < subdir + 2)
-                        return -ENODEV;
+                r = path_extract_directory(path ?: syspath, &p);
+                if (r < 0)
+                        return r;
 
-                *pos = '\0';
+                if (path_equal(p, "/sys"))
+                        return -ENODEV;
 
-                r = sd_device_new_from_syspath(ret, path);
-                if (r < 0)
-                        continue;
+                r = sd_device_new_from_syspath(ret, p);
+                if (r != -ENODEV)
+                        return r;
 
-                return 0;
+                free_and_replace(path, p);
         }
 }