1 /*
2 * Copyright (c) 2024 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 <time.h>
18 #include <unistd.h>
19
20 #include "init_utils.h"
21 #include "param_manager.h"
22 #include "param_persist.h"
23 #include "param_utils.h"
24
25 // for linux, no mutex
26 static ParamMutex g_saveMutex = {};
27
LoadOnePersistParam_(const uint32_t * context,const char * name,const char * value)28 static int LoadOnePersistParam_(const uint32_t *context, const char *name, const char *value)
29 {
30 UNUSED(context);
31 PARAM_CHECK(name != NULL, return -1, "param is invalid");
32 PARAM_CHECK(value != NULL, return -1, "value is invalid");
33 if (strncmp(name, "persist", strlen("persist")) != 0) {
34 PARAM_LOGE("%s is not persist param, do not load", name);
35 return 0;
36 }
37 uint32_t dataIndex = 0;
38 unsigned int mode = 0;
39 int result = 0;
40 char persetValue[PARAM_VALUE_LEN_MAX] = {0};
41 uint32_t len = PARAM_VALUE_LEN_MAX;
42 int ret = SystemReadParam(name, persetValue, &len);
43 if (ret != 0) {
44 mode |= LOAD_PARAM_PERSIST;
45 return WriteParam(name, value, &dataIndex, mode);
46 }
47
48 if (strcmp(persetValue, value) != 0) {
49 PARAM_LOGI("%s value is different, preset value is:%s, persist value is:%s", name, persetValue, value);
50 mode |= LOAD_PARAM_PERSIST;
51 return WriteParam(name, value, &dataIndex, mode);
52 }
53 return 0;
54 }
55
LoadPersistParam_(const char * fileName,char * buffer,uint32_t buffSize)56 static void LoadPersistParam_(const char *fileName, char *buffer, uint32_t buffSize)
57 {
58 FILE *fp = fopen(fileName, "r");
59 PARAM_WARNING_CHECK(fp != NULL, return, "No valid persist parameter file %s", fileName);
60
61 uint32_t paramNum = 0;
62 while (fgets(buffer, buffSize, fp) != NULL) {
63 buffer[buffSize - 1] = '\0';
64 int ret = SplitParamString(buffer, NULL, 0, LoadOnePersistParam_, NULL);
65 PARAM_CHECK(ret == 0, continue, "Failed to set param %d %s", ret, buffer);
66 paramNum++;
67 }
68 (void)fclose(fp);
69 PARAM_LOGI("LoadPersistParam from file %s paramNum %d", fileName, paramNum);
70 }
71
LoadPersistParam(void)72 static int LoadPersistParam(void)
73 {
74 CheckAndCreateDir(PARAM_PERSIST_SAVE_PATH);
75 const uint32_t buffSize = PARAM_NAME_LEN_MAX + PARAM_CONST_VALUE_LEN_MAX + 10; // 10 max len
76 char *buffer = malloc(buffSize);
77 PARAM_CHECK(buffer != NULL, return -1, "Failed to alloc");
78
79 int updaterMode = InUpdaterMode();
80 char *tmpPath = (updaterMode == 0) ? PARAM_PERSIST_SAVE_PATH : "/param/persist_parameters";
81 LoadPersistParam_(tmpPath, buffer, buffSize);
82 tmpPath = (updaterMode == 0) ? PARAM_PERSIST_SAVE_TMP_PATH : "/param/tmp_persist_parameters";
83 LoadPersistParam_(tmpPath, buffer, buffSize);
84 free(buffer);
85 return 0;
86 }
87
SavePersistParam(const char * name,const char * value)88 static int SavePersistParam(const char *name, const char *value)
89 {
90 ParamMutexPend(&g_saveMutex);
91 char *path = (InUpdaterMode() == 0) ? PARAM_PERSIST_SAVE_PATH : "/param/persist_parameters";
92 FILE *fp = fopen(path, "a+");
93 int ret = -1;
94 if (fp != NULL) {
95 ret = fprintf(fp, "%s=%s\n", name, value);
96 (void)fclose(fp);
97 }
98 ParamMutexPost(&g_saveMutex);
99 if (ret <= 0) {
100 PARAM_LOGE("Failed to save persist param %s", name);
101 }
102 return ret;
103 }
104
BatchSavePersistParamBegin(PERSIST_SAVE_HANDLE * handle)105 static int BatchSavePersistParamBegin(PERSIST_SAVE_HANDLE *handle)
106 {
107 ParamMutexPend(&g_saveMutex);
108 char *path = (InUpdaterMode() == 0) ? PARAM_PERSIST_SAVE_TMP_PATH : "/param/tmp_persist_parameters";
109 unlink(path);
110 FILE *fp = fopen(path, "w");
111 if (fp == NULL) {
112 ParamMutexPost(&g_saveMutex);
113 PARAM_LOGE("Open file %s fail error %d", path, errno);
114 return -1;
115 }
116 *handle = (PERSIST_SAVE_HANDLE)fp;
117 return 0;
118 }
119
BatchSavePersistParam(PERSIST_SAVE_HANDLE handle,const char * name,const char * value)120 static int BatchSavePersistParam(PERSIST_SAVE_HANDLE handle, const char *name, const char *value)
121 {
122 FILE *fp = (FILE *)handle;
123 int ret = fprintf(fp, "%s=%s\n", name, value);
124 PARAM_LOGV("BatchSavePersistParam %s=%s", name, value);
125 return (ret > 0) ? 0 : -1;
126 }
127
BatchSavePersistParamEnd(PERSIST_SAVE_HANDLE handle)128 static void BatchSavePersistParamEnd(PERSIST_SAVE_HANDLE handle)
129 {
130 int ret;
131 FILE *fp = (FILE *)handle;
132 (void)fflush(fp);
133 (void)fsync(fileno(fp));
134 (void)fclose(fp);
135 if (InUpdaterMode() == 0) {
136 unlink(PARAM_PERSIST_SAVE_PATH);
137 ret = rename(PARAM_PERSIST_SAVE_TMP_PATH, PARAM_PERSIST_SAVE_PATH);
138 } else {
139 unlink("/param/persist_parameters");
140 ret = rename("/param/tmp_persist_parameters", "/param/persist_parameters");
141 }
142 ParamMutexPost(&g_saveMutex);
143 PARAM_CHECK(ret == 0, return, "BatchSavePersistParamEnd %s fail error %d", PARAM_PERSIST_SAVE_TMP_PATH, errno);
144 }
145
RegisterPersistParamOps(PersistParamOps * ops)146 int RegisterPersistParamOps(PersistParamOps *ops)
147 {
148 ParamMutexCreate(&g_saveMutex);
149 PARAM_CHECK(ops != NULL, return -1, "Invalid ops");
150 ops->save = SavePersistParam;
151 ops->load = LoadPersistParam;
152 ops->batchSaveBegin = BatchSavePersistParamBegin;
153 ops->batchSave = BatchSavePersistParam;
154 ops->batchSaveEnd = BatchSavePersistParamEnd;
155 return 0;
156 }