Blame SOURCES/coreutils-8.22-dd-progress.patch

f180de
From af2a4ed22594badd2719c0123441d69b17bd8328 Mon Sep 17 00:00:00 2001
f180de
From: Federico Simoncelli <fsimonce@redhat.com>
f180de
Date: Fri, 26 Sep 2014 17:12:32 +0000
f180de
Subject: [PATCH] dd: new status=progress level to print stats periodically
f180de
f180de
* src/dd.c: Report the transfer progress every second when the
f180de
new status=progress level is used.  Adjust the handling and
f180de
description of the status= option so that they're treated as
f180de
mutually exclusive levels, rather than flags with implicit precedence.
f180de
* doc/coreutils.texi (dd invocation): Document the new progress
f180de
status level.  Reference the new level in the description of SIGUSR1.
f180de
* tests/dd/stats.sh: Add new test for status=progress.
f180de
* tests/dd/misc.sh: Change so status=none only takes precedence
f180de
if it's the last level specified.
f180de
---
f180de
diff --git a/doc/coreutils.texi b/doc/coreutils.texi
f180de
index 7d32af5..03bb710 100644
f180de
--- a/doc/coreutils.texi
f180de
+++ b/doc/coreutils.texi
f180de
@@ -8631,24 +8631,32 @@ will ensure that @samp{count=} corresponds to complete input blocks
f180de
 rather than the traditional POSIX specified behavior of counting
f180de
 input read operations.
f180de
 
f180de
-@item status=@var{which}
f180de
+@item status=@var{level}
f180de
 @opindex status
f180de
 Transfer information is normally output to stderr upon
f180de
 receipt of the @samp{INFO} signal or when @command{dd} exits.
f180de
-Specifying @var{which} will identify which information to suppress.
f180de
+Specifying @var{level} will adjust the amount of information printed,
f180de
+with the last @var{level} specified taking precedence.
f180de
 
f180de
 @table @samp
f180de
 
f180de
-@item noxfer
f180de
-@opindex noxfer @r{dd status=}
f180de
-Do not print the transfer rate and volume statistics
f180de
-that normally make up the last status line.
f180de
-
f180de
 @item none
f180de
 @opindex none @r{dd status=}
f180de
 Do not print any informational or warning messages to stderr.
f180de
 Error messages are output as normal.
f180de
 
f180de
+@item noxfer
f180de
+@opindex noxfer @r{dd status=}
f180de
+Do not print the final transfer rate and volume statistics
f180de
+that normally make up the last status line.
f180de
+
f180de
+@item progress
f180de
+@opindex progress @r{dd status=}
f180de
+Print the transfer rate and volume statistics on stderr,
f180de
+when processing each input block.  Statistics are output
f180de
+on a single line at most once every second, but updates
f180de
+can be delayed when waiting on I/O.
f180de
+
f180de
 @end table
f180de
 
f180de
 @item conv=@var{conversion}[,@var{conversion}]@dots{}
f180de
@@ -9033,6 +9041,9 @@ The above script will output in the following format
f180de
 5120000000 bytes (5.1 GB) copied, 18.913 seconds, 271 MB/s
f180de
 @end example
f180de
 
f180de
+Note also the @samp{status=progress} option which periodically updates
f180de
+the last line of the transfer statistics above.
f180de
+
f180de
 @vindex POSIXLY_CORRECT
f180de
 On systems lacking the @samp{INFO} signal @command{dd} responds to the
f180de
 @samp{USR1} signal instead, unless the @env{POSIXLY_CORRECT}
f180de
diff --git a/src/dd.c b/src/dd.c
f180de
index d22ec59..4018190 100644
f180de
--- a/src/dd.c
f180de
+++ b/src/dd.c
f180de
@@ -34,6 +34,7 @@
f180de
 #include "long-options.h"
f180de
 #include "quote.h"
f180de
 #include "quotearg.h"
