@@ -37,6 +37,7 @@ const char *progname;
3737/* Options and defaults */
3838bool debug = false; /* are we debugging? */
3939bool dryrun = false; /* are we performing a dry-run operation? */
40+ char * additional_ext = NULL ; /* Extension to remove from filenames */
4041
4142char * archiveLocation ; /* where to find the archive? */
4243char * restartWALFileName ; /* the file from which we can restart restore */
@@ -90,17 +91,37 @@ Initialize(void)
9091 }
9192}
9293
94+ static void
95+ TrimExtension (char * filename , char * extension )
96+ {
97+ int flen ;
98+ int elen ;
99+
100+ if (extension == NULL )
101+ return ;
102+
103+ elen = strlen (extension );
104+ flen = strlen (filename );
105+
106+ if (flen > elen && strcmp (filename + flen - elen , extension ) == 0 )
107+ filename [flen - elen ] = '\0' ;
108+ }
109+
93110static void
94111CleanupPriorWALFiles (void )
95112{
96113 int rc ;
97114 DIR * xldir ;
98115 struct dirent * xlde ;
116+ char walfile [MAXPGPATH ];
99117
100118 if ((xldir = opendir (archiveLocation )) != NULL )
101119 {
102120 while ((xlde = readdir (xldir )) != NULL )
103121 {
122+ strncpy (walfile , xlde -> d_name , MAXPGPATH );
123+ TrimExtension (walfile , additional_ext );
124+
104125 /*
105126 * We ignore the timeline part of the XLOG segment identifiers in
106127 * deciding whether a segment is still needed. This ensures that
@@ -114,10 +135,14 @@ CleanupPriorWALFiles(void)
114135 * file. Note that this means files are not removed in the order
115136 * they were originally written, in case this worries you.
116137 */
117- if (strlen (xlde -> d_name ) == XLOG_DATA_FNAME_LEN &&
118- strspn (xlde -> d_name , "0123456789ABCDEF" ) == XLOG_DATA_FNAME_LEN &&
119- strcmp (xlde -> d_name + 8 , exclusiveCleanupFileName + 8 ) < 0 )
138+ if (strlen (walfile ) == XLOG_DATA_FNAME_LEN &&
139+ strspn (walfile , "0123456789ABCDEF" ) == XLOG_DATA_FNAME_LEN &&
140+ strcmp (walfile + 8 , exclusiveCleanupFileName + 8 ) < 0 )
120141 {
142+ /*
143+ * Use the original file name again now, including any extension
144+ * that might have been chopped off before testing the sequence.
145+ */
121146 snprintf (WALFilePath , MAXPGPATH , "%s/%s" ,
122147 archiveLocation , xlde -> d_name );
123148
@@ -167,6 +192,8 @@ SetWALFileNameForCleanup(void)
167192{
168193 bool fnameOK = false;
169194
195+ TrimExtension (restartWALFileName , additional_ext );
196+
170197 /*
171198 * If restartWALFileName is a WAL file name then just use it directly. If
172199 * restartWALFileName is a .backup filename, make sure we use the prefix
@@ -223,6 +250,7 @@ usage(void)
223250 printf ("\nOptions:\n" );
224251 printf (" -d generates debug output (verbose mode)\n" );
225252 printf (" -n shows the names of the files that would have been removed (dry-run)\n" );
253+ printf (" -x EXT cleanup files if they have this same extension\n" );
226254 printf (" --help show this help, then exit\n" );
227255 printf (" --version output version information, then exit\n" );
228256 printf ("\n"
@@ -259,7 +287,7 @@ main(int argc, char **argv)
259287 }
260288 }
261289
262- while ((c = getopt (argc , argv , "dn" )) != -1 )
290+ while ((c = getopt (argc , argv , "x: dn" )) != -1 )
263291 {
264292 switch (c )
265293 {
@@ -269,6 +297,9 @@ main(int argc, char **argv)
269297 case 'n' : /* Dry-Run mode */
270298 dryrun = true;
271299 break ;
300+ case 'x' :
301+ additional_ext = optarg ; /* Extension to remove from xlogfile names */
302+ break ;
272303 default :
273304 fprintf (stderr , "Try \"%s --help\" for more information.\n" , progname );
274305 exit (2 );
0 commit comments