bug-textutils
[Top][All Lists]
Advanced

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

md5sum to include date + permission information


From: Richard Kettlewell
Subject: md5sum to include date + permission information
Date: Thu, 26 Apr 2001 11:11:06 +0100 (BST)

Dear textutils maintainer(s),

It would be useful for md5sum to have options to include date and
permission information in the generated hash.

This would allow (for example) convenient detection of files that are
identical not just in content but in these other ways as well.

The diff below, against textutils 2.0, is one way to do it.

ttfn/rjk

diff -x stamp-vti -x textutils.info -x version.texi -x cat-id-tbl.c -x 
textutils.pot -ruN textutils-2.0.orig/doc/textutils.texi 
textutils-2.0/doc/textutils.texi
--- textutils-2.0.orig/doc/textutils.texi       Sat Jul 31 10:03:30 1999
+++ textutils-2.0/doc/textutils.texi    Thu Apr 26 10:39:17 2001
@@ -2046,8 +2046,25 @@
 This option is useful only if all but a few lines in the checked input
 are valid.
 
address@hidden -p
address@hidden --permissions
address@hidden -p
address@hidden --permissions
+Include ownership and permission information in the hash.
+
address@hidden -m
address@hidden --mtime
address@hidden -m
address@hidden --mtime
+Include the last modified date in the hash.
+
 @end table
 
+Note that hashes produced using different combinations of the
address@hidden and/or @samp{--mtime} options are not comparable.
+Also hashes produced on different systems using either of these options
+may not be usefully comparable, due to differences in the way different
+systems represent the information.
 
 @node Operating on sorted files
 @chapter Operating on sorted files
diff -x stamp-vti -x textutils.info -x version.texi -x cat-id-tbl.c -x 
textutils.pot -ruN textutils-2.0.orig/lib/md5.c textutils-2.0/lib/md5.c
--- textutils-2.0.orig/lib/md5.c        Mon Jun 23 12:40:37 1997
+++ textutils-2.0/lib/md5.c     Thu Apr 26 10:40:22 2001
@@ -25,6 +25,7 @@
 #endif
 
 #include <sys/types.h>
+#include <sys/stat.h>
 
 #if STDC_HEADERS || defined _LIBC
 # include <stdlib.h>
@@ -127,9 +128,10 @@
    resulting message digest number will be written into the 16 bytes
    beginning at RESBLOCK.  */
 int
-md5_stream (stream, resblock)
+md5_stream (stream, resblock, flags)
      FILE *stream;
      void *resblock;
+     enum md5_stream_flags flags;
 {
   /* Important: BLOCKSIZE must be a multiple of 64.  */
 #define BLOCKSIZE 4096
@@ -173,6 +175,21 @@
   /* Add the last bytes if necessary.  */
   if (sum > 0)
     md5_process_bytes (buffer, sum, &ctx);
+
+  /* Add in any additional information relevant */
+  if (flags) {
+    struct stat stat_buf;
+
+    if (fstat (fileno (stream), &stat_buf) < 0)
+      return 1;
+    if (flags & md5_stream_include_mtime)
+      md5_process_bytes (&stat_buf.st_mtime, sizeof stat_buf.st_mtime, &ctx);
+    if (flags & md5_stream_include_permissions) {
+      md5_process_bytes (&stat_buf.st_mode, sizeof stat_buf.st_mode, &ctx);
+      md5_process_bytes (&stat_buf.st_uid, sizeof stat_buf.st_uid, &ctx);
+      md5_process_bytes (&stat_buf.st_gid, sizeof stat_buf.st_gid, &ctx);
+    }
+  }
 
   /* Construct result in desired memory.  */
   md5_finish_ctx (&ctx, resblock);
diff -x stamp-vti -x textutils.info -x version.texi -x cat-id-tbl.c -x 
textutils.pot -ruN textutils-2.0.orig/lib/md5.h textutils-2.0/lib/md5.h
--- textutils-2.0.orig/lib/md5.h        Mon Jun 23 12:40:37 1997
+++ textutils-2.0/lib/md5.h     Thu Apr 26 10:40:34 2001
@@ -90,6 +90,12 @@
   char buffer[128];
 };
 