f180de
+#include "verror.h"
f180de
 #include "xstrtol.h"
f180de
 #include "xtime.h"
f180de
 
f180de
@@ -132,11 +133,13 @@ enum
f180de
     C_SPARSE = 0200000
f180de
   };
f180de
 
f180de
-/* Status bit masks.  */
f180de
+/* Status levels.  */
f180de
 enum
f180de
   {
f180de
-    STATUS_NOXFER = 01,
f180de
-    STATUS_NONE = 02
f180de
+    STATUS_NONE = 1,
f180de
+    STATUS_NOXFER = 2,
f180de
+    STATUS_DEFAULT = 3,
f180de
+    STATUS_PROGRESS = 4
f180de
   };
f180de
 
f180de
 /* The name of the input file, or NULL for the standard input. */
f180de
@@ -188,7 +191,7 @@ static int input_flags = 0;
f180de
 static int output_flags = 0;
f180de
 
f180de
 /* Status flags for what is printed to stderr.  */
f180de
-static int status_flags = 0;
f180de
+static int status_level = STATUS_DEFAULT;
f180de
 
f180de
 /* If nonzero, filter characters through the translation table.  */
f180de
 static bool translation_needed = false;
f180de
@@ -211,6 +214,12 @@ static uintmax_t w_bytes = 0;
f180de
 /* Time that dd started.  */
f180de
 static xtime_t start_time;
f180de
 
f180de
+/* Previous time for periodic progress.  */
f180de
+static xtime_t previous_time;
f180de
+
f180de
+/* Whether a '\n' is pending after writing progress.  */
f180de
+static bool newline_pending;
f180de
+
f180de
 /* True if input is seekable.  */
f180de
 static bool input_seekable;
f180de
 
f180de
@@ -373,8 +382,9 @@ static struct symbol_value const flags[] =
f180de
 /* Status, for status="...".  */
f180de
 static struct symbol_value const statuses[] =
f180de
 {
f180de
-  {"noxfer",	STATUS_NOXFER},
f180de
   {"none",	STATUS_NONE},
f180de
+  {"noxfer",	STATUS_NOXFER},
f180de
+  {"progress",	STATUS_PROGRESS},
f180de
   {"",		0}
f180de
 };
f180de
 
f180de
@@ -517,6 +527,25 @@ maybe_close_stdout (void)
f180de
     _exit (EXIT_FAILURE);
f180de
 }
f180de
 
f180de
+/* Like error() but handle any pending newline.  */
f180de
+
f180de
+static void _GL_ATTRIBUTE_FORMAT ((__printf__, 3, 4))
f180de
+nl_error (int status, int errnum, const char *fmt, ...)
f180de
+{
f180de
+  if (newline_pending)
f180de
+    {
f180de
+      fputc ('\n', stderr);
f180de
+      newline_pending = false;
f180de
+    }
f180de
+
f180de
+  va_list ap;
f180de
+  va_start (ap, fmt);
f180de
+  verror (status, errnum, fmt, ap);
f180de
+  va_end (ap);
f180de
+}
f180de
+
f180de
+#define error nl_error
f180de
+
f180de
 void
f180de
 usage (int status)
