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

Commit 179cdd0

Browse files
committed
Add macros to check if a filename is a WAL segment or other such file.
We had many instances of the strlen + strspn combination to check for that. This makes the code a bit easier to read.
1 parent 16c73e7 commit 179cdd0

File tree

5 files changed

+31
-29
lines changed

5 files changed

+31
-29
lines changed

src/backend/access/transam/xlog.c

+3-8
Original file line numberDiff line numberDiff line change
@@ -3577,8 +3577,7 @@ RemoveOldXlogFiles(XLogSegNo segno, XLogRecPtr PriorRedoPtr, XLogRecPtr endptr)
35773577
while ((xlde = ReadDir(xldir, XLOGDIR)) != NULL)
35783578
{
35793579
/* Ignore files that are not XLOG segments */
3580-
if (strlen(xlde->d_name) != 24 ||
3581-
strspn(xlde->d_name, "0123456789ABCDEF") != 24)
3580+
if (!IsXLogFileName(xlde->d_name))
35823581
continue;
35833582

35843583
/*
@@ -3650,8 +3649,7 @@ RemoveNonParentXlogFiles(XLogRecPtr switchpoint, TimeLineID newTLI)
36503649
while ((xlde = ReadDir(xldir, XLOGDIR)) != NULL)
36513650
{
36523651
/* Ignore files that are not XLOG segments */
3653-
if (strlen(xlde->d_name) != 24 ||
3654-
strspn(xlde->d_name, "0123456789ABCDEF") != 24)
3652+
if (!IsXLogFileName(xlde->d_name))
36553653
continue;
36563654

36573655
/*
@@ -3839,10 +3837,7 @@ CleanupBackupHistory(void)
38393837

38403838
while ((xlde = ReadDir(xldir, XLOGDIR)) != NULL)
38413839
{
3842-
if (strlen(xlde->d_name) > 24 &&
3843-
strspn(xlde->d_name, "0123456789ABCDEF") == 24 &&
3844-
strcmp(xlde->d_name + strlen(xlde->d_name) - strlen(".backup"),
3845-
".backup") == 0)
3840+
if (IsBackupHistoryFileName(xlde->d_name))
38463841
{
38473842
if (XLogArchiveCheckDone(xlde->d_name))
38483843
{

src/backend/replication/basebackup.c

+2-5
Original file line numberDiff line numberDiff line change
@@ -350,17 +350,14 @@ perform_base_backup(basebackup_options *opt, DIR *tblspcdir)
350350
while ((de = ReadDir(dir, "pg_xlog")) != NULL)
351351
{
352352
/* Does it look like a WAL segment, and is it in the range? */
353-
if (strlen(de->d_name) == 24 &&
354-
strspn(de->d_name, "0123456789ABCDEF") == 24 &&
353+
if (IsXLogFileName(de->d_name) &&
355354
strcmp(de->d_name + 8, firstoff + 8) >= 0 &&
356355
strcmp(de->d_name + 8, lastoff + 8) <= 0)
357356
{
358357
walFileList = lappend(walFileList, pstrdup(de->d_name));
359358
}
360359
/* Does it look like a timeline history file? */
361-
else if (strlen(de->d_name) == 8 + strlen(".history") &&
362-
strspn(de->d_name, "0123456789ABCDEF") == 8 &&
363-
strcmp(de->d_name + 8, ".history") == 0)
360+
else if (IsTLHistoryFileName(de->d_name))
364361
{
365362
historyFileList = lappend(historyFileList, pstrdup(de->d_name));
366363
}

src/bin/pg_basebackup/pg_receivexlog.c

+2-14
Original file line numberDiff line numberDiff line change
@@ -188,23 +188,11 @@ FindStreamingStart(uint32 *tli)
188188

189189
/*
190190
* Check if the filename looks like an xlog file, or a .partial file.
191-
* Xlog files are always 24 characters, and .partial files are 32
192-
* characters.
193191
*/
194-
if (strlen(dirent->d_name) == 24)
195-
{
196-
if (strspn(dirent->d_name, "0123456789ABCDEF") != 24)
197-
continue;
192+
if (IsXLogFileName(dirent->d_name))
198193
ispartial = false;
199-
}
200-
else if (strlen(dirent->d_name) == 32)
201-
{
202-
if (strspn(dirent->d_name, "0123456789ABCDEF") != 24)
203-
continue;
204-
if (strcmp(&dirent->d_name[24], ".partial") != 0)
205-
continue;
194+
else if (IsPartialXLogFileName(dirent->d_name))
206195
ispartial = true;
207-
}
208196
else
209197
continue;
210198

src/bin/pg_resetxlog/pg_resetxlog.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -906,14 +906,18 @@ FindEndOfXLOG(void)
906906

907907
while (errno = 0, (xlde = readdir(xldir)) != NULL)
908908
{
909-
if (strlen(xlde->d_name) == 24 &&
910-
strspn(xlde->d_name, "0123456789ABCDEF") == 24)
909+
if (IsXLogFileName(xlde->d_name))
911910
{
912911
unsigned int tli,
913912
log,
914913
seg;
915914
XLogSegNo segno;
916915

916+
/*
917+
* Note: We don't use XLogFromFileName here, because we want
918+
* to use the segment size from the control file, not the size
919+
* the pg_resetxlog binary was compiled with
920+
*/
917921
sscanf(xlde->d_name, "%08X%08X%08X", &tli, &log, &seg);
918922
segno = ((uint64) log) * segs_per_xlogid + seg;
919923

src/include/access/xlog_internal.h

+18
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,14 @@ typedef XLogLongPageHeaderData *XLogLongPageHeader;
142142
(uint32) ((logSegNo) / XLogSegmentsPerXLogId), \
143143
(uint32) ((logSegNo) % XLogSegmentsPerXLogId))
144144

145+
#define IsXLogFileName(fname) \
146+
(strlen(fname) == 24 && strspn(fname, "0123456789ABCDEF") == 24)
147+
148+
#define IsPartialXLogFileName(fname) \
149+
(strlen(fname) == 24 + strlen(".partial") && \
150+
strspn(fname, "0123456789ABCDEF") == 24 && \
151+
strcmp((fname) + 24, ".partial") == 0)
152+
145153
#define XLogFromFileName(fname, tli, logSegNo) \
146154
do { \
147155
uint32 log; \
@@ -158,6 +166,11 @@ typedef XLogLongPageHeaderData *XLogLongPageHeader;
158166
#define TLHistoryFileName(fname, tli) \
159167
snprintf(fname, MAXFNAMELEN, "%08X.history", tli)
160168

169+
#define IsTLHistoryFileName(fname) \
170+
(strlen(fname) == 8 + strlen(".history") && \
171+
strspn(fname, "0123456789ABCDEF") == 8 && \
172+
strcmp((fname) + 8, ".history") == 0)
173+
161174
#define TLHistoryFilePath(path, tli) \
162175
snprintf(path, MAXPGPATH, XLOGDIR "/%08X.history", tli)
163176

@@ -169,6 +182,11 @@ typedef XLogLongPageHeaderData *XLogLongPageHeader;
169182
(uint32) ((logSegNo) / XLogSegmentsPerXLogId), \
170183
(uint32) ((logSegNo) % XLogSegmentsPerXLogId), offset)
171184

185+
#define IsBackupHistoryFileName(fname) \
186+
(strlen(fname) > 24 && \
187+
strspn(fname, "0123456789ABCDEF") == 24 && \
188+
strcmp((fname) + strlen(fname) - strlen(".backup"), ".backup") == 0)
189+
172190
#define BackupHistoryFilePath(path, tli, logSegNo, offset) \
173191
snprintf(path, MAXPGPATH, XLOGDIR "/%08X%08X%08X.%08X.backup", tli, \
174192
(uint32) ((logSegNo) / XLogSegmentsPerXLogId), \

0 commit comments

Comments
 (0)