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

Commit fb17468

Browse files
committed
Make use of xlog_internal.h's macros in WAL-related utilities.
Commit 179cdd0 added macros to check if a filename is a WAL segment or other such file. However there were still some instances of the strlen + strspn combination to check for that in WAL-related utilities like pg_archivecleanup. Those checks can be replaced with the macros. This patch makes use of the macros in those utilities and which would make the code a bit easier to read. Back-patch to 9.5. Michael Paquier
1 parent 1e24cf6 commit fb17468

File tree

4 files changed

+33
-43
lines changed

4 files changed

+33
-43
lines changed

contrib/pg_standby/pg_standby.c

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
#include "pg_getopt.h"
3434

35+
#include "access/xlog_internal.h"
36+
3537
const char *progname;
3638

3739
/* Options and defaults */
@@ -57,7 +59,7 @@ char *restartWALFileName; /* the file from which we can restart restore */
5759
char *priorWALFileName; /* the file we need to get from archive */
5860
char WALFilePath[MAXPGPATH]; /* the file path including archive */
5961
char restoreCommand[MAXPGPATH]; /* run this to restore */
60-
char exclusiveCleanupFileName[MAXPGPATH]; /* the file we need to
62+
char exclusiveCleanupFileName[MAXFNAMELEN]; /* the file we need to
6163
* get from archive */
6264

6365
/*
@@ -113,11 +115,6 @@ struct stat stat_buf;
113115
* folded in to later versions of this program.
114116
*/
115117

