coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] md5sum: Implemented --pedantic option to be more strict in v


From: Jim Meyering
Subject: Re: [PATCH] md5sum: Implemented --pedantic option to be more strict in verification mode
Date: Thu, 07 Jul 2011 09:22:25 +0200

schoenfeld / in-medias-res wrote:
>>From 156d7e829da3ab9d895a275c2cd02e52388bcd0d Mon Sep 17 00:00:00 2001
> From: Patrick Schoenfeld <address@hidden>
> Date: Mon, 17 Nov 2008 20:54:08 +0100
> Subject: [PATCH] md5sum: Implemented --pedantic option to be more strict in 
> verification mode
>
> * md5sum: Implemented a --pedantic option in --check mode, which lets md5sum
>   bail out with a non-zero exit code, if one or more improperly formatted line
>   is found. Feature request by Dan Jacobson.
>   (http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=353911)
...
> Please note that I'm still in process of copyright assignment.
> Send a mail to assign@... but got no reply yet.
...

> +  md5sum now recognizes a new option --pedantic when verifying md5sums in a 
> file
> +  (with --check) to let it return a non-zero exit code if one or more invalid
> +  lines are found
> +
>  ** Bug fixes

Thanks for the patch.
Your copyright assignment did go through.

I've rebased your patch and made a few minor changes (NEWS, --help,
indentation).
More will be required:
  - also describe the option in sections for sha1sum and the other
    sha*sum programs; probably use a macro to avoid duplication
  - when checking two or more files, don't stop processing (exit) upon
    an invalid line in the first.  i.e., process all files before exiting.
  - add tests
  - decide on the option name: I prefer --strict;  --pedantic comes
    with connotations that seem unjustified.
  - exercise all of this via the test suite

>From ed927a4be151e80799a49a0d9b479c85b264288d Mon Sep 17 00:00:00 2001
From: Patrick Schoenfeld <address@hidden>
Date: Thu, 7 Jul 2011 08:57:39 +0200
Subject: [PATCH] md5sum, sha1sum, etc: accept new option: --strict

Use this new option with --check when the input is expected to
consist solely of checksum lines.  With only --check, you would
get WARNINGs, but the program could still exit successfully.
With --strict, any invalid line makes the program exit non-zero.
* src/md5sum.c (FIXME):
* doc/coreutils.texi: Describe it.
* NEWS (New features): Mention it.
---
 NEWS               |    4 ++++
 doc/coreutils.texi |    6 ++++++
 src/md5sum.c       |   35 +++++++++++++++++++++++++++++++++--
 3 files changed, 43 insertions(+), 2 deletions(-)

diff --git a/NEWS b/NEWS
index d58df26..499084e 100644
--- a/NEWS
+++ b/NEWS
@@ -29,6 +29,10 @@ GNU coreutils NEWS                                    -*- 
outline -*-
   Note the use of single quotes, not double quotes.
   That creates files named xaa.xz, xab.xz and xac.xz.

+  md5sum accepts the new --pedantic option.  With --check, it makes the
+  tool exit non-zero for any invalid input line, rather than just warning.
+  This also affects sha1sum, sha224sum, sha384sum and sha512sum.
+
 ** Improvements

   shuf outputs small subsets of large permutations much more efficiently.
diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index c59af2f..8ccff69 100644
--- a/doc/coreutils.texi
+++ b/doc/coreutils.texi
@@ -3702,6 +3702,12 @@ md5sum invocation
 This option is useful only if all but a few lines in the checked input
 are valid.

+@itemx --pedantic
+@opindex --pedantic
+@cindex verifying MD5 checksums
+When verifying checksums, fail if one or more improperly formatted MD5 checksum
+line is found.
+
 @end table

 @exitstatus
diff --git a/src/md5sum.c b/src/md5sum.c
index 9bbdc60..9da9942 100644
--- a/src/md5sum.c
+++ b/src/md5sum.c
@@ -122,12 +122,17 @@ static bool warn = false;
 /* With --check, suppress the "OK" printed for each verified file.  */
 static bool quiet = false;

+/* With --check, exit with a non-zero return code, if any line is
+   improperly formatted. */
+static bool pedantic = false;
+
 /* For long options that have no equivalent short option, use a
    non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
 enum
 {
   STATUS_OPTION = CHAR_MAX + 1,
-  QUIET_OPTION
+  QUIET_OPTION,
+  PEDANTIC_OPTION
 };

 static struct option const long_options[] =
@@ -138,6 +143,7 @@ static struct option const long_options[] =
   { "status", no_argument, NULL, STATUS_OPTION },
   { "text", no_argument, NULL, 't' },
   { "warn", no_argument, NULL, 'w' },
+  { "pedantic", no_argument, NULL, PEDANTIC_OPTION },
   { GETOPT_HELP_OPTION_DECL },
   { GETOPT_VERSION_OPTION_DECL },
   { NULL, 0, NULL, 0 }
@@ -187,6 +193,9 @@ The following three options are useful only when verifying 
checksums:\n\
   -w, --warn           warn about improperly formatted checksum lines\n\
 \n\
 "), stdout);
+      fputs (_("\
+      --pedantic       with --check, exit non-zero for any invalid input\n\
+"), stdout);
       fputs (HELP_OPTION_DESCRIPTION, stdout);
       fputs (VERSION_OPTION_DESCRIPTION, stdout);
       printf (_("\
@@ -434,6 +443,7 @@ digest_check (const char *checkfile_name)
   FILE *checkfile_stream;
   uintmax_t n_misformatted_lines = 0;
   uintmax_t n_properly_formatted_lines = 0;
+  uintmax_t n_improperly_formatted_lines = 0;
   uintmax_t n_mismatched_checksums = 0;
   uintmax_t n_open_or_read_failures = 0;
   unsigned char bin_buffer_unaligned[DIGEST_BIN_BYTES + DIGEST_ALIGN];
@@ -501,6 +511,8 @@ digest_check (const char *checkfile_name)
                      checkfile_name, line_number,
                      DIGEST_TYPE_STRING);
             }
+
+          ++n_improperly_formatted_lines;
         }
       else
         {
@@ -599,8 +611,17 @@ digest_check (const char *checkfile_name)
                      select_plural (n_mismatched_checksums))),
                    n_mismatched_checksums);
         }
-    }

+      if (n_improperly_formatted_lines != 0)
+        {
+          if (pedantic)
+            {
+              /* Bail out if more then one improperly formatted line is found
+                 and pedantic option is set */
+              exit (EXIT_FAILURE);
+            }
+        }
+    }
   return (n_properly_formatted_lines != 0
           && n_mismatched_checksums == 0
           && n_open_or_read_failures == 0);
@@ -657,6 +678,9 @@ main (int argc, char **argv)
         warn = false;
         quiet = true;
         break;
+      case PEDANTIC_OPTION:
+        pedantic = true;
+        break;
       case_GETOPT_HELP_CHAR;
       case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
       default:
@@ -694,6 +718,13 @@ main (int argc, char **argv)
       usage (EXIT_FAILURE);
     }

+  if (pedantic & !do_check)
+   {
+     error (0, 0,
+        _("the --pedantic option is meaningful only when verifying 
checksums"));
+     usage (EXIT_FAILURE);
+   }
+
   if (!O_BINARY && binary < 0)
     binary = 0;

--
1.7.6.430.g34be2



reply via email to

[Prev in Thread] Current Thread [Next in Thread]