Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 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 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 17 | #include "androidfw/ApkAssets.h" |
| 18 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 19 | #include "android-base/errors.h" |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 20 | #include "android-base/logging.h" |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 21 | |
| 22 | namespace android { |
| 23 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 24 | using base::SystemErrorCodeToString; |
| 25 | using base::unique_fd; |
| 26 | |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 27 | constexpr const char* kResourcesArsc = "resources.arsc"; |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 28 | |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 29 | ApkAssets::ApkAssets(std::unique_ptr<Asset> resources_asset, |
| 30 | std::unique_ptr<LoadedArsc> loaded_arsc, |
| 31 | std::unique_ptr<AssetsProvider> assets, |
| 32 | package_property_t property_flags, |
| 33 | std::unique_ptr<Asset> idmap_asset, |
| 34 | std::unique_ptr<LoadedIdmap> loaded_idmap) |
| 35 | : resources_asset_(std::move(resources_asset)), |
| 36 | loaded_arsc_(std::move(loaded_arsc)), |
| 37 | assets_provider_(std::move(assets)), |
| 38 | property_flags_(property_flags), |
| 39 | idmap_asset_(std::move(idmap_asset)), |
| 40 | loaded_idmap_(std::move(loaded_idmap)) {} |
| 41 | |
| 42 | std::unique_ptr<ApkAssets> ApkAssets::Load(const std::string& path, package_property_t flags) { |
| 43 | return Load(ZipAssetsProvider::Create(path), flags); |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 44 | } |
Adam Lesinski | 03ebac8 | 2017-09-25 13:10:14 -0700 | [diff] [blame] | 45 | |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 46 | std::unique_ptr<ApkAssets> ApkAssets::LoadFromFd(base::unique_fd fd, |
| 47 | const std::string& debug_name, |
| 48 | package_property_t flags, |
| 49 | off64_t offset, |
| 50 | off64_t len) { |
| 51 | return Load(ZipAssetsProvider::Create(std::move(fd), debug_name, offset, len), flags); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 52 | } |
| 53 | |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 54 | std::unique_ptr<ApkAssets> ApkAssets::Load(std::unique_ptr<AssetsProvider> assets, |
| 55 | package_property_t flags) { |
| 56 | return LoadImpl(std::move(assets), flags, nullptr /* idmap_asset */, nullptr /* loaded_idmap */); |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 59 | std::unique_ptr<ApkAssets> ApkAssets::LoadTable(std::unique_ptr<Asset> resources_asset, |
| 60 | std::unique_ptr<AssetsProvider> assets, |
| 61 | package_property_t flags) { |
| 62 | if (resources_asset == nullptr) { |
| 63 | return {}; |
| 64 | } |
| 65 | return LoadImpl(std::move(resources_asset), std::move(assets), flags, nullptr /* idmap_asset */, |
| 66 | nullptr /* loaded_idmap */); |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 69 | std::unique_ptr<ApkAssets> ApkAssets::LoadOverlay(const std::string& idmap_path, |
| 70 | package_property_t flags) { |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 71 | CHECK((flags & PROPERTY_LOADER) == 0U) << "Cannot load RROs through loaders"; |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 72 | auto idmap_asset = AssetsProvider::CreateAssetFromFile(idmap_path); |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 73 | if (idmap_asset == nullptr) { |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 74 | LOG(ERROR) << "failed to read IDMAP " << idmap_path; |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 75 | return {}; |
| 76 | } |
| 77 | |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 78 | StringPiece idmap_data(reinterpret_cast<const char*>(idmap_asset->getBuffer(true /* aligned */)), |
| 79 | static_cast<size_t>(idmap_asset->getLength())); |
| 80 | auto loaded_idmap = LoadedIdmap::Load(idmap_path, idmap_data); |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 81 | if (loaded_idmap == nullptr) { |
| 82 | LOG(ERROR) << "failed to load IDMAP " << idmap_path; |
| 83 | return {}; |
| 84 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 85 | |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 86 | const std::string overlay_path(loaded_idmap->OverlayApkPath()); |
| 87 | auto overlay_assets = ZipAssetsProvider::Create(overlay_path); |
| 88 | if (overlay_assets == nullptr) { |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 89 | return {}; |
| 90 | } |
| 91 | |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 92 | return LoadImpl(std::move(overlay_assets), flags | PROPERTY_OVERLAY, std::move(idmap_asset), |
| 93 | std::move(loaded_idmap)); |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 96 | std::unique_ptr<ApkAssets> ApkAssets::LoadImpl(std::unique_ptr<AssetsProvider> assets, |
| 97 | package_property_t property_flags, |
| 98 | std::unique_ptr<Asset> idmap_asset, |
| 99 | std::unique_ptr<LoadedIdmap> loaded_idmap) { |
| 100 | if (assets == nullptr) { |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 101 | return {}; |
| 102 | } |
| 103 | |
Ryan Mitchell | 4ea1e42 | 2020-03-11 13:15:28 -0700 | [diff] [blame] | 104 | // Open the resource table via mmap unless it is compressed. This logic is taken care of by Open. |
| 105 | bool resources_asset_exists = false; |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 106 | auto resources_asset = assets->Open(kResourcesArsc, Asset::AccessMode::ACCESS_BUFFER, |
| 107 | &resources_asset_exists); |
| 108 | if (resources_asset == nullptr && resources_asset_exists) { |
| 109 | LOG(ERROR) << "Failed to open '" << kResourcesArsc << "' in APK '" << assets->GetDebugName() |
| 110 | << "'."; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 111 | return {}; |
| 112 | } |
| 113 | |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 114 | return LoadImpl(std::move(resources_asset), std::move(assets), property_flags, |
| 115 | std::move(idmap_asset), std::move(loaded_idmap)); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 118 | std::unique_ptr<ApkAssets> ApkAssets::LoadImpl(std::unique_ptr<Asset> resources_asset, |
| 119 | std::unique_ptr<AssetsProvider> assets, |
| 120 | package_property_t property_flags, |
| 121 | std::unique_ptr<Asset> idmap_asset, |
| 122 | std::unique_ptr<LoadedIdmap> loaded_idmap) { |
| 123 | if (assets == nullptr ) { |
Ryan Mitchell | d338dfe | 2019-02-23 15:33:08 +0800 | [diff] [blame] | 124 | return {}; |
| 125 | } |
| 126 | |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 127 | std::unique_ptr<LoadedArsc> loaded_arsc; |
| 128 | if (resources_asset != nullptr) { |
| 129 | const auto data = resources_asset->getIncFsBuffer(true /* aligned */); |
| 130 | const size_t length = resources_asset->getLength(); |
| 131 | if (!data || length == 0) { |
| 132 | LOG(ERROR) << "Failed to read resources table in APK '" << assets->GetDebugName() << "'."; |
| 133 | return {}; |
| 134 | } |
| 135 | loaded_arsc = LoadedArsc::Load(data, length, loaded_idmap.get(), property_flags); |
| 136 | } else { |
| 137 | loaded_arsc = LoadedArsc::CreateEmpty(); |
| 138 | } |
| 139 | |
| 140 | if (loaded_arsc == nullptr) { |
| 141 | LOG(ERROR) << "Failed to load resources table in APK '" << assets->GetDebugName() << "'."; |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 142 | return {}; |
| 143 | } |
| 144 | |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 145 | return std::unique_ptr<ApkAssets>(new ApkAssets(std::move(resources_asset), |
| 146 | std::move(loaded_arsc), std::move(assets), |
| 147 | property_flags, std::move(idmap_asset), |
| 148 | std::move(loaded_idmap))); |
| 149 | } |
| 150 | |
| 151 | const std::string& ApkAssets::GetPath() const { |
| 152 | return assets_provider_->GetDebugName(); |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Winson | b0085ce | 2019-02-19 12:48:22 -0800 | [diff] [blame] | 155 | bool ApkAssets::IsUpToDate() const { |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 156 | // Loaders are invalidated by the app, not the system, so assume they are up to date. |
| 157 | return IsLoader() || ((!loaded_idmap_ || loaded_idmap_->IsUpToDate()) |
| 158 | && assets_provider_->IsUpToDate()); |
Winson | b0085ce | 2019-02-19 12:48:22 -0800 | [diff] [blame] | 159 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 160 | } // namespace android |