param_persist.c 6.1 KB
Newer Older
Z
zhong_ning 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * 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 已提交
15
#include "sys_param.h"
Z
zhong_ning 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28

#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

Z
zhong_ning 已提交
29 30
#include "param_manager.h"
#include "param_trie.h"
Z
zhong_ning 已提交
31 32 33 34 35 36 37 38 39 40

#define LABEL "Manager"
#define MAX_BUFF 256

typedef struct {
    WorkSpace *workSpace;
    WorkSpace *persistWorkSpace;
    char *buffer;
} PersistContext;

Z
zhong_ning 已提交
41
static ParamPersistWorkSpace g_persistWorkSpace = {ATOMIC_VAR_INIT(0), };
Z
zhong_ning 已提交
42

Z
zhong_ning 已提交
43
static int ProcessParamTraversal(WorkSpace *workSpace, TrieNode *node, void *cookie)
Z
zhong_ning 已提交
44
{
Z
zhong_ning 已提交
45
    PARAM_CHECK(workSpace != 0 && node != NULL && cookie != NULL, return -1, "Invalid param");
Z
zhong_ning 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    TrieDataNode *current = (TrieDataNode *)node;
    if (current == NULL || current->dataIndex == 0) {
        return 0;
    }
    DataEntry *entry = (DataEntry *)GetTrieNode(workSpace, &current->dataIndex);
    if (entry == NULL) {
        return -1;
    }
    PersistContext *persistContext = (PersistContext *)cookie;
    int ret = GetDataName(entry, persistContext->buffer, MAX_BUFF);
    if (strncmp(persistContext->buffer, "persist.", strlen("persist.")) != 0) {
        return 0;
    }
    ret |= GetDataValue(entry, persistContext->buffer + MAX_BUFF, MAX_BUFF);
    if (ret == 0) { // 只支持新建
Z
zhong_ning 已提交
61 62 63
        //PARAM_LOGI("Insert new persist param from normal param %s %s",
        //    persistContext->buffer, persistContext->buffer + MAX_BUFF);
        ret = AddParam(persistContext->persistWorkSpace, persistContext->buffer, persistContext->buffer + MAX_BUFF);
Z
zhong_ning 已提交
64
    }
Z
zhong_ning 已提交
65
    PARAM_CHECK(ret == 0, return ret, "Failed to add persist param");
Z
zhong_ning 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    return ret;
}

static int ProcessPersistPropertTraversal(WorkSpace *workSpace, TrieNode *node, void *cookie)
{
    TrieDataNode *current = (TrieDataNode *)node;
    if (current == NULL || current->dataIndex == 0) {
        return 0;
    }
    DataEntry *entry = (DataEntry *)GetTrieNode(workSpace, &current->dataIndex);
    if (entry == NULL) {
        return -1;
    }
    PersistContext *persistContext = (PersistContext *)cookie;
    int ret = GetDataName(entry, persistContext->buffer, MAX_BUFF);
    ret |= GetDataValue(entry, persistContext->buffer + MAX_BUFF, MAX_BUFF);
    if (ret == 0) {
Z
zhong_ning 已提交
83 84 85
        //PARAM_LOGI("update normal param %s %s from persist param %u",
        //    persistContext->buffer, persistContext->buffer + MAX_BUFF, current->dataIndex);
        ret = WriteParam(persistContext->workSpace, persistContext->buffer, persistContext->buffer + MAX_BUFF);
Z
zhong_ning 已提交
86
    }
Z
zhong_ning 已提交
87
    PARAM_CHECK(ret == 0, return ret, "Failed to add persist param");
Z
zhong_ning 已提交
88 89 90
    return ret;
}

Z
zhong_ning 已提交
91
int InitPersistParamWorkSpace(const char *context)
Z
zhong_ning 已提交
92 93 94 95 96 97 98
{
    u_int32_t flags = atomic_load_explicit(&g_persistWorkSpace.flags, memory_order_relaxed);
    if ((flags & WORKSPACE_FLAGS_INIT) == WORKSPACE_FLAGS_INIT) {
        return 0;
    }
    g_persistWorkSpace.persistWorkSpace.compareTrieNode = CompareTrieDataNode;
    g_persistWorkSpace.persistWorkSpace.allocTrieNode = AllocateTrieDataNode;
Z
zhong_ning 已提交
99 100
    int ret = InitPersistWorkSpace(PARAM_PERSIST_PATH, &g_persistWorkSpace.persistWorkSpace);
    PARAM_CHECK(ret == 0, return ret, "Failed to init persist param");
Z
zhong_ning 已提交
101 102 103 104
    atomic_store_explicit(&g_persistWorkSpace.flags, WORKSPACE_FLAGS_INIT, memory_order_release);
    return ret;
}

