dcavalca / rpms / util-linux

Forked from rpms/util-linux 2 years ago
Clone

Blame SOURCES/0061-libsmartcols-introduce-default-sort-column.patch

a0f4b9
From 543e87865c5b9b7cb08ce8d55da1ef414154d213 Mon Sep 17 00:00:00 2001
a0f4b9
From: Karel Zak <kzak@redhat.com>
a0f4b9
Date: Wed, 24 Mar 2021 12:43:17 +0100
a0f4b9
Subject: [PATCH 61/63] libsmartcols: introduce default sort column
a0f4b9
a0f4b9
* add default sort column, set by scols_sort_table()
a0f4b9
a0f4b9
* sort tree according to default sort column also in scols_sort_table_by_tree()
a0f4b9
a0f4b9
The function scols_sort_table() does not sort tree branches if tree
a0f4b9
is not enabled. The function scols_sort_table_by_tree() does not care
a0f4b9
if tree is enabled and it always follows parent->child relations. For
a0f4b9
scols_sort_table_by_tree() we need to follow order in branches if
a0f4b9
previously scols_sort_table() has been called.
a0f4b9
a0f4b9
For example lsblk calls
a0f4b9
a0f4b9
	scols_sort_table(tb, cl);
a0f4b9
	scols_sort_table_by_tree(tb);
a0f4b9
a0f4b9
for list-like output (for example lsblk -P) and users assume the
a0f4b9
same order as for tree (lsblk --tree).
a0f4b9
a0f4b9
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=1940607
a0f4b9
Upstream: http://github.com/karelzak/util-linux/commit/529b51706ef06611a8165023f14e6593e06901de
a0f4b9
Signed-off-by: Karel Zak <kzak@redhat.com>
a0f4b9
---
a0f4b9
 libsmartcols/src/smartcolsP.h  |  3 ++
a0f4b9
 libsmartcols/src/table.c       | 60 ++++++++++++++++++++++++----------
a0f4b9
 libsmartcols/src/table_print.c |  6 ++--
a0f4b9
 misc-utils/lsblk.c             |  6 ++--
a0f4b9
 4 files changed, 52 insertions(+), 23 deletions(-)
