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

From: Jim Meyering <[email-masked]>
Date: Sat, 29 May 2010 10:21:46 +0200

Pádraig Brady wrote:
> Subject: [PATCH] truncate: improve handling of non regular files
>
> Previously we copied `dd` and suppressed error messages
> when truncating neither regular files or shared mem objects.
> This was valid for `dd`, as truncation is ancillary to copying
> it may also do, but for `truncate` we should display all errors.
> Also we used the st_size from non regular files which is undefined,
> so we display an error when the user tries this.
>
> * src/truncate (do_truncate):  Error when referencing the size
> of non regular files or non shared memory objects.  Display all
> errors returned by ftruncate().
> (main): Error when referencing the size of non regular files or
> non shared memory objects.  Don't suppress error messages for
> any file types that can't be opened for writing.
> * tests/misc/truncate-dir-fail: Check that referencing the
> size of a directory is not supported.
> * tests/misc/truncate-fifo: Ensure the test doesn't hang
> by using the `timeout` command.  Don't test the return from
> running ftruncate on the fifo as it's system dependent as
> to whether this fails or not.

Thanks again.
This all looks fine.

> -        }
> -      else if (S_ISREG (sb.st_mode) || S_ISDIR (sb.st_mode)
> -               || S_TYPEISSHM (&sb))
> -        {
> -          error (0, ftruncate_errno,
> -                 _("truncating %s at %" PRIdMAX " bytes"), quote (fname),
> -                 (intmax_t) nsize);
> -          return 1;
> -        }
> -      return 0;
> +      error (0, errno,
> +             _("truncating %s at %" PRIdMAX " bytes"), quote (fname),
> +             (intmax_t) nsize);

It's slightly clearer to say "failed ...":

                _("failed to truncate %s at %" PRIdMAX " bytes"), quote (fname),

Otherwise, e.g., truncating FILE ...: permission denied
might leave a user wondering whether or not the truncation
actually succeeded.  Sure, *we* know, but if they are not
as familiar with how command-line diagnostics work and/or
interpret the ":" as a "." or ";", then it looks like the
truncation succeeded.

...
> +++ b/tests/misc/truncate-fifo
> @@ -1,5 +1,5 @@
>  #!/bin/sh
> -# Make sure truncate works on fifos without hanging or errors
> +# Make sure truncate works on fifos without hanging
>
>  # Copyright (C) 2008-2010 Free Software Foundation, Inc.
>
> @@ -25,7 +25,7 @@ fi
>
>  mkfifo_or_skip_ "fifo"
>
> -
> -truncate -s0 "fifo" || fail=1
> +timeout 10 truncate -s0 "fifo"

Please remove the three sets of unneeded double quotes: s/"fifo"/fifo/.