#!/bin/bash
# helpers for btrfs-convert tests

# mount image of converted filesystem of a given type
# $1: type of the filesystem
run_check_mount_convert_dev()
{
	local fstype
	local loop_opt

	setup_root_helper

	fstype="$1"
	shift
	if [ -z "$fstype" ]; then
		_fail "Missing source filesystem type"
	fi
	if [ "$fstype" = 'btrfs' ]; then
		_fail "Incorrect type for converted filesystem: btrfs"
	fi

	if [[ -b "$TEST_DEV" ]]; then
		loop_opt=""
	elif [[ -f "$TEST_DEV" ]]; then
		loop_opt="-o loop"
	else
		_fail "Invalid \$TEST_DEV: $TEST_DEV"
	fi

	[[ -d "$TEST_MNT" ]] || {
		_fail "Invalid \$TEST_MNT: $TEST_MNT"
	}

	run_check $SUDO_HELPER mount $loop_opt -t "$fstype" "$@" "$TEST_DEV" "$TEST_MNT"
}

populate_fs() {

        for dataset_type in 'small' 'hardlink' 'fast_symlink' 'brokenlink' 'perm' 'sparse' 'acls' 'fifo' 'slow_symlink'; do
		generate_dataset "$dataset_type"
	done
}

# verbose message before the test, same arguments as convert_test
convert_test_preamble() {
	local features
	local msg

	features="$1"
	msg="$2"
	shift 3
	echo "    [TEST/conv]     $msg, btrfs" "${features:-defaults}"
	echo "creating test image with: $@" >> "$RESULTS"
}

#  prepare TEST_DEV before conversion, create filesystem and mount it, image
#  size is 512MB
#  $1: type of the filesystem
#  $2+: free form, command to create the filesystem, with appended -F
convert_test_prep_fs() {
	local fstype
	local force
	local mountopts
	local oldsize

	fstype="$1"
	shift
	# Use device size that was set before, or the default 512M
	oldsize=$(stat --format=%s "$TEST_DEV")
	if [ -z "$oldsize" ]; then
		oldssize=512M
	fi
	# TEST_DEV not removed as the file might have special permissions, eg.
	# when test image is on NFS and would not be writable for root
	run_check truncate -s 0 "$TEST_DEV"
	# 256MB is the smallest acceptable btrfs image.
	run_check truncate -s "$oldsize" "$TEST_DEV"
	force=
	mountopts=
	case "$fstype" in
	ext[234])
		force=-F ;;
	reiserfs)
		force=-ff
		mountopts="-o acl,user_xattr,attrs" ;;
	ntfs)
		force=-F ;;
	*)
		_fail "unknown filesystem to convert: $fstype"
	esac
	run_check "$@" $force "$TEST_DEV"

	# create a file to check btrfs-convert can convert regular file correct
	run_check_mount_convert_dev "$fstype" $mountopts

	# create a file inside the fs before convert, to make sure there is
	# data covering btrfs backup superblock range (64M)
	run_check $SUDO_HELPER dd if=/dev/zero bs=1M count=64 \
		of="$TEST_MNT/convert_space_holder" status=noxfer
}

# generate md5 checksums of files on $TEST_MNT
# $1: path where the checksums will be stored
convert_test_gen_checksums() {
	_assert_path "$1"

	run_check $SUDO_HELPER dd if=/dev/zero of="$TEST_MNT/test" "bs=$nodesize" \
		count=1 status=noxfer >/dev/null 2>&1
	run_check_stdout $SUDO_HELPER find "$TEST_MNT" -type f ! -name 'image' -exec md5sum {} \+ > "$1"
}
# list $TEST_MNT data set file permissions.
# $1: path where the permissions will be stored
convert_test_perm() {
	local PERMTMP

	_assert_path "$1"
	PERMTMP="$1"
	FILES_LIST=$($SUDO_HELPER mktemp --tmpdir btrfs-progs-convert-filelist.XXXXXX)

	run_check $SUDO_HELPER dd if=/dev/zero of="$TEST_MNT/test" "bs=$nodesize" \
		count=1 status=noxfer >/dev/null 2>&1
	run_check_stdout $SUDO_HELPER find "$TEST_MNT" -type f ! -name 'image' -fprint "$FILES_LIST"
	# Fix directory entries order
	$SUDO_HELPER sort "$FILES_LIST" -o "$FILES_LIST"
	for file in `$SUDO_HELPER cat "$FILES_LIST"` ;do
		run_check_stdout $SUDO_HELPER getfacl --absolute-names "$file" >> "$PERMTMP"
	done
	$SUDO_HELPER rm -- "$FILES_LIST"
}
# list acls of files on $TEST_MNT
# $1: path where the acls will be stored
convert_test_acl() {
	local ACLSTMP
	ACLTMP="$1"
	FILES_LIST=$($SUDO_HELPER mktemp --tmpdir btrfs-progs-convert-filelist.XXXXXX)

	run_check_stdout $SUDO_HELPER find "$TEST_MNT/acls" -type f -fprint "$FILES_LIST"
	# Fix directory entries order
	$SUDO_HELPER sort "$FILES_LIST" -o "$FILES_LIST"
	for file in `$SUDO_HELPER cat "$FILES_LIST"`;do
		run_check_stdout $SUDO_HELPER getfattr --absolute-names -d "$file" >> "$ACLTMP"
	done
	$SUDO_HELPER rm -- "$FILES_LIST"
}