f180de
 {
f180de
@@ -546,8 +575,10 @@ Copy a file, converting and formatting according to the operands.\n\
f180de
   oflag=FLAGS     write as per the comma separated symbol list\n\
f180de
   seek=N          skip N obs-sized blocks at start of output\n\
f180de
   skip=N          skip N ibs-sized blocks at start of input\n\
f180de
-  status=WHICH    WHICH info to suppress outputting to stderr;\n\
f180de
-                  'noxfer' suppresses transfer stats, 'none' suppresses all\n\
f180de
+  status=LEVEL    The LEVEL of information to print to stderr;\n\
f180de
+                  'none' suppresses everything but error messages,\n\
f180de
+                  'noxfer' suppresses the final transfer statistics,\n\
f180de
+                  'progress' shows periodic transfer statistics\n\
f180de
 "), stdout);
f180de
       fputs (_("\
f180de
 \n\
f180de
@@ -724,8 +755,7 @@ multiple_bits_set (int i)
f180de
 /* Print transfer statistics.  */
f180de
 
f180de
 static void
f180de
-print_stats (void)
f180de
-{
f180de
+print_xfer_stats (xtime_t progress_time) {
f180de
   char hbuf[LONGEST_HUMAN_READABLE + 1];
f180de
   int human_opts =
f180de
     (human_autoscale | human_round_to_nearest
f180de
@@ -733,23 +763,8 @@ print_stats (void)
f180de
   double delta_s;
f180de
   char const *bytes_per_second;
f180de
 
f180de
-  if (status_flags & STATUS_NONE)
f180de
-    return;
f180de
-
f180de
-  fprintf (stderr,
f180de
-           _("%"PRIuMAX"+%"PRIuMAX" records in\n"
f180de
-             "%"PRIuMAX"+%"PRIuMAX" records out\n"),
f180de
-           r_full, r_partial, w_full, w_partial);
f180de
-
f180de
-  if (r_truncate != 0)
f180de
-    fprintf (stderr,
f180de
-             ngettext ("%"PRIuMAX" truncated record\n",
f180de
-                       "%"PRIuMAX" truncated records\n",
f180de
-                       select_plural (r_truncate)),
f180de
-             r_truncate);
f180de
-
f180de
-  if (status_flags & STATUS_NOXFER)
f180de
-    return;
f180de
+  if (progress_time)
f180de
+    fputc ('\r', stderr);
f180de
 
f180de
   /* Use integer arithmetic to compute the transfer rate,
f180de
      since that makes it easy to use SI abbreviations.  */
f180de
@@ -761,7 +776,8 @@ print_stats (void)
f180de
            w_bytes,
f180de
            human_readable (w_bytes, hbuf, human_opts, 1, 1));
f180de
 
f180de
-  xtime_t now = gethrxtime ();
f180de
+  xtime_t now = progress_time ? progress_time : gethrxtime ();
f180de
+
f180de
   if (start_time < now)
f180de
     {
f180de
       double XTIME_PRECISIONe0 = XTIME_PRECISION;
f180de
@@ -787,7 +803,42 @@ print_stats (void)
f180de
      but that was incorrect for languages like Polish.  To fix this
f180de
      bug we now use SI symbols even though they're a bit more
f180de
      confusing in English.  */
f180de
-  fprintf (stderr, _(", %g s, %s/s\n"), delta_s, bytes_per_second);
f180de
+  char const *time_fmt = _(", %g s, %s/s\n");;
f180de
+  if (progress_time)
f180de
+    time_fmt = _(", %.6f s, %s/s");  /* OK with '\r' as increasing width.  */
f180de
+  fprintf (stderr, time_fmt, delta_s, bytes_per_second);
f180de
+
f180de
+  newline_pending = !!progress_time;
f180de
+}
f180de
+
f180de
+static void
f180de
+print_stats (void)
f180de
+{
f180de
+  if (status_level == STATUS_NONE)
f180de
+    return;
f180de
+
f180de
+  if (newline_pending)
f180de
+    {
f180de
+      fputc ('\n', stderr);
f180de
+      newline_pending = false;
f180de
+    }
f180de
+
f180de
+  fprintf (stderr,
f180de
+           _("%"PRIuMAX"+%"PRIuMAX" records in\n"
f180de
+             "%"PRIuMAX"+%"PRIuMAX" records out\n"),
f180de
+           r_full, r_partial, w_full, w_partial);
f180de
+
f180de
+  if (r_truncate != 0)
f180de
+    fprintf (stderr,
f180de
+             ngettext ("%"PRIuMAX" truncated record\n",
f180de
+                       "%"PRIuMAX" truncated records\n",
f180de
+                       select_plural (r_truncate)),
f180de
+             r_truncate);
f180de
+
f180de
+  if (status_level == STATUS_NOXFER)
f180de
+    return;
f180de
+
f180de
+  print_xfer_stats (0);
f180de
 }
f180de
 
f180de
 /* An ordinary signal was received; arrange for the program to exit.  */
f180de
@@ -1035,7 +1086,7 @@ iread (int fd, char *buf, size_t size)
f180de
       if (0 < prev_nread && prev_nread < size)
f180de
         {
f180de
           uintmax_t prev = prev_nread;
f180de
-          if (!(status_flags & STATUS_NONE))
f180de
+          if (status_level != STATUS_NONE)
f180de
             error (0, 0, ngettext (("warning: partial read (%"PRIuMAX" byte); "
f180de
                                     "suggest iflag=fullblock"),
f180de
                                    ("warning: partial read (%"PRIuMAX" bytes); "
f180de
@@ -1086,7 +1137,7 @@ iwrite (int fd, char const *buf, size_t size)
f180de
     {
f180de
       int old_flags = fcntl (STDOUT_FILENO, F_GETFL);
f180de
       if (fcntl (STDOUT_FILENO, F_SETFL, old_flags & ~O_DIRECT) != 0
f180de
-          && !(status_flags & STATUS_NONE))
f180de
+          && status_level != STATUS_NONE)
f180de
         error (0, errno, _("failed to turn off O_DIRECT: %s"),
f180de
                quote (output_file));
f180de
 
f180de
@@ -1219,7 +1270,7 @@ operand_matches (char const *str, char const *pattern, char delim)
f180de
 
f180de
 static int
f180de
 parse_symbols (char const *str, struct symbol_value const *table,
f180de
-               char const *error_msgid)
f180de
+               bool exclusive, char const *error_msgid)
f180de
 {
f180de
   int value = 0;
f180de
 
f180de
@@ -1241,7 +1292,10 @@ parse_symbols (char const *str, struct symbol_value const *table,
f180de
             }
f180de
         }
f180de
 
f180de
-      value |= entry->value;
f180de
+      if (exclusive)
f180de
+        value = entry->value;
f180de
+      else
f180de
+        value |= entry->value;
f180de
       if (!strcomma)
f180de
         break;
f180de
       str = strcomma + 1;
f180de
@@ -1316,17 +1370,17 @@ scanargs (int argc, char *const *argv)
f180de
       else if (operand_is (name, "of"))
f180de
         output_file = val;
f180de
       else if (operand_is (name, "conv"))
f180de
-        conversions_mask |= parse_symbols (val, conversions,
f180de
+        conversions_mask |= parse_symbols (val, conversions, false,
f180de
                                            N_("invalid conversion"));
f180de
       else if (operand_is (name, "iflag"))
f180de
-        input_flags |= parse_symbols (val, flags,
f180de
+        input_flags |= parse_symbols (val, flags, false,
f180de
                                       N_("invalid input flag"));
f180de
       else if (operand_is (name, "oflag"))
f180de
-        output_flags |= parse_symbols (val, flags,
f180de
+        output_flags |= parse_symbols (val, flags, false,
f180de
                                        N_("invalid output flag"));
f180de
       else if (operand_is (name, "status"))
f180de
-        status_flags |= parse_symbols (val, statuses,
f180de
-                                       N_("invalid status flag"));
f180de
+        status_level = parse_symbols (val, statuses, true,
f180de
+                                      N_("invalid status level"));
f180de
       else
f180de
         {
f180de
           bool invalid = false;
f180de
@@ -1613,7 +1667,7 @@ skip_via_lseek (char const *filename, int fdesc, off_t offset, int whence)
f180de
       && ioctl (fdesc, MTIOCGET, &s2) == 0
f180de
       && MT_SAME_POSITION (s1, s2))
f180de
     {
f180de
-      if (!(status_flags & STATUS_NONE))
f180de
+      if (status_level != STATUS_NONE)
f180de
         error (0, 0, _("warning: working around lseek kernel bug for file "
f180de
                        "(%s)\n  of mt_type=0x%0lx -- "
f180de
                        "see <sys/mtio.h> for the list of types"),
f180de
@@ -1787,7 +1841,7 @@ advance_input_after_read_error (size_t nbytes)
f180de
           if (offset == input_offset)
f180de
             return true;
f180de
           diff = input_offset - offset;
f180de
-          if (! (0 <= diff && diff <= nbytes) && !(status_flags & STATUS_NONE))
f180de
+          if (! (0 <= diff && diff <= nbytes) && status_level != STATUS_NONE)
f180de
             error (0, 0, _("warning: invalid file offset after failed read"));
f180de
           if (0 <= skip_via_lseek (input_file, STDIN_FILENO, diff, SEEK_CUR))
f180de
             return true;
f180de
@@ -1986,7 +2040,7 @@ dd_copy (void)
f180de
              2. pipe has not enough data
f180de
              3. partial reads  */
f180de
       if ((us_blocks || (!input_offset_overflow && us_bytes))
f180de
-          && !(status_flags & STATUS_NONE))
f180de
+          && status_level != STATUS_NONE)
f180de
         {
f180de
           error (0, 0,
f180de
                  _("%s: cannot skip to specified offset"), quote (input_file));
f180de
@@ -2029,6 +2083,19 @@ dd_copy (void)
f180de
 
f180de
   while (1)
f180de
     {
f180de
+      if (status_level == STATUS_PROGRESS)
f180de
+        {
f180de
+          xtime_t progress_time = gethrxtime ();
f180de
+          uintmax_t delta_xtime = progress_time;
f180de
+          delta_xtime -= previous_time;
f180de
+          double XTIME_PRECISIONe0 = XTIME_PRECISION;
f180de
+          if (delta_xtime / XTIME_PRECISIONe0 > 1)
f180de
+            {
f180de
+              print_xfer_stats (progress_time);
f180de
+              previous_time = progress_time;
f180de
+            }
f180de
+        }
f180de
+
f180de
       if (r_partial + r_full >= max_records + !!max_bytes)
f180de
         break;
f180de
 
f180de
@@ -2053,7 +2120,7 @@ dd_copy (void)
f180de
 
f180de
       if (nread < 0)
f180de
         {
f180de
-          if (!(conversions_mask & C_NOERROR) || !(status_flags & STATUS_NONE))
f180de
+          if (!(conversions_mask & C_NOERROR) || status_level != STATUS_NONE)
f180de
             error (0, errno, _("error reading %s"), quote (input_file));
f180de
 
f180de
           if (conversions_mask & C_NOERROR)
f180de
@@ -2345,7 +2412,7 @@ main (int argc, char **argv)
f180de
         }
f180de
     }
f180de
 
f180de
-  start_time = gethrxtime ();
f180de
+  start_time = previous_time = gethrxtime ();
f180de
 
f180de
   exit_status = dd_copy ();
f180de
 
f180de
diff --git a/tests/dd/misc.sh b/tests/dd/misc.sh
f180de
index f877fdd..34dfba7 100755
f180de
--- a/tests/dd/misc.sh
f180de
+++ b/tests/dd/misc.sh
f180de
@@ -35,9 +35,12 @@ dd status=none if=$tmp_in of=/dev/null 2> err || fail=1
f180de
 test -s err && { cat err; fail=1; }
f180de
 dd status=none if=$tmp_in skip=2 of=/dev/null 2> err || fail=1
f180de
 test -s err && { cat err; fail=1; }
f180de
-# check status=none is cumulative with status=noxfer
f180de
-dd status=none status=noxfer if=$tmp_in of=/dev/null 2> err || fail=1
f180de
+# check later status=none overrides earlier status=noxfer
f180de
+dd status=noxfer status=none if=$tmp_in of=/dev/null 2> err || fail=1
f180de
 test -s err && { cat err; fail=1; }
f180de
+# check later status=noxfer overrides earlier status=none
f180de
+dd status=none status=noxfer if=$tmp_in of=/dev/null 2> err || fail=1
f180de
+compare /dev/null err && fail=1
f180de
 
f180de
 dd if=$tmp_in of=$tmp_out 2> /dev/null || fail=1
f180de
 compare $tmp_in $tmp_out || fail=1
f180de
diff --git a/tests/dd/stats.sh b/tests/dd/stats.sh
f180de
new file mode 100755
f180de
index 0000000..24b8c49 100755
f180de
--- /dev/null
f180de
+++ b/tests/dd/stats.sh
f180de
@@ -0,0 +1,65 @@
f180de
+#!/bin/sh
f180de
+# Check stats output for SIG{INFO,USR1} and status=progress
f180de
+
f180de
+# Copyright (C) 2014 Free Software Foundation, Inc.
f180de
+
f180de
+# This program is free software: you can redistribute it and/or modify
f180de
+# it under the terms of the GNU General Public License as published by
f180de
+# the Free Software Foundation, either version 3 of the License, or
f180de
+# (at your option) any later version.
f180de
+
f180de
+# This program is distributed in the hope that it will be useful,
f180de
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
f180de
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
f180de
+# GNU General Public License for more details.
f180de
+
f180de
+# You should have received a copy of the GNU General Public License
f180de
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
f180de
+
f180de
+. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
f180de
+print_ver_ dd
f180de
+
f180de
+env kill -l | grep '^INFO$' && SIGINFO='INFO' || SIGINFO='USR1'
f180de
+
f180de
+# This to avoid races in the USR1 case
f180de
+# as the dd process will terminate by default until
f180de
+# it has its handler enabled.
f180de
+trap '' $SIGINFO
f180de
+
f180de
+mkfifo_or_skip_ fifo
f180de
+
f180de
+for open in '' '1'; do
f180de
+  # Run dd with the fullblock iflag to avoid short reads
f180de
+  # which can be triggered by reception of signals
f180de
+  dd iflag=fullblock if=/dev/zero of=fifo count=100 bs=5000000 2>err & pid=$!
f180de
+
f180de
+  # Note if we sleep here we give dd a chance to exec and block on open.
f180de
+  # Otherwise we're probably testing SIG_IGN in the forked shell or early dd.
f180de
+  test "$open" && sleep .1
f180de
+
f180de
+  # dd will block on open until fifo is opened for reading.
f180de
+  # Timeout in case dd goes away erroneously which we check for below.
f180de
+  timeout 10 sh -c 'wc -c < fifo > nwritten' &
f180de
+
f180de
+  # Send lots of signals immediately to ensure dd not killed due
f180de
+  # to race setting handler, or blocking on open of fifo.
f180de
+  # Many signals also check that short reads are handled.
f180de
+  until ! kill -s $SIGINFO $pid 2>/dev/null; do
f180de
+    sleep .01
f180de
+  done
f180de
+
f180de
+  wait
f180de
+
f180de
+  # Ensure all data processed and at least last status written
f180de
+  grep '500000000 bytes .* copied' err || { cat err; fail=1; }
f180de
+done
f180de
+
f180de
+progress_output()
f180de
+{
f180de
+  { sleep "$1"; echo 1; } | dd bs=1 status=progress of=/dev/null 2>err
f180de
+  # Progress output should be for "byte ... copied", while final is "bytes ..."
f180de
+  grep 'byte .* copied' err
f180de
+}
f180de
+retry_delay_ progress_output 1 4 || { cat err; fail=1; }
f180de
+
f180de
+Exit $fail
f180de
--
f180de
cgit v0.9.0.2