]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
md5sum: ensure a single status line per file
authorPádraig Brady <P@draigBrady.com>
Sun, 1 Nov 2015 18:48:22 +0000 (18:48 +0000)
committerPádraig Brady <P@draigBrady.com>
Wed, 4 Nov 2015 02:03:43 +0000 (02:03 +0000)
* src/md5sum.c: Use the same file name escaping method used
when generating and checking checksums.  I.E. ensure a single line
per file by starting the line with '\' for any file name containing '\n'
and replacing those with "\\n".
* NEWS: Move the item from changes in behavior to improvements,
since this is no longer a backwards incompat change when
processing stdout status messages.
* tests/misc/md5sum.pl: Remove quotes from expected status output.
* tests/misc/sha1sum.pl: Likewise.

NEWS
src/md5sum.c
tests/misc/md5sum.pl
tests/misc/sha1sum.pl

diff --git a/NEWS b/NEWS
index 349577f30afbd63553b02a41b6986359c14a67d8..30603e62ea0e13dd48b34602381b52318c85368c 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -26,14 +26,15 @@ GNU coreutils NEWS                                    -*- outline -*-
   df now prefers sources towards the root of a device when
   eliding duplicate bind mounted entries.
 
-  md5sum now quotes all printed file names to avoid confusing error messages.
-  This also affects sha1sum, sha224sum, sha256sum, sha384sum and sha512sum.
-
 ** Improvements
 
   All utilities now quote user supplied arguments in error strings,
   which avoids confusing error messages in the presence of '\r' chars etc.
 
+  md5sum now ensures a single line per file for status on standard output,
+  by using a '\' at the start of the line, and replacing any newlines with '\n'.
+  This also affects sha1sum, sha224sum, sha256sum, sha384sum and sha512sum.
+
   dircolors now supports globbing of TERM entries in its database.
   For example "TERM *256color*" is now supported.
 
index 564b7539236284f2faa17ada5dc9d58ae7c93858..16163cc9ea5bcc74dbd883e85fd78e4370d326e7 100644 (file)
@@ -417,6 +417,37 @@ hex_digits (unsigned char const *s)
   return *s == '\0';
 }
 
+/* If ESCAPE is true, then translate each NEWLINE byte to the string, "\\n",
+   and each backslash to "\\\\".  */
+static void
+print_filename (char const *file, bool escape)
+{
+  if (! escape)
+    {
+      fputs (file, stdout);
+      return;
+    }
+
+  while (*file)
+    {
+      switch (*file)
+        {
+        case '\n':
+          fputs ("\\n", stdout);
+          break;
+
+        case '\\':
+          fputs ("\\\\", stdout);
+          break;
+
+        default:
+          putchar (*file);
+          break;
+        }
+      file++;
+    }
+}
+
 /* An interface to the function, DIGEST_STREAM.
    Operate on FILENAME (it may be "-").
 
@@ -561,6 +592,9 @@ digest_check (const char *checkfile_name)
                                           '8', '9', 'a', 'b',
                                           'c', 'd', 'e', 'f' };
           bool ok;
+          /* Only escape in the edge case producing multiple lines,
+             to ease automatic processing of status output.  */
+          bool needs_escape = ! status_only && strchr (filename, '\n');
 
           ++n_properly_formatted_lines;
 
@@ -570,7 +604,12 @@ digest_check (const char *checkfile_name)
             {
               ++n_open_or_read_failures;
               if (!status_only)
-                printf (_("%s: FAILED open or read\n"), quote (filename));
+                {
+                  if (needs_escape)
+                    putchar ('\\');
+                  print_filename (filename, needs_escape);
+                  printf (": %s\n", _("FAILED open or read"));
+                }
             }
           else
             {
@@ -591,10 +630,17 @@ digest_check (const char *checkfile_name)
 
               if (!status_only)
                 {
+                  if (cnt != digest_bin_bytes || ! quiet)
+                    {
+                      if (needs_escape)
+                        putchar ('\\');
+                      print_filename (filename, needs_escape);
+                    }
+
                   if (cnt != digest_bin_bytes)
-                    printf ("%s: %s\n", quote (filename), _("FAILED"));
+                    printf (": %s\n", _("FAILED"));
                   else if (!quiet)
-                    printf ("%s: %s\n", quote (filename), _("OK"));
+                    printf (": %s\n", _("OK"));
                 }
             }
         }
@@ -657,37 +703,6 @@ digest_check (const char *checkfile_name)
           && (!strict || n_improperly_formatted_lines == 0));
 }
 
-/* If ESCAPE is true, then translate each NEWLINE byte to the string, "\\n",
-   and each backslash to "\\\\".  */
-static void
-print_filename (char const *file, bool escape)
-{
-  if (! escape)
-    {
-      fputs (file, stdout);
-      return;
-    }
-
-  while (*file)
-    {
-      switch (*file)
-        {
-        case '\n':
-          fputs ("\\n", stdout);
-          break;
-
-        case '\\':
-          fputs ("\\\\", stdout);
-          break;
-
-        default:
-          putchar (*file);
-          break;
-        }
-      file++;
-    }
-}
-
 int
 main (int argc, char **argv)
 {
index d29a4efa741fc72c07bfb65997821fd3496ae88e..c996582138b382b54c6272491710fc01f5f6fee1 100755 (executable)
@@ -44,12 +44,12 @@ my @Tests =
                                 {OUT=>"\\$degenerate  .\\\\foo\n"}],
      ['check-1', '--check', {AUX=> {f=> ''}},
                                 {IN=> {'f.md5' => "$degenerate  f\n"}},
-                                {OUT=>"'f': OK\n"}],
+                                {OUT=>"f: OK\n"}],
 
      # Same as above, but with an added empty line, to provoke --strict.
      ['ck-strict-1', '--check --strict', {AUX=> {f=> ''}},
                                 {IN=> {'f.md5' => "$degenerate  f\n\n"}},
-                                {OUT=>"'f': OK\n"},
+                                {OUT=>"f: OK\n"},
                                 {ERR=>"md5sum: "
                                  . "WARNING: 1 line is improperly formatted\n"},
                                 {EXIT=> 1}],
