common/btrfs: rename and enhance _require_btrfs to _require_btrfs_command
authorQu Wenruo <quwenruo@cn.fujitsu.com>
Thu, 8 Dec 2016 08:12:12 +0000 (16:12 +0800)
committerEryu Guan <eguan@redhat.com>
Fri, 9 Dec 2016 07:54:19 +0000 (15:54 +0800)
Rename _require_btrfs() to _require_btrfs_command() to avoid
confusion, as all other _require_btrfs_* has a quite clear suffix,
like _require_btrfs_mkfs_feature() or _require_btrfs_fs_feature().

Also enhance _require_btrfs_command() to accept 2nd level commands
or options.

Options will be determined by the first "-" char.  This is quite
useful for case like "btrfs inspect-internal dump-tree" and "btrfs
check --qgroup-report".

[eguan: make local vars in _require_btrfs_command "local"]

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
Reviewed-by: Eryu Guan <eguan@redhat.com>
Signed-off-by: Eryu Guan <eguan@redhat.com>
common/btrfs
tests/btrfs/004
tests/btrfs/048
tests/btrfs/059
tests/btrfs/131

index ab6497dafbd1f4cd00af52e9424805e8e818fdf8..d9350ea5a78202cf95d6a9763a18165d88379974 100644 (file)
@@ -10,16 +10,35 @@ _btrfs_get_subvolid()
        $BTRFS_UTIL_PROG sub list $mnt | grep $name | awk '{ print $2 }'
 }
 
+# _require_btrfs_command <command> [<subcommand>|<option>]
 # We check for btrfs and (optionally) features of the btrfs command
-_require_btrfs()
+# It can both subfunction like "inspect-internal dump-tree" and
+# options like "check --qgroup-report"
+_require_btrfs_command()
 {
-       cmd=$1
+       local cmd=$1
+       local param=$2
+       local safe_param
+
        _require_command "$BTRFS_UTIL_PROG" btrfs
        if [ -z "$1" ]; then
                return 1;
        fi
-       $BTRFS_UTIL_PROG $cmd --help >/dev/null 2>&1
+       $BTRFS_UTIL_PROG $cmd --help &> /dev/null
        [ $? -eq 0 ] || _notrun "$BTRFS_UTIL_PROG too old (must support $cmd)"
+
+       test -z "$param" && return
+
+       # If $param is an option, replace leading "-"s for grep
+       if [ ${param:0:1} == "-" ]; then
+               safe_param=$(echo $param | sed 's/^-*//')
+               $BTRFS_UTIL_PROG $cmd --help | grep -wq $safe_param || \
+                       _notrun "$BTRFS_UTIL_PROG too old (must support $cmd $param)"
+               return
+       fi
+
+       $BTRFS_UTIL_PROG $cmd $param --help &> /dev/null
+       [ $? -eq 0 ] || _notrun "$BTRFS_UTIL_PROG too old (must support $cmd $param)"
 }
 
 _run_btrfs_util_prog()
index 905770a50ca2a3f081f978c615e13195aefd3674..3f8330f2193cf82dff6bc56f7db8fba765559a68 100755 (executable)
@@ -51,7 +51,8 @@ _supported_fs btrfs
 _supported_os Linux
 _require_scratch
 _require_no_large_scratch_dev
-_require_btrfs inspect-internal
+_require_btrfs_command inspect-internal logical-resolve
+_require_btrfs_command inspect-internal inode-resolve
 _require_command "/usr/sbin/filefrag" filefrag
 
 rm -f $seqres.full
index 0b907b0300c922a406fc2707ce509bd16ba68c64..e03b3c57ad1915e74ce1bd8d8ae4465ba4faa823 100755 (executable)
@@ -48,7 +48,7 @@ _supported_fs btrfs
 _supported_os Linux
 _require_test
 _require_scratch
-_require_btrfs "property"
+_require_btrfs_command "property"
 
 send_files_dir=$TEST_DIR/btrfs-test-$seq
 
index 8f106d22fc2c40bfc56b4a7130f98669760215d6..2d1ec23eca0401f1fa447355dde455793c2629f8 100755 (executable)
@@ -51,7 +51,7 @@ _supported_fs btrfs
 _supported_os Linux
 _require_test
 _require_scratch
-_require_btrfs "property"
+_require_btrfs_command "property"
 
 rm -f $seqres.full
 
index d1a11d28a504cb20ec3085b15ace51a2f99b19e7..ce486e6870e100cc7e9a56a7eda82216afd98d82 100755 (executable)
@@ -48,7 +48,7 @@ rm -f $seqres.full
 _supported_fs btrfs
 _supported_os Linux
 _require_scratch
-_require_btrfs inspect-internal
+_require_btrfs_command inspect-internal dump-super
 
 mkfs_v1()
 {