init_import.c 2.5 KB
Newer Older
Z
zhong_ning 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright (c) 2021 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.
 */

#include "init_import.h"
#include <stdio.h>
Z
zhong_ning 已提交
18
#include <unistd.h>
Z
zhong_ning 已提交
19
#include "cJSON.h"
Z
zhong_ning 已提交
20
#include "init_cmds.h"
Z
zhong_ning 已提交
21
#include "init_log.h"
Z
zhong_ning 已提交
22
#include "init_read_cfg.h"
S
sun_fan 已提交
23
#include "securec.h"
Z
zhong_ning 已提交
24

S
sun_fan 已提交
25
#ifndef OHOS_LITE
S
sun_fan 已提交
26
static int ExtractCfgFile(char **cfgFile, const char *content)
S
sun_fan 已提交
27
{
Z
zhong_ning 已提交
28
    if ((!cfgFile) || (!content)) {
S
sun_fan 已提交
29 30
        return -1;
    }
Z
zhong_ning 已提交
31
    size_t cfgFileLen = strlen(content) + MAX_PARAM_VALUE_LEN + 1;
S
sun_fan 已提交
32 33 34
    if (cfgFileLen <= 0) {
        return -1;
    }
S
sun_fan 已提交
35
    if ((*cfgFile = malloc(cfgFileLen)) == NULL) {
Z
zhong_ning 已提交
36
        INIT_LOGW("Failed to allocate memory to import cfg file. err = %d", errno);
S
sun_fan 已提交
37 38
        return -1;
    }
Z
zhong_ning 已提交
39
    return GetParamValue(content, *cfgFile, cfgFileLen);
S
sun_fan 已提交
40 41 42
}
#endif

S
sun_fan 已提交
43
void ParseAllImports(const cJSON *root)
Z
zhong_ning 已提交
44
{
S
sun_fan 已提交
45
    cJSON *importAttr = cJSON_GetObjectItemCaseSensitive(root, "import");
S
sun_fan 已提交
46
    char *cfgFile = NULL;
Z
zhong_ning 已提交
47 48 49 50 51 52 53 54
    if (!cJSON_IsArray(importAttr)) {
        return;
    }
    int importAttrSize = cJSON_GetArraySize(importAttr);

    for (int i = 0; i < importAttrSize; i++) {
        cJSON *importItem = cJSON_GetArrayItem(importAttr, i);
        if (!cJSON_IsString(importItem)) {
Z
zhong_ning 已提交
55
            INIT_LOGE("Invalid type of import item. should be string");
Z
zhong_ning 已提交
56 57
            return;
        }
S
sun_fan 已提交
58 59
        char *importContent = cJSON_GetStringValue(importItem);
        if (importContent == NULL) {
Z
zhong_ning 已提交
60
            INIT_LOGE("cannot get import config file");
Z
zhong_ning 已提交
61 62
            return;
        }
S
sun_fan 已提交
63 64 65
// Only OHOS L2 support parameter.
#ifndef OHOS_LITE
        if (ExtractCfgFile(&cfgFile, importContent) < 0) {
Z
zhong_ning 已提交
66
            INIT_LOGW("Failed to import from %s", importContent);
S
sun_fan 已提交
67 68 69 70 71 72 73 74 75
            if (cfgFile != NULL) {
                free(cfgFile);
                cfgFile = NULL;
            }
            continue;
        }
#else
        cfgFile = importContent;
#endif
Z
zhong_ning 已提交
76
        INIT_LOGI("Import %s...", cfgFile);
S
sun_fan 已提交
77 78 79 80
        ParseInitCfg(cfgFile);
        // Do not forget to free memory.
        free(cfgFile);
        cfgFile = NULL;
Z
zhong_ning 已提交
81
    }
S
sun_fan 已提交
82
    INIT_LOGD("parse import file done");
Z
zhong_ning 已提交
83
    return;
Z
zhong_ning 已提交
84
}