]> granicus.if.org Git - postgresql/commitdiff
Add macros to check if a filename is a WAL segment or other such file.
authorHeikki Linnakangas <heikki.linnakangas@iki.fi>
Fri, 8 May 2015 18:58:57 +0000 (21:58 +0300)
committerHeikki Linnakangas <heikki.linnakangas@iki.fi>
Fri, 8 May 2015 18:58:57 +0000 (21:58 +0300)
We had many instances of the strlen + strspn combination to check for that.
This makes the code a bit easier to read.

src/backend/access/transam/xlog.c
src/backend/replication/basebackup.c
src/bin/pg_basebackup/pg_receivexlog.c
src/bin/pg_resetxlog/pg_resetxlog.c
src/include/access/xlog_internal.h

index 92822a192b0f5988e399b7542c6fc3f1163fe736..5097173c60952bf8dbef001c828d9759a8cb9c8d 100644 (file)
@@ -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))
                        {
index 3563fd997fd89e39ea10be04a5ee8a22df6580b3..de103c6f5b7300283915e2ece36867e51465a7cf 100644 (file)
@@ -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));
                        }
index e77d2b6d708aa29ff2108e867611f3c3f69a43db..53802af896b9c5562344d326b828bacce6c5cdbf 100644 (file)
@@ -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;
 
index 4a22575736877d94aadc39e20ba61634e6477d15..393d5801540bd0706d7d243525f4e2c5773c0eb7 100644 (file)
@@ -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;
 
index 75cf435e90f49f73fadbaddfe2ef3cf20d541277..714850ca68c054e0f81145ece16a88cc94219cb2 100644 (file)
@@ -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), \