Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/***********************************************************************************************************************************
Support Command
***********************************************************************************************************************************/
#include <build.h>
 
#include <unistd.h>
 
#include "command/check/report.h"
#include "common/debug.h"
#include "common/log.h"
#include "common/type/json.h"
#include "config/config.h"
#include "config/parse.h"
 
/***********************************************************************************************************************************
Render config
***********************************************************************************************************************************/
// Helper to render an individual config value
static void
checkReportConfigVal(JsonWrite *const json, const String *const optionName, const StringList *valueList, const bool env)
{
    FUNCTION_TEST_BEGIN();
        FUNCTION_TEST_PARAM(JSON_WRITE, json);
        FUNCTION_TEST_PARAM(STRING, optionName);
        FUNCTION_TEST_PARAM(STRING_LIST, valueList);
        FUNCTION_TEST_PARAM(BOOL, env);
    FUNCTION_TEST_END();
 
    ASSERT(json != NULL);
    ASSERT(optionName != NULL);
    ASSERT(valueList != NULL);
 
    MEM_CONTEXT_TEMP_BEGIN()
    {
        jsonWriteObjectBegin(json);
 
        const CfgParseOptionResult option = cfgParseOptionP(optionName);
 
        // Render value
        if (option.found)
        {
            jsonWriteKeyStrId(json, STRID5("val", 0x30360));
 
            // Multiple values
            if (option.multi)
            {
                ASSERT(strLstSize(valueList) >= 1);
                ASSERT(!cfgParseOptionSecure(option.id));
 
                // Split environment values
                if (env)
                {
                    ASSERT(strLstSize(valueList) == 1);
                    valueList = strLstNewSplitZ(strLstGet(valueList, 0), ":");
                }
 
                // Render each value
                jsonWriteArrayBegin(json);
 
                for (unsigned int valueIdx = 0; valueIdx < strLstSize(valueList); valueIdx++)
                    jsonWriteStr(json, strLstGet(valueList, valueIdx));
 
                jsonWriteArrayEnd(json);
            }
            // Single value
            else
            {
                ASSERT(strLstSize(valueList) == 1);
 
                if (cfgParseOptionSecure(option.id))
                    jsonWriteStr(json, strNewFmt("<%s>", strZ(optionName)));
                else
                    jsonWriteStr(json, strLstGet(valueList, 0));
            }
        }
 
        // Warn on invalid options or option modifiers
        if (!option.found || option.negate || option.reset)
        {
            jsonWriteKeyStrId(json, STRID5("warn", 0x748370));
 
            // Warn if the option not found
            if (!option.found)
            {
                jsonWriteZ(json, "invalid option");
            }
            // Warn if negate option
            else if (option.negate)
            {
                jsonWriteZ(json, "invalid negate option");
            }
            // Warn if reset option
            else
            {
                ASSERT(option.reset);
 
                jsonWriteZ(json, "invalid reset option");
            }
        }
 
        jsonWriteObjectEnd(json);
    }
    MEM_CONTEXT_TEMP_END();
 
    FUNCTION_TEST_RETURN_VOID();
}
 
// Helper to render the environment
static void
checkReportConfigEnv(JsonWrite *const json)
{
    FUNCTION_TEST_BEGIN();
        FUNCTION_TEST_PARAM(JSON_WRITE, json);
    FUNCTION_TEST_END();
 
    ASSERT(json != NULL);
 
    MEM_CONTEXT_TEMP_BEGIN()
    {
        jsonWriteKeyStrId(json, STRID5("env", 0x59c50));
        jsonWriteObjectBegin(json);
 
        // Loop through all environment variables and look for our env vars by matching the prefix
        unsigned int environIdx = 0;
        KeyValue *const keyValue = kvNew();
 
        while (environ[environIdx] != NULL)
        {
            const char *const environKeyValue = environ[environIdx];
            environIdx++;
 
            if (strstr(environKeyValue, PGBACKREST_ENV) == environKeyValue)
            {
                // Find the first = char
                const char *const equalPtr = strchr(environKeyValue, '=');
                ASSERT(equalPtr != NULL);
 
                // Get key and value
                const String *const key = strNewZN(environKeyValue, (size_t)(equalPtr - environKeyValue));
                const String *const value = STR(equalPtr + 1);
 
                kvPut(keyValue, VARSTR(key), VARSTR(value));
            }
        }
 
        // Render environment variables
        const StringList *const keyList = strLstSort(strLstNewVarLst(kvKeyList(keyValue)), sortOrderAsc);
 
        for (unsigned int keyIdx = 0; keyIdx < strLstSize(keyList); keyIdx++)
        {
            const String *const key = strLstGet(keyList, keyIdx);
            StringList *const valueList = strLstNew();
            strLstAdd(valueList, varStr(kvGet(keyValue, VARSTR(key))));
 
            jsonWriteKey(json, key);
            checkReportConfigVal(
                json, strReplaceChr(strLower(strNewZ(strZ(key) + PGBACKREST_ENV_SIZE)), '_', '-'), valueList, true);
        }
 
        jsonWriteObjectEnd(json);
    }
    MEM_CONTEXT_TEMP_END();
 
    FUNCTION_TEST_RETURN_VOID();
}
 
