1 /*
2 * Copyright (c) 2023-2023 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 "param_manager.h"
17
18 #include <iostream>
19 #include <fstream>
20 #include <set>
21
22 #include "hiview_platform.h"
23 #include "logger.h"
24 #include "param_const_common.h"
25 #include "param_reader.h"
26 #include "string_util.h"
27
28 namespace OHOS {
29 namespace HiviewDFX {
30 DEFINE_LOG_TAG("Hiview-ParamUpdate");
31
32 namespace {
33 const std::set<std::string> IGNORE_FILE_LIST = {
34 "CERT.ENC",
35 "CERT.SF",
36 "MANIFEST.MF"
37 };
38 }
39
IsFileNeedIgnore(const std::string & fileName)40 bool ParamManager::IsFileNeedIgnore(const std::string& fileName)
41 {
42 return IGNORE_FILE_LIST.find(fileName) != IGNORE_FILE_LIST.end();
43 }
44
InitParam()45 void ParamManager::InitParam()
46 {
47 if (!ParamReader::VerifyCertFile()) {
48 HIVIEW_LOGE("VerifyCertSfFile error, param is invalid");
49 return;
50 }
51
52 if (!VerifyConfigFiles()) {
53 HIVIEW_LOGE("VerifyParamFile error, param is invalid");
54 return;
55 }
56
57 CopyConfigFiles();
58 OnUpdateNotice(LOCAL_CFG_PATH, CLOUD_CFG_PATH);
59 };
60
CopyConfigFiles()61 bool ParamManager::CopyConfigFiles()
62 {
63 std::vector<std::string> files;
64 FileUtil::GetDirFiles(CFG_PATH, files, false);
65 for (std::string file : files) {
66 std::string name = file.substr(file.rfind("/") + 1);
67 if (IsFileNeedIgnore(name)) {
68 continue;
69 }
70 std::string dest = CLOUD_CFG_PATH + "/" + name;
71 int flag = FileUtil::CopyFile(file, dest);
72 if (flag != 0) {
73 HIVIEW_LOGI("copy file failed:%{public}s", name.c_str());
74 }
75 }
76 return true;
77 }
78
VerifyConfigFiles()79 bool ParamManager::VerifyConfigFiles()
80 {
81 std::vector<std::string> files;
82 FileUtil::GetDirFiles(CFG_PATH, files, false);
83 for (std::string file : files) {
84 std::string name = file.substr(file.rfind("/") + 1);
85 if (IsFileNeedIgnore(name)) {
86 continue;
87 }
88 if (!(ParamReader::VerifyParamFile(name))) {
89 HIVIEW_LOGE("VerifyParamFile error:%{public}s", name.c_str());
90 return false;
91 }
92 }
93 return true;
94 }
95
OnUpdateNotice(const std::string & localCfgPath,const std::string & cloudCfgPath)96 void ParamManager::OnUpdateNotice(const std::string& localCfgPath, const std::string& cloudCfgPath)
97 {
98 auto plugins = HiviewPlatform::GetInstance().GetPluginMap();
99 for (auto& plugin : plugins) {
100 auto businessPlugin = plugin.second;
101 if (businessPlugin != nullptr) {
102 businessPlugin->OnConfigUpdate(localCfgPath, cloudCfgPath);
103 }
104 }
105 };
106 }
107 }
108