Исходный код СУБД 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
/***********************************************************************************************************************************
Repository List Command
***********************************************************************************************************************************/
#include <build.h>
 
#include <unistd.h>
 
#include "command/repo/common.h"
#include "command/repo/ls.h"
#include "common/debug.h"
#include "common/io/fdWrite.h"
#include "common/log.h"
#include "common/memContext.h"
#include "common/regExp.h"
#include "common/type/json.h"
#include "common/type/string.h"
#include "config/config.h"
#include "storage/helper.h"
 
/***********************************************************************************************************************************
Render storage list
***********************************************************************************************************************************/
static void
storageListRenderInfo(const StorageInfo *const info, IoWrite *const write, const bool json)
{
    FUNCTION_TEST_BEGIN();
        FUNCTION_TEST_PARAM(STORAGE_INFO, info);
        FUNCTION_TEST_PARAM(IO_WRITE, write);
        FUNCTION_TEST_PARAM(BOOL, json);
    FUNCTION_TEST_END();
 
    FUNCTION_AUDIT_HELPER();
 
    ASSERT(info != NULL);
    ASSERT(write != NULL);
 
    // Render in json
    if (json)
    {
        ioWriteStr(write, jsonFromVar(VARSTR(info->name)));
        ioWrite(write, BUFSTRDEF(":{\"type\":\""));
 
        switch (info->type)
        {
            case storageTypeFile:
                ioWrite(write, BUFSTRDEF("file\""));
                break;
 
            case storageTypeLink:
                ioWrite(write, BUFSTRDEF("link\""));
                break;
 
            case storageTypePath:
                ioWrite(write, BUFSTRDEF("path\""));
                break;
 
            case storageTypeSpecial:
                ioWrite(write, BUFSTRDEF("special\""));
                break;
        }
 
        if (info->type == storageTypeFile)
        {
            ioWriteStr(write, strNewFmt(",\"size\":%" PRIu64, info->size));
            ioWriteStr(write, strNewFmt(",\"time\":%" PRId64, (int64_t)info->timeModified));
 
            if (info->versionId != NULL)
                ioWriteStr(write, strNewFmt(",\"version\":%s", strZ(jsonFromVar(VARSTR(info->versionId)))));
        }
 
        if (info->type == storageTypeLink)
            ioWriteStr(write, strNewFmt(",\"destination\":%s", strZ(jsonFromVar(VARSTR(info->linkDestination)))));
 
        ioWrite(write, BRACER_BUF);
    }
    // Render in text
    else
    {
        ioWrite(write, BUFSTR(info->name));
        ioWrite(write, LF_BUF);
    }
 
    FUNCTION_TEST_RETURN_VOID();
}
 
static void
storageListRender(IoWrite *const write)
{
    FUNCTION_LOG_BEGIN(logLevelDebug);
        FUNCTION_LOG_PARAM(IO_WRITE, write);
    FUNCTION_LOG_END();
 
    FUNCTION_AUDIT_HELPER();
 
    // Get path
    const String *path = NULL;
 
    // Get and validate if path is valid for repo
    if (strLstSize(cfgCommandParam()) == 1)
        path = repoPathIsValid(strLstGet(cfgCommandParam(), 0));
    else if (strLstSize(cfgCommandParam()) > 1)
        THROW(ParamInvalidError, "only one path may be specified");
 
    // Get options
    const bool json = cfgOptionSeq(cfgOptOutput) == CFGOPTVAL_OUTPUT_JSON;
    const String *const expression = cfgOptionStrNull(cfgOptFilter);
    RegExp *const regExp = expression == NULL ? NULL : regExpNew(expression);
 
    ioWriteOpen(write);
 
    if (json)
        ioWrite(write, BRACEL_BUF);
 
    // Check if this is a file
    StorageInfo info = storageInfoP(storageRepo(), path, .ignoreMissing = true);
 
    if (info.exists && info.type == storageTypeFile)
    {
        if (regExp == NULL || regExpMatch(regExp, storagePathP(storageRepo(), path)))
        {
            info.name = DOT_STR;
            storageListRenderInfo(&info, write, json);
        }
    }
    // Else try to list the path
    else
    {
        // The path will always be reported as existing so we don't get different results from storage that does not support paths
        bool first = true;
 
        if (json && (regExp == NULL || regExpMatch(regExp, DOT_STR)))
        {
            storageListRenderInfo(&(StorageInfo){.type = storageTypePath, .name = DOT_STR}, write, json);
            first = false;
        }
 
        // List content of the path
        StorageIterator *const storageItr = storageNewItrP(
            storageRepo(), path, .sortOrder = cfgOptionSeq(cfgOptSort), .expression = expression,
            .recurse = cfgOptionBool(cfgOptRecurse));
 
        while (storageItrMore(storageItr))
        {
            const StorageInfo info = storageItrNext(storageItr);
 
            // Add separator character
            if (!first && json)
                ioWrite(write, COMMA_BUF);
            else
                first = false;
 
            storageListRenderInfo(&info, write, json);
        }
    }
 
    if (json)
    {
        ioWrite(write, BRACER_BUF);
        ioWrite(write, LF_BUF);
    }
 
    ioWriteClose(write);
 
    FUNCTION_LOG_RETURN_VOID();
}
 
/**********************************************************************************************************************************/
FN_EXTERN void
cmdStorageList(void)
{
    FUNCTION_LOG_VOID(logLevelDebug);
 
    MEM_CONTEXT_TEMP_BEGIN()
    {
        TRY_BEGIN()
        {
            storageListRender(ioFdWriteNew(STRDEF("stdout"), STDOUT_FILENO, cfgOptionUInt64(cfgOptIoTimeout)));
        }
        // Ignore write errors because it's possible (even likely) that this output is being piped to something like head which will
        // exit when it gets what it needs and leave us writing to a broken pipe. It would be better to just ignore the broken pipe
        // error but currently we don't store system error codes.
        CATCH(FileWriteError)
        {
        }
        TRY_END();
    }
    MEM_CONTEXT_TEMP_END();
 
    FUNCTION_LOG_RETURN_VOID();
}