]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lsblk: reuse 'removable' flag from parent
authorKarel Zak <kzak@redhat.com>
Wed, 17 Oct 2018 11:48:43 +0000 (13:48 +0200)
committerKarel Zak <kzak@redhat.com>
Fri, 7 Dec 2018 11:32:58 +0000 (12:32 +0100)
It's used in the default output, let's make it a little bit more
effective.

Signed-off-by: Karel Zak <kzak@redhat.com>
misc-utils/lsblk-devtree.c
misc-utils/lsblk.c
misc-utils/lsblk.h

index 5bef44be0d59cfcfd767a80aa037591000e26422..efc165f54a84765348d7199973c8819be0bd38b3 100644 (file)
@@ -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);
index 6a678196635cb198a62ac8cb629fce7f1b27d697..aacc0e560506127dc9b2061d8c47533e84d2e4de 100644 (file)
@@ -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");
index f8e10c7ce44031c27fd012c4555f842c4b76249a..7cb4e3eeef572f684bb2bfd9247fd9afca5d5dd5 100644 (file)
@@ -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,