Skip to content

Commit 952bddf

Browse files
mcmilktonyhutter
authored andcommitted
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 openzfs#14712
1 parent fc2c025 commit 952bddf

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

cmd/zfs/zfs_main.c

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

3592-
if (pl->pl_prop == ZFS_PROP_AVAILABLE)
3593-
color_start(zfs_list_avail_color(zhp));
3592+
/*
3593+
* zfs_list_avail_color() needs ZFS_PROP_AVAILABLE + USED
3594+
* - so we need another for() search for the USED part
3595+
* - when no colors wanted, we can skip the whole thing
3596+
*/
3597+
if (use_color() && pl->pl_prop == ZFS_PROP_AVAILABLE) {
3598+
zprop_list_t *pl2 = cb->cb_proplist;
3599+
for (; pl2 != NULL; pl2 = pl2->pl_next) {
3600+
if (pl2->pl_prop == ZFS_PROP_USED) {
3601+
color_start(zfs_list_avail_color(zhp));
3602+
/* found it, no need for more loops */
3603+
break;
3604+
}
3605+
}
3606+
}
35943607

35953608
/*
35963609
* 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
@@ -163,6 +163,7 @@ int zfs_ioctl_fd(int fd, unsigned long request, struct zfs_cmd *zc);
163163
#define ANSI_RESET "\033[0m"
164164
#define ANSI_BOLD "\033[1m"
165165

166+
int use_color(void);
166167
void color_start(const char *color);
167168
void color_end(void);
168169
int printf_color(const char *color, char *format, ...);

lib/libzfs/libzfs.abi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5070,6 +5070,9 @@
50705070
<parameter type-id='9cf59a50'/>
50715071
<return type-id='48b5725f'/>
50725072
</function-decl>
5073+
<function-decl name='use_color' mangled-name='use_color' visibility='default' binding='global' size-in-bits='64' elf-symbol-id='use_color'>
5074+
<return type-id='95e97e5e'/>
5075+
</function-decl>
50735076
<function-decl name='mkdirp' visibility='default' binding='global' size-in-bits='64'>
50745077
<parameter type-id='80f4b756'/>
50755078
<parameter type-id='d50d396c'/>

lib/libzfs/libzfs_util.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2027,7 +2027,7 @@ zfs_version_print(void)
20272027
* Return 1 if the user requested ANSI color output, and our terminal supports
20282028
* it. Return 0 for no color.
20292029
*/
2030-
static int
2030+
int
20312031
use_color(void)
20322032
{
20332033
static int use_color = -1;

0 commit comments

Comments
 (0)