@@ -57,6 +57,8 @@ static XLogRecPtr endpos = InvalidXLogRecPtr;
57
57
58
58
59
59
static void usage (void );
60
+ static void parse_compress_options (char * option , char * * algorithm ,
61
+ char * * detail );
60
62
static DIR * get_destination_dir (char * dest_folder );
61
63
static void close_destination_dir (DIR * dest_dir , char * dest_folder );
62
64
static XLogRecPtr FindStreamingStart (uint32 * tli );
@@ -90,9 +92,8 @@ usage(void)
90
92
printf (_ (" --synchronous flush write-ahead log immediately after writing\n" ));
91
93
printf (_ (" -v, --verbose output verbose messages\n" ));
92
94
printf (_ (" -V, --version output version information, then exit\n" ));
93
- printf (_ (" --compression-method=METHOD\n"
94
- " method to compress logs\n" ));
95
- printf (_ (" -Z, --compress=1-9 compress logs with given compression level\n" ));
95
+ printf (_ (" -Z, --compress=METHOD[:DETAIL]\n"
96
+ " compress as specified\n" ));
96
97
printf (_ (" -?, --help show this help, then exit\n" ));
97
98
printf (_ ("\nConnection options:\n" ));
98
99
printf (_ (" -d, --dbname=CONNSTR connection string\n" ));
@@ -108,6 +109,66 @@ usage(void)
108
109
printf (_ ("%s home page: <%s>\n" ), PACKAGE_NAME , PACKAGE_URL );
109
110
}
110
111
112
+ /*
113
+ * Basic parsing of a value specified for -Z/--compress
114
+ *
115
+ * The parsing consists of a METHOD:DETAIL string fed later on to a more
116
+ * advanced routine in charge of proper validation checks. This only extracts
117
+ * METHOD and DETAIL. If only an integer is found, the method is implied by
118
+ * the value specified.
119
+ */
120
+ static void
121
+ parse_compress_options (char * option , char * * algorithm , char * * detail )
122
+ {
123
+ char * sep ;
124
+ char * endp ;
125
+ long result ;
126
+
127
+ /*
128
+ * Check whether the compression specification consists of a bare integer.
129
+ *
130
+ * For backward-compatibility, assume "none" if the integer found is zero
131
+ * and "gzip" otherwise.
132
+ */
133
+ result = strtol (option , & endp , 10 );
134
+ if (* endp == '\0' )
135
+ {
136
+ if (result == 0 )
137
+ {
138
+ * algorithm = pstrdup ("none" );
139
+ * detail = NULL ;
140
+ }
141
+ else
142
+ {
143
+ * algorithm = pstrdup ("gzip" );
144
+ * detail = pstrdup (option );
145
+ }
146
+ return ;
147
+ }
148
+
149
+ /*
150
+ * Check whether there is a compression detail following the algorithm
151
+ * name.
152
+ */
153
+ sep = strchr (option , ':' );
154
+ if (sep == NULL )
155
+ {
156
+ * algorithm = pstrdup (option );
157
+ * detail = NULL ;
158
+ }
159
+ else
160
+ {
161
+ char * alg ;
162
+
163
+ alg = palloc ((sep - option ) + 1 );
164
+ memcpy (alg , option , sep - option );
165
+ alg [sep - option ] = '\0' ;
166
+
167
+ * algorithm = alg ;
168
+ * detail = pstrdup (sep + 1 );
169
+ }
170
+ }
171
+
111
172
/*
112
173
* Check if the filename looks like a WAL file, letting caller know if this
113
174
* WAL segment is partial and/or compressed.
@@ -651,7 +712,6 @@ main(int argc, char **argv)
651
712
{"if-not-exists" , no_argument , NULL , 3 },
652
713
{"synchronous" , no_argument , NULL , 4 },
653
714
{"no-sync" , no_argument , NULL , 5 },
654
- {"compression-method" , required_argument , NULL , 6 },
655
715
{NULL , 0 , NULL , 0 }
656
716
};
657
717
@@ -660,6 +720,10 @@ main(int argc, char **argv)
660
720
char * db_name ;
661
721
uint32 hi ,
662
722
lo ;
723
+ pg_compress_specification compression_spec ;
724
+ char * compression_detail = NULL ;
725
+ char * compression_algorithm_str = "none" ;
726
+ char * error_detail = NULL ;
663
727
664
728
pg_logging_init (argv [0 ]);
665
729
progname = get_progname (argv [0 ]);
@@ -728,9 +792,8 @@ main(int argc, char **argv)
728
792
verbose ++ ;
729
793
break ;
730
794
case 'Z' :
731
- if (!option_parse_int (optarg , "-Z/--compress" , 1 , 9 ,
732
- & compresslevel ))
733
- exit (1 );
795
+ parse_compress_options (optarg , & compression_algorithm_str ,
796
+ & compression_detail );
734
797
break ;
735
798
/* action */
736
799
case 1 :
@@ -748,17 +811,6 @@ main(int argc, char **argv)
748
811
case 5 :
749
812
do_sync = false;
750
813
break ;
751
- case 6 :
752
- if (pg_strcasecmp (optarg , "gzip" ) == 0 )
753
- compression_algorithm = PG_COMPRESSION_GZIP ;
754
- else if (pg_strcasecmp (optarg , "lz4" ) == 0 )
755
- compression_algorithm = PG_COMPRESSION_LZ4 ;
756
- else if (pg_strcasecmp (optarg , "none" ) == 0 )
757
- compression_algorithm = PG_COMPRESSION_NONE ;
758
- else
759
- pg_fatal ("invalid value \"%s\" for option %s" ,
760
- optarg , "--compression-method" );
761
- break ;
762
814
default :
763
815
/* getopt_long already emitted a complaint */
764
816
pg_log_error_hint ("Try \"%s --help\" for more information." , progname );
@@ -810,24 +862,33 @@ main(int argc, char **argv)
810
862
exit (1 );
811
863
}
812
864
813
-
814
865
/*
815
- * Compression-related options.
866
+ * Compression options
816
867
*/
868
+ if (!parse_compress_algorithm (compression_algorithm_str ,
869
+ & compression_algorithm ))
870
+ pg_fatal ("unrecognized compression algorithm \"%s\"" ,
871
+ compression_algorithm_str );
872
+
873
+ parse_compress_specification (compression_algorithm , compression_detail ,
874
+ & compression_spec );
875
+ error_detail = validate_compress_specification (& compression_spec );
876
+ if (error_detail != NULL )
877
+ pg_fatal ("invalid compression specification: %s" ,
878
+ error_detail );
879
+
880
+ /* Extract the compression level, if found in the specification */
881
+ if ((compression_spec .options & PG_COMPRESSION_OPTION_LEVEL ) != 0 )
882
+ compresslevel = compression_spec .level ;
883
+
817
884
switch (compression_algorithm )
818
885
{
819
886
case PG_COMPRESSION_NONE :
820
- if (compresslevel != 0 )
821
- {
822
- pg_log_error ("cannot use --compress with --compression-method=%s" ,
823
- "none" );
824
- pg_log_error_hint ("Try \"%s --help\" for more information." , progname );
825
- exit (1 );
826
- }
887
+ /* nothing to do */
827
888
break ;
828
889
case PG_COMPRESSION_GZIP :
829
890
#ifdef HAVE_LIBZ
830
- if (compresslevel == 0 )
891
+ if (( compression_spec . options & PG_COMPRESSION_OPTION_LEVEL ) == 0 )
831
892
{
832
893
pg_log_info ("no value specified for --compress, switching to default" );
833
894
compresslevel = Z_DEFAULT_COMPRESSION ;
@@ -838,15 +899,7 @@ main(int argc, char **argv)
838
899
#endif
839
900
break ;
840
901
case PG_COMPRESSION_LZ4 :
841
- #ifdef USE_LZ4
842
- if (compresslevel != 0 )
843
- {
844
- pg_log_error ("cannot use --compress with --compression-method=%s" ,
845
- "lz4" );
846
- pg_log_error_hint ("Try \"%s --help\" for more information." , progname );
847
- exit (1 );
848
- }
849
- #else
902
+ #ifndef USE_LZ4
850
903
pg_fatal ("this build does not support compression with %s" ,
851
904
"LZ4" );
852
905
#endif
0 commit comments