1 /***********************************************************************************************************************************
2 Archive Info Handler
3 ***********************************************************************************************************************************/
4 #ifndef INFO_INFOARCHIVE_H
5 #define INFO_INFOARCHIVE_H
6 
7 /***********************************************************************************************************************************
8 Object type
9 ***********************************************************************************************************************************/
10 typedef struct InfoArchive InfoArchive;
11 
12 #include "common/crypto/common.h"
13 #include "common/type/object.h"
14 #include "common/type/string.h"
15 #include "info/infoPg.h"
16 #include "storage/storage.h"
17 
18 /***********************************************************************************************************************************
19 Archive info filename
20 ***********************************************************************************************************************************/
21 #define INFO_ARCHIVE_FILE                                           "archive.info"
22 #define REGEX_ARCHIVE_DIR_DB_VERSION                                "^[0-9]+(\\.[0-9]+)*-[0-9]+$"
23 
24 #define INFO_ARCHIVE_PATH_FILE                                      STORAGE_REPO_ARCHIVE "/" INFO_ARCHIVE_FILE
25     STRING_DECLARE(INFO_ARCHIVE_PATH_FILE_STR);
26 #define INFO_ARCHIVE_PATH_FILE_COPY                                 INFO_ARCHIVE_PATH_FILE INFO_COPY_EXT
27     STRING_DECLARE(INFO_ARCHIVE_PATH_FILE_COPY_STR);
28 
29 /***********************************************************************************************************************************
30 Constructors
31 ***********************************************************************************************************************************/
32 InfoArchive *infoArchiveNew(const unsigned int pgVersion, const uint64_t pgSystemId, const String *cipherPassSub);
33 
34 // Create new object and load contents from IoRead
35 InfoArchive *infoArchiveNewLoad(IoRead *read);
36 
37 /***********************************************************************************************************************************
38 Getters/Setters
39 ***********************************************************************************************************************************/
40 typedef struct InfoArchivePub
41 {
42     MemContext *memContext;                                         // Mem context
43     InfoPg *infoPg;                                                 // Contents of the DB data
44 } InfoArchivePub;
45 
46 // PostgreSQL info
47 __attribute__((always_inline)) static inline InfoPg *
infoArchivePg(const InfoArchive * const this)48 infoArchivePg(const InfoArchive *const this)
49 {
50     return THIS_PUB(InfoArchive)->infoPg;
51 }
52 
53 InfoArchive *infoArchivePgSet(InfoArchive *this, unsigned int pgVersion, uint64_t pgSystemId);
54 
55 // Current archive id
56 __attribute__((always_inline)) static inline const String *
infoArchiveId(const InfoArchive * const this)57 infoArchiveId(const InfoArchive *const this)
58 {
59     return infoPgArchiveId(infoArchivePg(this), infoPgDataCurrentId(infoArchivePg(this)));
60 }
61 
62 // Cipher passphrase
63 __attribute__((always_inline)) static inline const String *
infoArchiveCipherPass(const InfoArchive * const this)64 infoArchiveCipherPass(const InfoArchive *const this)
65 {
66     return infoPgCipherPass(infoArchivePg(this));
67 }
68 
69 /***********************************************************************************************************************************
70 Functions
71 ***********************************************************************************************************************************/
72 // Given a backrest history id and postgres systemId and version, return the archiveId of the best match
73 const String *infoArchiveIdHistoryMatch(
74     const InfoArchive *this, const unsigned int historyId, const unsigned int pgVersion, const uint64_t pgSystemId);
75 
76 // Move to a new parent mem context
77 __attribute__((always_inline)) static inline InfoArchive *
infoArchiveMove(InfoArchive * const this,MemContext * const parentNew)78 infoArchiveMove(InfoArchive *const this, MemContext *const parentNew)
79 {
80     return objMove(this, parentNew);
81 }
82 
83 /***********************************************************************************************************************************
84 Destructor
85 ***********************************************************************************************************************************/
86 __attribute__((always_inline)) static inline void
infoArchiveFree(InfoArchive * const this)87 infoArchiveFree(InfoArchive *const this)
88 {
89     objFree(this);
90 }
91 
92 /***********************************************************************************************************************************
93 Helper functions
94 ***********************************************************************************************************************************/
95 // Load archive info
96 InfoArchive *infoArchiveLoadFile(
97     const Storage *storage, const String *fileName, CipherType cipherType, const String *cipherPass);
98 
99 // Save archive info
100 void infoArchiveSaveFile(
101     InfoArchive *infoArchive, const Storage *storage, const String *fileName, CipherType cipherType, const String *cipherPass);
102 
103 /***********************************************************************************************************************************
104 Macros for function logging
105 ***********************************************************************************************************************************/
106 #define FUNCTION_LOG_INFO_ARCHIVE_TYPE                                                                                             \
107     InfoArchive *
108 #define FUNCTION_LOG_INFO_ARCHIVE_FORMAT(value, buffer, bufferSize)                                                                \
109     objToLog(value, "InfoArchive", buffer, bufferSize)
110 
111 #endif
112