Subject: bug#19508: [PATCH] diff: compare major, minor numbers of block/character special files

From: Ondřej Svoboda <[email-masked]>
Date: Sun, 04 Jan 2015 21:51:50 +0100

This deals with situation such as

   File dev1/block1 is a block special file while file dev2/block1 is
   a block special file

in case the major and minor numbers are identical.
If stat.st_rdev does not match, a message is given:

   Block special files dev1/block1 and dev2/block1 differ: 1,2 (3,2)
---
  src/diff.c | 23 +++++++++++++++++++++++
  src/diff.h |  1 +
  src/util.c |  9 +++++++++
  3 files changed, 33 insertions(+)

diff --git a/src/diff.c b/src/diff.c
index ff28377..a3e1b6f 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -1276,6 +1276,29 @@ compare_files (struct comparison const *parent,
  	      status = EXIT_FAILURE;
  	    }
  	}
+      else if ((S_ISBLK (cmp.file[0].stat.st_mode)
+	        && S_ISBLK (cmp.file[1].stat.st_mode))
+	       || (S_ISCHR (cmp.file[0].stat.st_mode)
+	           && S_ISCHR (cmp.file[1].stat.st_mode)))
+	{
+	  if (cmp.file[0].stat.st_rdev != cmp.file[1].stat.st_rdev)
+	    {
+	      char major_minor0[20], major_minor1[20];
+	      format_major_minor (major_minor0, cmp.file[0].stat.st_rdev);
+	      format_major_minor (major_minor1, cmp.file[1].stat.st_rdev);
+
+	      const char *message = S_ISBLK (cmp.file[0].stat.st_mode) ?
+			"Block special files %s and %s differ: %s (%s)\n" :
+			"Character special files %s and %s differ: %s (%s)\n";
+	      message5 (message,
+			file_label[0] ? file_label[0] : cmp.file[0].name,
+			file_label[1] ? file_label[1] : cmp.file[1].name,
+			major_minor0, major_minor1);
+
+	      /* This is a difference.  */
+	      status = EXIT_FAILURE;
+	    }
+	}
        else
  	{
  	  /* We have two files that are not to be compared.  */
diff --git a/src/diff.h b/src/diff.h
index 465e4bc..098e75f 100644
--- a/src/diff.h
+++ b/src/diff.h
@@ -390,3 +390,4 @@ extern void print_script (struct change *, struct 
change * (*) (struct change *)
  extern void setup_output (char const *, char const *, bool);
  extern void translate_range (struct file_data const *, lin, lin,
                               long int *, long int *);
+extern void format_major_minor (char *buffer, const dev_t st_rdev);
diff --git a/src/util.c b/src/util.c
index 2d6d3fc..4773003 100644
--- a/src/util.c
+++ b/src/util.c
@@ -24,6 +24,7 @@
  #include <system-quote.h>
  #include <xalloc.h>
  #include "xvasprintf.h"
+#include <sys/types.h>
   char const pr_program[] = PR_PROGRAM;
  @@ -907,3 +908,11 @@ debug_script (struct change *sp)
     fflush (stderr);
  }
+
+/* Format a device number as major,minor. */
+
+void
+format_major_minor (char *buffer, const dev_t st_rdev)
+{
+  sprintf (buffer, "%u,%u", (major (st_rdev)), (minor (st_rdev)));
+}
-- 
2.2.1