From a4ce723e6719ed80c36fd0c4fd85c962b6b25a45 Mon Sep 17 00:00:00 2001
From: Georgios Kokolatos <gkokolatos@pm.me>
Date: Mon, 28 Nov 2022 15:18:27 +0000
Subject: [PATCH v8 2/5] Make the pg_receivewal compression parsing function
 common

Also and relax parsing errors in the helper functions and re-introduce those as
an independed function.

As it is shown in the rest of the patch series, there is a lot of duplication
between pg_dump's parsing of compression options and pg_receivewal's. Now the
core work is done in common. However pg_dump would not error out if the
requested compression algorithm is not supported by the build, whereas other
callers will error out. Also it seems a bit weird for only one of the parsing
functions for compressions to error out on missing support and that one to not
be the one responsible for identifying the compression algorithm.

A new function is added to test the support of the algorithm allowing the user
to tune the behaviour.
---
 src/backend/backup/basebackup.c       |   1 +
 src/bin/pg_basebackup/pg_basebackup.c |   1 +
 src/bin/pg_basebackup/pg_receivewal.c |  65 +-------------
 src/common/compression.c              | 119 ++++++++++++++++++++++----
 src/include/common/compression.h      |   3 +
 5 files changed, 111 insertions(+), 78 deletions(-)

diff --git a/src/backend/backup/basebackup.c b/src/backend/backup/basebackup.c
index 74fb529380..70cd720823 100644
--- a/src/backend/backup/basebackup.c
+++ b/src/backend/backup/basebackup.c
@@ -942,6 +942,7 @@ parse_basebackup_options(List *options, basebackup_options *opt)
 
 		parse_compress_specification(opt->compression, compression_detail_str,
 									 &opt->compression_specification);
+		(void) test_compress_support(&opt->compression_specification);
 		error_detail =
 			validate_compress_specification(&opt->compression_specification);
 		if (error_detail != NULL)
diff --git a/src/bin/pg_basebackup/pg_basebackup.c b/src/bin/pg_basebackup/pg_basebackup.c
index 22836ca01a..cdd32c9763 100644
--- a/src/bin/pg_basebackup/pg_basebackup.c
+++ b/src/bin/pg_basebackup/pg_basebackup.c
@@ -2556,6 +2556,7 @@ main(int argc, char **argv)
 					 compression_algorithm);
 
 		parse_compress_specification(alg, compression_detail, &client_compress);
+		(void) test_compress_support(&client_compress);
 		error_detail = validate_compress_specification(&client_compress);
 		if (error_detail != NULL)
 			pg_fatal("invalid compression specification: %s",
diff --git a/src/bin/pg_basebackup/pg_receivewal.c b/src/bin/pg_basebackup/pg_receivewal.c
index 63207ca025..d4a3a4213d 100644
--- a/src/bin/pg_basebackup/pg_receivewal.c
+++ b/src/bin/pg_basebackup/pg_receivewal.c
@@ -57,8 +57,6 @@ static XLogRecPtr endpos = InvalidXLogRecPtr;
 
 
 static void usage(void);
-static void parse_compress_options(char *option, char **algorithm,
-								   char **detail);
 static DIR *get_destination_dir(char *dest_folder);
 static void close_destination_dir(DIR *dest_dir, char *dest_folder);
 static XLogRecPtr FindStreamingStart(uint32 *tli);
@@ -109,65 +107,6 @@ usage(void)
 	printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
 }
 
-/*
- * Basic parsing of a value specified for -Z/--compress
- *
- * The parsing consists of a METHOD:DETAIL string fed later on to a more
- * advanced routine in charge of proper validation checks.  This only extracts
- * METHOD and DETAIL.  If only an integer is found, the method is implied by
- * the value specified.
- */
-static void
-parse_compress_options(char *option, char **algorithm, char **detail)
-{
-	char	   *sep;
-	char	   *endp;
-	long		result;
-
-	/*
-	 * Check whether the compression specification consists of a bare integer.
-	 *
-	 * For backward-compatibility, assume "none" if the integer found is zero
-	 * and "gzip" otherwise.
-	 */
-	result = strtol(option, &endp, 10);
-	if (*endp == '\0')
-	{
-		if (result == 0)
-		{
-			*algorithm = pstrdup("none");
-			*detail = NULL;
-		}
-		else
-		{
-			*algorithm = pstrdup("gzip");
-			*detail = pstrdup(option);
-		}
-		return;
-	}
-
-	/*
-	 * Check whether there is a compression detail following the algorithm
-	 * name.
-	 */
-	sep = strchr(option, ':');
-	if (sep == NULL)
-	{
-		*algorithm = pstrdup(option);
-		*detail = NULL;
-	}
-	else
-	{
-		char	   *alg;
-
-		alg = palloc((sep - option) + 1);
-		memcpy(alg, option, sep - option);
-		alg[sep - option] = '\0';
-
-		*algorithm = alg;
-		*detail = pstrdup(sep + 1);
-	}
-}
 
 /*
  * Check if the filename looks like a WAL file, letting caller know if this
@@ -786,8 +725,8 @@ main(int argc, char **argv)
 				verbose++;
 				break;
 			case 'Z':
-				parse_compress_options(optarg, &compression_algorithm_str,
-									   &compression_detail);
+				parse_compress_user_options(optarg, &compression_algorithm_str,
+											&compression_detail);
 				break;
 /* action */
 			case 1:
diff --git a/src/common/compression.c b/src/common/compression.c
index df5b627834..57c23221a2 100644
--- a/src/common/compression.c
+++ b/src/common/compression.c
@@ -39,6 +39,66 @@
 static int	expect_integer_value(char *keyword, char *value,
 								 pg_compress_specification *result);
 
