• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     close(fd);
96     return 0;
97 #else
98     return UtilsFileStat(path, fileSize);
99 #endif
100 }
101 
ParamFileSync(int ft)102 static void ParamFileSync(int ft)
103 {
104 #ifdef PARAM_SUPPORT_POSIX
105     fsync(ft);
106 #else
107     (void)ft;
108 #endif
109 }
110 
LoadOnePersistParam_(const uint32_t * context,const char * name,const char * value)111 static int LoadOnePersistParam_(const uint32_t *context, const char *name, const char *value)
112 {
113     UNUSED(context);
114     PARAM_CHECK(name != NULL, return -1, "param is invalid");
115     PARAM_CHECK(value != NULL, return -1, "value is invalid");
116     if (strncmp(name, "persist", strlen("persist")) != 0) {
117         PARAM_LOGE("%s is not persist param, do not load", name);
118         return 0;
119     }
120     uint32_t dataIndex = 0;
121     unsigned int mode = 0;
122     int result = 0;
123     char persetValue[PARAM_VALUE_LEN_MAX] = {0};
124     uint32_t len = PARAM_VALUE_LEN_MAX;
125     int ret = SystemReadParam(name, persetValue, &len);
126     if (ret != 0) {
127         mode |= LOAD_PARAM_PERSIST;
128         return WriteParam(name, value, &dataIndex, mode);
129     }
130 
131     if (strcmp(persetValue, value) != 0) {
132         PARAM_LOGI("%s value is different, preset value is:%s, persist value is:%s", name, persetValue, value);
133         mode |= LOAD_PARAM_PERSIST;
134         return WriteParam(name, value, &dataIndex, mode);
135     }
136     return 0;
137 }
138 
LoadPersistParam(void)139 static int LoadPersistParam(void)
140 {
141     const char *path = PARAM_PERSIST_SAVE_TMP_PATH;
142     CheckAndCreateDir(path);
143     char *buffer = NULL;
144     int fd = -1;
145     uint32_t paramNum = 0;
146     do {
147         fd = ParamFileOpen(path, MODE_READ, 0);
148         if (fd < 0) {
149             path = PARAM_PERSIST_SAVE_PATH;
150             fd = ParamFileOpen(path, MODE_READ, 0);
151             PARAM_LOGI("LoadPersistParam open file %s", path);
152         }
153         PARAM_CHECK(fd >= 0, break, "No valid persist parameter file %s", path);
154         // read file
155         uint32_t fileSize = 0;
156         int ret = ParamFileStat(path, &fileSize);
157         PARAM_CHECK(ret == 0, break, "Failed to get file state %s", path);
158         buffer = calloc(fileSize, sizeof(char));
159         PARAM_CHECK(buffer != NULL, break, "Failed to get file");
160         ret = ParamFileRead(fd, buffer, fileSize);
161         PARAM_CHECK(ret >= 0, break, "Failed to read file %s", path);
162 
163         uint32_t currLen = 0;
164         char *tmp = buffer;
165         while (currLen < fileSize) {
166             if (buffer[currLen] == '\n') { // split line
167                 buffer[currLen] = '\0';
168                 ret = SplitParamString(tmp, NULL, 0, LoadOnePersistParam_, NULL);
169                 PARAM_CHECK(ret == 0, continue, "Failed to set param %d %s", ret, buffer);
170                 paramNum++;
171                 if (currLen + 1 >= fileSize) {
172                     break;
173                 }
174                 tmp = buffer + currLen + 1;
175             }
176             currLen++;
177         }
178     } while (0);
179     if (fd > 0) {
180         ParamFileClose(fd);
181     }
182     if (buffer != NULL) {
183         free(buffer);
184     }
185     PARAM_LOGI("LoadPersistParam paramNum %d", paramNum);
186     return 0;
187 }
188 
PersistWrite(int fd,const char * name,const char * value)189 static int PersistWrite(int fd, const char *name, const char *value)
190 {
191     int ret = ParamFileWrite(fd, name, strlen(name));
192     if (ret <= 0) {
193         PARAM_LOGE("Failed to save persist param %s", name);
194     }
195     ret = ParamFileWrite(fd, "=", strlen("="));
196     if (ret <= 0) {
197         PARAM_LOGE("Failed to save persist param %s", name);
198     }
199     ret = ParamFileWrite(fd, value, strlen(value));
200     if (ret <= 0) {
201         PARAM_LOGE("Failed to save persist param %s", name);
202     }
203     ret = ParamFileWrite(fd, "\n", strlen("\n"));
204     if (ret <= 0) {
205         PARAM_LOGE("Failed to save persist param %s", name);
206     }
207     return 0;
208 }
209 
SavePersistParam(const char * name,const char * value)210 static int SavePersistParam(const char *name, const char *value)
211 {
212     ParamMutexPend(&g_saveMutex);
213     int ret = -1;
214     int fd = ParamFileOpen(PARAM_PERSIST_SAVE_PATH, MODE_APPEND, 0);
215     if (fd > 0) {
216         ret = PersistWrite(fd, name, value);
217         ParamFileSync(fd);
218         ParamFileClose(fd);
219     }
220     if (ret != 0) {
221         PARAM_LOGE("SavePersistParam %s errno %d", name, errno);
222     }
223     ParamMutexPost(&g_saveMutex);
224     return ret;
225 }
226 
BatchSavePersistParamBegin(PERSIST_SAVE_HANDLE * handle)227 static int BatchSavePersistParamBegin(PERSIST_SAVE_HANDLE *handle)
228 {
229     ParamMutexPend(&g_saveMutex);
230     int fd = ParamFileOpen(PARAM_PERSIST_SAVE_PATH, MODE_CREATE, 0);
231     if (fd < 0) {
232         ParamMutexPost(&g_saveMutex);
233         PARAM_LOGE("Open file %s fail error %d", PARAM_PERSIST_SAVE_PATH, errno);
234         return -1;
235     }
236     *handle = (PERSIST_SAVE_HANDLE)fd;
237     return 0;
238 }
239 
BatchSavePersistParam(PERSIST_SAVE_HANDLE handle,const char * name,const char * value)240 static int BatchSavePersistParam(PERSIST_SAVE_HANDLE handle, const char *name, const char *value)
241 {
242     int fd = (int)handle;
243     int ret = PersistWrite(fd, name, value);
244     PARAM_CHECK(ret == 0, return -1, "Failed to write param %s", name);
245     PARAM_LOGV("BatchSavePersistParam %s=%s", name, value);
246     return 0;
247 }
248 
BatchSavePersistParamEnd(PERSIST_SAVE_HANDLE handle)249 static void BatchSavePersistParamEnd(PERSIST_SAVE_HANDLE handle)
250 {
251     int ret;
252     int fd = (int)handle;
253     ParamFileSync(fd);
254     ret = ParamFileClose(fd);
255     ParamMutexPost(&g_saveMutex);
256     PARAM_CHECK(ret == 0, return, "BatchSavePersistParamEnd fail error %d", errno);
257 }
258 
RegisterPersistParamOps(PersistParamOps * ops)259 int RegisterPersistParamOps(PersistParamOps *ops)
260 {
261     ParamMutexCreate(&g_saveMutex);
262     PARAM_CHECK(ops != NULL, return -1, "Invalid ops");
263     ops->save = SavePersistParam;
264     ops->load = LoadPersistParam;
265     ops->batchSaveBegin = BatchSavePersistParamBegin;
266     ops->batchSave = BatchSavePersistParam;
267     ops->batchSaveEnd = BatchSavePersistParamEnd;
268     return 0;
269 }
270