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 <behlendorf1@llnl.gov>
Reviewed-by: George Wilson <gwilson@delphix.com>
Signed-off-by: Tino Reichardt <milky-zfs@mcmilk.de>
Closes #14712
This commit is contained in:
Tino Reichardt 2023-04-05 18:57:01 +02:00 committed by Tony Hutter
parent fc2c0256c5
commit 3d9f37026d
4 changed files with 21 additions and 3 deletions

View File

@ -3589,8 +3589,21 @@ print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb)
right_justify = B_FALSE; right_justify = B_FALSE;
} }
if (pl->pl_prop == ZFS_PROP_AVAILABLE) /*
* 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)); 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 * If this is being called in scripted mode, or if this is the

View File

@ -163,6 +163,7 @@ int zfs_ioctl_fd(int fd, unsigned long request, struct zfs_cmd *zc);
#define ANSI_RESET "\033[0m" #define ANSI_RESET "\033[0m"
#define ANSI_BOLD "\033[1m" #define ANSI_BOLD "\033[1m"
int use_color(void);
void color_start(const char *color); void color_start(const char *color);
void color_end(void); void color_end(void);
int printf_color(const char *color, char *format, ...); int printf_color(const char *color, char *format, ...);

View File

@ -99,6 +99,7 @@
<elf-symbol name='sa_validate_shareopts' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/> <elf-symbol name='sa_validate_shareopts' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
<elf-symbol name='snapshot_namecheck' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/> <elf-symbol name='snapshot_namecheck' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
<elf-symbol name='unshare_one' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/> <elf-symbol name='unshare_one' 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='zcmd_alloc_dst_nvlist' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/> <elf-symbol name='zcmd_alloc_dst_nvlist' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
<elf-symbol name='zcmd_expand_dst_nvlist' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/> <elf-symbol name='zcmd_expand_dst_nvlist' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
<elf-symbol name='zcmd_free_nvlists' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/> <elf-symbol name='zcmd_free_nvlists' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
@ -5070,6 +5071,9 @@
<parameter type-id='9cf59a50'/> <parameter type-id='9cf59a50'/>
<return type-id='48b5725f'/> <return type-id='48b5725f'/>
</function-decl> </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' visibility='default' binding='global' size-in-bits='64'> <function-decl name='mkdirp' visibility='default' binding='global' size-in-bits='64'>
<parameter type-id='80f4b756'/> <parameter type-id='80f4b756'/>
<parameter type-id='d50d396c'/> <parameter type-id='d50d396c'/>

View File

@ -2027,7 +2027,7 @@ zfs_version_print(void)
* Return 1 if the user requested ANSI color output, and our terminal supports * Return 1 if the user requested ANSI color output, and our terminal supports
* it. Return 0 for no color. * it. Return 0 for no color.
*/ */
static int int
use_color(void) use_color(void)
{ {
static int use_color = -1; static int use_color = -1;