]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
blockdev-util: check if provided sd_device is for a whole block device
authorYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 5 Sep 2022 20:00:49 +0000 (05:00 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 7 Sep 2022 11:45:24 +0000 (20:45 +0900)
And split out partition_generator_new(), to make it usable in other
functions.

src/shared/blockdev-util.c

index 1a7419c549fbf3f91ffe2c925de4099971a079d8..9f3c9053dd959aa4b4e202881f77ae03c5e9d2d4 100644 (file)
@@ -509,6 +509,52 @@ int block_device_resize_partition(
         return RET_NERRNO(ioctl(fd, BLKPG, &ba));
 }
 
+static int partition_enumerator_new(sd_device *dev, sd_device_enumerator **ret) {
+        _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
+        const char *s;
+        int r;
+
+        assert(dev);
+        assert(ret);
+
+        r = sd_device_get_subsystem(dev, &s);
+        if (r < 0)
+                return r;
+
+        if (!streq(s, "block"))
+                return -ENOTBLK;
+
+        r = sd_device_get_devtype(dev, &s);
+        if (r < 0)
+                return r;
+
+        if (!streq(s, "disk")) /* Refuse invocation on partition block device, insist on "whole" device */
+                return -EINVAL;
+
+        r = sd_device_enumerator_new(&e);
+        if (r < 0)
+                return r;
+
+        r = sd_device_enumerator_allow_uninitialized(e);
+        if (r < 0)
+                return r;
+
+        r = sd_device_enumerator_add_match_parent(e, dev);
+        if (r < 0)
+                return r;
+
+        r = sd_device_enumerator_add_match_subsystem(e, "block", /* match = */ true);
+        if (r < 0)
+                return r;
+
+        r = sd_device_enumerator_add_match_property(e, "DEVTYPE", "partition");
+        if (r < 0)
+                return r;
+
+        *ret = TAKE_PTR(e);
+        return 0;
+}
+
 int block_device_remove_all_partitions(sd_device *dev, int fd) {
         _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
         _cleanup_(sd_device_unrefp) sd_device *dev_unref = NULL;
@@ -531,19 +577,7 @@ int block_device_remove_all_partitions(sd_device *dev, int fd) {
                 dev = dev_unref;
         }
 
-        r = sd_device_enumerator_new(&e);
-        if (r < 0)
-                return r;
-
-        r = sd_device_enumerator_add_match_parent(e, dev);
-        if (r < 0)
-                return r;
-
-        r = sd_device_enumerator_add_match_subsystem(e, "block", true);
-        if (r < 0)
-                return r;
-
-        r = sd_device_enumerator_add_match_property(e, "DEVTYPE", "partition");
+        r = partition_enumerator_new(dev, &e);
         if (r < 0)
                 return r;