bug#11424: coreutils: split tests hang on /dev/zero on GNU/Hurd
From:
Paul Eggert
Subject:
bug#11424: coreutils: split tests hang on /dev/zero on GNU/Hurd
Date:
Wed, 09 May 2012 23:55:42 -0700
User-agent:
Mozilla/5.0 (X11; Linux i686; rv:12.0) Gecko/20120430 Thunderbird/12.0.1
On 05/09/2012 11:36 PM, Jim Meyering wrote:
I see you pushed it.
Thanks for adding that line to NEWS:
You're welcome. I shook free some time to adjust the st_size patch,
and here's a new version that I think incorporates all the comments
so far:
From 0c9661af1b29e2999030cb93003d98673faabf83 Mon Sep 17 00:00:00 2001
From: Paul Eggert <address@hidden>
Date: Wed, 9 May 2012 23:53:16 -0700
Subject: [PATCH] maint: handle file sizes more reliably
Problem reported by Samuel Thibault in <http://debbugs.gnu.org/11424>.
* NEWS: Document this.
* src/dd.c (skip): Handle skipping past EOF on shared or typed
memory objects the same way as with regular files.
(dd_copy): It's OK to truncate shared memory objects.
* src/du.c (duinfo_add): Check for overflow.
(print_only_size): Report overflow.
(process_file): Ignore negative file sizes in the --apparent-size case.
* src/od.c (skip): Fix comment about st_size.
* src/split.c (main):
* src/truncate.c (do_ftruncate, main):
On files where st_size is not portable, fall back on using lseek
with SEEK_END to determine the size. Although strictly speaking
POSIX says the behavior is implementation-defined, in practice
if lseek returns a nonnegative value it's a reasonable one to
use for the file size.
* src/system.h (usable_st_size): Symlinks have reliable st_size too.
* tests/misc/truncate-dir-fail: Don't assume that getting the size
of a dir is not allowed, as it's now allowed on many platforms,
e.g., GNU/Linux.
---
NEWS | 3 ++
src/dd.c | 8 +++---
src/du.c | 12 ++++++---
src/od.c | 3 +-
src/split.c | 14 +++++++---
src/system.h | 3 +-
src/truncate.c | 56 ++++++++++++++++++++++++++++++-----------
tests/misc/truncate-dir-fail | 3 --
8 files changed, 69 insertions(+), 33 deletions(-)
diff --git a/NEWS b/NEWS
index 7ef2f54..e56f8fa 100644
--- a/NEWS
+++ b/NEWS
@@ -27,6 +27,9 @@ GNU coreutils NEWS -*-
outline -*-
** New features
+ split and truncate now allow any seekable files in situations where
+ the file size is needed, instead of insisting on regular files.
+
fmt now accepts the --goal=WIDTH (-g) option.
** Changes in behavior
diff --git a/src/dd.c b/src/dd.c
index 4626de2..163d514 100644
--- a/src/dd.c
+++ b/src/dd.c
@@ -1544,7 +1544,7 @@ skip (int fdesc, char const *file, uintmax_t records,
size_t blocksize,
struct stat st;
if (fstat (STDIN_FILENO, &st) != 0)
error (EXIT_FAILURE, errno, _("cannot fstat %s"), quote (file));
- if (S_ISREG (st.st_mode) && st.st_size < (input_offset + offset))
+ if (usable_st_size (&st) && st.st_size < input_offset + offset)
{
/* When skipping past EOF, return the number of _full_ blocks
* that are not skipped, and set offset to EOF, so the caller
@@ -2104,8 +2104,8 @@ dd_copy (void)
}
}
- /* If the last write was converted to a seek, then for a regular file,
- ftruncate to extend the size. */
+ /* If the last write was converted to a seek, then for a regular file
+ or shared memory object, ftruncate to extend the size. */
if (final_op_was_seek)
{
struct stat stdout_stat;
@@ -2114,7 +2114,7 @@ dd_copy (void)
error (0, errno, _("cannot fstat %s"), quote (output_file));
return EXIT_FAILURE;
}
- if (S_ISREG (stdout_stat.st_mode))
+ if (S_ISREG (stdout_stat.st_mode) || S_TYPEISSHM (&stdout_stat))
{
off_t output_offset = lseek (STDOUT_FILENO, 0, SEEK_CUR);
if (output_offset > stdout_stat.st_size)
diff --git a/src/du.c b/src/du.c
index 41c9535..7333941 100644
--- a/src/du.c
+++ b/src/du.c
@@ -99,7 +99,8 @@ duinfo_set (struct duinfo *a, uintmax_t size, struct timespec
tmax)
static inline void
duinfo_add (struct duinfo *a, struct duinfo const *b)
{
- a->size += b->size;
+ uintmax_t sum = a->size + b->size;
+ a->size = a->size <= sum ? sum : UINTMAX_MAX;
if (timespec_cmp (a->tmax, b->tmax) < 0)
a->tmax = b->tmax;
}
@@ -370,8 +371,11 @@ static void
print_only_size (uintmax_t n_bytes)
{
char buf[LONGEST_HUMAN_READABLE + 1];
- fputs (human_readable (n_bytes, buf, human_output_opts,
- 1, output_block_size), stdout);
+ fputs ((n_bytes == UINTMAX_MAX
+ ? _("Infinity")
+ : human_readable (n_bytes, buf, human_output_opts,
+ 1, output_block_size)),
+ stdout);
}
/* Print size (and optionally time) indicated by *PDUI, followed by STRING. */
{
- /* The st_size field is valid only for regular files
- (and for symbolic links, which cannot occur here).
+ /* The st_size field is valid for regular files.
If the number of bytes left to skip is larger than
the size of the current file, we can decrement n_skip
and go on to the next file. Skip this optimization also
diff --git a/src/split.c b/src/split.c
index 062aede..53ee271 100644
--- a/src/split.c
+++ b/src/split.c
@@ -1069,7 +1069,7 @@ main (int argc, char **argv)
static char const multipliers[] = "bEGKkMmPTYZ0";
int c;
int digits_optind = 0;
- off_t file_size;
+ off_t file_size IF_LINT (= 0);
initialize_main (&argc, &argv);
set_program_name (argv[0]);
@@ -1340,12 +1340,18 @@ main (int argc, char **argv)
if (in_blk_size == 0)
in_blk_size = io_blksize (stat_buf);
- /* stat.st_size is valid only for regular files. For others, use 0. */