Skip to contentSkip to conversation

cmp: Only return success when both files have the same contents

PATCH Superseded Glenn Washburn 2 participants 2 messages Oct 21, 2022

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 cmp: Only return success when both files have the same contents

From: Li Gen <[email protected]>

This allows the cmp command to be used in GRUB scripts to conditionally run commands based on whether two files are the same.

Update documentation accordingly.

---
Range-diff against v1:
1: 5832522ca2 ! 1: 3e6c866d87 cmp: Only return success when both files have the same contents
@@ Commit message
This allows the cmp command to be used in GRUB scripts to conditionally run commands based on whether two files are the same.

+ Update documentation accordingly.
+

+ ## docs/grub.texi ##
+@@ docs/grub.texi: bytes like this:
+ Differ at the offset 777: 0xbe [foo], 0xef [bar]
+ @end example
+
+-If they are completely identical, nothing will be printed.
++If they are completely identical, nothing will be printed, and @code{$?} ++will be set to 0. Otherwise, if the files are not identical, @code{$?} ++will be set to a nonzero value.
+ @end deffn
+
+
+
## grub-core/commands/cmp.c ##
@@ grub-core/commands/cmp.c: grub_cmd_cmp (grub_command_t cmd __attribute__ ((unused)),
grub_file_t file2 = 0;

 docs/grub.texi           | 4 +++-
 grub-core/commands/cmp.c | 4 +++-
 2 files changed, 6 insertions(+), 2 deletions(-)
docs/grub.texi +3 -1
index 4a6fb1a727..b12f47d351 100644
@@ -4486,7 +4486,9 @@ bytes like this:
 Differ at the offset 777: 0xbe [foo], 0xef [bar]
 @end example
 
-If they are completely identical, nothing will be printed.
+If they are completely identical, nothing will be printed, and @code{$?}
+will be set to 0. Otherwise, if the files are not identical, @code{$?}
+will be set to a nonzero value.
 @end deffn
 
 
grub-core/commands/cmp.c +3 -1
index e9c3b25d34..e1665cf27b 100644
@@ -38,6 +38,7 @@ grub_cmd_cmp (grub_command_t cmd __attribute__ ((unused)),
   grub_file_t file2 = 0;
   char *buf1 = 0;
   char *buf2 = 0;
+  grub_err_t err = GRUB_ERR_TEST_FAILURE;
 
   if (argc != 2)
     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("two arguments expected"));
@@ -91,6 +92,7 @@ grub_cmd_cmp (grub_command_t cmd __attribute__ ((unused)),
 
       /* TRANSLATORS: it's always exactly 2 files.  */
       grub_printf_ (N_("The files are identical.\n"));
+      err = GRUB_ERR_NONE;
     }
 
 cleanup:
@@ -102,7 +104,7 @@ cleanup:
   if (file2)
     grub_file_close (file2);
 
-  return grub_errno;
+  return err;
 }
 
 static grub_command_t cmd;
Signature · 1 line
2.34.1
#1 lore raw
Quoted 47 lines · Glenn Washburn

From: Li Gen <[email protected]>

This allows the cmp command to be used in GRUB scripts to conditionally run commands based on whether two files are the same.

Update documentation accordingly.

---
Range-diff against v1:
1: 5832522ca2 ! 1: 3e6c866d87 cmp: Only return success when both files have the same contents
@@ Commit message
This allows the cmp command to be used in GRUB scripts to conditionally run commands based on whether two files are the same.

+ Update documentation accordingly.
+

+ ## docs/grub.texi ##
+@@ docs/grub.texi: bytes like this:
+ Differ at the offset 777: 0xbe [foo], 0xef [bar]
+ @end example
+
+-If they are completely identical, nothing will be printed.
++If they are completely identical, nothing will be printed, and @code{$?} ++will be set to 0. Otherwise, if the files are not identical, @code{$?} ++will be set to a nonzero value.
+ @end deffn
+
+
+
## grub-core/commands/cmp.c ##
@@ grub-core/commands/cmp.c: grub_cmd_cmp (grub_command_t cmd __attribute__ ((unused)),
grub_file_t file2 = 0;

 docs/grub.texi           | 4 +++-
 grub-core/commands/cmp.c | 4 +++-
 2 files changed, 6 insertions(+), 2 deletions(-)
docs/grub.texi +0 -0
index 4a6fb1a727..b12f47d351 100644

docs/grub.texi bytes like this: · line 4491

Quoted 7 lines
docs/grub.texi +3 -1
@@ -4486,7 +4486,9 @@ bytes like this:
 Differ at the offset 777: 0xbe [foo], 0xef [bar]
 @end example
 
-If they are completely identical, nothing will be printed.
+If they are completely identical, nothing will be printed, and @code{$?}
+will be set to 0. Otherwise, if the files are not identical, @code{$?}
+will be set to a nonzero value.

I think the updated doc is not in line with the code.

Quoted 16 lines
docs/grub.texi +0 -0
 @end deffn
 
 
grub-core/commands/cmp.c +1 -0
index e9c3b25d34..e1665cf27b 100644
@@ -38,6 +38,7 @@ grub_cmd_cmp (grub_command_t cmd __attribute__ ((unused)),
   grub_file_t file2 = 0;
   char *buf1 = 0;
   char *buf2 = 0;
+  grub_err_t err = GRUB_ERR_TEST_FAILURE;
 
   if (argc != 2)
     return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("two arguments expected"));

grub-core/commands/cmp.c grub_cmd_cmp (grub_command_t cmd __attribute__ ((unused)), · line 94

Quoted 3 lines
grub-core/commands/cmp.c +0 -0
@@ -91,6 +92,7 @@ grub_cmd_cmp (grub_command_t cmd __attribute__ ((unused)),
 
       /* TRANSLATORS: it's always exactly 2 files.  */
       grub_printf_ (N_("The files are identical.\n"));

Did not we agree the cmp command should be quite by default?

grub-core/commands/cmp.c cleanup: · line 110

Quoted 13 lines
grub-core/commands/cmp.c +2 -1
+      err = GRUB_ERR_NONE;
     }
 
 cleanup:
@@ -102,7 +104,7 @@ cleanup:
   if (file2)
     grub_file_close (file2);
 
-  return grub_errno;
+  return err;
 }
 
 static grub_command_t cmd;

Daniel