#! /bin/bash
# SPDX-License-Identifier: GPL-2.0
# Copyright (c) 2000-2003,2008 Silicon Graphics, Inc.  All Rights Reserved.
#
# FS QA Test No. 073
#
# Test xfs_copy
#
. ./common/preamble
_begin_fstest copy auto

# Import common functions.
. ./common/filter
. ./common/attr

# don't put fs images in /tmp
imgs=$TEST_DIR/$$

# Override the default cleanup function.
_cleanup()
{
	cd /
	_scratch_unmount 2>/dev/null
	_unmount $imgs.loop 2>/dev/null
	_unmount $imgs.source_dir 2>/dev/null
	[ -n "$loop_dev1" ] && _destroy_loop_device $loop_dev1
	[ -n "$loop_dev2" ] && _destroy_loop_device $loop_dev2
	[ -d $imgs.loop ] && rmdir $imgs.loop
	[ -d $imgs.source_dir ] && rm -rf $imgs.source_dir
	rm -f $imgs.* $tmp.* /var/tmp/xfs_copy.log.*
}

_filter_copy()
{
	sed -e "s,$1,<DEVIMAGE>,g" -e "s,$2,<FSIMAGE1>,g" \
	    -e "s,$3,<DEVIMAGE>,g" -e "s,$4,<FSIMAGE2>,g"
}

_filter_path()
{
	sed -e "s,$1,<MNTPATH>,g" | LC_COLLATE=POSIX sort
}

_populate_scratch()
{
	POSIXLY_CORRECT=yes \
		dd if=/dev/zero of=$SCRATCH_MNT/big+attr count=1000 bs=4096
	[ "$FAST_POPULATE" = true ] && return
	echo $SCRATCH_MNT/big+attr | $here/src/fill2attr
	$here/src/fill2fs --bytes=1048576 --filesize=4096 --stddev=0 --force \
		--dir=$SCRATCH_MNT/fill --list=- > $tmp.manifest
}

_verify_copy()
{
	target=$1
	target_dir=$imgs.loop
	source=$2
	source_dir=$3

	[ $source = $SCRATCH_DEV ] && _scratch_mount

	echo checking new image
	_check_xfs_filesystem $target none none

	echo mounting new image on loopback
	rmdir $target_dir 2>/dev/null
	mkdir $target_dir

	loop_dev1=$(_create_loop_device $target)
	_mount $loop_dev1 $target_dir 2>/dev/null
	if [ $? -ne 0 ]; then
		echo retrying mount with nouuid option >>$seqres.full
		_mount -o nouuid $loop_dev1 $target_dir
		if [ $? -ne 0 ]; then
			echo mount failed - evil!
			return
		fi
	fi

	echo comparing new image files to old
	(cd $source_dir; find . -type f -print0 | xargs -0 md5sum) | \
	(cd $target_dir ; md5sum -c --quiet)

	echo comparing new image directories to old
	find $source_dir | _filter_path $source_dir > $tmp.manifest1
	find $target_dir | _filter_path $target_dir > $tmp.manifest2
	[ -s $tmp.manifest1 ] || echo no directory output
	diff -u $tmp.manifest1 $tmp.manifest2

	echo comparing new image geometry to old
	$XFS_INFO_PROG $source_dir \
		| _filter_copy $source $source_dir '/dev/loop[0-9]\+' '#' \
		| tr -s ' ' \
		> $tmp.geometry1
	$XFS_INFO_PROG $target_dir \
		| _filter_copy $target $target_dir '/dev/loop[0-9]\+' '#' \
		| tr -s ' ' \
		> $tmp.geometry2
	[ -s $tmp.geometry1 ] || echo no geometry output
	diff -u $tmp.geometry1 $tmp.geometry2

	echo unmounting and removing new image
	_unmount $source_dir
	_unmount $target_dir > /dev/null 2>&1
	_destroy_loop_device $loop_dev1
	unset loop_dev1
	rm -f $target
}

_require_test
_require_attrs
_require_xfs_copy
_require_scratch
_require_loop

_scratch_mkfs_xfs -dsize=41m,agcount=2 >>$seqres.full 2>&1
_scratch_mount

echo
echo === populating scratch device
_populate_scratch
_scratch_unmount 2>/dev/null

echo
echo === copying scratch device to single target
$XFS_COPY_PROG $SCRATCH_DEV $imgs.image | _filter_copy '#' $imgs.image '#' '#'
_verify_copy $imgs.image $SCRATCH_DEV $SCRATCH_MNT

echo
echo === copying scratch device to single target, duplicate UUID
$XFS_COPY_PROG -d $SCRATCH_DEV $imgs.image | _filter_copy '#' $imgs.image '#' '#'
_verify_copy $imgs.image $SCRATCH_DEV $SCRATCH_MNT

echo 
echo === copying scratch device to single target, large ro device
${MKFS_XFS_PROG} -dfile,name=$imgs.source,size=100g \
	| _filter_mkfs 2>/dev/null
rmdir $imgs.source_dir 2>/dev/null
mkdir $imgs.source_dir

loop_dev2=$(_create_loop_device $imgs.source)
_mount $loop_dev2 $imgs.source_dir
cp -a $here $imgs.source_dir
_mount -o remount,ro $loop_dev2 $imgs.source_dir
$XFS_COPY_PROG $loop_dev2 $imgs.image 2> /dev/null | _filter_copy '#' $imgs.image '#' '#'
_verify_copy $imgs.image $imgs.source $imgs.source_dir

_destroy_loop_device $loop_dev2
unset loop_dev2

echo
echo === copying scratch device to multiple targets
$XFS_COPY_PROG -L$imgs.log -b $SCRATCH_DEV $imgs.image1 $imgs.image2 \
	| _filter_copy '#' $imgs.image1 '#' $imgs.image2
_verify_copy $imgs.image1 $SCRATCH_DEV $SCRATCH_MNT
_verify_copy $imgs.image2 $SCRATCH_DEV $SCRATCH_MNT

# success, all done
status=0
exit
