1 /*
2 * Copyright (c) 2022 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 "quick_fix/patch_parser.h"
17
18 #include <sstream>
19
20 #include "app_log_tag_wrapper.h"
21 #include "app_log_wrapper.h"
22 #include "patch_extractor.h"
23 #include "patch_profile.h"
24
25 namespace OHOS {
26 namespace AppExecFwk {
27 namespace {
28 const std::string RESOURCES_RAW_FILE = "resources/rawfile/";
29 } // namespace
30
ParsePatchInfo(const std::string & pathName,AppQuickFix & appQuickFix) const31 ErrCode PatchParser::ParsePatchInfo(const std::string &pathName, AppQuickFix &appQuickFix) const
32 {
33 LOG_D(BMS_TAG_DEFAULT, "Parse patch.json from %{private}s", pathName.c_str());
34 if (pathName.empty()) {
35 return ERR_APPEXECFWK_PARSE_NO_PROFILE;
36 }
37 PatchExtractor patchExtractor(pathName);
38 if (!patchExtractor.Init()) {
39 LOG_E(BMS_TAG_DEFAULT, "patch extractor init failed");
40 return ERR_APPEXECFWK_PARSE_UNEXPECTED;
41 }
42
43 std::ostringstream outStreamForHatchInfo;
44 if (!patchExtractor.ExtractPatchProfile(outStreamForHatchInfo)) {
45 LOG_E(BMS_TAG_DEFAULT, "extract patch.json failed");
46 return ERR_APPEXECFWK_PARSE_NO_PROFILE;
47 }
48 LOG_D(BMS_TAG_DEFAULT, "extract patch complete");
49
50 PatchProfile patchProfile;
51 ErrCode ret = patchProfile.TransformTo(outStreamForHatchInfo, patchExtractor, appQuickFix);
52 if (ret != ERR_OK) {
53 LOG_E(BMS_TAG_DEFAULT, "transform stream to appQuickFix failed %{public}d", ret);
54 return ret;
55 }
56 return ERR_OK;
57 }
58
ParsePatchInfo(const std::vector<std::string> & filePaths,std::unordered_map<std::string,AppQuickFix> & appQuickFixes) const59 ErrCode PatchParser::ParsePatchInfo(const std::vector<std::string> &filePaths,
60 std::unordered_map<std::string, AppQuickFix> &appQuickFixes) const
61 {
62 LOG_D(BMS_TAG_DEFAULT, "Parse quick fix files start");
63 if (filePaths.empty()) {
64 return ERR_APPEXECFWK_PARSE_NO_PROFILE;
65 }
66 for (size_t index = 0; index < filePaths.size(); ++index) {
67 AppQuickFix appQuickFix;
68 ErrCode result = ParsePatchInfo(filePaths[index], appQuickFix);
69 if (result != ERR_OK) {
70 LOG_E(BMS_TAG_DEFAULT, "quick fix parse failed %{public}d", result);
71 return result;
72 }
73 appQuickFixes.emplace(filePaths[index], appQuickFix);
74 }
75 LOG_D(BMS_TAG_DEFAULT, "Parse quick fix files end");
76 return ERR_OK;
77 }
78
HasResourceFile(const std::string & filePath) const79 bool PatchParser::HasResourceFile(const std::string &filePath) const
80 {
81 LOG_D(BMS_TAG_DEFAULT, "check filePath has resource file start");
82 if (filePath.empty()) {
83 return false;
84 }
85 PatchExtractor patchExtractor(filePath);
86 if (!patchExtractor.Init()) {
87 LOG_E(BMS_TAG_DEFAULT, "patch extractor init failed");
88 return false;
89 }
90
91 return patchExtractor.IsDirExist(RESOURCES_RAW_FILE);
92 }
93
HasResourceFile(const std::vector<std::string> & filePaths) const94 bool PatchParser::HasResourceFile(const std::vector<std::string> &filePaths) const
95 {
96 LOG_D(BMS_TAG_DEFAULT, "check filePaths has resource file start");
97 for (size_t index = 0; index < filePaths.size(); ++index) {
98 if (HasResourceFile(filePaths[index])) {
99 return true;
100 }
101 }
102 LOG_D(BMS_TAG_DEFAULT, "does not exist resource rawfile");
103 return false;
104 }
105 } // namespace AppExecFwk
106 } // namespace OHOS