# do conversion with given features and nodesize, fsck afterwards
# $1: features, argument of -O, can be empty
# $2: nodesize, argument of -N, can be empty
# #3: (optional) filesystem type, default: btrfs
convert_test_do_convert() {
	export BTRFS_PROGS_DEBUG_STRICT_CHUNK_ALIGNMENT=y
	if [ "$3" = "ntfs" ]; then
		# Features and nodesize are not supported
		run_check "ntfs2btrfs" "$TEST_DEV"
	else
		run_check "$TOP/btrfs-convert" ${1:+-O "$1"} ${2:+-N "$2"} "$TEST_DEV"
	fi
	run_check "$TOP/btrfs" check "$TEST_DEV"
	run_check "$TOP/btrfs" inspect-internal dump-super -Ffa "$TEST_DEV"
	unset BTRFS_PROGS_DEBUG_STRICT_CHUNK_ALIGNMENT
}

# post conversion check, verify file permissions.
# $1: file with ext permissions.
convert_test_post_check_permissions() {
	local EXT_PERMTMP
	local BTRFS_PERMTMP

	_assert_path "$1"
	EXT_PERMTMP="$1"
	BTRFS_PERMTMP=$(mktemp --tmpdir btrfs-progs-convert-perms.XXXXXX)
	convert_test_perm "$BTRFS_PERMTMP"

	btrfs_perm=`md5sum "$BTRFS_PERMTMP" | cut -f1 -d' '`
	ext_perm=`md5sum "$EXT_PERMTMP" | cut -f1 -d' '`

	if [ "$btrfs_perm" != "$ext_perm" ];
	then
		btrfs_perm_file=`md5sum "$BTRFS_PERMTMP" | cut -f2 -d' '`
		ext_perm_file=`md5sum "$EXT_PERMTMP" | cut -f2 -d' '`
		_fail "file permission failed. Mismatched BTRFS:$btrfs_perm_file:$btrfs_perm EXT:$ext_perm_file:$ext_perm"
	fi

	rm -- "$BTRFS_PERMTMP"
}
# post conversion check, compare BTRFS file acls against EXT.
# $1: file with ext acls.
convert_test_post_check_acl() {
	local EXT_ACLTMP
	local BTRFS_ACLTMP

	_assert_path "$1"
	EXT_ACLTMP="$1"
	BTRFS_ACLTMP=$(mktemp --tmpdir btrfs-progs-convert-acls.XXXXXXX)
	convert_test_acl "$BTRFS_ACLTMP"

	btrfs_acl=`md5sum "$BTRFS_ACLTMP" | cut -f1 -d' '`
	ext_acl=`md5sum "$EXT_ACLTMP" | cut -f1 -d' '`

	if [ "$btrfs_acl" != "$ext_acl" ]
	then
		btrfs_acl_file=`md5sum "$BTRFS_ACLTMP" | cut -f2 -d' '`
		ext_acl_file=`md5sum "$EXT_ACLTMP" | cut -f2 -d' '`
		_fail "file acl failed. Mismatched BTRFS:$btrfs_acl_file:$btrfs_acl EXT:$ext_acl_file:$ext_acl"
	fi

	rm -- "$BTRFS_ACLTMP"
}