+/*
+ * Basic parsing of a value specified for -Z/--compress
+ *
+ * The parsing consists of a METHOD:DETAIL string fed later on to a more
+ * advanced routine in charge of proper validation checks.  This only extracts
+ * METHOD and DETAIL.  If only an integer is found, the method is implied by
+ * the value specified.
+ */
+void
+parse_compress_user_options(const char *option, char **algorithm, char **detail)
+{
+	char	   *sep;
+	char	   *endp;
+	long		result;
+
+	/*
+	 * Check whether the compression specification consists of a bare integer.
+	 *
+	 * For backward-compatibility, assume "none" if the integer found is zero
+	 * and "gzip" otherwise.
+	 */
+	result = strtol(option, &endp, 10);
+	if (*endp == '\0')
+	{
+		if (result == 0)
+		{
+			*algorithm = pstrdup("none");
+			*detail = NULL;
+		}
+		else
+		{
+			*algorithm = pstrdup("gzip");
+			*detail = pstrdup(option);
+		}
+		return;
+	}
+
+	/*
+	 * Check whether there is a compression detail following the algorithm
+	 * name.
+	 */
+	sep = strchr(option, ':');
+	if (sep == NULL)
+	{
+		*algorithm = pstrdup(option);
+		*detail = NULL;
+	}
+	else
+	{
+		char	   *alg;
+
+		alg = palloc((sep - option) + 1);
+		memcpy(alg, option, sep - option);
+		alg[sep - option] = '\0';
+
+		*algorithm = alg;
+		*detail = pstrdup(sep + 1);
+	}
+}
+
 /*
  * Look up a compression algorithm by name. Returns true and sets *algorithm
  * if the name is recognized. Otherwise returns false.
@@ -100,6 +160,9 @@ get_compress_algorithm_name(pg_compress_algorithm algorithm)
  *
  * Use validate_compress_specification() to find out whether a compression
  * specification is semantically sensible.
+ *
+ * Does not test whether this build of PostgreSQL supports the requested
+ * compression method.
  */
 void
 parse_compress_specification(pg_compress_algorithm algorithm, char *specification,
@@ -123,30 +186,16 @@ parse_compress_specification(pg_compress_algorithm algorithm, char *specificatio
 			result->level = 0;
 			break;
 		case PG_COMPRESSION_LZ4:
-#ifdef USE_LZ4
 			result->level = 0;	/* fast compression mode */
-#else
-			result->parse_error =
-				psprintf(_("this build does not support compression with %s"),
-						 "LZ4");
-#endif
 			break;
 		case PG_COMPRESSION_ZSTD:
 #ifdef USE_ZSTD
 			result->level = ZSTD_CLEVEL_DEFAULT;
-#else
-			result->parse_error =
-				psprintf(_("this build does not support compression with %s"),
-						 "ZSTD");
 #endif
 			break;
 		case PG_COMPRESSION_GZIP:
 #ifdef HAVE_LIBZ
 			result->level = Z_DEFAULT_COMPRESSION;
-#else
-			result->parse_error =
-				psprintf(_("this build does not support compression with %s"),
-						 "gzip");
 #endif
 			break;
 	}
@@ -265,7 +314,8 @@ parse_compress_specification(pg_compress_algorithm algorithm, char *specificatio
  * and return -1.
  */
 static int
-expect_integer_value(char *keyword, char *value, pg_compress_specification *result)
+expect_integer_value(char *keyword, char *value,
+					 pg_compress_specification *result)
 {
 	int			ivalue;
 	char	   *ivalue_endp;
@@ -356,3 +406,42 @@ validate_compress_specification(pg_compress_specification *spec)
 
 	return NULL;
 }
+
+/*
+ * Returns NULL if the compression algorithm is supported by this build.
+ * Otherwise, returns an error message. In the later case, the error is attached
+ * to pg_compress_specification unless another error preceeds it.
+ */
+char *
+test_compress_support(pg_compress_specification *spec)
+{
+	char *err = NULL;
+	switch (spec->algorithm)
+	{
+		case PG_COMPRESSION_NONE:
+			break;
+		case PG_COMPRESSION_GZIP:
+#ifndef HAVE_LIBZ
+			err = psprintf(_("this build does not support compression with %s"),
+							 "gzip");
+#endif
+			break;
+		case PG_COMPRESSION_LZ4:
+#ifndef USE_LZ4
+			err = psprintf(_("this build does not support compression with %s"),
+							 "LZ4");
+#endif
+			break;
+		case PG_COMPRESSION_ZSTD:
+#ifndef USE_ZSTD
+			err = psprintf(_("this build does not support compression with %s"),
+							 "ZSTD");
+#endif
+			break;
+	}
+
+	if (err && !spec->parse_error)
+		spec->parse_error = err;
+
+	return err;
+}
diff --git a/src/include/common/compression.h b/src/include/common/compression.h
index 5d680058ed..eb30a7b547 100644
--- a/src/include/common/compression.h
+++ b/src/include/common/compression.h
@@ -33,6 +33,8 @@ typedef struct pg_compress_specification
 	char	   *parse_error;	/* NULL if parsing was OK, else message */
 } pg_compress_specification;
 
+extern void parse_compress_user_options(const char *option, char **algorithm,
+										char **detail);
 extern bool parse_compress_algorithm(char *name, pg_compress_algorithm *algorithm);
 extern const char *get_compress_algorithm_name(pg_compress_algorithm algorithm);
 
@@ -40,6 +42,7 @@ extern void parse_compress_specification(pg_compress_algorithm algorithm,
 										 char *specification,
 										 pg_compress_specification *result);
 
+extern char *test_compress_support(pg_compress_specification *);
 extern char *validate_compress_specification(pg_compress_specification *);
 
 #endif
-- 
2.34.1

