1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef APKASSETS_H_ 18 #define APKASSETS_H_ 19 20 #include <memory> 21 #include <string> 22 23 #include "android-base/macros.h" 24 #include "android-base/unique_fd.h" 25 26 #include "androidfw/Asset.h" 27 #include "androidfw/LoadedArsc.h" 28 #include "androidfw/misc.h" 29 30 struct ZipArchive; 31 typedef ZipArchive* ZipArchiveHandle; 32 33 namespace android { 34 35 class LoadedIdmap; 36 37 // Holds an APK. 38 class ApkAssets { 39 public: 40 // Creates an ApkAssets. 41 // If `system` is true, the package is marked as a system package, and allows some functions to 42 // filter out this package when computing what configurations/resources are available. 43 static std::unique_ptr<const ApkAssets> Load(const std::string& path, bool system = false); 44 45 // Creates an ApkAssets, but forces any package with ID 0x7f to be loaded as a shared library. 46 // If `system` is true, the package is marked as a system package, and allows some functions to 47 // filter out this package when computing what configurations/resources are available. 48 static std::unique_ptr<const ApkAssets> LoadAsSharedLibrary(const std::string& path, 49 bool system = false); 50 51 // Creates an ApkAssets from an IDMAP, which contains the original APK path, and the overlay 52 // data. 53 // If `system` is true, the package is marked as a system package, and allows some functions to 54 // filter out this package when computing what configurations/resources are available. 55 static std::unique_ptr<const ApkAssets> LoadOverlay(const std::string& idmap_path, 56 bool system = false); 57 58 // Creates an ApkAssets from the given file descriptor, and takes ownership of the file 59 // descriptor. The `friendly_name` is some name that will be used to identify the source of 60 // this ApkAssets in log messages and other debug scenarios. 61 // If `system` is true, the package is marked as a system package, and allows some functions to 62 // filter out this package when computing what configurations/resources are available. 63 // If `force_shared_lib` is true, any package with ID 0x7f is loaded as a shared library. 64 static std::unique_ptr<const ApkAssets> LoadFromFd(base::unique_fd fd, 65 const std::string& friendly_name, bool system, 66 bool force_shared_lib); 67 68 std::unique_ptr<Asset> Open(const std::string& path, 69 Asset::AccessMode mode = Asset::AccessMode::ACCESS_RANDOM) const; 70 71 bool ForEachFile(const std::string& path, 72 const std::function<void(const StringPiece&, FileType)>& f) const; 73 GetPath()74 inline const std::string& GetPath() const { 75 return path_; 76 } 77 78 // This is never nullptr. GetLoadedArsc()79 inline const LoadedArsc* GetLoadedArsc() const { 80 return loaded_arsc_.get(); 81 } 82 IsOverlay()83 inline bool IsOverlay() const { 84 return idmap_asset_.get() != nullptr; 85 } 86 87 bool IsUpToDate() const; 88 89 private: 90 DISALLOW_COPY_AND_ASSIGN(ApkAssets); 91 92 static std::unique_ptr<const ApkAssets> LoadImpl(base::unique_fd fd, const std::string& path, 93 std::unique_ptr<Asset> idmap_asset, 94 std::unique_ptr<const LoadedIdmap> loaded_idmap, 95 bool system, bool load_as_shared_library); 96 97 // Creates an Asset from any file on the file system. 98 static std::unique_ptr<Asset> CreateAssetFromFile(const std::string& path); 99 100 ApkAssets(ZipArchiveHandle unmanaged_handle, const std::string& path, time_t last_mod_time); 101 102 using ZipArchivePtr = std::unique_ptr<ZipArchive, void(*)(ZipArchiveHandle)>; 103 104 ZipArchivePtr zip_handle_; 105 const std::string path_; 106 time_t last_mod_time_; 107 std::unique_ptr<Asset> resources_asset_; 108 std::unique_ptr<Asset> idmap_asset_; 109 std::unique_ptr<const LoadedArsc> loaded_arsc_; 110 }; 111 112 } // namespace android 113 114 #endif /* APKASSETS_H_ */ 115