]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lsblk: allow to specify tree column
authorKarel Zak <kzak@redhat.com>
Wed, 20 Feb 2019 11:49:24 +0000 (12:49 +0100)
committerKarel Zak <kzak@redhat.com>
Wed, 20 Feb 2019 11:49:24 +0000 (12:49 +0100)
* document --tree (was missing in the man page)
* add optional argument to --tree to specify tree

For example:

$ lsblk -o KNAME,SIZE,MOUNTPOINT --tree=KNAME /dev/dm-0
KNAME      SIZE MOUNTPOINT
dm-0     232.9G
└─dm-1   232.9G
  └─dm-2 232.9G

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

index 2787569fed5108b207dd47862e712fd8b6784829..07cd7a16fbbb1fdabde72cc9c39174bf939e9483 100644 (file)
@@ -30,6 +30,8 @@ is subject to change.  So whenever possible, you should avoid using default
 outputs in your scripts.  Always explicitly define expected columns by using
 .B \-\-output
 .I columns-list
 outputs in your scripts.  Always explicitly define expected columns by using
 .B \-\-output
 .I columns-list
+and
+.B \-\-list
 in environments where a stable output is required.
 .PP
 Note that
 in environments where a stable output is required.
 .PP
 Note that
@@ -135,6 +137,10 @@ Output info about SCSI devices only.  All partitions, slaves and holder devices
 Print dependencies in inverse order. If the \fB\-\-list\fR output is requested then
 the lines are still ordered by dependencies.
 .TP
 Print dependencies in inverse order. If the \fB\-\-list\fR output is requested then
 the lines are still ordered by dependencies.
 .TP
+.BR \-T , " \-\-tree" [ =\fIcolumn ]
+Force tree-like output format.  If \fIcolumn\fP is specified, then a tree is printed in the column.
+The default is NAME column.
+.TP
 .BR \-t , " \-\-topology"
 Output info about block-device topology.
 This option is equivalent to
 .BR \-t , " \-\-topology"
 Output info about block-device topology.
 This option is equivalent to
