diff --git a/src/backend/replication/basebackup.c b/src/backend/replication/basebackup.c index a5c4a41..f01c136 100644 --- a/src/backend/replication/basebackup.c +++ b/src/backend/replication/basebackup.c @@ -81,14 +81,6 @@ static void SendXlogRecPtrResult(XLogRecPtr ptr, TimeLineID tli); static int compareWalFileNames(const ListCell *a, const ListCell *b); static void throttle(size_t increment); static bool is_checksummed_file(const char *fullpath, const char *filename); -static void initialize_manifest_checksum(ChecksumCtx *cCtx, - ManifestCheckSum checksumAlgo); -static void update_manifest_checksum(ChecksumCtx *cCtx, - ManifestCheckSum checksumAlgo, - const char *buf, off_t cnt); -static int finalize_manifest_checksum(ChecksumCtx *cCtx, - ManifestCheckSum checksumAlgo, - char *checksumbuf); /* Was the backup currently in-progress initiated in recovery mode? */ static bool backup_started_in_recovery = false; @@ -2001,66 +1993,3 @@ throttle(size_t increment) */ throttled_last = GetCurrentTimestamp(); } - -/* - * Initialize the manifest checksum context according to the provided algorithm. - */ -static void -initialize_manifest_checksum(ChecksumCtx *cCtx, ManifestCheckSum checksumAlgo) -{ - switch (checksumAlgo) - { - case MC_SHA256: - pg_sha256_init(&cCtx->sha256_ctx); - break; - case MC_CRC32C: - INIT_CRC32C(cCtx->crc_ctx); - break; - case MC_NONE: - break; - } -} - -static void -update_manifest_checksum(ChecksumCtx *cCtx, ManifestCheckSum checksumAlgo, - const char *buf, off_t cnt) -{ - switch (checksumAlgo) - { - case MC_SHA256: - pg_sha256_update(&cCtx->sha256_ctx, (uint8 *) buf, cnt); - break; - case MC_CRC32C: - COMP_CRC32C(cCtx->crc_ctx, buf, cnt); - break; - case MC_NONE: - break; - } -} - -/* - * Function calculate the final checksum for the provided context and returns - * the length of checksum. - */ -static int -finalize_manifest_checksum(ChecksumCtx *cCtx, - ManifestCheckSum checksumAlgo, - char *checksumbuf) -{ - int checksumlen = 0; - switch (checksumAlgo) - { - case MC_SHA256: - pg_sha256_final(&cCtx->sha256_ctx, (uint8 *)checksumbuf); - checksumlen = PG_SHA256_DIGEST_LENGTH; - break; - case MC_CRC32C: - FIN_CRC32C(cCtx->crc_ctx); - pg_ltoa(cCtx->crc_ctx, checksumbuf); - checksumlen = strlen(checksumbuf); - break; - case MC_NONE: - break; - } - return checksumlen; -} diff --git a/src/common/Makefile b/src/common/Makefile index ffb0f6e..b34ef41 100644 --- a/src/common/Makefile +++ b/src/common/Makefile @@ -47,6 +47,7 @@ LIBS += $(PTHREAD_LIBS) # If you add objects here, see also src/tools/msvc/Mkvcbuild.pm OBJS_COMMON = \ + backup.o \ base64.o \ config_info.o \ controldata_utils.o \ diff --git a/src/common/backup.c b/src/common/backup.c new file mode 100644 index 0000000..e6f3c37 --- /dev/null +++ b/src/common/backup.c @@ -0,0 +1,85 @@ +/*------------------------------------------------------------------------- + * + * backup.c + * backup handling helpers + * + * + * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * + * IDENTIFICATION + * src/common/backup.c + * + *------------------------------------------------------------------------- + */ + + +#ifndef FRONTEND +#include "postgres.h" +#else +#include "postgres_fe.h" +#endif + +#include "common/backup.h" + +/* + * Initialize the manifest checksum context according to the provided algorithm. + */ +void +initialize_manifest_checksum(ChecksumCtx *cCtx, ManifestCheckSum checksumAlgo) +{ + switch (checksumAlgo) + { + case MC_SHA256: + pg_sha256_init(&cCtx->sha256_ctx); + break; + case MC_CRC32C: + INIT_CRC32C(cCtx->crc_ctx); + break; + case MC_NONE: + break; + } +} + +void +update_manifest_checksum(ChecksumCtx *cCtx, ManifestCheckSum checksumAlgo, const char *buf, off_t cnt) +{ + switch (checksumAlgo) + { + case MC_SHA256: + pg_sha256_update(&cCtx->sha256_ctx, (uint8 *) buf, cnt); + break; + case MC_CRC32C: + COMP_CRC32C(cCtx->crc_ctx, buf, cnt); + break; + case MC_NONE: + break; + } +} + +/* + * Function calculate the final checksum for the provided context and returns + * the length of checksum. + */ +int +finalize_manifest_checksum(ChecksumCtx *cCtx, ManifestCheckSum checksumAlgo, char *checksumbuf) +{ + int checksumlen = 0; + + switch (checksumAlgo) + { + case MC_SHA256: + pg_sha256_final(&cCtx->sha256_ctx, (uint8 *)checksumbuf); + checksumlen = PG_SHA256_DIGEST_LENGTH; + break; + case MC_CRC32C: + FIN_CRC32C(cCtx->crc_ctx); + sprintf(checksumbuf, "%u", cCtx->crc_ctx); + checksumlen = strlen(checksumbuf); + break; + case MC_NONE: + break; + } + return checksumlen; +} diff --git a/src/include/common/backup.h b/src/include/common/backup.h new file mode 100644 index 0000000..98ca6b2 --- /dev/null +++ b/src/include/common/backup.h @@ -0,0 +1,45 @@ +/* + * backup.h + * backup handling helpers + * + * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * src/include/common/string.h + */ +#ifndef COMMON_BACKUP_H +#define COMMON_BACKUP_H + +#include "common/sha2.h" +#include "port/pg_crc32c.h" +#include "lib/stringinfo.h" + +/* Checksum algorithm option for manifest */ +typedef enum ManifestCheckSum +{ + MC_NONE = 0, + MC_SHA256, + MC_CRC32C +} ManifestCheckSum; + +/* checksum algorithm context */ +typedef union checksumCtx +{ + pg_sha256_ctx sha256_ctx; + pg_crc32c crc_ctx; +} ChecksumCtx; + +/* Backup manifest info */ +typedef struct +{ + ManifestCheckSum checksumAlgo; + char checksum_label[10]; + ChecksumCtx cCtx; + StringInfo manifest; +} manifestinfo; + +extern void initialize_manifest_checksum(ChecksumCtx *cCtx, ManifestCheckSum checksumAlgo); +extern void update_manifest_checksum(ChecksumCtx *cCtx, ManifestCheckSum checksumAlgo, const char *buf, off_t cnt); +extern int finalize_manifest_checksum(ChecksumCtx *cCtx, ManifestCheckSum checksumAlgo, char *checksumbuf); + +#endif /* COMMON_BACKUP_H */ diff --git a/src/include/replication/basebackup.h b/src/include/replication/basebackup.h index 0b165e8..93fdb05 100644 --- a/src/include/replication/basebackup.h +++ b/src/include/replication/basebackup.h @@ -12,6 +12,7 @@ #ifndef _BASEBACKUP_H #define _BASEBACKUP_H +#include "common/backup.h" #include "common/sha2.h" #include "lib/stringinfo.h" #include "nodes/replnodes.h" @@ -31,30 +32,6 @@ typedef struct int64 size; } tablespaceinfo; -/* Checksum algorithm option for manifest */ -typedef enum ManifestCheckSum -{ - MC_NONE = 0, - MC_SHA256, - MC_CRC32C -} ManifestCheckSum; - -/* checksum algorithm context */ -typedef union checksumCtx -{ - pg_sha256_ctx sha256_ctx; - pg_crc32c crc_ctx; -} ChecksumCtx; - -/* Backup manifest info */ -typedef struct -{ - ManifestCheckSum checksumAlgo; - char checksum_label[10]; - ChecksumCtx cCtx; - StringInfo manifest; -} manifestinfo; - extern void SendBaseBackup(BaseBackupCmd *cmd); extern int64 sendTablespace(char *path, char *oid, bool sizeonly,