Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit bbc0224

Browse files
committed
Allow pg_archivecleanup to strip optional file extensions.
Greg Smith and Jaime Casanova, reviewed by Alex Shulgin and myself. e
1 parent b736aef commit bbc0224

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

contrib/pg_archivecleanup/pg_archivecleanup.c

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const char *progname;
3737
/* Options and defaults */
3838
bool debug = false; /* are we debugging? */
3939
bool dryrun = false; /* are we performing a dry-run operation? */
40+
char *additional_ext = NULL; /* Extension to remove from filenames */
4041

4142
char *archiveLocation; /* where to find the archive? */
4243
char *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+
93110
static void
94111
CleanupPriorWALFiles(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);

doc/src/sgml/pgarchivecleanup.sgml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,21 @@ pg_archivecleanup: removing file "archive/00000001000000370000000E"
107107
</listitem>
108108
</varlistentry>
109109

110+
<varlistentry>
111+
<term><option>-x</option> <replaceable>extension</></term>
112+
<listitem>
113+
<para>
114+
When using the program as a standalone utility, provide an extension
115+
that will be stripped from all file names before deciding if they
116+
should be deleted. This is typically useful for cleaning up archives
117+
that have been compressed during storage, and therefore have had an
118+
extension added by the compression program. Note that the
119+
<filename>.backup</> file name passed to the program should not
120+
include the extension.
121+
</para>
122+
</listitem>
123+
</varlistentry>
124+
110125
</variablelist>
111126
</para>
112127

0 commit comments

Comments
 (0)