• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef OHOS_ABILITY_BASE_EXTRACTOR_H
17 #define OHOS_ABILITY_BASE_EXTRACTOR_H
18 
19 #include <memory>
20 #include <mutex>
21 #include <optional>
22 #include <set>
23 #include <string>
24 #include <unordered_map>
25 #include <vector>
26 
27 #include "file_mapper.h"
28 #include "zip_file.h"
29 
30 namespace OHOS {
31 namespace AbilityBase {
32 struct FileInfo {
33     std::string fileName;
34     uint32_t offset = 0;
35     uint32_t length = 0;
36     uint16_t lastModTime = 0;
37     uint16_t lastModDate = 0;
38 };
39 
40 class Extractor {
41 public:
42     explicit Extractor(const std::string &source);
43     virtual ~Extractor();
44 
45     /**
46      * @brief Open compressed file.
47      * @return Returns true if the file is successfully opened; returns false otherwise.
48      */
49     virtual bool Init();
50 
51     /**
52      * @brief Extract to dest stream by file name.
53      * @param fileName Indicates the file name.
54      * @param dest Indicates the obtained std::ostream object.
55      * @return Returns true if the file extracted successfully; returns false otherwise.
56      */
57     bool ExtractByName(const std::string &fileName, std::ostream &dest) const;
58     /**
59      * @brief Extract to dest path on filesystem.
60      * @param fileName Indicates the file name.
61      * @param targetPath Indicates the target Path.
62      * @return Returns true if the file extracted to filesystem successfully; returns false otherwise.
63      */
64     bool ExtractFile(const std::string &fileName, const std::string &targetPath) const;
65     /**
66      * @brief Get specified type names in a zip file.
67      * @param fileNames Indicates the obtained file names in zip.
68      * @param suffix Indicates the suffix of file.
69      */
70     void GetSpecifiedTypeFiles(std::vector<std::string> &fileNames, const std::string &suffix);
71     /**
72      * @brief Has entry by name.
73      * @param entryName Indicates the entry name.
74      * @return Returns true if the ZipEntry is successfully finded; returns false otherwise.
75      */
76     bool HasEntry(const std::string &fileName) const;
77     bool IsDirExist(const std::string &dir) const;
78     bool IsStageBasedModel(std::string abilityName);
79     bool GetFileBuffer(const std::string& srcPath, std::ostringstream& dest);
80     bool GetFileList(const std::string& srcPath, std::vector<std::string>& assetList);
81     bool GetFileList(const std::string &srcPath, std::set<std::string> &fileSet);
82     bool IsSameHap(const std::string& hapPath) const;
83 
84     std::unique_ptr<FileMapper> GetData(const std::string &fileName, bool safeRegion = false) const;
85     /**
86      * Do not use this method unless you exactly know what you are doing.
87      * For file item that user will handle errors, to mmap to safe region.
88      * User should make sure the extractor's release goes after the data's.
89      */
90     std::unique_ptr<FileMapper> GetMmapData(const std::string &fileName);
91 
92     bool UnzipData(std::unique_ptr<FileMapper> fileMapper, std::unique_ptr<uint8_t[]> &dataPtr, size_t &len) const;
93     bool IsStageModel();
94 
95     bool GetFileInfo(const std::string &fileName, FileInfo &fileInfo) const;
96 
97     bool IsHapCompress(const std::string &fileName) const;
98 
99     bool ExtractToBufByName(const std::string &fileName, std::unique_ptr<uint8_t[]> &dataPtr, size_t &len);
100     /**
101      * For abc file only, to mmap to safe region.
102      */
103     std::shared_ptr<FileMapper> GetSafeData(const std::string &fileName);
104 private:
105     ZipFile zipFile_;
106     bool initial_ = false;
107     std::string hapPath_;
108     std::optional<bool> isStageModel_;
109 };
110 
111 class ExtractorUtil {
112 public:
113     static std::string GetLoadFilePath(const std::string &hapPath);
114     static std::shared_ptr<Extractor> GetExtractor(const std::string &hapPath, bool &newCreate, bool cache = false);
115     static void DeleteExtractor(const std::string &hapPath);
116 
117 private:
118     static std::mutex mapMutex_;
119     static std::unordered_map<std::string, std::shared_ptr<Extractor>> extractorMap_;
120 };
121 }  // namespace AbilityBase
122 }  // namespace OHOS
123 #endif  // OHOS_ABILITY_BASE_EXTRACTOR_H
124