Z
zhong_ning 已提交
105
int RefreshPersistParams(ParamWorkSpace *workSpace, const char *context)
Z
zhong_ning 已提交
106
{
Z
zhong_ning 已提交
107 108
    int ret = InitPersistParamWorkSpace(context);
    PARAM_CHECK(ret == 0, return ret, "Failed to init persist param");
Z
zhong_ning 已提交
109 110
    u_int32_t flags = atomic_load_explicit(&g_persistWorkSpace.flags, memory_order_relaxed);
    if ((flags & WORKSPACE_FLAGS_LOADED) == WORKSPACE_FLAGS_LOADED) {
Z
zhong_ning 已提交
111
        PARAM_LOGE("RefreshPersistParams has been loaded");
Z
zhong_ning 已提交
112 113 114 115 116
        return 0;
    }

    // 申请临时的缓存,用于数据读取
    char *buffer = (char *)malloc(MAX_BUFF + MAX_BUFF);
Z
zhong_ning 已提交
117
    PARAM_CHECK(buffer != NULL, return -1, "Failed to malloc memory for param");
Z
zhong_ning 已提交
118
    PersistContext persistContext = {
Z
zhong_ning 已提交
119
        &workSpace->paramSpace, &g_persistWorkSpace.persistWorkSpace, buffer
Z
zhong_ning 已提交
120 121
    };

Z
zhong_ning 已提交
122 123 124
    // 遍历当前的参数,并把persist的写入
    ret = TraversalTrieDataNode(&workSpace->paramSpace,
        (TrieDataNode *)workSpace->paramSpace.rootNode, ProcessParamTraversal, &persistContext);
Z
zhong_ning 已提交
125

Z
zhong_ning 已提交
126
    // 修改默认参数值
Z
zhong_ning 已提交
127 128 129 130 131 132 133 134
    ret = TraversalTrieDataNode(&g_persistWorkSpace.persistWorkSpace,
        (TrieDataNode *)g_persistWorkSpace.persistWorkSpace.rootNode, ProcessPersistPropertTraversal, &persistContext);

    atomic_store_explicit(&g_persistWorkSpace.flags, flags | WORKSPACE_FLAGS_LOADED, memory_order_release);
    free(buffer);
    return ret;
}

Z
zhong_ning 已提交
135
void ClosePersistParamWorkSpace()
Z
zhong_ning 已提交
136 137 138 139 140
{
    CloseWorkSpace(&g_persistWorkSpace.persistWorkSpace);
    atomic_store_explicit(&g_persistWorkSpace.flags, 0, memory_order_release);
}

Z
zhong_ning 已提交
141
int WritePersistParam(const char *name, const char *value)
Z
zhong_ning 已提交
142
{
Z
zhong_ning 已提交
143 144
    PARAM_CHECK(value != NULL && name != NULL, return PARAM_CODE_INVALID_PARAM, "Invalid param");
    if (strncmp(name, "persist.", strlen("persist.")) != 0) {
Z
zhong_ning 已提交
145 146
        return 0;
    }
Z
zhong_ning 已提交
147 148 149 150
    int ret = InitPersistParamWorkSpace("");
    PARAM_CHECK(ret == 0, return ret, "Failed to init persist param");
    u_int32_t flags = atomic_load_explicit(&g_persistWorkSpace.flags, memory_order_relaxed);
    if ((flags & WORKSPACE_FLAGS_LOADED) != WORKSPACE_FLAGS_LOADED) {
Z
zhong_ning 已提交
151 152
        return 0;
    }
Z
zhong_ning 已提交
153
    return WriteParam(&g_persistWorkSpace.persistWorkSpace, name, value);
Z
zhong_ning 已提交
154
}