Subject: Re: bug#6268: Suggestion: truncate should allow -r and -s options together

From: "Richard W.M. Jones" <[email-masked]>
Date: Tue, 1 Jun 2010 10:32:40 +0100

On Fri, May 28, 2010 at 09:59:39AM +0200, Jim Meyering wrote:
> Pádraig Brady wrote:
> > Subject: [PATCH] truncate: support sizes relative to an existing file
> >
> > * doc/coreutils.texi (truncate invocation): Mention that --reference
> > bases the --size rather than just setting it.
> > * src/truncate.c (usage): Likewise. Also remove the clause
> > describing --size and --reference as being mutually exclusive.
> > (do_truncate): Add an extra parameter to hold the size
> > of a referenced file, and use it if positive.
> > (main): Pass the size of a referenced file to do_truncate().
> > * tests/misc/truncate-parameters: Adjust for the new combinations.
> > * NEWS: Mention the change
> > Suggested by Richard W.M. Jones
> 
> Nice.  Thanks!
> 
> Not new with this change, but what about --ref=non-regular-file ?
> 
> Perhaps truncate should refuse to use st_size info from
> a non-regular file.
> 
> Here's a pathological example:
> 
>   $ echo abcdefgh > bar
>   $ strace -e ftruncate ./truncate --ref=/dev/tty bar
>   ftruncate(3, 0)                         = 0
>   $ wc -c bar
>   0 bar
> 
> It's obvious that such an example is not likely in practice, but since
> currently truncate's --ref uses stat, it would follow a symlink, too.
> 
> For a directory you'd get unportable and probably surprising results.
> Using such a reference size deserves at least a warning.

And of course the right behaviour for block devices might be to get
their size (ie. blockdev --getsize64 or the equivalent set of ioctls).

Rich.

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
virt-p2v converts physical machines to virtual machines.  Boot with a
live CD or over the network (PXE) and turn machines into Xen guests.
http://et.redhat.com/~rjones/virt-p2v