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_wrapper.h"
21 #include "patch_extractor.h"
22 #include "patch_profile.h"
23
24 namespace OHOS {
25 namespace AppExecFwk {
ParsePatchInfo(const std::string & pathName,AppQuickFix & appQuickFix) const26 ErrCode PatchParser::ParsePatchInfo(const std::string &pathName, AppQuickFix &appQuickFix) const
27 {
28 APP_LOGD("Parse patch.json from %{private}s", pathName.c_str());
29 if (pathName.empty()) {
30 return ERR_APPEXECFWK_PARSE_NO_PROFILE;
31 }
32 PatchExtractor patchExtractor(pathName);
33 if (!patchExtractor.Init()) {
34 APP_LOGE("patch extractor init failed");
35 return ERR_APPEXECFWK_PARSE_UNEXPECTED;
36 }
37
38 std::ostringstream outStreamForHatchInfo;
39 if (!patchExtractor.ExtractPatchProfile(outStreamForHatchInfo)) {
40 APP_LOGE("extract patch.json failed");
41 return ERR_APPEXECFWK_PARSE_NO_PROFILE;
42 }
43 APP_LOGD("extract patch complete");
44
45 PatchProfile patchProfile;
46 ErrCode ret = patchProfile.TransformTo(outStreamForHatchInfo, patchExtractor, appQuickFix);
47 if (ret != ERR_OK) {
48 APP_LOGE("transform stream to appQuickFix failed %{public}d", ret);
49 return ret;
50 }
51 return ERR_OK;
52 }
53
ParsePatchInfo(const std::vector<std::string> & filePaths,std::unordered_map<std::string,AppQuickFix> & appQuickFixes) const54 ErrCode PatchParser::ParsePatchInfo(const std::vector<std::string> &filePaths,
55 std::unordered_map<std::string, AppQuickFix> &appQuickFixes) const
56 {
57 APP_LOGD("Parse quick fix files start.");
58 if (filePaths.empty()) {
59 return ERR_APPEXECFWK_PARSE_NO_PROFILE;
60 }
61 for (size_t index = 0; index < filePaths.size(); ++index) {
62 AppQuickFix appQuickFix;
63 ErrCode result = ParsePatchInfo(filePaths[index], appQuickFix);
64 if (result != ERR_OK) {
65 APP_LOGE("quick fix parse failed %{public}d", result);
66 return result;
67 }
68 appQuickFixes.emplace(filePaths[index], appQuickFix);
69 }
70 APP_LOGD("Parse quick fix files end.");
71 return ERR_OK;
72 }
73 } // namespace AppExecFwk
74 } // namespace OHOS