• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
21 #include "app_log_wrapper.h"
22 #include "bundle_constants.h"
23 #include "string_ex.h"
24 
25 namespace OHOS {
26 namespace AppExecFwk {
BaseExtractor(const std::string & source)27 BaseExtractor::BaseExtractor(const std::string &source) : sourceFile_(source), zipFile_(source)
28 {
29     APP_LOGI("BaseExtractor instance is created");
30 }
31 
~BaseExtractor()32 BaseExtractor::~BaseExtractor()
33 {
34     APP_LOGI("BaseExtractor instance is destroyed");
35 }
36 
Init()37 bool BaseExtractor::Init()
38 {
39     if (!zipFile_.Open()) {
40         APP_LOGE("open zip file failed");
41         return false;
42     }
43     ZipEntry zipEntry;
44     isNewVersion_ = zipFile_.GetEntry(Constants::MODULE_PROFILE_NAME, zipEntry);
45     initial_ = true;
46     APP_LOGI("success");
47     return true;
48 }
49 
HasEntry(const std::string & fileName) const50 bool BaseExtractor::HasEntry(const std::string &fileName) const
51 {
52     if (!initial_) {
53         APP_LOGE("extractor is not initial");
54         return false;
55     }
56 
57     return zipFile_.HasEntry(fileName);
58 }
59 
IsDirExist(const std::string & dir) const60 bool BaseExtractor::IsDirExist(const std::string &dir) const
61 {
62     if (!initial_) {
63         APP_LOGE("extractor is not initial");
64         return false;
65     }
66     if (dir.empty()) {
67         APP_LOGE("param dir empty");
68         return false;
69     }
70     return zipFile_.IsDirExist(dir);
71 }
72 
ExtractByName(const std::string & fileName,std::ostream & dest) const73 bool BaseExtractor::ExtractByName(const std::string &fileName, std::ostream &dest) const
74 {
75     if (!initial_) {
76         APP_LOGE("extractor is not initial");
77         return false;
78     }
79     if (!zipFile_.ExtractFile(fileName, dest)) {
80         APP_LOGE("extractor is not ExtractFile");
81         return false;
82     }
83     return true;
84 }
85 
ExtractFile(const std::string & fileName,const std::string & targetPath) const86 bool BaseExtractor::ExtractFile(const std::string &fileName, const std::string &targetPath) const
87 {
88     APP_LOGD("begin to extract %{public}s file into %{private}s targetPath", fileName.c_str(), targetPath.c_str());
89     std::ofstream fileStream;
90     fileStream.open(targetPath, std::ios_base::out | std::ios_base::binary);
91     if (!fileStream.is_open()) {
92         APP_LOGE("fail to open %{private}s file to write", targetPath.c_str());
93         return false;
94     }
95     if ((!ExtractByName(fileName, fileStream)) || (!fileStream.good())) {
96         APP_LOGE("fail to extract %{public}s zip file into stream", fileName.c_str());
97         fileStream.clear();
98         fileStream.close();
99         if (remove(targetPath.c_str()) != 0) {
100             APP_LOGE("fail to remove %{private}s file which writes stream error", targetPath.c_str());
101         }
102         return false;
103     }
104     fileStream.clear();
105     fileStream.close();
106     return true;
107 }
108 
GetZipFileNames(std::vector<std::string> & fileNames)109 bool BaseExtractor::GetZipFileNames(std::vector<std::string> &fileNames)
110 {
111     auto &entryMap = zipFile_.GetAllEntries();
112     auto entryFilter = [&fileNames](const auto &entry) {
113         auto position = entry.first.rfind(Constants::QUICK_FIX_FILE_SUFFIX);
114         bool isHqfFile = false;
115         if (position != std::string::npos) {
116             std::string suffixStr = entry.first.substr(position);
117             isHqfFile = suffixStr == Constants::QUICK_FIX_FILE_SUFFIX;
118         }
119         if ((entry.first.find(Constants::RELATIVE_PATH) == std::string::npos) && !isHqfFile) {
120             fileNames.emplace_back(entry.first);
121         }
122     };
123     for_each(entryMap.begin(), entryMap.end(), entryFilter);
124     return true;
125 }
126 
IsStageBasedModel(std::string abilityName)127 bool BaseExtractor::IsStageBasedModel(std::string abilityName)
128 {
129     auto &entryMap = zipFile_.GetAllEntries();
130     std::vector<std::string> splitStrs;
131     OHOS::SplitStr(abilityName, ".", splitStrs);
132     std::string name = splitStrs.empty() ? abilityName : splitStrs.back();
133     std::string entry = "assets/js/" + name + "/" + name + ".js";
134     bool isStageBasedModel = entryMap.find(entry) != entryMap.end();
135     APP_LOGI("name:%{public}s isStageBasedModel:%{public}d", abilityName.c_str(), isStageBasedModel);
136     return isStageBasedModel;
137 }
138 
IsNewVersion() const139 bool BaseExtractor::IsNewVersion() const
140 {
141     return isNewVersion_;
142 }
143 }  // namespace AppExecFwk
144 }  // namespace OHOS
145