index ffa1f82bfbec07816c800a9f10e1be2b7a81373d..454f067c82891e592c1fcbf0605cb06b3f4e6e90 100644 (file)
@@ -146,7 +146,7 @@ struct colinfo {
 
 /* columns descriptions */
 static struct colinfo infos[] = {
 
 /* columns descriptions */
 static struct colinfo infos[] = {
-       [COL_NAME]   = { "NAME",    0.25, SCOLS_FL_TREE | SCOLS_FL_NOEXTREMES, N_("device name") },
+       [COL_NAME]   = { "NAME",    0.25, SCOLS_FL_NOEXTREMES, N_("device name") },
        [COL_KNAME]  = { "KNAME",   0.3, 0, N_("internal kernel device name") },
        [COL_PKNAME] = { "PKNAME",  0.3, 0, N_("internal parent kernel device name") },
        [COL_PATH]   = { "PATH",    0.3,  0, N_("path to the device node") },
        [COL_KNAME]  = { "KNAME",   0.3, 0, N_("internal kernel device name") },
        [COL_PKNAME] = { "PKNAME",  0.3, 0, N_("internal parent kernel device name") },
        [COL_PATH]   = { "PATH",    0.3,  0, N_("path to the device node") },
@@ -1725,7 +1725,7 @@ static void __attribute__((__noreturn__)) usage(void)
        fputs(_(" -O, --output-all     output all columns\n"), out);
        fputs(_(" -P, --pairs          use key=\"value\" output format\n"), out);
        fputs(_(" -S, --scsi           output info about SCSI devices\n"), out);
        fputs(_(" -O, --output-all     output all columns\n"), out);
        fputs(_(" -P, --pairs          use key=\"value\" output format\n"), out);
        fputs(_(" -S, --scsi           output info about SCSI devices\n"), out);
-       fputs(_(" -T, --tree           use tree format output\n"), out);
+       fputs(_(" -T, --tree[=<column>] use tree format output\n"), out);
        fputs(_(" -a, --all            print all devices\n"), out);
        fputs(_(" -b, --bytes          print SIZE in bytes rather than in human readable format\n"), out);
        fputs(_(" -d, --nodeps         don't print slaves or holders\n"), out);
        fputs(_(" -a, --all            print all devices\n"), out);
        fputs(_(" -b, --bytes          print SIZE in bytes rather than in human readable format\n"), out);
        fputs(_(" -d, --nodeps         don't print slaves or holders\n"), out);
@@ -1769,13 +1769,14 @@ int main(int argc, char *argv[])
        struct lsblk _ls = {
                .sort_id = -1,
                .dedup_id = -1,
        struct lsblk _ls = {
                .sort_id = -1,
                .dedup_id = -1,
-               .flags = LSBLK_TREE
+               .flags = LSBLK_TREE,
+               .tree_id = COL_NAME
        };
        struct lsblk_devtree *tr = NULL;
        int c, status = EXIT_FAILURE;
        char *outarg = NULL;
        size_t i;
        };
        struct lsblk_devtree *tr = NULL;
        int c, status = EXIT_FAILURE;
        char *outarg = NULL;
        size_t i;
-       int force_tree = 0;
+       int force_tree = 0, has_tree_col = 0;
 
        enum {
                OPT_SYSROOT = CHAR_MAX + 1
 
        enum {
                OPT_SYSROOT = CHAR_MAX + 1
@@ -1808,7 +1809,7 @@ int main(int argc, char *argv[])
                { "scsi",       no_argument,       NULL, 'S' },
                { "sort",       required_argument, NULL, 'x' },
                { "sysroot",    required_argument, NULL, OPT_SYSROOT },
                { "scsi",       no_argument,       NULL, 'S' },
                { "sort",       required_argument, NULL, 'x' },
                { "sysroot",    required_argument, NULL, OPT_SYSROOT },
-               { "tree",       no_argument,       NULL, 'T' },
+               { "tree",       optional_argument, NULL, 'T' },
                { "version",    no_argument,       NULL, 'V' },
                { NULL, 0, NULL, 0 },
        };
                { "version",    no_argument,       NULL, 'V' },
                { NULL, 0, NULL, 0 },
        };
@@ -1836,7 +1837,7 @@ int main(int argc, char *argv[])
        lsblk_init_debug();
 
        while((c = getopt_long(argc, argv,
        lsblk_init_debug();
 
        while((c = getopt_long(argc, argv,
-                              "abdDzE:e:fhJlnMmo:OpPiI:rstVSTx:", longopts, NULL)) != -1) {
+                              "abdDzE:e:fhJlnMmo:OpPiI:rstVST:x:", longopts, NULL)) != -1) {
 
                err_exclusive_options(c, longopts, excl, excl_st);
 
 
                err_exclusive_options(c, longopts, excl, excl_st);
 
@@ -1948,8 +1949,9 @@ int main(int argc, char *argv[])
                        break;
                case 'T':
                        force_tree = 1;
                        break;
                case 'T':
                        force_tree = 1;
+                       if (optarg)
+                               lsblk->tree_id = column_name_to_id(optarg, strlen(optarg));
                        break;
                        break;
-
                case OPT_SYSROOT:
                        lsblk->sysroot = optarg;
                        break;
                case OPT_SYSROOT:
                        lsblk->sysroot = optarg;
                        break;
@@ -2039,8 +2041,14 @@ int main(int argc, char *argv[])
                struct libscols_column *cl;
                int id = get_column_id(i), fl = ci->flags;
 
                struct libscols_column *cl;
                int id = get_column_id(i), fl = ci->flags;
 
-               if (!(lsblk->flags & LSBLK_TREE) && id == COL_NAME)
-                       fl &= ~SCOLS_FL_TREE;
+               if ((lsblk->flags & LSBLK_TREE)
+                   && has_tree_col == 0
+                   && id == lsblk->tree_id) {
+                       fl |= SCOLS_FL_TREE;
+                       fl &= ~SCOLS_FL_RIGHT;
+                       has_tree_col = 1;
+               }
+
                if (lsblk->sort_hidden && lsblk->sort_id == id)
                        fl |= SCOLS_FL_HIDDEN;
                if (lsblk->dedup_hidden && lsblk->dedup_id == id)
                if (lsblk->sort_hidden && lsblk->sort_id == id)
                        fl |= SCOLS_FL_HIDDEN;
                if (lsblk->dedup_hidden && lsblk->dedup_id == id)
index e15c5b42ad6c06a50df417de674eeed4682c53be..da2edc09b71ee3f74941ae3db83173b2676b1ee4 100644 (file)
@@ -35,7 +35,9 @@ struct lsblk {
        struct libscols_table *table;   /* output table */
 
        struct libscols_column *sort_col;/* sort output by this column */
        struct libscols_table *table;   /* output table */
 
        struct libscols_column *sort_col;/* sort output by this column */
-       int sort_id;
+
+       int sort_id;                    /* id of the sort column */
+       int tree_id;                    /* od of column used for tree */
 
        int dedup_id;
 
 
        int dedup_id;