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 <ctype.h>
17 #include <errno.h>
18 #include <fcntl.h>
19 #include <time.h>
20 #include <unistd.h>
21
22 #include "init_utils.h"
23 #include "param_manager.h"
24 #include "param_persist.h"
25 #include "param_utils.h"
26 #include "securec.h"
27 #include "utils_file.h"
28
29 // for linux, no mutex
30 static ParamMutex g_saveMutex = {0};
31 #ifdef PARAM_SUPPORT_POSIX
32 #define MODE_READ O_RDONLY
33 #define MODE_APPEND (O_RDWR | O_CREAT | O_APPEND)
34 #define MODE_CREATE (O_RDWR | O_CREAT | O_TRUNC)
35 #else
36 #define MODE_READ O_RDONLY_FS
37 #define MODE_APPEND (O_RDWR_FS | O_CREAT_FS | O_APPEND_FS)
38 #define MODE_CREATE (O_RDWR_FS | O_CREAT_FS | O_TRUNC_FS)
39 #endif
40
ParamFileOpen(const char * path,int oflag,int mode)41 static int ParamFileOpen(const char* path, int oflag, int mode)
42 {
43 #ifdef PARAM_SUPPORT_POSIX
44 return open(path, oflag, mode);
45 #else
46 return UtilsFileOpen(path, oflag, mode);
47 #endif
48 }
49
ParamFileClose(int fd)50 static int ParamFileClose(int fd)
51 {
52 #ifdef PARAM_SUPPORT_POSIX
53 return close(fd);
54 #else
55 return UtilsFileClose(fd);
56 #endif
57 }
58
ParamFileRead(int fd,char * buf,unsigned int len)59 static int ParamFileRead(int fd, char* buf, unsigned int len)
60 {
61 #ifdef PARAM_SUPPORT_POSIX
62 return read(fd, buf, len);
63 #else
64 return UtilsFileRead(fd, buf, len);
65 #endif
66 }
67
ParamFileWrite(int fd,const char * buf,unsigned int len)68 static int ParamFileWrite(int fd, const char* buf, unsigned int len)
69 {
70 #ifdef PARAM_SUPPORT_POSIX
71 return write(fd, buf, len);
72 #else
73 return UtilsFileWrite(fd, buf, len);
74 #endif
75 }
76
ParamFileDelete(const char * path)77 static int ParamFileDelete(const char* path)
78 {
79 #ifdef PARAM_SUPPORT_POSIX
80 return unlink(path);
81 #else
82 return UtilsFileDelete(path);
83 #endif
84 }
85
ParamFileStat(const char * path,unsigned int * fileSize)86 static int ParamFileStat(const char* path, unsigned int* fileSize)
87 {
88 #ifdef PARAM_SUPPORT_POSIX
89 int fd = open(path, O_RDONLY);
90 if (fd < 0) {
91 return -1;
92 }
93 *fileSize = lseek(fd, 0, SEEK_END);
94 lseek(fd, 0, SEEK_SET);
95 return 0;
96 #else
97 return UtilsFileStat(path, fileSize);
98 #endif
99 }
100
ParamFileSync(int ft)101 static void ParamFileSync(int ft)
102 {
103 #ifdef PARAM_SUPPORT_POSIX
104 fsync(ft);
105 #else
106 (void)ft;
107 #endif
108 }
109
LoadOnePersistParam_(const uint32_t * context,const char * name,const char * value)110 static int LoadOnePersistParam_(const uint32_t *context, const char *name, const char *value)
111 {
112 (void)context;
113 uint32_t dataIndex = 0;
114 int ret = WriteParam(name, value, &dataIndex, 0);
115 PARAM_CHECK(ret == 0, return ret, "Failed to write param %d name:%s %s", ret, name, value);
116 return 0;
117 }
118
LoadPersistParam(void)119 static int LoadPersistParam(void)
120 {
121 const char *path = PARAM_PERSIST_SAVE_TMP_PATH;
122 CheckAndCreateDir(path);
123 char *buffer = NULL;
124 int fd = -1;
125 uint32_t paramNum = 0;
126 do {
127 fd = ParamFileOpen(path, MODE_READ, 0);
128 if (fd < 0) {
129 path = PARAM_PERSIST_SAVE_PATH;
130 fd = ParamFileOpen(path, MODE_READ, 0);
131 PARAM_LOGI("LoadPersistParam open file %s", path);
132 }
133 PARAM_CHECK(fd >= 0, break, "No valid persist parameter file %s", path);
134 // read file
135 uint32_t fileSize = 0;
136 int ret = ParamFileStat(path, &fileSize);
137 PARAM_CHECK(ret == 0, break, "Failed to get file state %s", path);
138 buffer = malloc(fileSize);
139 PARAM_CHECK(buffer != NULL, break, "Failed to get file");
140 ret = ParamFileRead(fd, buffer, fileSize);
141 PARAM_CHECK(ret >= 0, break, "Failed to read file %s", path);
142
143 uint32_t currLen = 0;
144 char *tmp = buffer;
145 while (currLen < fileSize) {
146 if (buffer[currLen] == '\n') { // split line
147 buffer[currLen] = '\0';
148 ret = SplitParamString(tmp, NULL, 0, LoadOnePersistParam_, NULL);
149 PARAM_CHECK(ret == 0, continue, "Failed to set param %d %s", ret, buffer);
150 paramNum++;
151 if (currLen + 1 >= fileSize) {
152 break;
153 }
154 tmp = buffer + currLen + 1;
155 }
156 currLen++;
157 }
158 } while (0);
159 if (fd > 0) {
160 ParamFileClose(fd);
161 }
162 if (buffer != NULL) {
163 free(buffer);
164 }
165 PARAM_LOGI("LoadPersistParam paramNum %d", paramNum);
166 return 0;
167 }
168
PersistWrite(int fd,const char * name,const char * value)169 static int PersistWrite(int fd, const char *name, const char *value)
170 {
171 int ret = ParamFileWrite(fd, name, strlen(name));
172 if (ret <= 0) {
173 PARAM_LOGE("Failed to save persist param %s", name);
174 }
175 ret = ParamFileWrite(fd, "=", strlen("="));
176 if (ret <= 0) {
177 PARAM_LOGE("Failed to save persist param %s", name);
178 }
179 ret = ParamFileWrite(fd, value, strlen(value));
180 if (ret <= 0) {
181 PARAM_LOGE("Failed to save persist param %s", name);
182 }
183 ret = ParamFileWrite(fd, "\n", strlen("\n"));
184 if (ret <= 0) {
185 PARAM_LOGE("Failed to save persist param %s", name);
186 }
187 return 0;
188 }
189
SavePersistParam(const char * name,const char * value)190 static int SavePersistParam(const char *name, const char *value)
191 {
192 ParamMutexPend(&g_saveMutex);
193 int ret = -1;
194 int fd = ParamFileOpen(PARAM_PERSIST_SAVE_PATH, MODE_APPEND, 0);
195 if (fd > 0) {
196 ret = PersistWrite(fd, name, value);
197 ParamFileSync(fd);
198 ParamFileClose(fd);
199 }
200 if (ret != 0) {
201 PARAM_LOGE("SavePersistParam %s errno %d", name, errno);
202 }
203 ParamMutexPost(&g_saveMutex);
204 return ret;
205 }
206
BatchSavePersistParamBegin(PERSIST_SAVE_HANDLE * handle)207 static int BatchSavePersistParamBegin(PERSIST_SAVE_HANDLE *handle)
208 {
209 ParamMutexPend(&g_saveMutex);
210 int fd = ParamFileOpen(PARAM_PERSIST_SAVE_PATH, MODE_CREATE, 0);
211 if (fd < 0) {
212 ParamMutexPost(&g_saveMutex);
213 PARAM_LOGE("Open file %s fail error %d", PARAM_PERSIST_SAVE_PATH, errno);
214 return -1;
215 }
216 *handle = (PERSIST_SAVE_HANDLE)fd;
217 return 0;
218 }
219
BatchSavePersistParam(PERSIST_SAVE_HANDLE handle,const char * name,const char * value)220 static int BatchSavePersistParam(PERSIST_SAVE_HANDLE handle, const char *name, const char *value)
221 {
222 int fd = (int)handle;
223 int ret = PersistWrite(fd, name, value);
224 PARAM_CHECK(ret == 0, return -1, "Failed to write param %s", name);
225 PARAM_LOGV("BatchSavePersistParam %s=%s", name, value);
226 return 0;
227 }
228
BatchSavePersistParamEnd(PERSIST_SAVE_HANDLE handle)229 static void BatchSavePersistParamEnd(PERSIST_SAVE_HANDLE handle)
230 {
231 int ret;
232 int fd = (int)handle;
233 ParamFileSync(fd);
234 ret = ParamFileClose(fd);
235 ParamMutexPost(&g_saveMutex);
236 PARAM_CHECK(ret == 0, return, "BatchSavePersistParamEnd fail error %d", errno);
237 }
238
RegisterPersistParamOps(PersistParamOps * ops)239 int RegisterPersistParamOps(PersistParamOps *ops)
240 {
241 ParamMutexCreate(&g_saveMutex);
242 PARAM_CHECK(ops != NULL, return -1, "Invalid ops");
243 ops->save = SavePersistParam;
244 ops->load = LoadPersistParam;
245 ops->batchSaveBegin = BatchSavePersistParamBegin;
246 ops->batchSave = BatchSavePersistParam;
247 ops->batchSaveEnd = BatchSavePersistParamEnd;
248 return 0;
249 }
250