138465Smsmith#!/bin/sh
238465Smsmith# $FreeBSD$
338465Smsmith
438465Smsmithdesc="ftruncate descrease/increase file size"
538465Smsmith
638465Smsmithdir=`dirname $0`
738465Smsmith. ${dir}/../misc.sh
838465Smsmith
938465Smsmithecho "1..21"
1038465Smsmith
1138465Smsmithn0=`namegen`
1238465Smsmithn1=`namegen`
1338465Smsmith
1438465Smsmithexpect 0 mkdir ${n1} 0755
1538465Smsmithcdir=`pwd`
1638465Smsmithcd ${n1}
1738465Smsmith
1838465Smsmithexpect 0 create ${n0} 0644
1938465Smsmithexpect 0 open ${n0} O_RDWR : ftruncate 0 1234567
2038465Smsmithexpect 1234567 lstat ${n0} size
2138465Smsmithexpect 0 open ${n0} O_WRONLY : ftruncate 0 567
2238465Smsmithexpect 567 lstat ${n0} size
2338465Smsmithexpect 0 unlink ${n0}
2438465Smsmith
2538465Smsmithdd if=/dev/random of=${n0} bs=12345 count=1 >/dev/null 2>&1
2638465Smsmithexpect 0 open ${n0} O_RDWR : ftruncate 0 23456
27124140Sobrienexpect 23456 lstat ${n0} size
28124140Sobrienexpect 0 open ${n0} O_WRONLY : ftruncate 0 1
29124140Sobrienexpect 1 lstat ${n0} size
3038465Smsmithexpect 0 unlink ${n0}
3167227Sobrien
3238465Smsmith# successful ftruncate(2) updates ctime.
33106738Sjakeexpect 0 create ${n0} 0644
3438465Smsmithctime1=`${fstest} stat ${n0} ctime`
3538465Smsmithsleep 1
3638465Smsmithexpect 0 open ${n0} O_RDWR : ftruncate 0 123
3738465Smsmithctime2=`${fstest} stat ${n0} ctime`
3838465Smsmithtest_check $ctime1 -lt $ctime2
3938465Smsmithexpect 0 unlink ${n0}
4038465Smsmith
4167227Sobrien# unsuccessful ftruncate(2) does not update ctime.
4238465Smsmithexpect 0 create ${n0} 0644
4367227Sobrienctime1=`${fstest} stat ${n0} ctime`
4438465Smsmithsleep 1
45106738Sjakeexpect EINVAL -u 65534 open ${n0} O_RDONLY : ftruncate 0 123
4638465Smsmithctime2=`${fstest} stat ${n0} ctime`
4738465Smsmithtest_check $ctime1 -eq $ctime2
4838465Smsmithexpect 0 unlink ${n0}
4938465Smsmith
5038465Smsmith# third argument should not affect permission.
51123703Sgrehanexpect 0 open ${n0} O_CREAT,O_RDWR 0 : ftruncate 0 0
52123703Sgrehanexpect 0 unlink ${n0}
5338465Smsmithexpect 0 chmod . 0777
5467227Sobrienexpect 0 -u 65534 open ${n0} O_CREAT,O_RDWR 0 : ftruncate 0 0
5538465Smsmithexpect 0 unlink ${n0}
5638465Smsmith
5738465Smsmithcd ${cdir}
5838465Smsmithexpect 0 rmdir ${n1}
5938465Smsmith