#!/bin/bash

echo -n "Check if ext2 fallthrus still fall through after a umount/remount... "

mount -o ro,loop ./img/ro ./mnt/union

dd if=/dev/zero of=./img/rw bs=4096 seek=255 count=1 >/dev/null 2>&1
/sbin/mkfs.ext2 -F -O whiteout,fallthru ./img/rw >/dev/null 2>&1
mount -o loop,union ./img/rw ./mnt/union/

# Create some fallthrus
ls ./mnt/union/ >/dev/null

# See if we can lookup through a fallthru
stat ./mnt/union/ro_file >/dev/null

if [ "$?" != "0" ]; then
    echo "Lookup of fallthru failed!"
    exit 1
fi

# Mount it somewhere else and make sure we haven't copied up the file
umount ./mnt/union
mount -o loop ./img/rw ./mnt/scratch

stat ./mnt/scratch/ro_file >/dev/null 2>&1

if [ "$?" = "0" ]; then
    echo "Test won't work, stat copied up the file instead of making a fallthru!"
    exit 1
fi

# Unmount it, remount it as a union, see if fallthru still works
umount ./mnt/scratch
mount -o loop,union ./img/rw ./mnt/union/

stat ./mnt/union/ro_file >/dev/null

if [ "$?" != "0" ]; then
    echo "Lookup of fallthru after umount/mount failed!"
    exit 1
fi

umount ./mnt/union
if [ "$?" != "0" ]; then
    echo "umount failed!"
    exit 1
fi
umount ./mnt/union
if [ "$?" != "0" ]; then
    echo "umount failed!"
    exit 1
fi

exit 0
