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