#!/bin/bash
# SPDX-License-Identifier: GPL-3.0+
# Copyright (C) 2017 Omar Sandoval
#
# Test loop device partition scanning. Regression test for commit e02898b42380
# ("loop: fix LO_FLAGS_PARTSCAN hang") and commit 758a58d0bc67 ("loop: set
# GENHD_FL_NO_PART_SCAN after blkdev_reread_part()").

. tests/loop/rc

DESCRIPTION="scan loop device partitions"
QUICK=1

requires() {
	_have_program parted
}

find_loop_partition_devices() {
	lsblk -lno NAME | sed -n "s/^${1}p//p" | sort
}

find_loop_partition_sysfs() {
	find "$sysfs/" -mindepth 1 -maxdepth 1 -name "$1"'p*' -printf '%f\n' |
		sed -n "s/^${1}p//p" | sort
}

test() {
	echo "Running ${TEST_NAME}"

	truncate -s 1G "$TMPDIR/img"
	parted "$TMPDIR/img" --script \
		mklabel msdos \
		mkpart primary 0% 50% \
		mkpart primary 50% 100%

	local loop_device name sysfs
	loop_device="$(losetup -P -f --show "$TMPDIR/img")"
	udevadm settle
	name="$(basename "$loop_device")"
	sysfs="/sys/block/$name"

	echo "Partition devices"
	find_loop_partition_devices "$name"
	echo "Partition sysfs nodes"
	find_loop_partition_sysfs "$name"

	losetup -d "$loop_device"
	udevadm settle

	echo "Partition devices after detach"
	find_loop_partition_devices "$name"
	echo "Partition sysfs nodes after detach"
	find_loop_partition_sysfs "$name"

	rm "$TMPDIR/img"
	echo "Test complete"
}
