We had many instances of the strlen + strspn combination to check for that.
This makes the code a bit easier to read.
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;
/*
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;
/*
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))
{
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));
}
/*
* 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;
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;
(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; \
#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)
(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), \