# post conversion checks, verify md5sums
convert_test_post_check_checksums() {
	_assert_path "$1"
	run_check_stdout $SUDO_HELPER md5sum -c "$1" |
		grep -q 'FAILED' && _fail "file validation failed"
}

# Check if convert can mount the image based on features
# $1: name of the feature
convert_can_mount() {
	local features="$1"

	if [ "$features" = 'block-group-tree' ]; then
		if ! [ -f "/sys/fs/btrfs/features/block_group_tree" ]; then
			_log "Skip mount checks due to missing support of block-group-tree"
			return 1
		fi
	fi
	if [ "$features" = 'raid-stripe-tree' ]; then
		if ! [ -f "/sys/fs/btrfs/features/raid_stripe_tree" ]; then
			_log "Skip mount checks due to missing support of raid-stripe-tree"
			return 1
		fi
	fi

	return 0
}

# post conversion checks, all three in one call, on an unmounted image
# $1: features for mount compatibility checks
# $2: file with checksums
# $3: file with permissions.
# $4: file with acl entries.
convert_test_post_checks_all() {
	local features="$1"

	_assert_path "$2"
	_assert_path "$3"
	_assert_path "$4"

	if ! convert_can_mount "$features"; then
		return 0
	fi

	run_check_mount_test_dev
	convert_test_post_check_checksums "$2"
	convert_test_post_check_permissions "$3"
	convert_test_post_check_acl "$4"

	# Create a large file to trigger data chunk allocation
	generate_dataset "large"
	run_check_umount_test_dev
}

# do rollback and fsck
# $1: filesystem name or alias (ext2 includes ext3 and ext4),
convert_test_post_rollback() {
	local types

	run_check "$TOP/btrfs-convert" --rollback "$TEST_DEV"
	if [ -z "$1" ]; then
		_fail "missing filesystem type to check"
	fi
	case "$1" in
		ext[234]) types=ext2,ext3,ext4 ;;
		reiserfs) types=reiserfs ;;
		ntfs) types=ntfs ;;
		*) _fail "unknown filesystem type to check: $1" ;;
	esac

	run_check fsck -n -t "$types" "$TEST_DEV"
}

# simple wrapper for a convert test
# $1: type of the converted filesystem
# $2: btrfs features, argument to -O
# $3: description of the test "ext2 8k nodesize"
# $4: nodesize value
# $5 + rest: command to create the ext2 image
convert_test() {
	local fstype
	local features
	local nodesize
	local msg
	local CHECKSUMTMP
	local EXT_PERMTMP
	local EXT_ACLTMP

	fstype="$1"
	features="$2"
	msg="$3"
	nodesize="$4"
	shift 4
	convert_test_preamble "$features" "$msg" "$nodesize" "$@"
	convert_test_prep_fs "$fstype" "$@"
	populate_fs
	CHECKSUMTMP=$(mktemp --tmpdir btrfs-progs-convert-csums.XXXXXX)
	EXT_PERMTMP=$(mktemp --tmpdir btrfs-progs-convert-perms.XXXXXX)
	EXT_ACLTMP=$(mktemp --tmpdir btrfs-progs-convert-acls.XXXXXX)
	convert_test_gen_checksums "$CHECKSUMTMP"
	convert_test_perm "$EXT_PERMTMP"
	convert_test_acl "$EXT_ACLTMP"

	run_check_umount_test_dev

	convert_test_do_convert "$features" "$nodesize" "$fstype"
	convert_test_post_checks_all "$features" "$CHECKSUMTMP" "$EXT_PERMTMP" "$EXT_ACLTMP"
	rm -- "$CHECKSUMTMP"
	rm -- "$EXT_PERMTMP"
	rm -- "$EXT_ACLTMP"

	convert_test_post_rollback "$fstype"
}

load_module_reiserfs()
{
	$SUDO_HELPER modprobe reiserfs
}

check_kernel_support_reiserfs()
{
	if ! grep -iq 'reiserfs' /proc/filesystems; then
		echo "WARNING: reiserfs filesystem not listed in /proc/filesystems, some tests might be skipped"
		return 1
	fi
	return 0
}

load_module_ntfs()
{
	# Load NTFS3 that has write support
	$SUDO_HELPER modprobe ntfs3
}

check_kernel_support_ntfs()
{
	if ! grep -iq 'ntfs' /proc/filesystems; then
		echo "WARNING: ntfs filesystem not listed in /proc/filesystems, some tests might be skipped"
		return 1
	fi
	return 0
}