1 /***********************************************************************************************************************************
2 Archive Common
3 ***********************************************************************************************************************************/
4 #ifndef COMMAND_ARCHIVE_COMMON_H
5 #define COMMAND_ARCHIVE_COMMON_H
6 
7 #include <sys/types.h>
8 
9 #include "common/type/stringId.h"
10 
11 /***********************************************************************************************************************************
12 Archive mode enum
13 
14 Used for functions that are common to both archive-push and archive-get so they can tailor their behavior to the command being run.
15 ***********************************************************************************************************************************/
16 typedef enum
17 {
18     archiveModeGet = STRID5("get", 0x50a70),
19     archiveModePush = STRID5("push", 0x44eb00),
20 } ArchiveMode;
21 
22 #include "common/compress/helper.h"
23 #include "common/type/stringList.h"
24 #include "storage/storage.h"
25 
26 /***********************************************************************************************************************************
27 Status file extension constants
28 ***********************************************************************************************************************************/
29 #define STATUS_EXT_ERROR                                            ".error"
30 #define STATUS_EXT_ERROR_SIZE                                       (sizeof(STATUS_EXT_ERROR) - 1)
31 
32 #define STATUS_EXT_OK                                               ".ok"
33 #define STATUS_EXT_OK_SIZE                                          (sizeof(STATUS_EXT_OK) - 1)
34 
35 /***********************************************************************************************************************************
36 WAL segment constants
37 ***********************************************************************************************************************************/
38 // Extension for partial segments
39 #define WAL_SEGMENT_PARTIAL_EXT                                     ".partial"
40 
41 // Match when the first 24 characters match a WAL segment
42 #define WAL_SEGMENT_PREFIX_REGEXP                                   "^[0-F]{24}"
43 
44 // Match on a WAL segment without checksum appended
45 #define WAL_SEGMENT_REGEXP                                          WAL_SEGMENT_PREFIX_REGEXP "$"
46     STRING_DECLARE(WAL_SEGMENT_REGEXP_STR);
47 
48 // Match on a WAL segment with partial allowed
49 #define WAL_SEGMENT_PARTIAL_REGEXP                                  WAL_SEGMENT_PREFIX_REGEXP "(\\.partial){0,1}$"
50     STRING_DECLARE(WAL_SEGMENT_PARTIAL_REGEXP_STR);
51 
52 // Defines the size of standard WAL segment name -- hopefully this won't change
53 #define WAL_SEGMENT_NAME_SIZE                                       ((unsigned int)24)
54 
55 // WAL segment directory/file
56 #define WAL_SEGMENT_DIR_REGEXP                                      "^[0-F]{16}$"
57     STRING_DECLARE(WAL_SEGMENT_DIR_REGEXP_STR);
58 #define WAL_SEGMENT_FILE_REGEXP                                     "^[0-F]{24}-[0-f]{40}" COMPRESS_TYPE_REGEXP "{0,1}$"
59     STRING_DECLARE(WAL_SEGMENT_FILE_REGEXP_STR);
60 
61 // Timeline history file
62 #define WAL_TIMELINE_HISTORY_REGEXP                                 "^[0-F]{8}.history$"
63     STRING_DECLARE(WAL_TIMELINE_HISTORY_REGEXP_STR);
64 
65 /***********************************************************************************************************************************
66 Functions
67 ***********************************************************************************************************************************/
68 // Remove errors for an archive file.  This should be done before forking the async process to prevent a race condition where an
69 // old error may be reported rather than waiting for the async process to succeed or fail.
70 void archiveAsyncErrorClear(ArchiveMode archiveMode, const String *archiveFile);
71 
72 // Check for ok/error status files in the spool in/out directory. throwOnError determines whether an error will be thrown when an
73 // error file is found. warnOnOk determines whether a warning will be output when found in an ok file.
74 bool archiveAsyncStatus(ArchiveMode archiveMode, const String *walSegment, bool throwOnError, bool warnOnOk);
75 
76 // Write an ok status file
77 void archiveAsyncStatusOkWrite(ArchiveMode archiveMode, const String *walSegment, const String *warning);
78 
79 // Write an error status file
80 void archiveAsyncStatusErrorWrite(ArchiveMode archiveMode, const String *walSegment, int code, const String *message);
81 
82 // Execute the async process.  This function will only return in the calling process and the implementation is platform depedent.
83 void archiveAsyncExec(ArchiveMode archiveMode, const StringList *commandExec);
84 
85 // Comparator function for sorting archive ids by the database history id (the number after the dash) e.g. 9.4-1, 10-2
86 int archiveIdComparator(const void *item1, const void *item2);
87 
88 // Is the segment partial?
89 bool walIsPartial(const String *walSegment);
90 
91 // Is the file a segment or some other file (e.g. .history, .backup, etc)
92 bool walIsSegment(const String *walSegment);
93 
94 // Generates the location of the wal directory using a relative wal path and the supplied pg path
95 String *walPath(const String *walFile, const String *pgPath, const String *command);
96 
97 // Find a WAL segment in the repository. The file name can have several things appended such as a hash, compression extension, and
98 // partial extension so it is possible to have multiple files that match the segment, though more than one match is not a good
99 // thing.
100 String *walSegmentFind(const Storage *storage, const String *archiveId, const String *walSegment, TimeMSec timeout);
101 
102 // Get the next WAL segment given a WAL segment and WAL segment size
103 String *walSegmentNext(const String *walSegment, size_t walSegmentSize, unsigned int pgVersion);
104 
105 // Build a list of WAL segments based on a beginning WAL and number of WAL in the range (inclusive)
106 StringList *walSegmentRange(const String *walSegmentBegin, size_t walSegmentSize, unsigned int pgVersion, unsigned int range);
107 
108 #endif
109