• 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 <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     uint32_t dataIndex = 0;
31     int ret = WriteParam(name, value, &dataIndex, 0);
32     PARAM_CHECK(ret == 0, return ret, "Failed to write param %d name:%s %s", ret, name, value);
33     return 0;
34 }
35 
LoadPersistParam()36 static int LoadPersistParam()
37 {
38     CheckAndCreateDir(PARAM_PERSIST_SAVE_PATH);
39     int updaterMode = InUpdaterMode();
40     char *tmpPath = (updaterMode == 0) ? PARAM_PERSIST_SAVE_TMP_PATH : "/param/tmp_persist_parameters";
41     FILE *fp = fopen(tmpPath, "r");
42     if (fp == NULL) {
43         tmpPath = (updaterMode == 0) ? PARAM_PERSIST_SAVE_PATH : "/param/persist_parameters";
44         fp = fopen(tmpPath, "r");
45         PARAM_LOGI("Load persist param, from file %s", tmpPath);
46     }
47     PARAM_CHECK(fp != NULL, return -1, "No valid persist parameter file %s", PARAM_PERSIST_SAVE_PATH);
48 
49     const int buffSize = PARAM_NAME_LEN_MAX + PARAM_CONST_VALUE_LEN_MAX + 10;  // 10 max len
50     char *buffer = malloc(buffSize);
51     if (buffer == NULL) {
52         (void)fclose(fp);
53         return -1;
54     }
55     uint32_t paramNum = 0;
56     while (fgets(buffer, buffSize, fp) != NULL) {
57         buffer[buffSize - 1] = '\0';
58         int ret = SplitParamString(buffer, NULL, 0, LoadOnePersistParam_, NULL);
59         PARAM_CHECK(ret == 0, continue, "Failed to set param %d %s", ret, buffer);
60         paramNum++;
61     }
62     (void)fclose(fp);
63     free(buffer);
64     PARAM_LOGI("LoadPersistParam from file %s paramNum %d", tmpPath, paramNum);
65     return 0;
66 }
67 
SavePersistParam(const char * name,const char * value)68 static int SavePersistParam(const char *name, const char *value)
69 {
70     ParamMutexPend(&g_saveMutex);
71     char *path = (InUpdaterMode() == 0) ? PARAM_PERSIST_SAVE_PATH : "/param/persist_parameters";
72     FILE *fp = fopen(path, "a+");
73     int ret = -1;
74     if (fp != NULL) {
75         ret = fprintf(fp, "%s=%s\n", name, value);
76         (void)fclose(fp);
77     }
78     ParamMutexPost(&g_saveMutex);
79     if (ret <= 0) {
80         PARAM_LOGE("Failed to save persist param %s", name);
81     }
82     return ret;
83 }
84 
BatchSavePersistParamBegin(PERSIST_SAVE_HANDLE * handle)85 static int BatchSavePersistParamBegin(PERSIST_SAVE_HANDLE *handle)
86 {
87     ParamMutexPend(&g_saveMutex);
88     char *path = (InUpdaterMode() == 0) ? PARAM_PERSIST_SAVE_TMP_PATH : "/param/tmp_persist_parameters";
89     unlink(path);
90     FILE *fp = fopen(path, "w");
91     if (fp == NULL) {
92         ParamMutexPost(&g_saveMutex);
93         PARAM_LOGE("Open file %s fail error %d", path, errno);
94         return -1;
95     }
96     *handle = (PERSIST_SAVE_HANDLE)fp;
97     return 0;
98 }
99 
BatchSavePersistParam(PERSIST_SAVE_HANDLE handle,const char * name,const char * value)100 static int BatchSavePersistParam(PERSIST_SAVE_HANDLE handle, const char *name, const char *value)
101 {
102     FILE *fp = (FILE *)handle;
103     int ret = fprintf(fp, "%s=%s\n", name, value);
104     PARAM_CHECK(ret > 0, return -1, "Failed to write param");
105     PARAM_LOGV("BatchSavePersistParam %s=%s", name, value);
106     return 0;
107 }
108 
BatchSavePersistParamEnd(PERSIST_SAVE_HANDLE handle)109 static void BatchSavePersistParamEnd(PERSIST_SAVE_HANDLE handle)
110 {
111     int ret;
112     FILE *fp = (FILE *)handle;
113     (void)fclose(fp);
114     if (InUpdaterMode() == 0) {
115         unlink(PARAM_PERSIST_SAVE_PATH);
116         ret = rename(PARAM_PERSIST_SAVE_TMP_PATH, PARAM_PERSIST_SAVE_PATH);
117     } else {
118         unlink("/param/persist_parameters");
119         ret = rename("/param/tmp_persist_parameters", "/param/persist_parameters");
120     }
121     ParamMutexPost(&g_saveMutex);
122     PARAM_CHECK(ret == 0, return, "BatchSavePersistParamEnd %s fail error %d", PARAM_PERSIST_SAVE_TMP_PATH, errno);
123 }
124 
RegisterPersistParamOps(PersistParamOps * ops)125 int RegisterPersistParamOps(PersistParamOps *ops)
126 {
127     ParamMutexCreate(&g_saveMutex);
128     PARAM_CHECK(ops != NULL, return -1, "Invalid ops");
129     ops->save = SavePersistParam;
130     ops->load = LoadPersistParam;
131     ops->batchSaveBegin = BatchSavePersistParamBegin;
132     ops->batchSave = BatchSavePersistParam;
133     ops->batchSaveEnd = BatchSavePersistParamEnd;
134     return 0;
135 }