]> git.proxmox.com Git - mirror_zfs.git/commitdiff
Fix "Add colored output to zfs list"
authorTino Reichardt <milky-zfs@mcmilk.de>
Wed, 5 Apr 2023 16:57:01 +0000 (18:57 +0200)
committerGitHub <noreply@github.com>
Wed, 5 Apr 2023 16:57:01 +0000 (09:57 -0700)
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 <behlendorf1@llnl.gov>
Reviewed-by: George Wilson <gwilson@delphix.com>
Signed-off-by: Tino Reichardt <milky-zfs@mcmilk.de>
Closes #14712

cmd/zfs/zfs_main.c
include/libzutil.h
lib/libzfs/libzfs.abi
lib/libzfs/libzfs_util.c

index f918036cb9b7fb050f8ebc29d9a664c886ca4bd0..d65b0157903713bb661a8bbb51f4d8664668c08e 100644 (file)
@@ -3556,8 +3556,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
index 465e463f0c1f21d8f05c2de98c2ab18f687568ef..237ff976ba626ccd2b2417034bac1b30b9455fd7 100644 (file)
@@ -182,6 +182,7 @@ struct zfs_cmd;
 #define        ANSI_RESET      "\033[0m"
 #define        ANSI_BOLD       "\033[1m"
 
+_LIBZUTIL_H int use_color(void);
 _LIBZUTIL_H void color_start(const char *color);
 _LIBZUTIL_H void color_end(void);
 _LIBZUTIL_H int printf_color(const char *color, const char *format, ...);
index 99e1b8cdf695b3e5f44fc4b1ce816c0a6ffea78a..2b61710f55923731bda2e8fe369356fdeeefd7c5 100644 (file)
     <elf-symbol name='tpool_suspend' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
     <elf-symbol name='tpool_suspended' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
     <elf-symbol name='tpool_wait' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
+    <elf-symbol name='use_color' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
     <elf-symbol name='update_vdev_config_dev_strs' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
     <elf-symbol name='vdev_expand_proplist' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
     <elf-symbol name='vdev_name_to_prop' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
       <parameter type-id='9cf59a50'/>
       <return type-id='48b5725f'/>
     </function-decl>
+    <function-decl name='use_color' mangled-name='use_color' visibility='default' binding='global' size-in-bits='64' elf-symbol-id='use_color'>
+      <return type-id='95e97e5e'/>
+    </function-decl>
     <function-decl name='mkdirp' mangled-name='mkdirp' visibility='default' binding='global' size-in-bits='64' elf-symbol-id='mkdirp'>
       <parameter type-id='80f4b756'/>
       <parameter type-id='d50d396c'/>
index 60695f8a63f4e195fde05d6931fe10a05f2bf405..393971ddf13c9327e4f2da4a923a54d7e933aa02 100644 (file)
@@ -1965,7 +1965,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;