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 * @brief Deprecated, use ExtractToBufByName instead. Extract to dest stream by file name. 52 * @param fileName Indicates the file name. 53 * @param dest Indicates the obtained std::ostream object. 54 * @return Returns true if the file extracted successfully; returns false otherwise. 55 */ 56 bool ExtractByName(const std::string &fileName, std::ostream &dest) const; 57 /** 58 * @brief Get specified type names in a zip file. 59 * @param fileNames Indicates the obtained file names in zip. 60 * @param suffix Indicates the suffix of file. 61 */ 62 void GetSpecifiedTypeFiles(std::vector<std::string> &fileNames, const std::string &suffix); 63 /** 64 * @brief Has entry by name. 65 * @param entryName Indicates the entry name. 66 * @return Returns true if the ZipEntry is successfully finded; returns false otherwise. 67 */ 68 bool HasEntry(const std::string &fileName) const; 69 bool IsDirExist(const std::string &dir); 70 /** 71 * @brief deprecated, use ExtractToBufByName instead 72 */ 73 bool GetFileBuffer(const std::string& srcPath, std::ostringstream& dest); 74 bool GetFileList(const std::string& srcPath, std::vector<std::string>& assetList); 75 bool GetFileList(const std::string &srcPath, std::set<std::string> &fileSet); 76 77 std::unique_ptr<FileMapper> GetData(const std::string &fileName) const; 78 /** 79 * Do not use this method unless you exactly know what you are doing. 80 * For file item that user will handle errors, to mmap to safe region. 81 * User should make sure the extractor's release goes after the data's. 82 */ 83 std::unique_ptr<FileMapper> GetMmapData(const std::string &fileName); 84 85 bool UnzipData(std::unique_ptr<FileMapper> fileMapper, std::unique_ptr<uint8_t[]> &dataPtr, size_t &len) const; 86 bool IsStageModel(); 87 88 bool GetFileInfo(const std::string &fileName, FileInfo &fileInfo) const; 89 90 bool IsHapCompress(const std::string &fileName) const; 91 92 bool ExtractToBufByName(const std::string &fileName, std::unique_ptr<uint8_t[]> &dataPtr, size_t &len) const; 93 /** 94 * For abc file only, to mmap to safe region. 95 */ 96 std::shared_ptr<FileMapper> GetSafeData(const std::string &fileName); SetCacheMode(CacheMode cacheMode)97 void SetCacheMode(CacheMode cacheMode) 98 { 99 zipFile_.SetCacheMode(cacheMode); 100 } 101 private: 102 ZipFile zipFile_; 103 bool initial_ = false; 104 std::string hapPath_; 105 std::optional<bool> isStageModel_; 106 }; 107 108 class ExtractorUtil { 109 public: 110 static std::string GetLoadFilePath(const std::string &hapPath); 111 static std::shared_ptr<Extractor> GetExtractor(const std::string &hapPath, bool &newCreate, bool cache = false); 112 static void DeleteExtractor(const std::string &hapPath); 113 114 private: 115 static std::mutex mapMutex_; 116 static std::unordered_map<std::string, std::shared_ptr<Extractor>> extractorMap_; 117 }; 118 } // namespace AbilityBase 119 } // namespace OHOS 120 #endif // OHOS_ABILITY_BASE_EXTRACTOR_H 121