1 /*
2  * Copyright (c) 2023-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 "param_manager.h"
17 
18 #include <iostream>
19 #include <fstream>
20 #include <set>
21 
22 #include "hiview_platform.h"
23 #include "hiview_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     "version.txt"
38 };
39 }
40 
IsFileNeedIgnore(const std::string & fileName)41 bool ParamManager::IsFileNeedIgnore(const std::string& fileName)
42 {
43     return IGNORE_FILE_LIST.find(fileName) != IGNORE_FILE_LIST.end();
44 }
45 
InitParam()46 void ParamManager::InitParam()
47 {
48     if (!ParamReader::VerifyCertFile()) {
49         HIVIEW_LOGE("VerifyCertSfFile error, param is invalid");
50         return;
51     }
52 
53     std::vector<std::string> validFiles;
54     GetValidFiles(validFiles);
55     if (validFiles.empty()) {
56         return;
57     }
58     CopyConfigFiles(validFiles);
59     OnUpdateNotice(LOCAL_CFG_PATH, CLOUD_CFG_PATH);
60 }
61 
CopyFile(const std::string & srcFile,const std::string & dstFile)62 bool ParamManager::CopyFile(const std::string& srcFile, const std::string& dstFile)
63 {
64     std::string dstPath(FileUtil::ExtractFilePath(dstFile));
65     // create subdir if not exist
66     if (dstPath != CLOUD_CFG_PATH && !FileUtil::ForceCreateDirectory(dstPath)) {
67         HIVIEW_LOGW("create dst path failed: %{public}s", dstPath.c_str());
68         return false;
69     }
70     return FileUtil::CopyFile(srcFile, dstFile) == 0;
71 }
72 
CopyConfigFiles(const std::vector<std::string> & files)73 bool ParamManager::CopyConfigFiles(const std::vector<std::string>& files)
74 {
75     for (const std::string& file : files) {
76         std::string dstFile(file);
77         dstFile.replace(0, CFG_PATH.length(), CLOUD_CFG_PATH);
78         if (!CopyFile(file, dstFile)) {
79             HIVIEW_LOGI("copy file failed: %{public}s", file.c_str());
80         }
81     }
82     return true;
83 }
84 
GetValidFiles(std::vector<std::string> & validFiles)85 void ParamManager::GetValidFiles(std::vector<std::string>& validFiles)
86 {
87     std::vector<std::string> files;
88     FileUtil::GetDirFiles(CFG_PATH, files, true);
89     for (const std::string& file : files) {
90         std::string fileName(FileUtil::ExtractFileName(file));
91         if (IsFileNeedIgnore(fileName)) {
92             continue;
93         }
94         std::string relativedPath(file.substr(CFG_PATH.length()));
95         if (!ParamReader::VerifyParamFile(relativedPath)) {
96             HIVIEW_LOGE("verify file failed: %{public}s", fileName.c_str());
97             validFiles.clear();
98             break;
99         }
100         validFiles.emplace_back(file);
101     }
102 }
103 
OnUpdateNotice(const std::string & localCfgPath,const std::string & cloudCfgPath)104 void ParamManager::OnUpdateNotice(const std::string& localCfgPath, const std::string& cloudCfgPath)
105 {
106     auto plugins = HiviewPlatform::GetInstance().GetPluginMap();
107     for (auto& plugin : plugins) {
108         auto businessPlugin = plugin.second;
109         if (businessPlugin != nullptr) {
110             businessPlugin->OnConfigUpdate(localCfgPath, cloudCfgPath);
111         }
112     }
113 };
114 }
115 }
116