Merge pull request #24725 from yuwata/blockdev-util-introduce-block_device_get_whole_disk
blockdev-util: introduce block_device_get_whole_disk()
This commit is contained in:
+53
-11
@@ -20,6 +20,55 @@
|
||||
#include "missing_magic.h"
|
||||
#include "parse-util.h"
|
||||
|
||||
static int block_device_is_whole_disk(sd_device *dev) {
|
||||
const char *s;
|
||||
int r;
|
||||
|
||||
assert(dev);
|
||||
|
||||
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;
|
||||
|
||||
return streq(s, "disk");
|
||||
}
|
||||
|
||||
int block_device_get_whole_disk(sd_device *dev, sd_device **ret) {
|
||||
int r;
|
||||
|
||||
assert(dev);
|
||||
assert(ret);
|
||||
|
||||
/* Do not unref returned sd_device object. */
|
||||
|
||||
r = block_device_is_whole_disk(dev);
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (r == 0) {
|
||||
r = sd_device_get_parent(dev, &dev);
|
||||
if (r == -ENOENT) /* Already removed? Let's return a recognizable error. */
|
||||
return -ENODEV;
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = block_device_is_whole_disk(dev);
|
||||
if (r < 0)
|
||||
return r;
|
||||
if (r == 0)
|
||||
return -ENXIO;
|
||||
}
|
||||
|
||||
*ret = dev;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int block_get_whole_disk(dev_t d, dev_t *ret) {
|
||||
char p[SYS_BLOCK_PATH_MAX("/partition")];
|
||||
_cleanup_free_ char *s = NULL;
|
||||
@@ -518,19 +567,12 @@ int partition_enumerator_new(sd_device *dev, sd_device_enumerator **ret) {
|
||||
assert(dev);
|
||||
assert(ret);
|
||||
|
||||
r = sd_device_get_subsystem(dev, &s);
|
||||
/* Refuse invocation on partition block device, insist on "whole" device */
|
||||
r = block_device_is_whole_disk(dev);
|
||||
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;
|
||||
if (r == 0)
|
||||
return -ENXIO; /* return a recognizable error */
|
||||
|
||||
r = sd_device_enumerator_new(&e);
|
||||
if (r < 0)
|
||||
|
||||
@@ -14,6 +14,8 @@
|
||||
#define xsprintf_sys_block_path(buf, suffix, devno) \
|
||||
xsprintf(buf, "/sys/dev/block/%u:%u%s", major(devno), minor(devno), strempty(suffix))
|
||||
|
||||
int block_device_get_whole_disk(sd_device *dev, sd_device **ret);
|
||||
|
||||
int block_get_whole_disk(dev_t d, dev_t *ret);
|
||||
int block_get_originating(dev_t d, dev_t *ret);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user