Skip to content

Commit 6ecdd35

Browse files
authored
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 <[email protected]> Reviewed-by: George Wilson <[email protected]> Signed-off-by: Tino Reichardt <[email protected]> Closes #14712
1 parent 3399a30 commit 6ecdd35

File tree

4 files changed

+21
-3
lines changed

4 files changed

+21
-3
lines changed

cmd/zfs/zfs_main.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3556,8 +3556,21 @@ print_dataset(zfs_handle_t *zhp, list_cbdata_t *cb)
35563556
right_justify = B_FALSE;
35573557
}
35583558

3559-
if (pl->pl_prop == ZFS_PROP_AVAILABLE)
3560-
color_start(zfs_list_avail_color(zhp));
3559+
/*
3560+
* zfs_list_avail_color() needs ZFS_PROP_AVAILABLE + USED
3561+
* - so we need another for() search for the USED part
3562+
* - when no colors wanted, we can skip the whole thing
3563+
*/
3564+
if (use_color() && pl->pl_prop == ZFS_PROP_AVAILABLE) {
3565+
zprop_list_t *pl2 = cb->cb_proplist;
3566+
for (; pl2 != NULL; pl2 = pl2->pl_next) {
3567+
if (pl2->pl_prop == ZFS_PROP_USED) {
3568+
color_start(zfs_list_avail_color(zhp));
3569+
/* found it, no need for more loops */
3570+
break;
3571+
}
3572+
}
3573+
}
35613574

35623575
/*
35633576
* If this is being called in scripted mode, or if this is the

include/libzutil.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ struct zfs_cmd;
182182
#define ANSI_RESET "\033[0m"
183183
#define ANSI_BOLD "\033[1m"
184184

185+
_LIBZUTIL_H int use_color(void);
185186
_LIBZUTIL_H void color_start(const char *color);
186187
_LIBZUTIL_H void color_end(void);
187188
_LIBZUTIL_H int printf_color(const char *color, const char *format, ...);

lib/libzfs/libzfs.abi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@
259259
<elf-symbol name='tpool_suspend' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
260260
<elf-symbol name='tpool_suspended' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
261261
<elf-symbol name='tpool_wait' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
262+
<elf-symbol name='use_color' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
262263
<elf-symbol name='update_vdev_config_dev_strs' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
263264
<elf-symbol name='vdev_expand_proplist' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
264265
<elf-symbol name='vdev_name_to_prop' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
@@ -5338,6 +5339,9 @@
53385339
<parameter type-id='9cf59a50'/>
53395340
<return type-id='48b5725f'/>
53405341
</function-decl>
5342+
<function-decl name='use_color' mangled-name='use_color' visibility='default' binding='global' size-in-bits='64' elf-symbol-id='use_color'>
5343+
<return type-id='95e97e5e'/>
5344+
</function-decl>
53415345
<function-decl name='mkdirp' mangled-name='mkdirp' visibility='default' binding='global' size-in-bits='64' elf-symbol-id='mkdirp'>
53425346
<parameter type-id='80f4b756'/>
53435347
<parameter type-id='d50d396c'/>

lib/libzfs/libzfs_util.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1965,7 +1965,7 @@ zfs_version_print(void)
19651965
* Return 1 if the user requested ANSI color output, and our terminal supports
19661966
* it. Return 0 for no color.
19671967
*/
1968-
static int
1968+
int
19691969
use_color(void)
19701970
{
19711971
static int use_color = -1;

0 commit comments

Comments
 (0)