From 7720bebed0b8456fec741f9083d3fd71278dea42 Mon Sep 17 00:00:00 2001
From: Amul Sul <amul.sul@enterprisedb.com>
Date: Wed, 31 Jul 2024 11:43:52 +0530
Subject: [PATCH v5 04/12] Refactor: move few global variable to
 verifier_context struct

Global variables are:
	1. show_progress
	2. skip_checksums
	3. total_size
	4. done_size
---
 src/bin/pg_verifybackup/pg_verifybackup.c | 50 +++++++++++------------
 1 file changed, 25 insertions(+), 25 deletions(-)

diff --git a/src/bin/pg_verifybackup/pg_verifybackup.c b/src/bin/pg_verifybackup/pg_verifybackup.c
index d77e70fbe38..71585ffc50e 100644
--- a/src/bin/pg_verifybackup/pg_verifybackup.c
+++ b/src/bin/pg_verifybackup/pg_verifybackup.c
@@ -113,8 +113,14 @@ typedef struct verifier_context
 	manifest_data *manifest;
 	char	   *backup_directory;
 	SimpleStringList ignore_list;
+	bool		skip_checksums;
 	bool		exit_on_error;
 	bool		saw_any_error;
+
+	/* Progress indicators */
+	bool		show_progress;
+	uint64		total_size;
+	uint64		done_size;
 } verifier_context;
 
 static manifest_data *parse_manifest_file(char *manifest_path);
@@ -157,19 +163,11 @@ static void report_fatal_error(const char *pg_restrict fmt,...)
 			pg_attribute_printf(1, 2) pg_attribute_noreturn();
 static bool should_ignore_relpath(verifier_context *context, const char *relpath);
 
-static void progress_report(bool finished);
+static void progress_report(verifier_context *context, bool finished);
 static void usage(void);
 
 static const char *progname;
 
-/* options */
-static bool show_progress = false;
-static bool skip_checksums = false;
-
-/* Progress indicators */
-static uint64 total_size = 0;
-static uint64 done_size = 0;
-
 /*
  * Main entry point.
  */
@@ -260,13 +258,13 @@ main(int argc, char **argv)
 				no_parse_wal = true;
 				break;
 			case 'P':
-				show_progress = true;
+				context.show_progress = true;
 				break;
 			case 'q':
 				quiet = true;
 				break;
 			case 's':
-				skip_checksums = true;
+				context.skip_checksums = true;
 				break;
 			case 'w':
 				wal_directory = pstrdup(optarg);
@@ -299,7 +297,7 @@ main(int argc, char **argv)
 	}
 
 	/* Complain if the specified arguments conflict */
-	if (show_progress && quiet)
+	if (context.show_progress && quiet)
 		pg_fatal("cannot specify both %s and %s",
 				 "-P/--progress", "-q/--quiet");
 
@@ -363,7 +361,7 @@ main(int argc, char **argv)
 	 * Now do the expensive work of verifying file checksums, unless we were
 	 * told to skip it.
 	 */
-	if (!skip_checksums)
+	if (!context.skip_checksums)
 		verify_backup_checksums(&context);
 
 	/*
@@ -739,8 +737,9 @@ verify_backup_file(verifier_context *context, char *relpath, char *fullpath)
 		verify_control_file(fullpath, context->manifest->system_identifier);
 
 	/* Update statistics for progress report, if necessary */
-	if (show_progress && !skip_checksums && should_verify_checksum(m))
-		total_size += m->size;
+	if (context->show_progress && !context->skip_checksums &&
+		should_verify_checksum(m))
+		context->total_size += m->size;
 
 	/*
 	 * We don't verify checksums at this stage. We first finish verifying that
@@ -815,7 +814,7 @@ verify_backup_checksums(verifier_context *context)
 	manifest_file *m;
 	uint8	   *buffer;
 
-	progress_report(false);
+	progress_report(context, false);
 
 	buffer = pg_malloc(READ_CHUNK_SIZE * sizeof(uint8));
 
@@ -841,7 +840,7 @@ verify_backup_checksums(verifier_context *context)
 
 	pfree(buffer);
 
-	progress_report(true);
+	progress_report(context, true);
 }
 
 /*
@@ -889,8 +888,8 @@ verify_file_checksum(verifier_context *context, manifest_file *m,
 		}
 
 		/* Report progress */
-		done_size += rc;
-		progress_report(false);
+		context->done_size += rc;
+		progress_report(context, false);
 	}
 	if (rc < 0)
 		report_backup_error(context, "could not read file \"%s\": %m",
@@ -1036,7 +1035,7 @@ should_ignore_relpath(verifier_context *context, const char *relpath)
 }
 
 /*
- * Print a progress report based on the global variables.
+ * Print a progress report based on the variables in verifier_context.
  *
  * Progress report is written at maximum once per second, unless the finished
  * parameter is set to true.
@@ -1045,7 +1044,7 @@ should_ignore_relpath(verifier_context *context, const char *relpath)
  * is moved to the next line.
  */
 static void
-progress_report(bool finished)
+progress_report(verifier_context *context, bool finished)
 {
 	static pg_time_t last_progress_report = 0;
 	pg_time_t	now;
@@ -1053,7 +1052,7 @@ progress_report(bool finished)
 	char		totalsize_str[32];
 	char		donesize_str[32];
 
-	if (!show_progress)
+	if (!context->show_progress)
 		return;
 
 	now = time(NULL);
@@ -1061,12 +1060,13 @@ progress_report(bool finished)
 		return;					/* Max once per second */
 
 	last_progress_report = now;
-	percent_size = total_size ? (int) ((done_size * 100 / total_size)) : 0;
+	percent_size = context->total_size ?
+		(int) ((context->done_size * 100 / context->total_size)) : 0;
 
 	snprintf(totalsize_str, sizeof(totalsize_str), UINT64_FORMAT,
-			 total_size / 1024);
+			 context->total_size / 1024);
 	snprintf(donesize_str, sizeof(donesize_str), UINT64_FORMAT,
-			 done_size / 1024);
+			 context->done_size / 1024);
 
 	fprintf(stderr,
 			_("%*s/%s kB (%d%%) verified"),
-- 
2.18.0

