From 3105d480ab82093ca2873e423782f5b2edd9fbb7 Mon Sep 17 00:00:00 2001
From: Justin Pryzby <pryzbyj@telsasoft.com>
Date: Sat, 14 Jan 2023 10:58:23 -0600
Subject: [PATCH 1/7] pg_dump: pass pg_compress_specification as a pointer..

..as is done everywhere else.
---
 src/bin/pg_dump/compress_io.c         | 30 +++++++++++++--------------
 src/bin/pg_dump/compress_io.h         |  8 +++----
 src/bin/pg_dump/pg_backup.h           |  2 +-
 src/bin/pg_dump/pg_backup_archiver.c  | 12 +++++------
 src/bin/pg_dump/pg_backup_custom.c    |  6 +++---
 src/bin/pg_dump/pg_backup_directory.c |  8 +++----
 src/bin/pg_dump/pg_dump.c             |  2 +-
 7 files changed, 34 insertions(+), 34 deletions(-)

diff --git a/src/bin/pg_dump/compress_io.c b/src/bin/pg_dump/compress_io.c
index 7a2c80bbc4c..62a9527fa48 100644
--- a/src/bin/pg_dump/compress_io.c
+++ b/src/bin/pg_dump/compress_io.c
@@ -94,19 +94,19 @@ static void WriteDataToArchiveNone(ArchiveHandle *AH, CompressorState *cs,
 
 /* Allocate a new compressor */
 CompressorState *
-AllocateCompressor(const pg_compress_specification compression_spec,
+AllocateCompressor(const pg_compress_specification *compression_spec,
 				   WriteFunc writeF)
 {
 	CompressorState *cs;
 
 #ifndef HAVE_LIBZ
-	if (compression_spec.algorithm == PG_COMPRESSION_GZIP)
+	if (compression_spec->algorithm == PG_COMPRESSION_GZIP)
 		pg_fatal("this build does not support compression with %s", "gzip");
 #endif
 
 	cs = (CompressorState *) pg_malloc0(sizeof(CompressorState));
 	cs->writeF = writeF;
-	cs->compression_spec = compression_spec;
+	cs->compression_spec = *compression_spec; // XXX
 
 	/*
 	 * Perform compression algorithm specific initialization.
@@ -125,12 +125,12 @@ AllocateCompressor(const pg_compress_specification compression_spec,
  */
 void
 ReadDataFromArchive(ArchiveHandle *AH,
-					const pg_compress_specification compression_spec,
+					const pg_compress_specification *compression_spec,
 					ReadFunc readF)
 {
-	if (compression_spec.algorithm == PG_COMPRESSION_NONE)
+	if (compression_spec->algorithm == PG_COMPRESSION_NONE)
 		ReadDataFromArchiveNone(AH, readF);
-	if (compression_spec.algorithm == PG_COMPRESSION_GZIP)
+	if (compression_spec->algorithm == PG_COMPRESSION_GZIP)
 	{
 #ifdef HAVE_LIBZ
 		ReadDataFromArchiveZlib(AH, readF);
@@ -432,13 +432,13 @@ cfopen_read(const char *path, const char *mode)
 	if (hasSuffix(path, ".gz"))
 	{
 		compression_spec.algorithm = PG_COMPRESSION_GZIP;
-		fp = cfopen(path, mode, compression_spec);
+		fp = cfopen(path, mode, &compression_spec);
 	}
 	else
 #endif
 	{
 		compression_spec.algorithm = PG_COMPRESSION_NONE;
-		fp = cfopen(path, mode, compression_spec);
+		fp = cfopen(path, mode, &compression_spec);
 #ifdef HAVE_LIBZ
 		if (fp == NULL)
 		{
@@ -446,7 +446,7 @@ cfopen_read(const char *path, const char *mode)
 
 			fname = psprintf("%s.gz", path);
 			compression_spec.algorithm = PG_COMPRESSION_GZIP;
-			fp = cfopen(fname, mode, compression_spec);
+			fp = cfopen(fname, mode, &compression_spec);
 			free_keep_errno(fname);
 		}
 #endif
@@ -467,11 +467,11 @@ cfopen_read(const char *path, const char *mode)
  */
 cfp *
 cfopen_write(const char *path, const char *mode,
-			 const pg_compress_specification compression_spec)
+			 const pg_compress_specification *compression_spec)
 {
 	cfp		   *fp;
 
-	if (compression_spec.algorithm == PG_COMPRESSION_NONE)
+	if (compression_spec->algorithm == PG_COMPRESSION_NONE)
 		fp = cfopen(path, mode, compression_spec);
 	else
 	{
@@ -497,20 +497,20 @@ cfopen_write(const char *path, const char *mode,
  */
 cfp *
 cfopen(const char *path, const char *mode,
-	   const pg_compress_specification compression_spec)
+	   const pg_compress_specification *compression_spec)
 {
 	cfp		   *fp = pg_malloc(sizeof(cfp));
 
-	if (compression_spec.algorithm == PG_COMPRESSION_GZIP)
+	if (compression_spec->algorithm == PG_COMPRESSION_GZIP)
 	{
 #ifdef HAVE_LIBZ
-		if (compression_spec.level != Z_DEFAULT_COMPRESSION)
+		if (compression_spec->level != Z_DEFAULT_COMPRESSION)
 		{
 			/* user has specified a compression level, so tell zlib to use it */
 			char		mode_compression[32];
 
 			snprintf(mode_compression, sizeof(mode_compression), "%s%d",
-					 mode, compression_spec.level);
+					 mode, compression_spec->level);
 			fp->compressedfp = gzopen(path, mode_compression);
 		}
 		else
diff --git a/src/bin/pg_dump/compress_io.h b/src/bin/pg_dump/compress_io.h
index a429dc4789d..34f4e5e1e14 100644
--- a/src/bin/pg_dump/compress_io.h
+++ b/src/bin/pg_dump/compress_io.h
@@ -40,10 +40,10 @@ typedef size_t (*ReadFunc) (ArchiveHandle *AH, char **buf, size_t *buflen);
 /* struct definition appears in compress_io.c */
 typedef struct CompressorState CompressorState;
 
-extern CompressorState *AllocateCompressor(const pg_compress_specification compression_spec,
+extern CompressorState *AllocateCompressor(const pg_compress_specification *compression_spec,
 										   WriteFunc writeF);
 extern void ReadDataFromArchive(ArchiveHandle *AH,
-								const pg_compress_specification compression_spec,
+								const pg_compress_specification *compression_spec,
 								ReadFunc readF);
 extern void WriteDataToArchive(ArchiveHandle *AH, CompressorState *cs,
 							   const void *data, size_t dLen);
@@ -53,10 +53,10 @@ extern void EndCompressor(ArchiveHandle *AH, CompressorState *cs);
 typedef struct cfp cfp;
 
 extern cfp *cfopen(const char *path, const char *mode,
-				   const pg_compress_specification compression_spec);
+				   const pg_compress_specification *compression_spec);
 extern cfp *cfopen_read(const char *path, const char *mode);
 extern cfp *cfopen_write(const char *path, const char *mode,
-						 const pg_compress_specification compression_spec);
+						 const pg_compress_specification *compression_spec);
 extern int	cfread(void *ptr, int size, cfp *fp);
 extern int	cfwrite(const void *ptr, int size, cfp *fp);
 extern int	cfgetc(cfp *fp);
diff --git a/src/bin/pg_dump/pg_backup.h b/src/bin/pg_dump/pg_backup.h
index aba780ef4b1..216e24e7ec5 100644
--- a/src/bin/pg_dump/pg_backup.h
+++ b/src/bin/pg_dump/pg_backup.h
@@ -305,7 +305,7 @@ extern Archive *OpenArchive(const char *FileSpec, const ArchiveFormat fmt);
 
 /* Create a new archive */
 extern Archive *CreateArchive(const char *FileSpec, const ArchiveFormat fmt,
-							  const pg_compress_specification compression_spec,
+							  const pg_compress_specification *compression_spec,
 							  bool dosync, ArchiveMode mode,
 							  SetupWorkerPtrType setupDumpWorker);
 
diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c
index 7f7a0f1ce7b..0d91b75c748 100644
--- a/src/bin/pg_dump/pg_backup_archiver.c
+++ b/src/bin/pg_dump/pg_backup_archiver.c
@@ -70,7 +70,7 @@ typedef struct _parallelReadyList
 
 
 static ArchiveHandle *_allocAH(const char *FileSpec, const ArchiveFormat fmt,
-							   const pg_compress_specification compression_spec,
+							   const pg_compress_specification *compression_spec,
 							   bool dosync, ArchiveMode mode,
 							   SetupWorkerPtrType setupWorkerPtr);
 static void _getObjectDescription(PQExpBuffer buf, const TocEntry *te);
@@ -241,7 +241,7 @@ setupRestoreWorker(Archive *AHX)
 /* Public */
 Archive *
 CreateArchive(const char *FileSpec, const ArchiveFormat fmt,
-			  const pg_compress_specification compression_spec,
+			  const pg_compress_specification *compression_spec,
 			  bool dosync, ArchiveMode mode,
 			  SetupWorkerPtrType setupDumpWorker)
 
@@ -261,7 +261,7 @@ OpenArchive(const char *FileSpec, const ArchiveFormat fmt)
 	pg_compress_specification compression_spec = {0};
 
 	compression_spec.algorithm = PG_COMPRESSION_NONE;
-	AH = _allocAH(FileSpec, fmt, compression_spec, true,
+	AH = _allocAH(FileSpec, fmt, &compression_spec, true,
 				  archModeRead, setupRestoreWorker);
 
 	return (Archive *) AH;
@@ -2214,7 +2214,7 @@ _discoverArchiveFormat(ArchiveHandle *AH)
  */
 static ArchiveHandle *
 _allocAH(const char *FileSpec, const ArchiveFormat fmt,
-		 const pg_compress_specification compression_spec,
+		 const pg_compress_specification *compression_spec,
 		 bool dosync, ArchiveMode mode,
 		 SetupWorkerPtrType setupWorkerPtr)
 {
@@ -2266,7 +2266,7 @@ _allocAH(const char *FileSpec, const ArchiveFormat fmt,
 	AH->toc->prev = AH->toc;
 
 	AH->mode = mode;
-	AH->compression_spec = compression_spec;
+	AH->compression_spec = *compression_spec; // XXX
 	AH->dosync = dosync;
 
 	memset(&(AH->sqlparse), 0, sizeof(AH->sqlparse));
@@ -2281,7 +2281,7 @@ _allocAH(const char *FileSpec, const ArchiveFormat fmt,
 	 * Force stdin/stdout into binary mode if that is what we are using.
 	 */
 #ifdef WIN32
-	if ((fmt != archNull || compression_spec.algorithm != PG_COMPRESSION_NONE) &&
+	if ((fmt != archNull || compression_spec->algorithm != PG_COMPRESSION_NONE) &&
 		(AH->fSpec == NULL || strcmp(AH->fSpec, "") == 0))
 	{
 		if (mode == archModeWrite)
diff --git a/src/bin/pg_dump/pg_backup_custom.c b/src/bin/pg_dump/pg_backup_custom.c
index d1e54644a94..0e87444de85 100644
--- a/src/bin/pg_dump/pg_backup_custom.c
+++ b/src/bin/pg_dump/pg_backup_custom.c
@@ -298,7 +298,7 @@ _StartData(ArchiveHandle *AH, TocEntry *te)
 	_WriteByte(AH, BLK_DATA);	/* Block type */
 	WriteInt(AH, te->dumpId);	/* For sanity check */
 
-	ctx->cs = AllocateCompressor(AH->compression_spec, _CustomWriteFunc);
+	ctx->cs = AllocateCompressor(&AH->compression_spec, _CustomWriteFunc);
 }
 
 /*
@@ -377,7 +377,7 @@ _StartLO(ArchiveHandle *AH, TocEntry *te, Oid oid)
 
 	WriteInt(AH, oid);
 
-	ctx->cs = AllocateCompressor(AH->compression_spec, _CustomWriteFunc);
+	ctx->cs = AllocateCompressor(&AH->compression_spec, _CustomWriteFunc);
 }
 
 /*
@@ -566,7 +566,7 @@ _PrintTocData(ArchiveHandle *AH, TocEntry *te)
 static void
 _PrintData(ArchiveHandle *AH)
 {
-	ReadDataFromArchive(AH, AH->compression_spec, _CustomReadFunc);
+	ReadDataFromArchive(AH, &AH->compression_spec, _CustomReadFunc);
 }
 
 static void
diff --git a/src/bin/pg_dump/pg_backup_directory.c b/src/bin/pg_dump/pg_backup_directory.c
index 6800c3cceef..ffb8a0e4dd7 100644
--- a/src/bin/pg_dump/pg_backup_directory.c
+++ b/src/bin/pg_dump/pg_backup_directory.c
@@ -328,7 +328,7 @@ _StartData(ArchiveHandle *AH, TocEntry *te)
 	setFilePath(AH, fname, tctx->filename);
 
 	ctx->dataFH = cfopen_write(fname, PG_BINARY_W,
-							   AH->compression_spec);
+							   &AH->compression_spec);
 	if (ctx->dataFH == NULL)
 		pg_fatal("could not open output file \"%s\": %m", fname);
 }
@@ -584,7 +584,7 @@ _CloseArchive(ArchiveHandle *AH)
 
 		/* The TOC is always created uncompressed */
 		compression_spec.algorithm = PG_COMPRESSION_NONE;
-		tocFH = cfopen_write(fname, PG_BINARY_W, compression_spec);
+		tocFH = cfopen_write(fname, PG_BINARY_W, &compression_spec);
 		if (tocFH == NULL)
 			pg_fatal("could not open output file \"%s\": %m", fname);
 		ctx->dataFH = tocFH;
@@ -649,7 +649,7 @@ _StartLOs(ArchiveHandle *AH, TocEntry *te)
 
 	/* The LO TOC file is never compressed */
 	compression_spec.algorithm = PG_COMPRESSION_NONE;
-	ctx->LOsTocFH = cfopen_write(fname, "ab", compression_spec);
+	ctx->LOsTocFH = cfopen_write(fname, "ab", &compression_spec);
 	if (ctx->LOsTocFH == NULL)
 		pg_fatal("could not open output file \"%s\": %m", fname);
 }
@@ -667,7 +667,7 @@ _StartLO(ArchiveHandle *AH, TocEntry *te, Oid oid)
 
 	snprintf(fname, MAXPGPATH, "%s/blob_%u.dat", ctx->directory, oid);
 
-	ctx->dataFH = cfopen_write(fname, PG_BINARY_W, AH->compression_spec);
+	ctx->dataFH = cfopen_write(fname, PG_BINARY_W, &AH->compression_spec);
 
 	if (ctx->dataFH == NULL)
 		pg_fatal("could not open output file \"%s\": %m", fname);
diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c
index 2c0a9699729..20f73729fac 100644
--- a/src/bin/pg_dump/pg_dump.c
+++ b/src/bin/pg_dump/pg_dump.c
@@ -751,7 +751,7 @@ main(int argc, char **argv)
 		pg_fatal("parallel backup only supported by the directory format");
 
 	/* Open the output file */
-	fout = CreateArchive(filename, archiveFormat, compression_spec,
+	fout = CreateArchive(filename, archiveFormat, &compression_spec,
 						 dosync, archiveMode, setupDumpWorker);
 
 	/* Make dump options accessible right away */
-- 
2.25.1

