Исходный код СУБД 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
/***********************************************************************************************************************************
Exec Configuration
***********************************************************************************************************************************/
#include <build.h>
 
#include <string.h>
 
#include "common/debug.h"
#include "common/log.h"
#include "config/config.intern.h"
#include "config/exec.h"
#include "config/parse.h"
 
/**********************************************************************************************************************************/
FN_EXTERN StringList *
cfgExecParam(
    const ConfigCommand commandId, const ConfigCommandRole commandRoleId, const KeyValue *const optionReplace, const bool local,
    const bool quote)
{
    FUNCTION_LOG_BEGIN(logLevelTrace);
        FUNCTION_LOG_PARAM(ENUM, commandId);
        FUNCTION_LOG_PARAM(ENUM, commandRoleId);
        FUNCTION_LOG_PARAM(KEY_VALUE, optionReplace);
        FUNCTION_LOG_PARAM(BOOL, local);                            // Will the new process be running on the same host?
        FUNCTION_LOG_PARAM(BOOL, quote);                            // Do parameters with spaces need to be quoted?
    FUNCTION_LOG_END();
 
    StringList *const result = strLstNew();
 
    MEM_CONTEXT_TEMP_BEGIN()
    {
        // Loop though options and add the ones that apply to the specified command
        for (ConfigOption optionId = 0; optionId < CFG_OPTION_TOTAL; optionId++)
        {
            // Skip the option if it is not valid for the original/specified command or if is secure. Also skip repo1-cipher-type
            // because there's no point of passing it if the other process doesn't have access to repo1-cipher-pass. There is
            // probably a better way to do this last part...
            if (!cfgParseOptionValid(commandId, commandRoleId, optionId) || cfgParseOptionSecure(optionId) ||
                optionId == cfgOptRepoCipherType)
            {
                continue;
            }
 
            // Loop through option indexes. The group is read from the parse rules rather than the config because an option that
            // is valid for the specified command may not be valid for the command being run, and in that case the config has no
            // group data for it.
            const unsigned int optionIdxTotal =
                cfgParseOptionGroup(optionId) ? cfgOptionGroupIdxTotal(cfgParseOptionGroupId(optionId)) : 1;
 
            for (unsigned int optionIdx = 0; optionIdx < optionIdxTotal; optionIdx++)
            {
                // First check for a replacement
                const Variant *const key = VARSTRZ(cfgOptionIdxName(optionId, optionIdx));
                const Variant *value = NULL;
                bool exists = false;
 
                // If an option is requested to be replaced (usually because remote processes do not have access to the config)
                // then if the option exists, get the new value for replacement
                if (optionReplace != NULL)
                {
                    exists = kvKeyExists(optionReplace, key);
 
                    if (exists)
                        value = kvGet(optionReplace, key);
                }
 
                // If the key exists but its value is NULL then skip this option
                if (exists && value == NULL)
                    continue;
 
                // If no replacement then see if this option is not default
                if (value == NULL && cfgOptionValid(optionId))
                {
                    if (cfgOptionIdxNegate(optionId, optionIdx))
                        value = BOOL_FALSE_VAR;
                    else if (cfgOptionIdxSource(optionId, optionIdx) != cfgSourceDefault)
                        value = cfgOptionIdxVar(optionId, optionIdx);
                }
 
                // If the option was reset
                if (value == NULL && cfgOptionValid(optionId) && cfgOptionIdxReset(optionId, optionIdx))
                {
                    strLstAddFmt(result, "--reset-%s", cfgOptionIdxName(optionId, optionIdx));
                }
                // Else format the value if found, even if the option is not valid for the command
                else if (value != NULL && (!local || exists || cfgOptionIdxSource(optionId, optionIdx) == cfgSourceParam))
                {
                    if (varType(value) == varTypeBool)
                        strLstAddFmt(result, "--%s%s", varBool(value) ? "" : "no-", cfgOptionIdxName(optionId, optionIdx));
                    else
                    {
                        StringList *valueList = NULL;
 
                        if (varType(value) == varTypeKeyValue)
                        {
                            valueList = strLstNew();
 
                            const KeyValue *const optionKv = varKv(value);
                            const VariantList *const keyList = kvKeyList(optionKv);
 
                            for (unsigned int keyIdx = 0; keyIdx < varLstSize(keyList); keyIdx++)
                            {
                                strLstAddFmt(
                                    valueList, "%s=%s", strZ(varStr(varLstGet(keyList, keyIdx))),
                                    strZ(varStrForce(kvGet(optionKv, varLstGet(keyList, keyIdx)))));
                            }
                        }
                        else if (varType(value) == varTypeVariantList)
                        {
                            valueList = strLstNewVarLst(varVarLst(value));
                        }
                        // Else only one value
                        else
                        {
                            valueList = strLstNew();
 
                            strLstAdd(
                                valueList,
                                exists ? cfgOptionDisplayVar(value, cfgParseOptionType(optionId)) :
                                    cfgOptionIdxDisplay(optionId, optionIdx));
                        }
 
                        // Output options and values
                        for (unsigned int valueListIdx = 0; valueListIdx < strLstSize(valueList); valueListIdx++)
                        {
                            const String *value = strLstGet(valueList, valueListIdx);
 
                            if (quote && strchr(strZ(value), ' ') != NULL)
                                value = strNewFmt("\"%s\"", strZ(value));
 
                            strLstAddFmt(result, "--%s=%s", cfgOptionIdxName(optionId, optionIdx), strZ(value));
                        }
                    }
                }
            }
        }
 
        // Add the command
        strLstAdd(result, cfgParseCommandRoleName(commandId, commandRoleId));
    }
    MEM_CONTEXT_TEMP_END();
 
    FUNCTION_LOG_RETURN(STRING_LIST, result);
}