a0f4b9
a0f4b9
diff --git a/libsmartcols/src/smartcolsP.h b/libsmartcols/src/smartcolsP.h
a0f4b9
index 510e7a980..8037fb9f5 100644
a0f4b9
--- a/libsmartcols/src/smartcolsP.h
a0f4b9
+++ b/libsmartcols/src/smartcolsP.h
a0f4b9
@@ -160,6 +160,9 @@ struct libscols_table {
a0f4b9
 
a0f4b9
 	struct list_head	tb_columns;
a0f4b9
 	struct list_head	tb_lines;
a0f4b9
+
a0f4b9
+	struct libscols_column	*dflt_sort_column;	/* default sort column, set by scols_sort_table() */
a0f4b9
+
a0f4b9
 	struct libscols_symbols	*symbols;
a0f4b9
 	struct libscols_cell	title;		/* optional table title (for humans) */
a0f4b9
 
a0f4b9
diff --git a/libsmartcols/src/table.c b/libsmartcols/src/table.c
a0f4b9
index 979a09a39..bbabc0817 100644
a0f4b9
--- a/libsmartcols/src/table.c
a0f4b9
+++ b/libsmartcols/src/table.c
a0f4b9
@@ -224,6 +224,8 @@ int scols_table_remove_column(struct libscols_table *tb,
a0f4b9
 
a0f4b9
 	if (cl->flags & SCOLS_FL_TREE)
a0f4b9
 		tb->ntreecols--;
a0f4b9
+	if (tb->dflt_sort_column == cl)
a0f4b9
+		tb->dflt_sort_column = NULL;
a0f4b9
 
a0f4b9
 	DBG(TAB, ul_debugobj(tb, "remove column"));
a0f4b9
 	list_del_init(&cl->cl_columns);
a0f4b9
@@ -1362,41 +1364,63 @@ static int sort_line_children(struct libscols_line *ln, struct libscols_column *
a0f4b9
 	return 0;
a0f4b9
 }
a0f4b9
 
a0f4b9
+static int  __scols_sort_tree(struct libscols_table *tb, struct libscols_column *cl)
a0f4b9
+{
a0f4b9
+	struct libscols_line *ln;
a0f4b9
+	struct libscols_iter itr;
a0f4b9
+
a0f4b9
+	if (!tb || !cl || !cl->cmpfunc)
a0f4b9
+		return -EINVAL;
a0f4b9
+
a0f4b9
+	scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
a0f4b9
+	while (scols_table_next_line(tb, &itr, &ln) == 0)
a0f4b9
+		sort_line_children(ln, cl);
a0f4b9
+	return 0;
a0f4b9
+}
a0f4b9
+
a0f4b9
 /**
a0f4b9
  * scols_sort_table:
a0f4b9
  * @tb: table
a0f4b9
- * @cl: order by this column
a0f4b9
+ * @cl: order by this column or NULL
a0f4b9
  *
a0f4b9
  * Orders the table by the column. See also scols_column_set_cmpfunc(). If the
a0f4b9
  * tree output is enabled then children in the tree are recursively sorted too.
a0f4b9
  *
a0f4b9
+ * The column @cl is saved as the default sort column to the @tb and the next time
a0f4b9
+ * is possible to call scols_sort_table(tb, NULL). The saved column is also used by
a0f4b9
+ * scols_sort_table_by_tree().
a0f4b9
+ *
a0f4b9
  * Returns: 0, a negative value in case of an error.
a0f4b9
  */
a0f4b9
 int scols_sort_table(struct libscols_table *tb, struct libscols_column *cl)
a0f4b9
 {
a0f4b9
-	if (!tb || !cl || !cl->cmpfunc)
a0f4b9
+	if (!tb)
a0f4b9
+		return -EINVAL;
a0f4b9
+	if (!cl)
a0f4b9
+		cl = tb->dflt_sort_column;
a0f4b9
+	if (!cl || !cl->cmpfunc)
a0f4b9
 		return -EINVAL;
a0f4b9
 
a0f4b9
-	DBG(TAB, ul_debugobj(tb, "sorting table"));
a0f4b9
+	DBG(TAB, ul_debugobj(tb, "sorting table by %zu column", cl->seqnum));
a0f4b9
 	list_sort(&tb->tb_lines, cells_cmp_wrapper_lines, cl);
a0f4b9
 
a0f4b9
-	if (scols_table_is_tree(tb)) {
a0f4b9
-		struct libscols_line *ln;
a0f4b9
-		struct libscols_iter itr;
a0f4b9
+	if (scols_table_is_tree(tb))
a0f4b9
+		__scols_sort_tree(tb, cl);
a0f4b9
 
a0f4b9
-		scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
a0f4b9
-		while (scols_table_next_line(tb, &itr, &ln) == 0)
a0f4b9
-			sort_line_children(ln, cl);
a0f4b9
-	}
a0f4b9
+	if (cl && cl != tb->dflt_sort_column)
a0f4b9
+		tb->dflt_sort_column = cl;
a0f4b9
 
a0f4b9
 	return 0;
a0f4b9
 }
a0f4b9
 
a0f4b9
+/*
a0f4b9
+ * Move all @ln's children after @ln in the table.
a0f4b9
+ */
a0f4b9
 static struct libscols_line *move_line_and_children(struct libscols_line *ln, struct libscols_line *pre)
a0f4b9
 {
a0f4b9
 	if (pre) {
a0f4b9
 		list_del_init(&ln->ln_lines);			/* remove from old position */
a0f4b9
-	        list_add(&ln->ln_lines, &pre->ln_lines);        /* add to the new place (behind @pre) */
a0f4b9
+	        list_add(&ln->ln_lines, &pre->ln_lines);        /* add to the new place (after @pre) */
a0f4b9
 	}
a0f4b9
 	pre = ln;
a0f4b9
 
a0f4b9
@@ -1418,7 +1442,10 @@ static struct libscols_line *move_line_and_children(struct libscols_line *ln, st
a0f4b9
  * @tb: table
a0f4b9
  *
a0f4b9
  * Reorders lines in the table by parent->child relation. Note that order of
a0f4b9
- * the lines in the table is independent on the tree hierarchy.
a0f4b9
+ * the lines in the table is independent on the tree hierarchy by default.
a0f4b9
+ *
a0f4b9
+ * The children of the lines are sorted according to the default sort column
a0f4b9
+ * if scols_sort_table() has been previously called.
a0f4b9
  *
a0f4b9
  * Since: 2.30
a0f4b9
  *
a0f4b9
@@ -1434,13 +1461,12 @@ int scols_sort_table_by_tree(struct libscols_table *tb)
a0f4b9
 
a0f4b9
 	DBG(TAB, ul_debugobj(tb, "sorting table by tree"));
a0f4b9
 
a0f4b9
-	scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
a0f4b9
-	while (scols_table_next_line(tb, &itr, &ln) == 0) {
a0f4b9
-		if (ln->parent)
a0f4b9
-			continue;
a0f4b9
+	if (tb->dflt_sort_column)
a0f4b9
+		__scols_sort_tree(tb, tb->dflt_sort_column);
a0f4b9
 
a0f4b9
+	scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
a0f4b9
+	while (scols_table_next_line(tb, &itr, &ln) == 0)
a0f4b9
 		move_line_and_children(ln, NULL);
a0f4b9
-	}
a0f4b9
 
a0f4b9
 	return 0;
a0f4b9
 }
a0f4b9
diff --git a/libsmartcols/src/table_print.c b/libsmartcols/src/table_print.c
a0f4b9
index 8ecfc30e2..337dbbd84 100644
a0f4b9
--- a/libsmartcols/src/table_print.c
a0f4b9
+++ b/libsmartcols/src/table_print.c
a0f4b9
@@ -585,11 +585,9 @@ static int cell_to_buffer(struct libscols_table *tb,
a0f4b9
 
a0f4b9
 	ce = scols_line_get_cell(ln, cl->seqnum);
a0f4b9
 	data = ce ? scols_cell_get_data(ce) : NULL;
a0f4b9
-	if (!data)
a0f4b9
-		return 0;
a0f4b9
 
a0f4b9
 	if (!scols_column_is_tree(cl))
a0f4b9
-		return buffer_set_data(buf, data);
a0f4b9
+		return data ? buffer_set_data(buf, data) : 0;
a0f4b9
 
a0f4b9
 	/*
a0f4b9
 	 * Tree stuff
a0f4b9
@@ -605,7 +603,7 @@ static int cell_to_buffer(struct libscols_table *tb,
a0f4b9
 			buffer_set_art_index(buf);
a0f4b9
 	}
a0f4b9
 
a0f4b9
-	if (!rc)
a0f4b9
+	if (!rc && data)
a0f4b9
 		rc = buffer_append_data(buf, data);
a0f4b9
 	return rc;
a0f4b9
 }
a0f4b9
diff --git a/misc-utils/lsblk.c b/misc-utils/lsblk.c
a0f4b9
index d0369d3e7..cc7894ecf 100644
a0f4b9
--- a/misc-utils/lsblk.c
a0f4b9
+++ b/misc-utils/lsblk.c
a0f4b9
@@ -1894,10 +1894,12 @@ int main(int argc, char *argv[])
a0f4b9
 		 * /sys is no more sorted */
a0f4b9
 		lsblk->sort_id = COL_MAJMIN;
a0f4b9
 
a0f4b9
-	/* For --inverse --list we still follow parent->child relation */
a0f4b9
-	if (lsblk->inverse && !(lsblk->flags & LSBLK_TREE))
a0f4b9
+	/* For --{inverse,raw,pairs} --list we still follow parent->child relation */
a0f4b9
+	if (!(lsblk->flags & LSBLK_TREE)
a0f4b9
+	    && (lsblk->inverse || lsblk->flags & LSBLK_EXPORT || lsblk->flags & LSBLK_RAW))
a0f4b9
 		lsblk->force_tree_order = 1;
a0f4b9
 
a0f4b9
+
a0f4b9
 	if (lsblk->sort_id >= 0 && column_id_to_number(lsblk->sort_id) < 0) {
a0f4b9
 		/* the sort column is not between output columns -- add as hidden */
a0f4b9
 		add_column(lsblk->sort_id);
a0f4b9
-- 
a0f4b9
2.31.1
a0f4b9