From 3d9f37026d65a9ab3244c04a37fc49d02841ba10 Mon Sep 17 00:00:00 2001 From: Tino Reichardt Date: Wed, 5 Apr 2023 18:57:01 +0200 Subject: [PATCH] Fix "Add colored output to zfs list" Running `zfs list -o avail rpool` resulted in a core dump. This commit will fix this. Run the needed overhead only, when `use_color()` is true. Reviewed-by: Brian Behlendorf Reviewed-by: George Wilson Signed-off-by: Tino Reichardt Closes #14712 --- cmd/zfs/zfs_main.c | 17 +++++++++++++++-- include/libzutil.h | 1 + lib/libzfs/libzfs.abi | 4 ++++ lib/libzfs/libzfs_util.c | 2 +- 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/cmd/zfs/zfs_main.c b/cmd/zfs/zfs_main.c index ff6510e440..2d81ef31c4 100644 --- a/cmd/zfs/zfs_main.c +++ b/cmd/zfs/zfs_main.c @@ -3589,8 +3589,21 @@ print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb) right_justify = B_FALSE; } - if (pl->pl_prop == ZFS_PROP_AVAILABLE) - color_start(zfs_list_avail_color(zhp)); + /* + * zfs_list_avail_color() needs ZFS_PROP_AVAILABLE + USED + * - so we need another for() search for the USED part + * - when no colors wanted, we can skip the whole thing + */ + if (use_color() && pl->pl_prop == ZFS_PROP_AVAILABLE) { + zprop_list_t *pl2 = cb->cb_proplist; + for (; pl2 != NULL; pl2 = pl2->pl_next) { + if (pl2->pl_prop == ZFS_PROP_USED) { + color_start(zfs_list_avail_color(zhp)); + /* found it, no need for more loops */ + break; + } + } + } /* * If this is being called in scripted mode, or if this is the diff --git a/include/libzutil.h b/include/libzutil.h index 328487d78f..15024a4e88 100644 --- a/include/libzutil.h +++ b/include/libzutil.h @@ -163,6 +163,7 @@ int zfs_ioctl_fd(int fd, unsigned long request, struct zfs_cmd *zc); #define ANSI_RESET "\033[0m" #define ANSI_BOLD "\033[1m" +int use_color(void); void color_start(const char *color); void color_end(void); int printf_color(const char *color, char *format, ...); diff --git a/lib/libzfs/libzfs.abi b/lib/libzfs/libzfs.abi index 51c77ab34a..13ce19df9b 100644 --- a/lib/libzfs/libzfs.abi +++ b/lib/libzfs/libzfs.abi @@ -99,6 +99,7 @@ + @@ -5070,6 +5071,9 @@ + + + diff --git a/lib/libzfs/libzfs_util.c b/lib/libzfs/libzfs_util.c index 8aee9d30cd..7c4d310782 100644 --- a/lib/libzfs/libzfs_util.c +++ b/lib/libzfs/libzfs_util.c @@ -2027,7 +2027,7 @@ zfs_version_print(void) * Return 1 if the user requested ANSI color output, and our terminal supports * it. Return 0 for no color. */ -static int +int use_color(void) { static int use_color = -1;