param_persistadp.c 3.9 KB
Newer Older
S
sun_fan 已提交
1
/*
Z
zhong_ning 已提交
2
 * Copyright (c) 2021 Huawei Device Co., Ltd.
S
sun_fan 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * 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 <errno.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>

X
xionglei6 已提交
21
#include "init_utils.h"
S
sun_fan 已提交
22 23 24 25 26 27 28 29 30 31 32
#include "param_persist.h"
#include "param_utils.h"

typedef struct {
    void *context;
    PersistParamGetPtr persistParamGet;
} PersistAdpContext;

static int LoadPersistParam(PersistParamGetPtr persistParamGet, void *context)
{
    CheckAndCreateDir(PARAM_PERSIST_SAVE_PATH);
X
xionglei6 已提交
33 34 35
    int updaterMode = InUpdaterMode();
    char *tmpPath = (updaterMode == 0) ? PARAM_PERSIST_SAVE_TMP_PATH : "/param/tmp_persist_parameters";
    FILE *fp = fopen(tmpPath, "r");
S
sun_fan 已提交
36
    if (fp == NULL) {
X
xionglei6 已提交
37
        fp = fopen((updaterMode == 0) ? PARAM_PERSIST_SAVE_PATH : "/param/persist_parameters", "r");
S
sun_fan 已提交
38 39 40 41
        PARAM_LOGI("LoadPersistParam open file %s", PARAM_PERSIST_SAVE_PATH);
    }
    PARAM_CHECK(fp != NULL, return -1, "No valid persist parameter file %s", PARAM_PERSIST_SAVE_PATH);

4
411148299@qq.com 已提交
42 43
    char *buff = (char *)calloc(1, PARAM_BUFFER_SIZE);
    SubStringInfo *info = calloc(1, sizeof(SubStringInfo) * (SUBSTR_INFO_VALUE + 1));
S
sun_fan 已提交
44
    while (info != NULL && buff != NULL && fgets(buff, PARAM_BUFFER_SIZE, fp) != NULL) {
4
411148299@qq.com 已提交
45
        buff[PARAM_BUFFER_SIZE - 1] = '\0';
S
sun_fan 已提交
46 47 48 49 50 51 52
        int subStrNumber = GetSubStringInfo(buff, strlen(buff), '=', info, SUBSTR_INFO_VALUE + 1);
        if (subStrNumber <= SUBSTR_INFO_VALUE) {
            continue;
        }
        int ret = persistParamGet(info[0].value, info[1].value, context);
        PARAM_CHECK(ret == 0, continue, "Failed to set param %d %s", ret, buff);
    }
4
411148299@qq.com 已提交
53 54 55 56 57 58
    if (info) {
        free(info);
    }
    if (buff) {
        free(buff);
    }
S
sun_fan 已提交
59 60 61 62
    (void)fclose(fp);
    return 0;
}

63 64 65 66 67 68
static int SavePersistParam(const char *name, const char *value)
{
    PARAM_LOGD("SavePersistParam %s=%s", name, value);
    return 0;
}

S
sun_fan 已提交
69 70
static int BatchSavePersistParamBegin(PERSIST_SAVE_HANDLE *handle)
{
X
xionglei6 已提交
71
    FILE *fp = fopen((InUpdaterMode() == 0) ? PARAM_PERSIST_SAVE_TMP_PATH : "/param/tmp_persist_parameters", "w");
4
411148299@qq.com 已提交
72
    PARAM_CHECK(fp != NULL, return -1, "Open file %s fail error %d", PARAM_PERSIST_SAVE_TMP_PATH, errno);
S
sun_fan 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    *handle = (PERSIST_SAVE_HANDLE)fp;
    return 0;
}

static int BatchSavePersistParam(PERSIST_SAVE_HANDLE handle, const char *name, const char *value)
{
    FILE *fp = (FILE *)handle;
    int ret = fprintf(fp, "%s=%s\n", name, value);
    PARAM_CHECK(ret > 0, return -1, "Failed to write param");
    PARAM_LOGD("BatchSavePersistParam %s=%s", name, value);
    return 0;
}

static void BatchSavePersistParamEnd(PERSIST_SAVE_HANDLE handle)
{
X
xionglei6 已提交
88
    int ret;
S
sun_fan 已提交
89 90
    FILE *fp = (FILE *)handle;
    (void)fclose(fp);
X
xionglei6 已提交
91 92 93 94 95 96 97
    if (InUpdaterMode() == 0) {
        unlink(PARAM_PERSIST_SAVE_PATH);
        ret = rename(PARAM_PERSIST_SAVE_TMP_PATH, PARAM_PERSIST_SAVE_PATH);
    } else {
        unlink("/param/persist_parameters");
        ret = rename("/param/tmp_persist_parameters", "/param/persist_parameters");
    }
4
411148299@qq.com 已提交
98
    PARAM_CHECK(ret == 0, return, "BatchSavePersistParamEnd %s fail error %d", PARAM_PERSIST_SAVE_TMP_PATH, errno);
S
sun_fan 已提交
99 100 101 102
}

int RegisterPersistParamOps(PersistParamOps *ops)
{
4
411148299@qq.com 已提交
103
    PARAM_CHECK(ops != NULL, return -1, "Invalid ops");
104
    ops->save = SavePersistParam;
S
sun_fan 已提交
105 106 107 108 109
    ops->load = LoadPersistParam;
    ops->batchSaveBegin = BatchSavePersistParamBegin;
    ops->batchSave = BatchSavePersistParam;
    ops->batchSaveEnd = BatchSavePersistParamEnd;
    return 0;
X
xionglei6 已提交
110
}