Skip to content

Commit df19717

Browse files
matt-fiddRageLtMan
authored andcommitted
zfs get -p only outputs 3 columns if "clones" property is empty
get_clones_string currently returns an empty string for filesystem snapshots which have no clones. This breaks parsable `zfs get` output as only three columns are output, instead of 4. Reviewed-by: Brian Behlendorf <[email protected]> Signed-off-by: Matt Fiddaman <[email protected]> Co-authored-by: matt <[email protected]> Closes openzfs#11837
1 parent cfcab20 commit df19717

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

lib/libzfs/libzfs_dataset.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* Copyright 2017-2018 RackTop Systems.
3333
* Copyright (c) 2019 Datto Inc.
3434
* Copyright (c) 2019, loli10K <[email protected]>
35+
* Copyright (c) 2021 Matt Fiddaman
3536
*/
3637

3738
#include <ctype.h>
@@ -2385,7 +2386,7 @@ get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen)
23852386
nvpair_t *pair;
23862387

23872388
value = zfs_get_clones_nvl(zhp);
2388-
if (value == NULL)
2389+
if (value == NULL || nvlist_empty(value))
23892390
return (-1);
23902391

23912392
propbuf[0] = '\0';

tests/zfs-tests/tests/functional/cli_root/zfs_get/zfs_get_001_pos.ksh

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#
2929
# Copyright (c) 2016 by Delphix. All rights reserved.
30+
# Copyright (c) 2021 Matt Fiddaman
3031
#
3132

3233
. $STF_SUITE/tests/functional/cli_root/zfs_get/zfs_get_common.kshlib
@@ -72,7 +73,8 @@ typeset all_props=("${zfs_props[@]}" \
7273
"${zfs_props_os[@]}" \
7374
"${userquota_props[@]}")
7475
typeset dataset=($TESTPOOL/$TESTCTR $TESTPOOL/$TESTFS $TESTPOOL/$TESTVOL \
75-
$TESTPOOL/$TESTFS@$TESTSNAP $TESTPOOL/$TESTVOL@$TESTSNAP)
76+
$TESTPOOL/$TESTFS@$TESTSNAP $TESTPOOL/$TESTVOL@$TESTSNAP
77+
$TESTPOOL/$TESTFS@$TESTSNAP1 $TESTPOOL/$TESTCLONE)
7678

7779
typeset bookmark_props=(creation)
7880
typeset bookmark=($TESTPOOL/$TESTFS#$TESTBKMARK $TESTPOOL/$TESTVOL#$TESTBKMARK)
@@ -102,13 +104,17 @@ function check_return_value
102104

103105
if [[ $item == $p ]]; then
104106
((found += 1))
107+
cols=$(echo $line | awk '{print NF}')
105108
break
106109
fi
107110
done < $TESTDIR/$TESTFILE0
108111

109112
if ((found == 0)); then
110113
log_fail "'zfs get $opt $props $dst' return " \
111114
"error message.'$p' haven't been found."
115+
elif [[ "$opt" == "-p" ]] && ((cols != 4)); then
116+
log_fail "'zfs get $opt $props $dst' returned " \
117+
"$cols columns instead of 4."
112118
fi
113119
done
114120

@@ -123,6 +129,10 @@ log_onexit cleanup
123129
create_snapshot $TESTPOOL/$TESTFS $TESTSNAP
124130
create_snapshot $TESTPOOL/$TESTVOL $TESTSNAP
125131

132+
# Create second snapshot and clone it
133+
create_snapshot $TESTPOOL/$TESTFS $TESTSNAP1
134+
create_clone $TESTPOOL/$TESTFS@$TESTSNAP1 $TESTPOOL/$TESTCLONE
135+
126136
# Create filesystem and volume's bookmark
127137
create_bookmark $TESTPOOL/$TESTFS $TESTSNAP $TESTBKMARK
128138
create_bookmark $TESTPOOL/$TESTVOL $TESTSNAP $TESTBKMARK

tests/zfs-tests/tests/functional/cli_root/zfs_get/zfs_get_common.kshlib

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#
2828
# Copyright (c) 2016 by Delphix. All rights reserved.
29+
# Copyright (c) 2021 Matt Fiddaman
2930
#
3031

3132
. $STF_SUITE/include/libtest.shlib
@@ -87,8 +88,8 @@ function gen_option_str # $elements $prefix $separator $counter
8788
}
8889

8990
#
90-
# Cleanup the volume snapshot, filesystem snapshot, volume bookmark, and
91-
# filesystem bookmark that were created for this test case.
91+
# Cleanup the volume snapshot, filesystem snapshots, clone, volume bookmark,
92+
# and filesystem bookmark that were created for this test case.
9293
#
9394
function cleanup
9495
{
@@ -97,6 +98,11 @@ function cleanup
9798
datasetexists $TESTPOOL/$TESTFS@$TESTSNAP && \
9899
destroy_snapshot $TESTPOOL/$TESTFS@$TESTSNAP
99100

101+
datasetexists $TESTPOOL/$TESTCLONE && \
102+
destroy_clone $TESTPOOL/$TESTCLONE
103+
datasetexists $TESTPOOL/$TESTFS@$TESTSNAP1 && \
104+
destroy_snapshot $TESTPOOL/$TESTFS@$TESTSNAP1
105+
100106
bkmarkexists $TESTPOOL/$TESTVOL#$TESTBKMARK && \
101107
destroy_bookmark $TESTPOOL/$TESTVOL#$TESTBKMARK
102108
bkmarkexists $TESTPOOL/$TESTFS#$TESTBKMARK && \

0 commit comments

Comments
 (0)