From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1p5sjs-0000LZ-Q5 for mharc-grub-devel@gnu.org; Thu, 15 Dec 2022 13:13:48 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1p5sjr-0000LL-Af for grub-devel@gnu.org; Thu, 15 Dec 2022 13:13:47 -0500 Received: from mail-yw1-x1132.google.com ([2607:f8b0:4864:20::1132]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1p5sjp-0004kh-0m for grub-devel@gnu.org; Thu, 15 Dec 2022 13:13:47 -0500 Received: by mail-yw1-x1132.google.com with SMTP id 00721157ae682-3c090251d59so969537b3.4 for ; Thu, 15 Dec 2022 10:13:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=efficientek-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=3oQ0/HsPEpcHc9FEiVTAUkldyO3YezwaZbl6gNRcQY4=; b=j4zAO5YM/5M+UkgnQN7ZayWIwtcOqnb9wZhWpva4rJaZnIbYYdxBYDM1T1RDkIgGRi RV90C1bH6L5h5gMRuB5esDzgTs6oXFjaLABxzd+hx+xs8Xe4sbYZmqcFq4/Ek7dlodVI xwgh2wKaR7oZ9w0Em/lcGkovKO6Z6JuT0tIs8UDZvGmfKZpivfAMGtIqRwQefnC6AaUs r/Pvgg4+Hp4YXYduZqmMNCZN3mvFzEnIv9mc+YD0x18CJzoWlg6hhe6d3zwzOcrqmQDm bqlMoCZH5euJghGSIMaUg7GnYY6QyuR9XO44v7mnSQEQm1dcXhekOyylvUF8GxFRL2gS +QNQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=3oQ0/HsPEpcHc9FEiVTAUkldyO3YezwaZbl6gNRcQY4=; b=uzh7+qhRPjIzio0AT1X9AZph0tWisQZQVuPh4iZHe+dN+XD/4XTzZZLfRy+B/7A56y s+fVgWxGCh00gOyGVCV+2NdDhmvSjMwABEPLT8ukB0w/LQfjlBNpHPXIQCBjhMXt7FqH qQy9WWQICFJopAJdun4i0+afLjXhhrWZ2nRzYrb7Hc293ERPBPFy7XIrssuEPkFRzCZh xoh10NmPW7iLJ/MvELmptM/wgDb4PuH81CVjxiM5M5qOoYC3L9SQo/xxF7yJM9yzvnQh PDVPQf+s8lbF+SSg/71DWUHXrAdPUp8XoRTELLge+/HSDoFIAhC0hIc7fjUz4mVlLNos 4Qnw== X-Gm-Message-State: ANoB5pnsYxwUHUz9MDiQ6yut5o0v0KZFxGAJyCOwy8Rc+v8T8t9dqSEw DeMPN4icO4NFTZrD7juy5YUbkU9JHk/jmzwu X-Google-Smtp-Source: AA0mqf4xn9skqsIz7WwX9YQe7FZsprSdWf2o2d/EWfA/rVWxsNEp376oxRl0xQvyWGKBjtrMadIrAg== X-Received: by 2002:a05:7500:24a3:b0:ea:5b83:8798 with SMTP id ba35-20020a05750024a300b000ea5b838798mr3578290gab.45.1671128023011; Thu, 15 Dec 2022 10:13:43 -0800 (PST) Received: from crass-HP-ZBook-15-G2.lan ([37.218.244.251]) by smtp.gmail.com with ESMTPSA id q11-20020a37f70b000000b006faaf6dc55asm12434314qkj.22.2022.12.15.10.13.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Dec 2022 10:13:42 -0800 (PST) From: Glenn Washburn To: "grub-devel @ gnu . org" , Daniel Kiper Cc: Glenn Washburn , Li Gen Subject: [PATCH v3] cmp: Only return success when both files have the same contents Date: Thu, 15 Dec 2022 12:13:30 -0600 Message-Id: <20221215181330.109211-1-development@efficientek.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::1132; envelope-from=development@efficientek.com; helo=mail-yw1-x1132.google.com X-Spam_score_int: 14 X-Spam_score: 1.4 X-Spam_bar: + X-Spam_report: (1.4 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_SBL_CSS=3.335, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 15 Dec 2022 18:13:47 -0000 This allows the cmp command to be used in GRUB scripts to conditionally run commands based on whether two files are the same. The command is now quiet by default and the -v switch can be given to enable verbose mode, the previous behavior. Update documentation accordingly. Suggested-by: Li Gen Signed-off-by: Glenn Washburn --- docs/grub.texi | 12 ++++++++---- grub-core/commands/cmp.c | 38 +++++++++++++++++++++++++------------- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/docs/grub.texi b/docs/grub.texi index ff44b1cbe9..1752fcb7db 100644 --- a/docs/grub.texi +++ b/docs/grub.texi @@ -4508,9 +4508,14 @@ on platforms that support CMOS. @node cmp @subsection cmp -@deffn Command cmp file1 file2 -Compare the file @var{file1} with the file @var{file2}. If they differ -in size, print the sizes like this: +@deffn Command cmp [@option{-v}] file1 file2 +Compare the file @var{file1} with the file @var{file2}. If they are completely +identical, @code{$?} will be set to 0. Otherwise, if the files are not identical, +@code{$?} will be set to a nonzero value. + +By default nothing will be output. If the @option{-v} is used, verbose mode is +enabled. In this mode when when the files differ in size, print the sizes like +this: @example Differ in size: 0x1234 [foo], 0x4321 [bar] @@ -4523,7 +4528,6 @@ bytes like this: Differ at the offset 777: 0xbe [foo], 0xef [bar] @end example -If they are completely identical, nothing will be printed. @end deffn diff --git a/grub-core/commands/cmp.c b/grub-core/commands/cmp.c index e9c3b25d34..46185fbbcb 100644 --- a/grub-core/commands/cmp.c +++ b/grub-core/commands/cmp.c @@ -22,14 +22,21 @@ #include #include #include +#include #include GRUB_MOD_LICENSE ("GPLv3+"); #define BUFFER_SIZE 512 +static const struct grub_arg_option options[] = + { + {0, 'v', 0, N_("Enable verbose output"), 0, 0}, + {0, 0, 0, 0, 0, 0} + }; + static grub_err_t -grub_cmd_cmp (grub_command_t cmd __attribute__ ((unused)), +grub_cmd_cmp (grub_extcmd_context_t ctxt, int argc, char **args) { grub_ssize_t rd1, rd2; @@ -38,19 +45,20 @@ 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_printf_ (N_("Compare file `%s' with `%s':\n"), args[0], - args[1]); + if (ctxt->state[0].set) + grub_printf_ (N_("Compare file `%s' with `%s':\n"), args[0], args[1]); file1 = grub_file_open (args[0], GRUB_FILE_TYPE_CMP); file2 = grub_file_open (args[1], GRUB_FILE_TYPE_CMP); if (! file1 || ! file2) goto cleanup; - if (grub_file_size (file1) != grub_file_size (file2)) + if (ctxt->state[0].set && (grub_file_size (file1) != grub_file_size (file2))) grub_printf_ (N_("Files differ in size: %llu [%s], %llu [%s]\n"), (unsigned long long) grub_file_size (file1), args[0], (unsigned long long) grub_file_size (file2), args[1]); @@ -78,9 +86,10 @@ grub_cmd_cmp (grub_command_t cmd __attribute__ ((unused)), { if (buf1[i] != buf2[i]) { - grub_printf_ (N_("Files differ at the offset %llu: 0x%x [%s], 0x%x [%s]\n"), - (unsigned long long) (i + pos), buf1[i], - args[0], buf2[i], args[1]); + if (ctxt->state[0].set) + grub_printf_ (N_("Files differ at the offset %llu: 0x%x [%s], 0x%x [%s]\n"), + (unsigned long long) (i + pos), buf1[i], + args[0], buf2[i], args[1]); goto cleanup; } } @@ -90,7 +99,9 @@ grub_cmd_cmp (grub_command_t cmd __attribute__ ((unused)), while (rd2); /* TRANSLATORS: it's always exactly 2 files. */ - grub_printf_ (N_("The files are identical.\n")); + if (ctxt->state[0].set) + grub_printf_ (N_("The files are identical.\n")); + err = GRUB_ERR_NONE; } cleanup: @@ -102,18 +113,19 @@ cleanup: if (file2) grub_file_close (file2); - return grub_errno; + return err; } -static grub_command_t cmd; +static grub_extcmd_t cmd; GRUB_MOD_INIT(cmp) { - cmd = grub_register_command ("cmp", grub_cmd_cmp, - N_("FILE1 FILE2"), N_("Compare two files.")); + cmd = grub_register_extcmd ("cmp", grub_cmd_cmp, 0, + N_("FILE1 FILE2"), N_("Compare two files."), + options); } GRUB_MOD_FINI(cmp) { - grub_unregister_command (cmd); + grub_unregister_extcmd (cmd); } -- 2.34.1