From 81a8936c56b25192f23f82798da7f6a9ddf76feb Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Wed, 17 Oct 2018 13:48:43 +0200 Subject: lsblk: reuse 'removable' flag from parent It's used in the default output, let's make it a little bit more effective. Signed-off-by: Karel Zak --- misc-utils/lsblk-devtree.c | 2 +- misc-utils/lsblk.c | 34 +++++++++++++++++++++++++++++----- misc-utils/lsblk.h | 1 + 3 files changed, 31 insertions(+), 6 deletions(-) (limited to 'misc-utils') diff --git a/misc-utils/lsblk-devtree.c b/misc-utils/lsblk-devtree.c index 5bef44be0..efc165f54 100644 --- a/misc-utils/lsblk-devtree.c +++ b/misc-utils/lsblk-devtree.c @@ -21,7 +21,7 @@ struct lsblk_device *lsblk_new_device(struct lsblk_devtree *tree) return NULL; dev->refcount = 1; - + dev->removable = -1; dev->tree = tree; INIT_LIST_HEAD(&dev->deps); diff --git a/misc-utils/lsblk.c b/misc-utils/lsblk.c index 6a6781966..aacc0e560 100644 --- a/misc-utils/lsblk.c +++ b/misc-utils/lsblk.c @@ -674,6 +674,34 @@ static struct stat *device_get_stat(struct lsblk_device *dev) return &dev->st; } +static int is_removable_device(struct lsblk_device *dev, struct lsblk_device *parent) +{ + struct path_cxt *pc; + + if (dev->removable != -1) + goto done; + if (ul_path_scanf(dev->sysfs, "removable", "%d", &dev->removable) == 1) + goto done; + + if (parent) { + pc = sysfs_blkdev_get_parent(dev->sysfs); + if (!pc) + goto done; + + if (pc == parent->sysfs) + /* dev is partition and parent is whole-disk */ + dev->removable = is_removable_device(parent, NULL); + else + /* parent is something else, use sysfs parent */ + ul_path_scanf(pc, "removable", "%d", &dev->removable); + } + +done: + if (dev->removable == -1) + dev->removable = 0; + return dev->removable; +} + /* * Generates data (string) for column specified by column ID for specified device */ @@ -807,11 +835,7 @@ static void set_scols_data( str = xstrdup(is_readonly_device(dev) ? "1" : "0"); break; case COL_RM: - ul_path_read_string(dev->sysfs, &str, "removable"); - if (!str && sysfs_blkdev_get_parent(dev->sysfs)) - ul_path_read_string(sysfs_blkdev_get_parent(dev->sysfs), - &str, - "removable"); + str = xstrdup(is_removable_device(dev, parent) ? "1" : "0"); break; case COL_HOTPLUG: str = sysfs_blkdev_is_hotpluggable(dev->sysfs) ? xstrdup("1") : xstrdup("0"); diff --git a/misc-utils/lsblk.h b/misc-utils/lsblk.h index f8e10c7ce..7cb4e3eee 100644 --- a/misc-utils/lsblk.h +++ b/misc-utils/lsblk.h @@ -109,6 +109,7 @@ struct lsblk_device { int discard; /* supports discard */ uint64_t size; /* device size */ + int removable; /* unknown:-1, yes:1, not:0 */ unsigned int is_mounted : 1, is_swap : 1, -- cgit v1.2.3-55-g7522