Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 29 Jul 2013 19:56:34 +1000 (EST)
From:      Bruce Evans <brde@optusnet.com.au>
To:        Chris Johns <chrisj@rtems.org>
Cc:        freebsd-standards@freebsd.org
Subject:   Re: truncate and open(O_TRUNC) times.
Message-ID:  <20130729191944.M928@besplex.bde.org>
In-Reply-To: <51F5813D.2030806@rtems.org>
References:  <51F5813D.2030806@rtems.org>

next in thread | previous in thread | raw e-mail | index | archive | help
On Mon, 29 Jul 2013, Chris Johns wrote:

> In the RTEMS project we have some test code that appears to fail on FreeBSD. 
> You can find a stripped down version at 
> http://www.rtems.org/ftp/pub/rtems/people/chrisj/fstimes/truncate-time-test.c
>
> The code does ..
>
>  fd = open (file01, O_CREAT | O_WRONLY, mode);
>  n = write (fd, databuf, len);
>  assert (n == len);
>  status = close (fd);
>  assert (status == 0);
> 
>  sleep(2);
>
>  status = truncate (file01, len);
>  assert (status == 0);
>
> The length does not change and given the file does not change our 
> interpretation of the truncate call is the times should not change.

POSIX is weirdly different for truncate().  It only requires truncate()
to change the times if it changed the size (this is not weird, but unusual),
while it requires ftruncate() to change the times if it succeeded.

>
> In the case of ..
>
>  fd = open (file03, O_CREAT | O_WRONLY, mode);
>  status = close (fd);
>  assert (status == 0);
>
>  sleep(2);
>
>  fd = open (file03, O_TRUNC | O_WRONLY, mode);
>  status = close (fd);
>  assert (status == 0);
>
> the times do change as expected.

POSIX requires open() with O_TRUNC to change the times if it succeeded
(so it acts like ftruncate() on its descriptor).

FreeBSD can't possibly do this as weirdly as POSIX specifies, since
it uses the same VOP_SETATTR() API to set the size for ftruncate() and
truncate(), and this API doesn't say who made the call.  Most file
systems seem to force the setting of the times if successful.  E.g.,
ffs_truncate() does nothing much except set the times if the size
didn't change.

Bruce



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20130729191944.M928>