+/* Flag argument to md5_stream.  */
+enum md5_stream_flags {
+  md5_stream_include_permissions = 1,
+  md5_stream_include_mtime = 2,
+};
+
 /*
  * The following three functions are build up the low level used in
  * the functions `md5_stream' and `md5_buffer'.
@@ -134,8 +140,10 @@
 
 /* Compute MD5 message digest for bytes read from STREAM.  The
    resulting message digest number will be written into the 16 bytes
-   beginning at RESBLOCK.  */
-extern int md5_stream __P ((FILE *stream, void *resblock));
+   beginning at RESBLOCK.  The FLAGS argument determines what
+   additional information (if any) to include in the hash.  */
+extern int md5_stream __P ((FILE *stream, void *resblock,
+                           enum md5_stream_flags flags));
 
 /* Compute MD5 message digest for LEN bytes beginning at BUFFER.  The
    result is always in little endian byte order, so that a byte-wise
diff -x stamp-vti -x textutils.info -x version.texi -x cat-id-tbl.c -x 
textutils.pot -ruN textutils-2.0.orig/man/md5sum.1 textutils-2.0/man/md5sum.1
--- textutils-2.0.orig/man/md5sum.1     Fri Aug  6 20:24:08 1999
+++ textutils-2.0/man/md5sum.1  Thu Apr 26 10:55:19 2001
@@ -1,5 +1,5 @@
 .\" DO NOT MODIFY THIS FILE!  It was generated by help2man 1.012.
-.TH MD5SUM "1" "August 1999" "GNU textutils 2.0" FSF
+.TH MD5SUM "1" "April 2001" "GNU textutils 2.0" FSF
 .SH NAME
 md5sum \- compute and check MD5 message digest
 .SH SYNOPSIS
@@ -23,6 +23,12 @@
 .TP
 \fB\-t\fR, \fB\-\-text\fR
 read files in text mode (default)
+.TP
+\fB\-m\fR, \fB\-\-mtime\fR
+include last modified date in hash
+.TP
+\fB\-p\fR, \fB\-\-permissions\fR
+include permission information in hash
 .SS "The following two options are useful only when verifying checksums:"
 .TP
 \fB\-\-status\fR
diff -x stamp-vti -x textutils.info -x version.texi -x cat-id-tbl.c -x 
textutils.pot -ruN textutils-2.0.orig/src/md5sum.c textutils-2.0/src/md5sum.c
--- textutils-2.0.orig/src/md5sum.c     Mon May  3 18:55:37 1999
+++ textutils-2.0/src/md5sum.c  Thu Apr 26 10:52:22 2001
@@ -17,6 +17,7 @@
    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
 /* Written by Ulrich Drepper <address@hidden>.  */
+/* Modified by Richard Kettlewell <address@hidden> */
 
 #ifdef HAVE_CONFIG_H
 # include <config.h>
@@ -88,6 +89,8 @@
   { "string", required_argument, 0, 1 },
   { "text", no_argument, 0, 't' },
   { "warn", no_argument, 0, 'w' },
+  { "mtime", no_argument, 0, 'm' },
+  { "permissions", no_argument, 0, 'p' },
   { GETOPT_HELP_OPTION_DECL },
   { GETOPT_VERSION_OPTION_DECL },
   { NULL, 0, NULL, 0 }
@@ -110,6 +113,8 @@
   -b, --binary            read files in binary mode (default on DOS/Windows)\n\
   -c, --check             check MD5 sums against given list\n\
   -t, --text              read files in text mode (default)\n\
+  -m, --mtime             include last modified date in hash\n\
+  -p, --permissions       include permission information in hash\n\
 \n\
 The following two options are useful only when verifying checksums:\n\
       --status            don't output anything, status code shows success\n\
@@ -236,7 +241,8 @@
    to indicate success.  */
 
 static int
-md5_file (const char *filename, int binary, unsigned char *md5_result)
+md5_file (const char *filename, int binary, unsigned char *md5_result,
+         enum md5_stream_flags streamflags)
 {
   FILE *fp;
   int err;
@@ -266,7 +272,7 @@
        }
     }
 
-  err = md5_stream (fp, md5_result);
+  err = md5_stream (fp, md5_result, streamflags);
   if (err)
     {
       error (0, errno, "%s", filename);
@@ -285,7 +291,7 @@
 }
 
 static int
-md5_check (const char *checkfile_name)
+md5_check (const char *checkfile_name, enum md5_stream_flags streamflags)
 {
   FILE *checkfile_stream;
   int n_properly_formated_lines = 0;
@@ -357,7 +363,7 @@
 
          ++n_properly_formated_lines;
 
-         fail = md5_file (filename, binary, md5buffer);
+         fail = md5_file (filename, binary, md5buffer, streamflags);
 
          if (fail)
            {
@@ -456,6 +462,7 @@
   size_t n_strings = 0;
   size_t err = 0;
   int file_type_specified = 0;
+  enum md5_stream_flags streamflags = 0;
 
 #if O_BINARY
   /* Binary is default on MSDOS, so the actual file contents
@@ -472,7 +479,7 @@
   bindtextdomain (PACKAGE, LOCALEDIR);
   textdomain (PACKAGE);
 
-  while ((opt = getopt_long (argc, argv, "bctw", long_options, NULL)) != -1)
+  while ((opt = getopt_long (argc, argv, "bctwmp", long_options, NULL)) != -1)
     switch (opt)
       {
       case 0:                  /* long option */
@@ -506,6 +513,12 @@
        status_only = 0;
        warn = 1;
        break;
+      case 'm':
+       streamflags |= md5_stream_include_mtime;
+       break;
+      case 'p':
+       streamflags |= md5_stream_include_permissions;
+       break;
       case_GETOPT_HELP_CHAR;
       case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
       default:
@@ -569,7 +582,7 @@
          usage (EXIT_FAILURE);
        }
 
-      err = md5_check ((optind == argc) ? "-" : argv[optind]);
+      err = md5_check ((optind == argc) ? "-" : argv[optind], streamflags);
     }
   else
     {
@@ -581,7 +594,7 @@
          int fail;
          char *file = argv[optind];
 
-         fail = md5_file (file, binary, md5buffer);
+         fail = md5_file (file, binary, md5buffer, streamflags);
          err |= fail;
          if (!fail)
            {




reply via email to

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