v8-0007-Refactor-split-verify_file_checksum-function.patch
application/x-patch
Filename: v8-0007-Refactor-split-verify_file_checksum-function.patch
Type: application/x-patch
Part: 6
Patch
Format: format-patch
Series: patch v8-0007
Subject: Refactor: split verify_file_checksum() function.
| File | + | − |
|---|---|---|
| src/bin/pg_verifybackup/pg_verifybackup.c | 16 | 2 |
| src/bin/pg_verifybackup/pg_verifybackup.h | 3 | 0 |
From 62ecfedd2666b23d171539fed738281702f824fd Mon Sep 17 00:00:00 2001
From: Amul Sul <amul.sul@enterprisedb.com>
Date: Thu, 1 Aug 2024 16:45:55 +0530
Subject: [PATCH v8 07/12] Refactor: split verify_file_checksum() function.
Move the core functionality of verify_file_checksum to a new function
to reuse it instead of duplicating the code.
The verify_file_checksum() function is designed to take a file path,
open and read the file contents, and then calculate the checksum.
However, for TAR backups, instead of a file path, we receive the file
content in chunks, and the checksum needs to be calculated
incrementally. While the initial operations for plain and TAR backup
checksum calculations differ, the final checks and error handling are
the same. By moving the shared logic to a separate function, we can
reuse the code for both types of backups.
---
src/bin/pg_verifybackup/pg_verifybackup.c | 18 ++++++++++++++++--
src/bin/pg_verifybackup/pg_verifybackup.h | 3 +++
2 files changed, 19 insertions(+), 2 deletions(-)
diff --git a/src/bin/pg_verifybackup/pg_verifybackup.c b/src/bin/pg_verifybackup/pg_verifybackup.c
index 8eadaac72e3..44b2cd49e0c 100644
--- a/src/bin/pg_verifybackup/pg_verifybackup.c
+++ b/src/bin/pg_verifybackup/pg_verifybackup.c
@@ -782,7 +782,6 @@ verify_file_checksum(verifier_context *context, manifest_file *m,
int rc;
size_t bytes_read = 0;
uint8 checksumbuf[PG_CHECKSUM_MAX_LENGTH];
- int checksumlen;
/* Open the target file. */
if ((fd = open(fullpath, O_RDONLY | PG_BINARY, 0)) < 0)
@@ -848,8 +847,23 @@ verify_file_checksum(verifier_context *context, manifest_file *m,
return;
}
+ /* Do the final computation and verification. */
+ verify_checksum(context, m, &checksum_ctx, checksumbuf);
+}
+
+/*
+ * A helper function to finalize checksum computation and verify it against the
+ * backup manifest information.
+ */
+void
+verify_checksum(verifier_context *context, manifest_file *m,
+ pg_checksum_context *checksum_ctx, uint8 *checksumbuf)
+{
+ int checksumlen;
+ const char *relpath = m->pathname;
+
/* Get the final checksum. */
- checksumlen = pg_checksum_final(&checksum_ctx, checksumbuf);
+ checksumlen = pg_checksum_final(checksum_ctx, checksumbuf);
if (checksumlen < 0)
{
report_backup_error(context,
diff --git a/src/bin/pg_verifybackup/pg_verifybackup.h b/src/bin/pg_verifybackup/pg_verifybackup.h
index 98c75916255..1bc5f7a6b4a 100644
--- a/src/bin/pg_verifybackup/pg_verifybackup.h
+++ b/src/bin/pg_verifybackup/pg_verifybackup.h
@@ -107,6 +107,9 @@ typedef struct verifier_context
extern manifest_file *verify_manifest_entry(verifier_context *context,
char *relpath, int64 filesize);
+extern void verify_checksum(verifier_context *context, manifest_file *m,
+ pg_checksum_context *checksum_ctx,
+ uint8 *checksumbuf);
extern void report_backup_error(verifier_context *context,
const char *pg_restrict fmt,...)
--
2.18.0