1 /*
2 * Copyright (c) 2021-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 "base_extractor.h"
17
18 #include <dirent.h>
19 #include <fstream>
20 #include <limits>
21
22 #include "app_log_wrapper.h"
23 #include "bundle_constants.h"
24 #include "string_ex.h"
25
26 namespace OHOS {
27 namespace AppExecFwk {
28 namespace {
29 constexpr const char* MODULE_PROFILE_NAME = "module.json";
30 }
31
BaseExtractor(const std::string & source)32 BaseExtractor::BaseExtractor(const std::string &source) : sourceFile_(source), zipFile_(source)
33 {
34 APP_LOGI("BaseExtractor instance is created");
35 }
36
~BaseExtractor()37 BaseExtractor::~BaseExtractor()
38 {
39 APP_LOGI("BaseExtractor instance is destroyed");
40 }
41
Init()42 bool BaseExtractor::Init()
43 {
44 if (!zipFile_.Open()) {
45 APP_LOGE("open zip file failed");
46 return false;
47 }
48 ZipEntry zipEntry;
49 isNewVersion_ = zipFile_.GetEntry(MODULE_PROFILE_NAME, zipEntry);
50 initial_ = true;
51 APP_LOGI("success");
52 return true;
53 }
54
HasEntry(const std::string & fileName) const55 bool BaseExtractor::HasEntry(const std::string &fileName) const
56 {
57 if (!initial_) {
58 APP_LOGE("extractor is not initial");
59 return false;
60 }
61
62 return zipFile_.HasEntry(fileName);
63 }
64
IsDirExist(const std::string & dir) const65 bool BaseExtractor::IsDirExist(const std::string &dir) const
66 {
67 if (!initial_) {
68 APP_LOGE("extractor is not initial");
69 return false;
70 }
71 if (dir.empty()) {
72 APP_LOGE("param dir empty");
73 return false;
74 }
75 return zipFile_.IsDirExist(dir);
76 }
77
ExtractByName(const std::string & fileName,std::ostream & dest) const78 bool BaseExtractor::ExtractByName(const std::string &fileName, std::ostream &dest) const
79 {
80 if (!initial_) {
81 APP_LOGE("extractor is not initial");
82 return false;
83 }
84 if (!zipFile_.ExtractFile(fileName, dest)) {
85 APP_LOGE("extractor is not ExtractFile");
86 return false;
87 }
88 return true;
89 }
90
ExtractFile(const std::string & fileName,const std::string & targetPath) const91 bool BaseExtractor::ExtractFile(const std::string &fileName, const std::string &targetPath) const
92 {
93 APP_LOGD("begin to extract %{public}s file into %{private}s targetPath", fileName.c_str(), targetPath.c_str());
94 std::ofstream fileStream;
95 fileStream.open(targetPath, std::ios_base::out | std::ios_base::binary);
96 if (!fileStream.is_open()) {
97 APP_LOGE("fail to open %{private}s file to write", targetPath.c_str());
98 return false;
99 }
100 if ((!ExtractByName(fileName, fileStream)) || (!fileStream.good())) {
101 APP_LOGE("fail to extract %{public}s zip file into stream", fileName.c_str());
102 fileStream.clear();
103 fileStream.close();
104 if (remove(targetPath.c_str()) != 0) {
105 APP_LOGE("fail to remove %{private}s file which writes stream error", targetPath.c_str());
106 }
107 return false;
108 }
109 fileStream.clear();
110 fileStream.close();
111 return true;
112 }
113
GetZipFileNames(std::vector<std::string> & fileNames) const114 bool BaseExtractor::GetZipFileNames(std::vector<std::string> &fileNames) const
115 {
116 auto &entryMap = zipFile_.GetAllEntries();
117 auto entryFilter = [&fileNames](const auto &entry) {
118 auto position = entry.first.rfind(Constants::QUICK_FIX_FILE_SUFFIX);
119 bool isHqfFile = false;
120 if (position != std::string::npos) {
121 std::string suffixStr = entry.first.substr(position);
122 isHqfFile = suffixStr == Constants::QUICK_FIX_FILE_SUFFIX;
123 }
124 if ((entry.first.find(Constants::RELATIVE_PATH) == std::string::npos) && !isHqfFile) {
125 fileNames.emplace_back(entry.first);
126 }
127 };
128 for_each(entryMap.begin(), entryMap.end(), entryFilter);
129 return true;
130 }
131
IsStageBasedModel(std::string abilityName)132 bool BaseExtractor::IsStageBasedModel(std::string abilityName)
133 {
134 auto &entryMap = zipFile_.GetAllEntries();
135 std::vector<std::string> splitStrs;
136 OHOS::SplitStr(abilityName, ".", splitStrs);
137 std::string name = splitStrs.empty() ? abilityName : splitStrs.back();
138 std::string entry = "assets/js/" + name + "/" + name + ".js";
139 bool isStageBasedModel = entryMap.find(entry) != entryMap.end();
140 APP_LOGI("name:%{public}s isStageBasedModel:%{public}d", abilityName.c_str(), isStageBasedModel);
141 return isStageBasedModel;
142 }
143
IsNewVersion() const144 bool BaseExtractor::IsNewVersion() const
145 {
146 return isNewVersion_;
147 }
148
GetFileInfo(const std::string & fileName,uint32_t & offset,uint32_t & length) const149 bool BaseExtractor::GetFileInfo(const std::string &fileName, uint32_t &offset, uint32_t &length) const
150 {
151 if (!initial_) {
152 APP_LOGE("extractor is not initial");
153 return false;
154 }
155 ZipPos tmpOffset = 0;
156 if (!zipFile_.GetDataOffsetRelative(fileName, tmpOffset, length)) {
157 APP_LOGE("GetDataOffsetRelative failed");
158 return false;
159 }
160 if (tmpOffset > std::numeric_limits<uint32_t>::max()) {
161 APP_LOGE("offset too large");
162 return false;
163 }
164 offset = static_cast<uint32_t>(tmpOffset);
165 return true;
166 }
167 } // namespace AppExecFwk
168 } // namespace OHOS
169