param_trie.c 16.3 KB
Newer Older
Z
zhong_ning 已提交
1
/*
Z
zhong_ning 已提交
2
 * Copyright (c) 2021 Huawei Device Co., Ltd.
Z
zhong_ning 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

Z
zhong_ning 已提交
16
#include "param_trie.h"
Z
zhong_ning 已提交
17 18 19 20

#include <errno.h>
#include <unistd.h>

M
Mupceet 已提交
21 22
#include "init_param.h"
#include "param_osadp.h"
S
sun_fan 已提交
23
#include "param_utils.h"
Z
zhong_ning 已提交
24

M
Mupceet 已提交
25
int GetRealFileName(WorkSpace *workSpace, char *buffer, uint32_t size)
Z
zhong_ning 已提交
26
{
M
Mupceet 已提交
27 28 29 30 31 32 33 34 35 36
    int ret = sprintf_s(buffer, size, "%s/%s", PARAM_STORAGE_PATH, workSpace->fileName);
    PARAM_CHECK(ret > 0, return -1, "Failed to copy file name %s", workSpace->fileName);
    buffer[ret] = '\0';
    CheckAndCreateDir(buffer);
    return 0;
}

static int InitWorkSpace_(WorkSpace *workSpace, uint32_t spaceSize, int readOnly)
{
    static uint32_t startIndex = 0;
S
sun_fan 已提交
37
    PARAM_CHECK(workSpace != NULL, return PARAM_CODE_INVALID_PARAM, "Invalid workSpace");
Z
zhong_ning 已提交
38
    PARAM_CHECK(workSpace->allocTrieNode != NULL,
S
sun_fan 已提交
39
        return PARAM_CODE_INVALID_PARAM, "Invalid allocTrieNode %s", workSpace->fileName);
Z
zhong_ning 已提交
40
    PARAM_CHECK(workSpace->compareTrieNode != NULL,
S
sun_fan 已提交
41
        return PARAM_CODE_INVALID_PARAM, "Invalid compareTrieNode %s", workSpace->fileName);
Z
zhong_ning 已提交
42

M
Mupceet 已提交
43 44 45 46 47 48
    char buffer[FILENAME_LEN_MAX] = {0};
    int ret = GetRealFileName(workSpace, buffer, sizeof(buffer));
    PARAM_CHECK(ret == 0, return -1, "Failed to get file name %s", workSpace->fileName);
    void *areaAddr = GetSharedMem(buffer, &workSpace->memHandle, spaceSize, readOnly);
    PARAM_CHECK(areaAddr != NULL, return PARAM_CODE_ERROR_MAP_FILE,
        "Failed to map memory error %d areaAddr %p spaceSize %d", errno, areaAddr, spaceSize);
Z
zhong_ning 已提交
49
    if (!readOnly) {
S
sun_fan 已提交
50 51 52 53
        workSpace->area = (ParamTrieHeader *)areaAddr;
        workSpace->area->trieNodeCount = 0;
        workSpace->area->paramNodeCount = 0;
        workSpace->area->securityNodeCount = 0;
M
Mupceet 已提交
54 55
        workSpace->area->startIndex = startIndex;
        startIndex += spaceSize;
S
sun_fan 已提交
56 57 58
        workSpace->area->dataSize = spaceSize - sizeof(ParamTrieHeader);
        workSpace->area->currOffset = 0;
        uint32_t offset = workSpace->allocTrieNode(workSpace, "#", 1);
Z
zhong_ning 已提交
59 60
        workSpace->area->firstNode = offset;
    } else {
S
sun_fan 已提交
61
        workSpace->area = (ParamTrieHeader *)areaAddr;
Z
zhong_ning 已提交
62
    }
X
xionglei6 已提交
63
    PARAM_LOGV("InitWorkSpace success, readOnly %d currOffset %u firstNode %u dataSize %u",
Z
zhong_ning 已提交
64
        readOnly, workSpace->area->currOffset, workSpace->area->firstNode, workSpace->area->dataSize);
Z
zhong_ning 已提交
65 66 67
    return 0;
}

S
sun_fan 已提交
68
static uint32_t AllocateParamTrieNode(WorkSpace *workSpace, const char *key, uint32_t keyLen)
Z
zhong_ning 已提交
69
{
S
sun_fan 已提交
70 71
    uint32_t len = keyLen + sizeof(ParamTrieNode) + 1;
    len = PARAM_ALIGN(len);
Z
zhong_ning 已提交
72
    PARAM_CHECK((workSpace->area->currOffset + len) < workSpace->area->dataSize, return 0,
Z
zhong_ning 已提交
73
        "Failed to allocate currOffset %d, dataSize %d", workSpace->area->currOffset, workSpace->area->dataSize);
M
Mupceet 已提交
74
    ParamTrieNode *node = (ParamTrieNode *)(workSpace->area->data + workSpace->area->currOffset);
S
sun_fan 已提交
75
    node->length = keyLen;
Z
zhong_ning 已提交
76
    int ret = memcpy_s(node->key, keyLen, key, keyLen);
S
sun_fan 已提交
77
    PARAM_CHECK(ret == EOK, return 0, "Failed to copy key");
Z
zhong_ning 已提交
78 79 80 81 82 83
    node->key[keyLen] = '\0';
    node->left = 0;
    node->right = 0;
    node->child = 0;
    node->dataIndex = 0;
    node->labelIndex = 0;
S
sun_fan 已提交
84
    uint32_t offset = workSpace->area->currOffset;
Z
zhong_ning 已提交
85
    workSpace->area->currOffset += len;
S
sun_fan 已提交
86
    workSpace->area->trieNodeCount++;
Z
zhong_ning 已提交
87 88 89
    return offset;
}

4
411148299@qq.com 已提交
90
static int CompareParamTrieNode(const ParamTrieNode *node, const char *key, uint32_t keyLen)
Z
zhong_ning 已提交
91
{
S
sun_fan 已提交
92 93 94 95
    if (node->length > keyLen) {
        return -1;
    } else if (node->length < keyLen) {
        return 1;
Z
zhong_ning 已提交
96
    }
S
sun_fan 已提交
97
    return strncmp(node->key, key, keyLen);
Z
zhong_ning 已提交
98 99
}

M
Mupceet 已提交
100
int InitWorkSpace(WorkSpace *workSpace, int onlyRead, uint32_t spaceSize)
Z
zhong_ning 已提交
101
{
S
sun_fan 已提交
102
    PARAM_CHECK(workSpace != NULL, return PARAM_CODE_INVALID_NAME, "Invalid workSpace");
M
Mupceet 已提交
103
    if (PARAM_TEST_FLAG(workSpace->flags, WORKSPACE_FLAGS_INIT)) {
S
sun_fan 已提交
104 105 106 107
        return 0;
    }
    workSpace->compareTrieNode = CompareParamTrieNode;
    workSpace->allocTrieNode = AllocateParamTrieNode;
M
Mupceet 已提交
108
    int ret = InitWorkSpace_(workSpace, spaceSize, onlyRead);
S
sun_fan 已提交
109
    PARAM_CHECK(ret == 0, return ret, "Failed to init workspace  %s", workSpace->fileName);
M
Mupceet 已提交
110 111
    PARAMSPACE_AREA_INIT_LOCK(workSpace);
    PARAM_SET_FLAG(workSpace->flags, WORKSPACE_FLAGS_INIT);
S
sun_fan 已提交
112
    return ret;
Z
zhong_ning 已提交
113 114
}

S
sun_fan 已提交
115
void CloseWorkSpace(WorkSpace *workSpace)
Z
zhong_ning 已提交
116
{
M
Mupceet 已提交
117 118 119 120 121 122 123 124 125 126 127
    PARAM_CHECK(workSpace != NULL, return, "The workspace is null");
    if (!PARAM_TEST_FLAG(workSpace->flags, WORKSPACE_FLAGS_INIT)) {
        free(workSpace);
        return;
    }
    ListRemove(&workSpace->node);
    PARAM_CHECK(workSpace->area != NULL, return, "The workspace area is null");
#ifdef WORKSPACE_AREA_NEED_MUTEX
    ParamRWMutexDelete(&workSpace->rwlock);
#endif
    FreeSharedMem(&workSpace->memHandle, workSpace->area, workSpace->area->dataSize);
S
sun_fan 已提交
128
    workSpace->area = NULL;
M
Mupceet 已提交
129
    free(workSpace);
Z
zhong_ning 已提交
130 131
}

4
411148299@qq.com 已提交
132
static ParamTrieNode *GetTrieRoot(const WorkSpace *workSpace)
Z
zhong_ning 已提交
133
{
4
411148299@qq.com 已提交
134
    PARAM_CHECK(workSpace != NULL && workSpace->area != NULL, return NULL, "The workspace is null");
S
sun_fan 已提交
135
    return (ParamTrieNode *)(workSpace->area->data + workSpace->area->firstNode);
Z
zhong_ning 已提交
136 137
}

S
sun_fan 已提交
138
static void GetNextKey(const char **remainingKey, char **subKey, uint32_t *subKeyLen)
Z
zhong_ning 已提交
139
{
S
sun_fan 已提交
140 141 142 143 144
    *subKey = strchr(*remainingKey, '.');
    if (*subKey != NULL) {
        *subKeyLen = *subKey - *remainingKey;
    } else {
        *subKeyLen = strlen(*remainingKey);
Z
zhong_ning 已提交
145 146 147
    }
}

S
sun_fan 已提交
148
static ParamTrieNode *AddToSubTrie(WorkSpace *workSpace, ParamTrieNode *current, const char *key, uint32_t keyLen)
Z
zhong_ning 已提交
149
{
4
411148299@qq.com 已提交
150
    if (current == NULL || workSpace == NULL || key == NULL) {
S
sun_fan 已提交
151
        return NULL;
Z
zhong_ning 已提交
152
    }
S
sun_fan 已提交
153 154 155 156 157 158 159 160 161
    ParamTrieNode *subTrie = NULL;
    int ret = workSpace->compareTrieNode(current, key, keyLen);
    if (ret == 0) {
        return current;
    }
    if (ret < 0) {
        subTrie = GetTrieNode(workSpace, current->left);
        if (subTrie == NULL) {
            uint32_t offset = workSpace->allocTrieNode(workSpace, key, keyLen);
M
Mupceet 已提交
162 163
            PARAM_CHECK(offset != 0, return NULL,
                "Failed to allocate key '%s' in space '%s'", key, workSpace->fileName);
S
sun_fan 已提交
164 165 166
            SaveIndex(&current->left, offset);
            return GetTrieNode(workSpace, current->left);
        }
Z
zhong_ning 已提交
167
    } else {
S
sun_fan 已提交
168 169 170
        subTrie = GetTrieNode(workSpace, current->right);
        if (subTrie == NULL) {
            uint32_t offset = workSpace->allocTrieNode(workSpace, key, keyLen);
M
Mupceet 已提交
171 172
            PARAM_CHECK(offset != 0, return NULL,
                "Failed to allocate key '%s' in space '%s'", key, workSpace->fileName);
S
sun_fan 已提交
173 174 175
            SaveIndex(&current->right, offset);
            return GetTrieNode(workSpace, current->right);
        }
Z
zhong_ning 已提交
176
    }
S
sun_fan 已提交
177
    return AddToSubTrie(workSpace, subTrie, key, keyLen);
Z
zhong_ning 已提交
178 179
}

S
sun_fan 已提交
180
ParamTrieNode *AddTrieNode(WorkSpace *workSpace, const char *key, uint32_t keyLen)
Z
zhong_ning 已提交
181
{
S
sun_fan 已提交
182
    PARAM_CHECK(key != NULL && keyLen > 0, return NULL, "Invalid param ");
4
411148299@qq.com 已提交
183
    PARAM_CHECK(workSpace != NULL && workSpace->area != NULL, return 0, "Invalid workSpace %s", key);
Z
zhong_ning 已提交
184 185
    PARAM_CHECK(workSpace->allocTrieNode != NULL, return NULL, "Invalid param %s", key);
    PARAM_CHECK(workSpace->compareTrieNode != NULL, return NULL, "Invalid param %s", key);
Z
zhong_ning 已提交
186
    const char *remainingKey = key;
S
sun_fan 已提交
187
    ParamTrieNode *current = GetTrieRoot(workSpace);
Z
zhong_ning 已提交
188
    PARAM_CHECK(current != NULL, return NULL, "Invalid current param %s", key);
Z
zhong_ning 已提交
189
    while (1) {
S
sun_fan 已提交
190
        uint32_t subKeyLen = 0;
Z
zhong_ning 已提交
191
        char *subKey = NULL;
Z
zhong_ning 已提交
192
        GetNextKey(&remainingKey, &subKey, &subKeyLen);
Z
zhong_ning 已提交
193 194 195
        if (!subKeyLen) {
            return NULL;
        }
M
Mupceet 已提交
196
        if (current->child != 0) {  // 如果child存在,则检查是否匹配
S
sun_fan 已提交
197 198
            ParamTrieNode *next = GetTrieNode(workSpace, current->child);
            current = AddToSubTrie(workSpace, next, remainingKey, subKeyLen);
Z
zhong_ning 已提交
199
        } else {
S
sun_fan 已提交
200
            uint32_t dataOffset = workSpace->allocTrieNode(workSpace, remainingKey, subKeyLen);
M
Mupceet 已提交
201 202
            PARAM_CHECK(dataOffset != 0, return NULL,
                "Failed to allocate key '%s' in space '%s'", key, workSpace->fileName);
S
sun_fan 已提交
203 204
            SaveIndex(&current->child, dataOffset);
            current = (ParamTrieNode *)GetTrieNode(workSpace, current->child);
Z
zhong_ning 已提交
205 206 207 208 209 210 211 212 213 214 215 216
        }
        if (current == NULL) {
            return NULL;
        }
        if (subKey == NULL || strcmp(subKey, ".") == 0) {
            break;
        }
        remainingKey = subKey + 1;
    }
    return current;
}

4
411148299@qq.com 已提交
217
static ParamTrieNode *FindSubTrie(const WorkSpace *workSpace,
S
sun_fan 已提交
218
    ParamTrieNode *current, const char *key, uint32_t keyLen, uint32_t *matchLabel)
Z
zhong_ning 已提交
219
{
S
sun_fan 已提交
220 221
    if (current == NULL) {
        return NULL;
Z
zhong_ning 已提交
222
    }
S
sun_fan 已提交
223 224 225
    ParamTrieNode *subTrie = NULL;
    int ret = workSpace->compareTrieNode(current, key, keyLen);
    if (ret == 0) {
M
Mupceet 已提交
226
        if (matchLabel != NULL && current->labelIndex != 0) {
S
sun_fan 已提交
227
            *matchLabel = current->labelIndex;
Z
zhong_ning 已提交
228
        }
S
sun_fan 已提交
229 230 231 232 233 234
        return current;
    }
    if (ret < 0) {
        subTrie = (ParamTrieNode *)GetTrieNode(workSpace, current->left);
        if (subTrie == NULL) {
            return NULL;
Z
zhong_ning 已提交
235
        }
S
sun_fan 已提交
236 237 238 239
    } else {
        subTrie = (ParamTrieNode *)GetTrieNode(workSpace, current->right);
        if (subTrie == NULL) {
            return NULL;
Z
zhong_ning 已提交
240 241
        }
    }
S
sun_fan 已提交
242
    return FindSubTrie(workSpace, subTrie, key, keyLen, matchLabel);
Z
zhong_ning 已提交
243 244
}

M
Mupceet 已提交
245
ParamTrieNode *FindTrieNode_(const WorkSpace *workSpace, const char *key, uint32_t keyLen, uint32_t *matchLabel)
Z
zhong_ning 已提交
246
{
S
sun_fan 已提交
247
    PARAM_CHECK(key != NULL && keyLen > 0, return NULL, "Invalid key ");
4
411148299@qq.com 已提交
248
    PARAM_CHECK(workSpace != NULL && workSpace->area != NULL, return 0, "Invalid workSpace %s", key);
S
sun_fan 已提交
249 250
    PARAM_CHECK(workSpace->allocTrieNode != NULL, return NULL, "Invalid alloc function %s", key);
    PARAM_CHECK(workSpace->compareTrieNode != NULL, return NULL, "Invalid compare function %s", key);
Z
zhong_ning 已提交
251
    const char *remainingKey = key;
S
sun_fan 已提交
252
    ParamTrieNode *current = GetTrieRoot(workSpace);
Z
zhong_ning 已提交
253
    PARAM_CHECK(current != NULL, return NULL, "Invalid current param %s", key);
S
sun_fan 已提交
254 255 256 257 258 259 260
    if (matchLabel != NULL) {
        *matchLabel = current->labelIndex;
    }
    int ret = workSpace->compareTrieNode(current, key, keyLen);
    if (ret == 0) {
        return current;
    }
Z
zhong_ning 已提交
261
    while (1) {
S
sun_fan 已提交
262
        uint32_t subKeyLen = 0;
Z
zhong_ning 已提交
263
        char *subKey = NULL;
Z
zhong_ning 已提交
264
        GetNextKey(&remainingKey, &subKey, &subKeyLen);
Z
zhong_ning 已提交
265
        if (!subKeyLen) {
S
sun_fan 已提交
266
            return NULL;
Z
zhong_ning 已提交
267
        }
S
sun_fan 已提交
268 269 270
        if (current->child != 0) {
            ParamTrieNode *next = GetTrieNode(workSpace, current->child);
            current = FindSubTrie(workSpace, next, remainingKey, subKeyLen, matchLabel);
Z
zhong_ning 已提交
271
        } else {
S
sun_fan 已提交
272
            current = FindSubTrie(workSpace, current, remainingKey, subKeyLen, matchLabel);
Z
zhong_ning 已提交
273 274
        }
        if (current == NULL) {
S
sun_fan 已提交
275 276 277
            return NULL;
        } else if (matchLabel != NULL && current->labelIndex != 0) {
            *matchLabel = current->labelIndex;
Z
zhong_ning 已提交
278 279 280 281 282 283
        }
        if (subKey == NULL || strcmp(subKey, ".") == 0) {
            break;
        }
        remainingKey = subKey + 1;
    }
S
sun_fan 已提交
284
    return current;
Z
zhong_ning 已提交
285 286
}

4
411148299@qq.com 已提交
287
static int TraversalSubTrieNode(const WorkSpace *workSpace,
M
Mupceet 已提交
288
    const ParamTrieNode *current, TraversalTrieNodePtr walkFunc, const void *cookie)
Z
zhong_ning 已提交
289
{
S
sun_fan 已提交
290 291
    if (current == NULL) {
        return 0;
Z
zhong_ning 已提交
292
    }
S
sun_fan 已提交
293 294 295 296 297
    walkFunc(workSpace, (ParamTrieNode *)current, cookie);
    TraversalSubTrieNode(workSpace, GetTrieNode(workSpace, current->child), walkFunc, cookie);
    TraversalSubTrieNode(workSpace, GetTrieNode(workSpace, current->left), walkFunc, cookie);
    TraversalSubTrieNode(workSpace, GetTrieNode(workSpace, current->right), walkFunc, cookie);
    return 0;
Z
zhong_ning 已提交
298 299
}

4
411148299@qq.com 已提交
300
int TraversalTrieNode(const WorkSpace *workSpace,
M
Mupceet 已提交
301
    const ParamTrieNode *root, TraversalTrieNodePtr walkFunc, const void *cookie)
Z
zhong_ning 已提交
302
{
Z
zhong_ning 已提交
303
    PARAM_CHECK(walkFunc != NULL, return PARAM_CODE_INVALID_PARAM, "Invalid param");
4
411148299@qq.com 已提交
304 305
    PARAM_CHECK(workSpace != NULL && workSpace->area != NULL, return 0, "Invalid workSpace");
    ParamTrieNode *current = (ParamTrieNode *)root;
S
sun_fan 已提交
306 307 308
    if (root == NULL) {
        current = GetTrieRoot(workSpace);
    }
Z
zhong_ning 已提交
309 310 311
    if (current == NULL) {
        return 0;
    }
S
sun_fan 已提交
312 313 314 315 316
    walkFunc(workSpace, (ParamTrieNode *)current, cookie);
    TraversalSubTrieNode(workSpace, GetTrieNode(workSpace, current->child), walkFunc, cookie);
    if (root == NULL) {
        TraversalSubTrieNode(workSpace, GetTrieNode(workSpace, current->left), walkFunc, cookie);
        TraversalSubTrieNode(workSpace, GetTrieNode(workSpace, current->right), walkFunc, cookie);
Z
zhong_ning 已提交
317 318 319 320
    }
    return 0;
}

S
sun_fan 已提交
321
uint32_t AddParamSecruityNode(WorkSpace *workSpace, const ParamAuditData *auditData)
Z
zhong_ning 已提交
322
{
4
411148299@qq.com 已提交
323
    PARAM_CHECK(workSpace != NULL && workSpace->area != NULL, return 0, "Invalid param");
S
sun_fan 已提交
324
    PARAM_CHECK(auditData != NULL && auditData->name != NULL, return 0, "Invalid auditData");
M
Mupceet 已提交
325 326
#ifdef PARAM_SUPPORT_SELINUX
    const uint32_t labelLen = strlen(auditData->label);
S
sun_fan 已提交
327
    uint32_t realLen = sizeof(ParamSecruityNode) + PARAM_ALIGN(labelLen + 1);
M
Mupceet 已提交
328 329 330
#else
    uint32_t realLen = sizeof(ParamSecruityNode);
#endif
S
sun_fan 已提交
331 332 333 334 335 336 337 338
    PARAM_CHECK((workSpace->area->currOffset + realLen) < workSpace->area->dataSize, return 0,
        "Failed to allocate currOffset %u, dataSize %u datalen %u",
        workSpace->area->currOffset, workSpace->area->dataSize, realLen);
    ParamSecruityNode *node = (ParamSecruityNode *)(workSpace->area->data + workSpace->area->currOffset);
    node->uid = auditData->dacData.uid;
    node->gid = auditData->dacData.gid;
    node->mode = auditData->dacData.mode;
    node->length = 0;
M
Mupceet 已提交
339
#ifdef PARAM_SUPPORT_SELINUX
S
sun_fan 已提交
340 341 342 343 344
    if (labelLen != 0) {
        int ret = memcpy_s(node->data, labelLen, auditData->label, labelLen);
        PARAM_CHECK(ret == EOK, return 0, "Failed to copy key");
        node->data[labelLen] = '\0';
        node->length = labelLen;
Z
zhong_ning 已提交
345
    }
M
Mupceet 已提交
346
#endif
S
sun_fan 已提交
347 348 349 350
    uint32_t offset = workSpace->area->currOffset;
    workSpace->area->currOffset += realLen;
    workSpace->area->securityNodeCount++;
    return offset;
Z
zhong_ning 已提交
351 352
}

S
sun_fan 已提交
353
uint32_t AddParamNode(WorkSpace *workSpace, const char *key, uint32_t keyLen, const char *value, uint32_t valueLen)
Z
zhong_ning 已提交
354
{
4
411148299@qq.com 已提交
355
    PARAM_CHECK(workSpace != NULL && workSpace->area != NULL, return 0, "Invalid param");
Z
zhong_ning 已提交
356
    PARAM_CHECK(key != NULL && value != NULL, return 0, "Invalid param");
S
sun_fan 已提交
357 358

    uint32_t realLen = sizeof(ParamNode) + 1 + 1;
M
Mupceet 已提交
359 360
    // for const parameter, alloc memory on demand
    if ((valueLen > PARAM_VALUE_LEN_MAX) || IS_READY_ONLY(key)) {
Z
zhong_ning 已提交
361
        realLen += keyLen + valueLen;
Z
zhong_ning 已提交
362
    } else {
Z
zhong_ning 已提交
363
        realLen += keyLen + PARAM_VALUE_LEN_MAX;
Z
zhong_ning 已提交
364
    }
S
sun_fan 已提交
365
    realLen = PARAM_ALIGN(realLen);
Z
zhong_ning 已提交
366
    PARAM_CHECK((workSpace->area->currOffset + realLen) < workSpace->area->dataSize, return 0,
S
sun_fan 已提交
367 368 369 370 371
        "Failed to allocate currOffset %u, dataSize %u datalen %u",
        workSpace->area->currOffset, workSpace->area->dataSize, realLen);

    ParamNode *node = (ParamNode *)(workSpace->area->data + workSpace->area->currOffset);
    atomic_init(&node->commitId, 0);
Z
zhong_ning 已提交
372

S
sun_fan 已提交
373 374 375 376 377
    node->keyLength = keyLen;
    node->valueLength = valueLen;
    int ret = sprintf_s(node->data, realLen - 1, "%s=%s", key, value);
    PARAM_CHECK(ret > EOK, return 0, "Failed to sprint key and value");
    uint32_t offset = workSpace->area->currOffset;
Z
zhong_ning 已提交
378
    workSpace->area->currOffset += realLen;
S
sun_fan 已提交
379
    workSpace->area->paramNodeCount++;
Z
zhong_ning 已提交
380 381 382
    return offset;
}

4
411148299@qq.com 已提交
383
ParamTrieNode *GetTrieNode(const WorkSpace *workSpace, uint32_t offset)
Z
zhong_ning 已提交
384
{
4
411148299@qq.com 已提交
385
    PARAM_CHECK(workSpace != NULL && workSpace->area != NULL, return NULL, "Invalid param");
S
sun_fan 已提交
386 387
    if (offset == 0 || offset > workSpace->area->dataSize) {
        return NULL;
Z
zhong_ning 已提交
388
    }
S
sun_fan 已提交
389
    return (ParamTrieNode *)(workSpace->area->data + offset);
Z
zhong_ning 已提交
390 391
}

S
sun_fan 已提交
392
void SaveIndex(uint32_t *index, uint32_t offset)
Z
zhong_ning 已提交
393
{
4
411148299@qq.com 已提交
394
    PARAM_CHECK(index != NULL, return, "Invalid index");
S
sun_fan 已提交
395
    *index = offset;
Z
zhong_ning 已提交
396
}
M
Mupceet 已提交
397 398 399 400 401 402 403 404 405 406

ParamTrieNode *FindTrieNode(WorkSpace *workSpace, const char *key, uint32_t keyLen, uint32_t *matchLabel)
{
    PARAM_CHECK(workSpace != NULL, return NULL, "Invalid workSpace");
    ParamTrieNode *node = NULL;
    PARAMSPACE_AREA_RD_LOCK(workSpace);
    node = FindTrieNode_(workSpace, key, keyLen, matchLabel);
    PARAMSPACE_AREA_RW_UNLOCK(workSpace);
    return node;
}