From: Yu Watanabe Date: Mon, 5 Sep 2022 20:00:49 +0000 (+0900) Subject: blockdev-util: check if provided sd_device is for a whole block device X-Git-Tag: v252-rc1~236^2~9 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=af15ee0368b96d95061ae7d28db99d7e341a6208;p=thirdparty%2Fsystemd.git blockdev-util: check if provided sd_device is for a whole block device And split out partition_generator_new(), to make it usable in other functions. --- diff --git a/src/shared/blockdev-util.c b/src/shared/blockdev-util.c index 1a7419c549f..9f3c9053dd9 100644 --- a/src/shared/blockdev-util.c +++ b/src/shared/blockdev-util.c @@ -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;