mirror of
https://git.postgresql.org/git/postgresql.git
synced 2024-12-15 08:20:16 +08:00
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.
This commit is contained in:
parent
16c73e773b
commit
179cdd0981
@ -3577,8 +3577,7 @@ RemoveOldXlogFiles(XLogSegNo segno, XLogRecPtr PriorRedoPtr, XLogRecPtr endptr)
|
||||
while ((xlde = ReadDir(xldir, XLOGDIR)) != NULL)
|
||||
{
|
||||
/* Ignore files that are not XLOG segments */
|
||||
if (strlen(xlde->d_name) != 24 ||
|
||||
strspn(xlde->d_name, "0123456789ABCDEF") != 24)
|
||||
if (!IsXLogFileName(xlde->d_name))
|
||||
continue;
|
||||
|
||||
/*
|
||||
@ -3650,8 +3649,7 @@ RemoveNonParentXlogFiles(XLogRecPtr switchpoint, TimeLineID newTLI)
|
||||
while ((xlde = ReadDir(xldir, XLOGDIR)) != NULL)
|
||||
{
|
||||
/* Ignore files that are not XLOG segments */
|
||||
if (strlen(xlde->d_name) != 24 ||
|
||||
strspn(xlde->d_name, "0123456789ABCDEF") != 24)
|
||||
if (!IsXLogFileName(xlde->d_name))
|
||||
continue;
|
||||
|
||||
/*
|
||||
@ -3839,10 +3837,7 @@ CleanupBackupHistory(void)
|
||||
|
||||
while ((xlde = ReadDir(xldir, XLOGDIR)) != NULL)
|
||||
{
|
||||
if (strlen(xlde->d_name) > 24 &&
|
||||
strspn(xlde->d_name, "0123456789ABCDEF") == 24 &&
|
||||
strcmp(xlde->d_name + strlen(xlde->d_name) - strlen(".backup"),
|
||||
".backup") == 0)
|
||||
if (IsBackupHistoryFileName(xlde->d_name))
|
||||
{
|
||||
if (XLogArchiveCheckDone(xlde->d_name))
|
||||
{
|
||||
|
@ -350,17 +350,14 @@ perform_base_backup(basebackup_options *opt, DIR *tblspcdir)
|
||||
while ((de = ReadDir(dir, "pg_xlog")) != NULL)
|
||||
{
|
||||
/* Does it look like a WAL segment, and is it in the range? */
|
||||
if (strlen(de->d_name) == 24 &&
|
||||
strspn(de->d_name, "0123456789ABCDEF") == 24 &&
|
||||
if (IsXLogFileName(de->d_name) &&
|
||||
strcmp(de->d_name + 8, firstoff + 8) >= 0 &&
|
||||
strcmp(de->d_name + 8, lastoff + 8) <= 0)
|
||||
{
|
||||
walFileList = lappend(walFileList, pstrdup(de->d_name));
|
||||
}
|
||||
/* Does it look like a timeline history file? */
|
||||
else if (strlen(de->d_name) == 8 + strlen(".history") &&
|
||||
strspn(de->d_name, "0123456789ABCDEF") == 8 &&
|
||||
strcmp(de->d_name + 8, ".history") == 0)
|
||||
else if (IsTLHistoryFileName(de->d_name))
|
||||
{
|
||||
historyFileList = lappend(historyFileList, pstrdup(de->d_name));
|
||||
}
|
||||
|
@ -188,23 +188,11 @@ FindStreamingStart(uint32 *tli)
|
||||
|
||||
/*
|
||||
* Check if the filename looks like an xlog file, or a .partial file.
|
||||
* Xlog files are always 24 characters, and .partial files are 32
|
||||
* characters.
|
||||
*/
|
||||
if (strlen(dirent->d_name) == 24)
|
||||
{
|
||||
if (strspn(dirent->d_name, "0123456789ABCDEF") != 24)
|
||||
continue;
|
||||
if (IsXLogFileName(dirent->d_name))
|
||||
ispartial = false;
|
||||
}
|
||||
else if (strlen(dirent->d_name) == 32)
|
||||
{
|
||||
if (strspn(dirent->d_name, "0123456789ABCDEF") != 24)
|
||||
continue;
|
||||
if (strcmp(&dirent->d_name[24], ".partial") != 0)
|
||||
continue;
|
||||
else if (IsPartialXLogFileName(dirent->d_name))
|
||||
ispartial = true;
|
||||
}
|
||||
else
|
||||
continue;
|
||||
|
||||
|
@ -906,14 +906,18 @@ FindEndOfXLOG(void)
|
||||
|
||||
while (errno = 0, (xlde = readdir(xldir)) != NULL)
|
||||
{
|
||||
if (strlen(xlde->d_name) == 24 &&
|
||||
strspn(xlde->d_name, "0123456789ABCDEF") == 24)
|
||||
if (IsXLogFileName(xlde->d_name))
|
||||
{
|
||||
unsigned int tli,
|
||||
log,
|
||||
seg;
|
||||
XLogSegNo segno;
|
||||
|
||||
/*
|
||||
* Note: We don't use XLogFromFileName here, because we want
|
||||
* to use the segment size from the control file, not the size
|
||||
* the pg_resetxlog binary was compiled with
|
||||
*/
|
||||
sscanf(xlde->d_name, "%08X%08X%08X", &tli, &log, &seg);
|
||||
segno = ((uint64) log) * segs_per_xlogid + seg;
|
||||
|
||||
|
@ -142,6 +142,14 @@ typedef XLogLongPageHeaderData *XLogLongPageHeader;
|
||||
(uint32) ((logSegNo) / XLogSegmentsPerXLogId), \
|
||||
(uint32) ((logSegNo) % XLogSegmentsPerXLogId))
|
||||
|
||||
#define IsXLogFileName(fname) \
|
||||
(strlen(fname) == 24 && strspn(fname, "0123456789ABCDEF") == 24)
|
||||
|
||||
#define IsPartialXLogFileName(fname) \
|
||||
(strlen(fname) == 24 + strlen(".partial") && \
|
||||
strspn(fname, "0123456789ABCDEF") == 24 && \
|
||||
strcmp((fname) + 24, ".partial") == 0)
|
||||
|
||||
#define XLogFromFileName(fname, tli, logSegNo) \
|
||||
do { \
|
||||
uint32 log; \
|
||||
@ -158,6 +166,11 @@ typedef XLogLongPageHeaderData *XLogLongPageHeader;
|
||||
#define TLHistoryFileName(fname, tli) \
|
||||
snprintf(fname, MAXFNAMELEN, "%08X.history", tli)
|
||||
|
||||
#define IsTLHistoryFileName(fname) \
|
||||
(strlen(fname) == 8 + strlen(".history") && \
|
||||
strspn(fname, "0123456789ABCDEF") == 8 && \
|
||||
strcmp((fname) + 8, ".history") == 0)
|
||||
|
||||
#define TLHistoryFilePath(path, tli) \
|
||||
snprintf(path, MAXPGPATH, XLOGDIR "/%08X.history", tli)
|
||||
|
||||
@ -169,6 +182,11 @@ typedef XLogLongPageHeaderData *XLogLongPageHeader;
|
||||
(uint32) ((logSegNo) / XLogSegmentsPerXLogId), \
|
||||
(uint32) ((logSegNo) % XLogSegmentsPerXLogId), offset)
|
||||
|
||||
#define IsBackupHistoryFileName(fname) \
|
||||
(strlen(fname) > 24 && \
|
||||
strspn(fname, "0123456789ABCDEF") == 24 && \
|
||||
strcmp((fname) + strlen(fname) - strlen(".backup"), ".backup") == 0)
|
||||
|
||||
#define BackupHistoryFilePath(path, tli, logSegNo, offset) \
|
||||
snprintf(path, MAXPGPATH, XLOGDIR "/%08X%08X%08X.%08X.backup", tli, \
|
||||
(uint32) ((logSegNo) / XLogSegmentsPerXLogId), \
|
||||
|
Loading…
Reference in New Issue
Block a user