116-
#define XLOG_DATA_FNAME_LEN 24
117-
/* Reworked from access/xlog_internal.h */
118-
#define XLogFileName(fname, tli, log, seg) \
119-
snprintf(fname, XLOG_DATA_FNAME_LEN + 1, "%08X%08X%08X", tli, log, seg)
120-
121118
/*
122119
* Initialize allows customized commands into the warm standby program.
123120
*
@@ -182,10 +179,7 @@ CustomizableNextWALFileReady()
182179
* If it's a backup file, return immediately. If it's a regular file
183180
* return only if it's the right size already.
184181
*/
185-
if (strlen(nextWALFileName) > 24 &&
186-
strspn(nextWALFileName, "0123456789ABCDEF") == 24 &&
187-
strcmp(nextWALFileName + strlen(nextWALFileName) - strlen(".backup"),
188-
".backup") == 0)
182+
if (IsBackupHistoryFileName(nextWALFileName))
189183
{
190184
nextWALFileType = XLOG_BACKUP_LABEL;
191185
return true;
@@ -261,8 +255,7 @@ CustomizableCleanupPriorWALFiles(void)
261255
* are not removed in the order they were originally written,
262256
* in case this worries you.
263257
*/
264-
if (strlen(xlde->d_name) == XLOG_DATA_FNAME_LEN &&
265-
strspn(xlde->d_name, "0123456789ABCDEF") == XLOG_DATA_FNAME_LEN &&
258+
if (IsXLogFileName(xlde->d_name) &&
266259
strcmp(xlde->d_name + 8, exclusiveCleanupFileName + 8) < 0)
267260
{
268261
#ifdef WIN32
@@ -366,7 +359,7 @@ SetWALFileNameForCleanup(void)
366359
}
367360
}
368361

369-
XLogFileName(exclusiveCleanupFileName, tli, log, seg);
362+
XLogFileNameById(exclusiveCleanupFileName, tli, log, seg);
370363

371364
return cleanup;
372365
}
@@ -740,10 +733,7 @@ main(int argc, char **argv)
740733
* Check for initial history file: always the first file to be requested
741734
* It's OK if the file isn't there - all other files need to wait
742735
*/
743-
if (strlen(nextWALFileName) > 8 &&
744-
strspn(nextWALFileName, "0123456789ABCDEF") == 8 &&
745-
strcmp(nextWALFileName + strlen(nextWALFileName) - strlen(".history"),
746-
".history") == 0)
736+
if (IsTLHistoryFileName(nextWALFileName))
747737
{
748738
nextWALFileType = XLOG_HISTORY;
749739
if (RestoreWALFileForRecovery())

src/bin/pg_archivecleanup/pg_archivecleanup.c

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
#include "pg_getopt.h"
2323

24+
#include "access/xlog_internal.h"
25+
2426
const char *progname;
2527

2628
/* Options and defaults */
@@ -31,7 +33,7 @@ char *additional_ext = NULL; /* Extension to remove from filenames */
3133
char *archiveLocation; /* where to find the archive? */
3234
char *restartWALFileName; /* the file from which we can restart restore */
3335
char WALFilePath[MAXPGPATH]; /* the file path including archive */
34-
char exclusiveCleanupFileName[MAXPGPATH]; /* the oldest file we
36+
char exclusiveCleanupFileName[MAXFNAMELEN]; /* the oldest file we
3537
* want to remain in
3638
* archive */
3739

@@ -51,12 +53,6 @@ char exclusiveCleanupFileName[MAXPGPATH]; /* the oldest file we
5153
* folded in to later versions of this program.
5254
*/
5355

54-
#define XLOG_DATA_FNAME_LEN 24
55-
/* Reworked from access/xlog_internal.h */
56-
#define XLogFileName(fname, tli, log, seg) \
57-
snprintf(fname, XLOG_DATA_FNAME_LEN + 1, "%08X%08X%08X", tli, log, seg)
58-
#define XLOG_BACKUP_FNAME_LEN 40
59-
6056
/*
6157
* Initialize allows customized commands into the archive cleanup program.
6258
*
@@ -110,7 +106,7 @@ CleanupPriorWALFiles(void)
110106
{
111107
/*
112108
* Truncation is essentially harmless, because we skip names of
113-
* length other than XLOG_DATA_FNAME_LEN. (In principle, one
109+
* length other than XLOG_FNAME_LEN. (In principle, one
114110
* could use a 1000-character additional_ext and get trouble.)
115111
*/
116112
strlcpy(walfile, xlde->d_name, MAXPGPATH);
@@ -129,8 +125,7 @@ CleanupPriorWALFiles(void)
129125
* file. Note that this means files are not removed in the order
130126
* they were originally written, in case this worries you.
131127
*/
132-
if (strlen(walfile) == XLOG_DATA_FNAME_LEN &&
133-
strspn(walfile, "0123456789ABCDEF") == XLOG_DATA_FNAME_LEN &&
128+
if (IsXLogFileName(walfile) &&
134129
strcmp(walfile + 8, exclusiveCleanupFileName + 8) < 0)
135130
{
136131
/*
@@ -202,13 +197,12 @@ SetWALFileNameForCleanup(void)
202197
* 000000010000000000000010.00000020.backup is after
203198
* 000000010000000000000010.
204199
*/
205-
if (strlen(restartWALFileName) == XLOG_DATA_FNAME_LEN &&
206-
strspn(restartWALFileName, "0123456789ABCDEF") == XLOG_DATA_FNAME_LEN)
200+
if (IsXLogFileName(restartWALFileName))
207201
{
208202
strcpy(exclusiveCleanupFileName, restartWALFileName);
209203
fnameOK = true;
210204
}
211-
else if (strlen(restartWALFileName) == XLOG_BACKUP_FNAME_LEN)
205+
else if (IsBackupHistoryFileName(restartWALFileName))
212206
{
213207
int args;
214208
uint32 tli = 1,
@@ -225,7 +219,7 @@ SetWALFileNameForCleanup(void)
225219
* Use just the prefix of the filename, ignore everything after
226220
* first period
227221
*/
228-
XLogFileName(exclusiveCleanupFileName, tli, log, seg);
222+
XLogFileNameById(exclusiveCleanupFileName, tli, log, seg);
229223
}
230224
}
231225

src/bin/pg_resetxlog/pg_resetxlog.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ main(int argc, char *argv[])
261261
break;
262262

263263
case 'l':
264-
if (strspn(optarg, "01234567890ABCDEFabcdef") != 24)
264+
if (strspn(optarg, "01234567890ABCDEFabcdef") != XLOG_FNAME_LEN)
265265
{
266266
fprintf(stderr, _("%s: invalid argument for option %s\n"), progname, "-l");
267267
fprintf(stderr, _("Try \"%s --help\" for more information.\n"), progname);
@@ -976,8 +976,7 @@ KillExistingXLOG(void)
976976

977977
while (errno = 0, (xlde = readdir(xldir)) != NULL)
978978
{
979-
if (strlen(xlde->d_name) == 24 &&
980-
strspn(xlde->d_name, "0123456789ABCDEF") == 24)
979+
if (IsXLogFileName(xlde->d_name))
981980
{
982981
snprintf(path, MAXPGPATH, "%s/%s", XLOGDIR, xlde->d_name);
983982
if (unlink(path) < 0)
@@ -1027,9 +1026,9 @@ KillExistingArchiveStatus(void)
10271026

10281027
while (errno = 0, (xlde = readdir(xldir)) != NULL)
10291028
{
1030-
if (strspn(xlde->d_name, "0123456789ABCDEF") == 24 &&
1031-
(strcmp(xlde->d_name + 24, ".ready") == 0 ||
1032-
strcmp(xlde->d_name + 24, ".done") == 0))
1029+
if (strspn(xlde->d_name, "0123456789ABCDEF") == XLOG_FNAME_LEN &&
1030+
(strcmp(xlde->d_name + XLOG_FNAME_LEN, ".ready") == 0 ||
1031+
strcmp(xlde->d_name + XLOG_FNAME_LEN, ".done") == 0))
10331032
{
10341033
snprintf(path, MAXPGPATH, "%s/%s", ARCHSTATDIR, xlde->d_name);
10351034
if (unlink(path) < 0)

src/include/access/xlog_internal.h

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,23 +137,30 @@ typedef XLogLongPageHeaderData *XLogLongPageHeader;
137137
*/
138138
#define MAXFNAMELEN 64
139139

140+
/* Length of XLog file name */
141+
#define XLOG_FNAME_LEN 24
142+
140143
#define XLogFileName(fname, tli, logSegNo) \
141144
snprintf(fname, MAXFNAMELEN, "%08X%08X%08X", tli, \
142145
(uint32) ((logSegNo) / XLogSegmentsPerXLogId), \
143146
(uint32) ((logSegNo) % XLogSegmentsPerXLogId))
144147

148+
#define XLogFileNameById(fname, tli, log, seg) \
149+
snprintf(fname, MAXFNAMELEN, "%08X%08X%08X", tli, log, seg)
150+
145151
#define IsXLogFileName(fname) \
146-
(strlen(fname) == 24 && strspn(fname, "0123456789ABCDEF") == 24)
152+
(strlen(fname) == XLOG_FNAME_LEN && \
153+
strspn(fname, "0123456789ABCDEF") == XLOG_FNAME_LEN)
147154

148155
/*
149156
* XLOG segment with .partial suffix. Used by pg_receivexlog and at end of
150157
* archive recovery, when we want to archive a WAL segment but it might not
151158
* be complete yet.
152159
*/
153160
#define IsPartialXLogFileName(fname) \
154-
(strlen(fname) == 24 + strlen(".partial") && \
155-
strspn(fname, "0123456789ABCDEF") == 24 && \
156-
strcmp((fname) + 24, ".partial") == 0)
161+
(strlen(fname) == XLOG_FNAME_LEN + strlen(".partial") && \
162+
strspn(fname, "0123456789ABCDEF") == XLOG_FNAME_LEN && \
163+
strcmp((fname) + XLOG_FNAME_LEN, ".partial") == 0)
157164

158165
#define XLogFromFileName(fname, tli, logSegNo) \
159166
do { \
@@ -188,8 +195,8 @@ typedef XLogLongPageHeaderData *XLogLongPageHeader;
188195
(uint32) ((logSegNo) % XLogSegmentsPerXLogId), offset)
189196

190197
#define IsBackupHistoryFileName(fname) \
191-
(strlen(fname) > 24 && \
192-
strspn(fname, "0123456789ABCDEF") == 24 && \
198+
(strlen(fname) > XLOG_FNAME_LEN && \
199+
strspn(fname, "0123456789ABCDEF") == XLOG_FNAME_LEN && \
193200
strcmp((fname) + strlen(fname) - strlen(".backup"), ".backup") == 0)
194201

195202
#define BackupHistoryFilePath(path, tli, logSegNo, offset) \

0 commit comments

Comments
 (0)