1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <errno.h>
17 #include <fcntl.h>
18 #include <time.h>
19 #include <unistd.h>
20
21 #include "init_utils.h"
22 #include "param_persist.h"
23 #include "param_utils.h"
24
25 typedef struct {
26 void *context;
27 PersistParamGetPtr persistParamGet;
28 } PersistAdpContext;
29
LoadPersistParam(PersistParamGetPtr persistParamGet,void * context)30 static int LoadPersistParam(PersistParamGetPtr persistParamGet, void *context)
31 {
32 CheckAndCreateDir(PARAM_PERSIST_SAVE_PATH);
33 int updaterMode = InUpdaterMode();
34 char *tmpPath = (updaterMode == 0) ? PARAM_PERSIST_SAVE_TMP_PATH : "/param/tmp_persist_parameters";
35 FILE *fp = fopen(tmpPath, "r");
36 if (fp == NULL) {
37 fp = fopen((updaterMode == 0) ? PARAM_PERSIST_SAVE_PATH : "/param/persist_parameters", "r");
38 PARAM_LOGI("LoadPersistParam open file %s", PARAM_PERSIST_SAVE_PATH);
39 }
40 PARAM_CHECK(fp != NULL, return -1, "No valid persist parameter file %s", PARAM_PERSIST_SAVE_PATH);
41
42 char *buff = (char *)calloc(1, PARAM_BUFFER_SIZE);
43 SubStringInfo *info = calloc(1, sizeof(SubStringInfo) * (SUBSTR_INFO_VALUE + 1));
44 while (info != NULL && buff != NULL && fgets(buff, PARAM_BUFFER_SIZE, fp) != NULL) {
45 buff[PARAM_BUFFER_SIZE - 1] = '\0';
46 int subStrNumber = GetSubStringInfo(buff, strlen(buff), '=', info, SUBSTR_INFO_VALUE + 1);
47 if (subStrNumber <= SUBSTR_INFO_VALUE) {
48 continue;
49 }
50 int ret = persistParamGet(info[0].value, info[1].value, context);
51 PARAM_CHECK(ret == 0, continue, "Failed to set param %d %s", ret, buff);
52 }
53 if (info != NULL) {
54 free(info);
55 }
56 if (buff != NULL) {
57 free(buff);
58 }
59 (void)fclose(fp);
60 return 0;
61 }
62
SavePersistParam(const char * name,const char * value)63 static int SavePersistParam(const char *name, const char *value)
64 {
65 PARAM_LOGV("SavePersistParam %s=%s", name, value);
66 return 0;
67 }
68
BatchSavePersistParamBegin(PERSIST_SAVE_HANDLE * handle)69 static int BatchSavePersistParamBegin(PERSIST_SAVE_HANDLE *handle)
70 {
71 FILE *fp = fopen((InUpdaterMode() == 0) ? PARAM_PERSIST_SAVE_TMP_PATH : "/param/tmp_persist_parameters", "w");
72 PARAM_CHECK(fp != NULL, return -1, "Open file %s fail error %d", PARAM_PERSIST_SAVE_TMP_PATH, errno);
73 *handle = (PERSIST_SAVE_HANDLE)fp;
74 return 0;
75 }
76
BatchSavePersistParam(PERSIST_SAVE_HANDLE handle,const char * name,const char * value)77 static int BatchSavePersistParam(PERSIST_SAVE_HANDLE handle, const char *name, const char *value)
78 {
79 FILE *fp = (FILE *)handle;
80 int ret = fprintf(fp, "%s=%s\n", name, value);
81 PARAM_CHECK(ret > 0, return -1, "Failed to write param");
82 PARAM_LOGV("BatchSavePersistParam %s=%s", name, value);
83 return 0;
84 }
85
BatchSavePersistParamEnd(PERSIST_SAVE_HANDLE handle)86 static void BatchSavePersistParamEnd(PERSIST_SAVE_HANDLE handle)
87 {
88 int ret;
89 FILE *fp = (FILE *)handle;
90 (void)fclose(fp);
91 if (InUpdaterMode() == 0) {
92 unlink(PARAM_PERSIST_SAVE_PATH);
93 ret = rename(PARAM_PERSIST_SAVE_TMP_PATH, PARAM_PERSIST_SAVE_PATH);
94 } else {
95 unlink("/param/persist_parameters");
96 ret = rename("/param/tmp_persist_parameters", "/param/persist_parameters");
97 }
98 PARAM_CHECK(ret == 0, return, "BatchSavePersistParamEnd %s fail error %d", PARAM_PERSIST_SAVE_TMP_PATH, errno);
99 }
100
RegisterPersistParamOps(PersistParamOps * ops)101 int RegisterPersistParamOps(PersistParamOps *ops)
102 {
103 PARAM_CHECK(ops != NULL, return -1, "Invalid ops");
104 ops->save = SavePersistParam;
105 ops->load = LoadPersistParam;
106 ops->batchSaveBegin = BatchSavePersistParamBegin;
107 ops->batchSave = BatchSavePersistParam;
108 ops->batchSaveEnd = BatchSavePersistParamEnd;
109 return 0;
110 }