diff mbox

[1/3] btrfs-progs: tests: Move extract_image out of check_all_images for common use

Message ID 154afcdd14c259551d843a157814fd0ff3589e8f.1441373012.git.zhaolei@cn.fujitsu.com (mailing list archive)
State New, archived
Headers show

Commit Message

Zhaolei Sept. 4, 2015, 1:23 p.m. UTC
Move code for extract image file to a function from check_all_images()
for common use, so caller can use this function to extrace single
image file.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
---
 tests/common | 73 ++++++++++++++++++++++++++++++++++--------------------------
 1 file changed, 41 insertions(+), 32 deletions(-)

Comments

David Sterba Sept. 22, 2015, 7:14 a.m. UTC | #1
On Fri, Sep 04, 2015 at 09:23:49PM +0800, Zhao Lei wrote:
> Move code for extract image file to a function from check_all_images()
> for common use, so caller can use this function to extrace single
> image file.

Moving the code to a wrapper is good but return value via a variable is
not. The function is string-to-string, so it can just echo it at the
end.
--
To unsubscribe from this list: send the line "unsubscribe linux-btrfs" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/tests/common b/tests/common
index 63b0d9f..0c0efef 100644
--- a/tests/common
+++ b/tests/common
@@ -62,6 +62,44 @@  check_image()
 	run_check $TOP/btrfs check $image
 }
 
+extract_image()
+{
+	local image="$1"
+
+	local cleanme=
+	case "$image" in
+	*.img)
+		rm -f $image.restored
+		: ;;
+	*.img.xz)
+		xz --decompress --keep "$image" || \
+			_fail "failed to decompress image $image"
+		image=${image%%.xz}
+		rm -f $image.restored
+		cleanme=$image
+		;;
+	*.raw)
+		cp --sparse=auto $image $image.restored
+		;;
+	*.raw.xz)
+		xz --decompress --keep "$image" || \
+			_fail "failed to decompress image $image"
+		image=${image%%.xz}
+		mv "$image" "$image".restored
+		;;
+	esac
+
+	if ! [ -f $image.restored ]; then
+		echo "restoring image $(basename $image)" >> $RESULTS
+		$TOP/btrfs-image -r $image $image.restored || \
+			_fail "failed to restore image $image"
+	fi
+
+	[[ "$cleanme" ]] && rm -f "$cleanme"
+
+	EXTRACT_IMAGE_OUTPUT="$image.restored"
+}
+
 # Process all image dumps in a given directory,
 # - raw btrfs filesystem images, suffix .raw
 # - dtto compressed by XZ, suffix .raw.xz
@@ -75,38 +113,9 @@  check_all_images()
 				-iname '*.raw' -o 	\
 				-iname '*.raw.xz' \) | sort)
 	do
-		cleanme=
-		case "$image" in
-		*.img)
-			rm -f $image.restored
-			: ;;
-		*.img.xz)
-			xz --decompress --keep "$image" || \
-				_fail "failed to decompress image $image"
-			image=${image%%.xz}
-			rm -f $image.restored
-			cleanme=$image
-			;;
-		*.raw)
-			cp --sparse=auto $image $image.restored
-			;;
-		*.raw.xz)
-			xz --decompress --keep "$image" || \
-				_fail "failed to decompress image $image"
-			image=${image%%.xz}
-			mv "$image" "$image".restored
-			;;
-		esac
-
-		if ! [ -f $image.restored ]; then
-			echo "restoring image $(basename $image)" >> $RESULTS
-			$TOP/btrfs-image -r $image $image.restored || \
-				_fail "failed to restore image $image"
-		fi
-
-		check_image $image.restored
-
-		rm -f $image.restored $cleanme
+		extract_image "$image"
+		check_image "$EXTRACT_IMAGE_OUTPUT"
+		rm -f "$EXTRACT_IMAGE_OUTPUT"
 	done
 }