1 /***********************************************************************************************************************************
2 Info Handler
3 ***********************************************************************************************************************************/
4 #ifndef INFO_INFO_H
5 #define INFO_INFO_H
6
7 /***********************************************************************************************************************************
8 Object type
9 ***********************************************************************************************************************************/
10 typedef struct Info Info;
11 typedef struct InfoSave InfoSave;
12
13 #include "common/ini.h"
14 #include "storage/storage.h"
15
16 /***********************************************************************************************************************************
17 Constants
18 ***********************************************************************************************************************************/
19 #define INFO_COPY_EXT ".copy"
20
21 #define INFO_KEY_FORMAT "backrest-format"
22 STRING_DECLARE(INFO_KEY_FORMAT_STR);
23 #define INFO_KEY_VERSION "backrest-version"
24 STRING_DECLARE(INFO_KEY_VERSION_STR);
25
26 /***********************************************************************************************************************************
27 Function types for loading and saving
28 ***********************************************************************************************************************************/
29 // The purpose of this callback is to attempt a load (from file or otherwise). Return true when the load is successful or throw an
30 // error. Return false when there are no more loads to try, but always make at least one load attempt. The try parameter will
31 // start at 0 and be incremented on each call.
32 typedef bool InfoLoadCallback(void *data, unsigned int try);
33
34 typedef void InfoLoadNewCallback(void *data, const String *section, const String *key, const Variant *value);
35 typedef void InfoSaveCallback(void *data, const String *sectionNext, InfoSave *infoSaveData);
36
37 /***********************************************************************************************************************************
38 Constructors
39 ***********************************************************************************************************************************/
40 Info *infoNew(const String *cipherPassSub);
41
42 // Create new object and load contents from a file
43 Info *infoNewLoad(IoRead *read, InfoLoadNewCallback *callbackFunction, void *callbackData);
44
45 /***********************************************************************************************************************************
46 Getters/Setters
47 ***********************************************************************************************************************************/
48 typedef struct InfoPub
49 {
50 const String *backrestVersion; // pgBackRest version
51 const String *cipherPass; // Cipher passphrase if set
52 } InfoPub;
53
54 // Cipher passphrase if set
55 __attribute__((always_inline)) static inline const String *
infoCipherPass(const Info * const this)56 infoCipherPass(const Info *const this)
57 {
58 return THIS_PUB(Info)->cipherPass;
59 }
60
61 void infoCipherPassSet(Info *this, const String *cipherPass);
62
63 // pgBackRest version
64 __attribute__((always_inline)) static inline const String *
infoBackrestVersion(const Info * const this)65 infoBackrestVersion(const Info *const this)
66 {
67 return THIS_PUB(Info)->backrestVersion;
68 }
69
70 /***********************************************************************************************************************************
71 Functions
72 ***********************************************************************************************************************************/
73 // Save to file
74 void infoSave(Info *this, IoWrite *write, InfoSaveCallback *callbackFunction, void *callbackData);
75
76 // Check if the section should be saved
77 bool infoSaveSection(InfoSave *infoSaveData, const String *section, const String *sectionNext);
78
79 // Save a JSON formatted value and update checksum
80 void infoSaveValue(InfoSave *infoSaveData, const String *section, const String *key, const String *jsonValue);
81
82 /***********************************************************************************************************************************
83 Helper functions
84 ***********************************************************************************************************************************/
85 // Load info file(s) and throw error for each attempt if none are successful
86 void infoLoad(const String *error, InfoLoadCallback *callbackFunction, void *callbackData);
87
88 /***********************************************************************************************************************************
89 Macros for function logging
90 ***********************************************************************************************************************************/
91 #define FUNCTION_LOG_INFO_TYPE \
92 Info *
93 #define FUNCTION_LOG_INFO_FORMAT(value, buffer, bufferSize) \
94 objToLog(value, "Info", buffer, bufferSize)
95
96 #define FUNCTION_LOG_INFO_SAVE_TYPE \
97 InfoSave *
98 #define FUNCTION_LOG_INFO_SAVE_FORMAT(value, buffer, bufferSize) \
99 objToLog(value, "InfoSave", buffer, bufferSize)
100
101 #endif
102