@@ -58,7 +58,7 @@ my @Tests =
      # lines are processed in spite of the preceding invalid input line.
      ['ck-strict-2', '--check --strict', {AUX=> {f=> ''}},
                                 {IN=> {'in.md5' => "\n$degenerate  f\n"}},
-                                {OUT=>"'f': OK\n"},
+                                {OUT=>"f: OK\n"},
                                 {ERR=>"md5sum: "
                                  . "WARNING: 1 line is improperly formatted\n"},
                                 {EXIT=> 1}],
@@ -69,7 +69,7 @@ my @Tests =
                                 {OUT=>""}],
      ['check-quiet2', '--check', '--quiet',
                                 {IN=>{'f.md5' => "$degenerate  f\n"}},
-                                {AUX=> {f=> 'foo'}}, {OUT=>"'f': FAILED\n"},
+                                {AUX=> {f=> 'foo'}}, {OUT=>"f: FAILED\n"},
                                 {ERR=>"md5sum: WARNING: 1 computed"
                                        . " checksum did NOT match\n"},
                                 {EXIT=> 1}],
@@ -80,7 +80,7 @@ my @Tests =
                                       . "$degenerate  f\n"
                                       . "invalid\n" }},
                                 {AUX=> {f=> 'foo'}},
-                                {OUT=>"'f': FAILED\n'f': FAILED\n"},
+                                {OUT=>"f: FAILED\nf: FAILED\n"},
                 {ERR=>"md5sum: WARNING: 1 line is improperly formatted\n"
                     . "md5sum: WARNING: 2 computed checksums did NOT match\n"},
                                 {EXIT=> 1}],
@@ -91,7 +91,7 @@ my @Tests =
                                       . "$degenerate  f\n"
                                       . "invalid\n" }},
                                 {AUX=> {f=> 'foo'}},
-                                {OUT=>"'f': FAILED\n'f': FAILED\n"},
+                                {OUT=>"f: FAILED\nf: FAILED\n"},
               {ERR=>"md5sum: 'f.md5': 3: "
                               . "improperly formatted MD5 checksum line\n"
                   . "md5sum: WARNING: 1 line is improperly formatted\n"
@@ -106,7 +106,7 @@ my @Tests =
                                        . "MD5 checksum lines found\n"},
                                 {EXIT=> 1}],
      ['check-bsd2', '--check', {IN=> {'f.md5' => "MD5 (f) = $degenerate\n"}},
-                                {AUX=> {f=> ''}}, {OUT=>"'f': OK\n"}],
+                                {AUX=> {f=> ''}}, {OUT=>"f: OK\n"}],
      ['check-bsd3', '--check', '--status',
                                 {IN=> {'f.md5' => "MD5 (f) = $degenerate\n"}},
                                 {AUX=> {f=> 'bar'}}, {EXIT=> 1}],
@@ -116,7 +116,7 @@ my @Tests =
                                        . "MD5 checksum lines found\n"},
                                 {EXIT=> 1}],
      ['check-openssl2', '--check', {IN=> {'f.md5' => "MD5(f)= $degenerate\n"}},
-                                {AUX=> {f=> ''}}, {OUT=>"'f': OK\n"}],
+                                {AUX=> {f=> ''}}, {OUT=>"f: OK\n"}],
      ['check-openssl3', '--check', '--status',
                                 {IN=> {'f.md5' => "MD5(f)= $degenerate\n"}},
                                 {AUX=> {f=> 'bar'}}, {EXIT=> 1}],
index 0c22e7a18c1868fefef93b165fc11e654a502476..b58a7f4a14033b39f0b03377d4072cdace29b9cc 100755 (executable)
@@ -58,7 +58,7 @@ my @Tests =
                         {EXIT=> 1}],
      ['check-bsd2', '--check',
                         {IN=> {'f.sha1' => "SHA1 (f) = $sha_degenerate\n"}},
-                        {AUX=> {f=> ''}}, {OUT=>"'f': OK\n"}],
+                        {AUX=> {f=> ''}}, {OUT=>"f: OK\n"}],
      ['check-bsd3', '--check', '--status',
                         {IN=> {'f.sha1' => "SHA1 (f) = $sha_degenerate\n"}},
                         {AUX=> {f=> 'bar'}}, {EXIT=> 1}],
@@ -70,7 +70,7 @@ my @Tests =
                         {EXIT=> 1}],
      ['check-openssl2', '--check',
                         {IN=> {'f.sha1' => "SHA1(f)= $sha_degenerate\n"}},
-                        {AUX=> {f=> ''}}, {OUT=>"'f': OK\n"}],
+                        {AUX=> {f=> ''}}, {OUT=>"f: OK\n"}],
      ['check-openssl3', '--check', '--status',
                         {IN=> {'f.sha1' => "SHA1(f)= $sha_degenerate\n"}},
                         {AUX=> {f=> 'bar'}}, {EXIT=> 1}],