// Helper to render the config file
static void
checkReportConfigFile(JsonWrite *const json)
{
    FUNCTION_TEST_BEGIN();
        FUNCTION_TEST_PARAM(JSON_WRITE, json);
    FUNCTION_TEST_END();
 
    ASSERT(json != NULL);
 
    MEM_CONTEXT_TEMP_BEGIN()
    {
        const Ini *const ini = cfgParseIni();
 
        jsonWriteKeyStrId(json, STRID5("file", 0x2b1260));
 
        // If no config
        if (ini == NULL)
        {
            jsonWriteNull(json);
        }
        // Else config exists
        else
        {
            // Iterate config sections
            const StringList *const sectionList = strLstSort(iniSectionList(ini), sortOrderAsc);
 
            jsonWriteObjectBegin(json);
 
            for (unsigned int sectionIdx = 0; sectionIdx < strLstSize(sectionList); sectionIdx++)
            {
                const String *const section = strLstGet(sectionList, sectionIdx);
 
                jsonWriteKey(json, section);
                jsonWriteObjectBegin(json);
 
                // Iterate config keys
                const StringList *const keyList = strLstSort(iniSectionKeyList(ini, section), sortOrderAsc);
 
                for (unsigned int keyIdx = 0; keyIdx < strLstSize(keyList); keyIdx++)
                {
                    const String *const key = strLstGet(keyList, keyIdx);
 
                    // Render value
                    StringList *valueList;
 
                    if (iniSectionKeyIsList(ini, section, key))
                        valueList = iniGetList(ini, section, key);
                    else
                    {
                        valueList = strLstNew();
                        strLstAdd(valueList, iniGet(ini, section, key));
                    }
 
                    jsonWriteKey(json, key);
                    checkReportConfigVal(json, key, valueList, false);
                }
 
                jsonWriteObjectEnd(json);
            }
 
            jsonWriteObjectEnd(json);
        }
    }
    MEM_CONTEXT_TEMP_END();
 
    FUNCTION_TEST_RETURN_VOID();
}
 
static void
checkReportConfig(JsonWrite *const json)
{
    FUNCTION_TEST_BEGIN();
        FUNCTION_TEST_PARAM(JSON_WRITE, json);
    FUNCTION_TEST_END();
 
    ASSERT(json != NULL);
 
    jsonWriteKeyStrId(json, STRID5("cfg", 0x1cc30));
    jsonWriteObjectBegin(json);
 
    checkReportConfigEnv(json);
    checkReportConfigFile(json);
 
    jsonWriteObjectEnd(json);
 
    FUNCTION_TEST_RETURN_VOID();
}
 
/**********************************************************************************************************************************/
FN_EXTERN String *
checkReport(void)
{
    FUNCTION_TEST_VOID();
 
    String *const result = strNew();
 
    MEM_CONTEXT_TEMP_BEGIN()
    {
        JsonWrite *const json = jsonWriteNewP(.json = result);
 
        // Render report
        jsonWriteObjectBegin(json);
        checkReportConfig(json);
        jsonWriteObjectEnd(json);
    }
    MEM_CONTEXT_TEMP_END();
 
    FUNCTION_TEST_RETURN(STRING, result);
}