#
# Idempotent creat calls
#
TEST=`basename $0`

# Check for root squashing
touch fnord
mapuid=`ls -ln fnord | awk '{ print $3 }'`
mapgid=`ls -ln fnord | awk '{ print $4 }'`
rm -f fnord
myuid=`id -u`
mygid=`id -g`

if [ $mapuid != $myuid -o $mapgid != $mygid ]; then
	if [ $myuid = 0 ]; then
                echo "$TEST: Server seems to perform root squashing: "
	else
                echo "$TEST: Server performs unexpected mapping of uids: "
	fi
	echo "$TEST: Mapping uid $myuid -> $mapuid, gid $mygid -> $mapgid"
fi


rm -rf zap
mkdir zap

# Create some files using mode 066 umask 022.
interrupt -ns INT -t 20 -m "${TEST}init (idempotent creat)" \
	-- dirop -n 100 -m 0666 -x 022 -U $mapuid -G $mapgid \
					-f zap/foo creat

# Check whether the files are there and have the correct mode
interrupt -ns INT -t 20 -m "${TEST}a (idempotent creat - mode check)" \
	-- dirop -z -n 100 -m 0666 -x 022 -U $mapuid -G $mapgid \
					-f zap/foo creat
rm -rf zap

# Now chck for uid/gid changes. For this, we have to create
# files owned by a non-root user and creat them a second time as root.
if [ `id -u` != 0 ]; then
	echo "$TEST: Not running as root - skipping uid/gid creat checks"
	exit 0
fi
if [ $mapuid != 0 ]; then
	echo "$TEST: Server does root squashing - skipping uid/gid creat checks"
	exit 0
fi

mkdir -m 777 zap
dirop -z -n 100 -u 5555 -g 5555 -f zap/foo creat

# Now try with a different uid and see if it changes
interrupt -ns INT -t 20 -m "${TEST}b (idempotent creat - uid check)" \
	-- dirop -z -n 100 -u 0 -U 5555 -f zap/foo creat

# Now try with a different gid and see if it changes
interrupt -ns INT -t 20 -m "${TEST}c (idempotent creat - gid check)" \
	-- dirop -z -n 100 -g 0 -G 5555 -f zap/foo creat

rm -rf zap
