blob: 9c743cea592a6c03d6df5980af826973d71b0e64 [file] [log] [blame]
Adam Lesinski7ad11102016-10-28 16:39:15 -07001/*
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 Lesinski7ad11102016-10-28 16:39:15 -070017#include "androidfw/ApkAssets.h"
18
Adam Lesinski970bd8d2017-09-25 13:21:55 -070019#include "android-base/errors.h"
Adam Lesinski7ad11102016-10-28 16:39:15 -070020#include "android-base/logging.h"
Adam Lesinski7ad11102016-10-28 16:39:15 -070021
22namespace android {
23
Adam Lesinski970bd8d2017-09-25 13:21:55 -070024using base::SystemErrorCodeToString;
25using base::unique_fd;
26
Ryan Mitchell1a48fa62021-01-10 08:36:36 -080027constexpr const char* kResourcesArsc = "resources.arsc";
Adam Lesinski970bd8d2017-09-25 13:21:55 -070028
Ryan Mitchell1a48fa62021-01-10 08:36:36 -080029ApkAssets::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
42std::unique_ptr<ApkAssets> ApkAssets::Load(const std::string& path, package_property_t flags) {
43 return Load(ZipAssetsProvider::Create(path), flags);
Adam Lesinski970bd8d2017-09-25 13:21:55 -070044}
Adam Lesinski03ebac82017-09-25 13:10:14 -070045
Ryan Mitchell1a48fa62021-01-10 08:36:36 -080046std::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 Lesinskida431a22016-12-29 16:08:16 -050052}
53
Ryan Mitchell1a48fa62021-01-10 08:36:36 -080054std::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 Mitchellef40d2e2020-03-11 10:26:08 -070057}
58
Ryan Mitchell1a48fa62021-01-10 08:36:36 -080059std::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 Mitchellef40d2e2020-03-11 10:26:08 -070067}
68
Ryan Mitchell1a48fa62021-01-10 08:36:36 -080069std::unique_ptr<ApkAssets> ApkAssets::LoadOverlay(const std::string& idmap_path,
70 package_property_t flags) {
Ryan Mitchellef40d2e2020-03-11 10:26:08 -070071 CHECK((flags & PROPERTY_LOADER) == 0U) << "Cannot load RROs through loaders";
Ryan Mitchell1a48fa62021-01-10 08:36:36 -080072 auto idmap_asset = AssetsProvider::CreateAssetFromFile(idmap_path);
Adam Lesinski970bd8d2017-09-25 13:21:55 -070073 if (idmap_asset == nullptr) {
Ryan Mitchell1a48fa62021-01-10 08:36:36 -080074 LOG(ERROR) << "failed to read IDMAP " << idmap_path;
Adam Lesinski970bd8d2017-09-25 13:21:55 -070075 return {};
76 }
77
Ryan Mitchell1a48fa62021-01-10 08:36:36 -080078 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 Lesinski970bd8d2017-09-25 13:21:55 -070081 if (loaded_idmap == nullptr) {
82 LOG(ERROR) << "failed to load IDMAP " << idmap_path;
83 return {};
84 }
Adam Lesinski7ad11102016-10-28 16:39:15 -070085
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080086 std::unique_ptr<AssetsProvider> overlay_assets;
Ryan Mitchell1a48fa62021-01-10 08:36:36 -080087 const std::string overlay_path(loaded_idmap->OverlayApkPath());
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080088 if (IsFabricatedOverlay(overlay_path)) {
89 // Fabricated overlays do not contain resource definitions. All of the overlay resource values
90 // are defined inline in the idmap.
91 overlay_assets = EmptyAssetsProvider::Create();
92 } else {
93 // The overlay should be an APK.
94 overlay_assets = ZipAssetsProvider::Create(overlay_path);
95 }
Ryan Mitchell1a48fa62021-01-10 08:36:36 -080096 if (overlay_assets == nullptr) {
Ryan Mitchellef40d2e2020-03-11 10:26:08 -070097 return {};
98 }
99
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800100 return LoadImpl(std::move(overlay_assets), flags | PROPERTY_OVERLAY, std::move(idmap_asset),
101 std::move(loaded_idmap));
Ryan Mitchellef40d2e2020-03-11 10:26:08 -0700102}
103
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800104std::unique_ptr<ApkAssets> ApkAssets::LoadImpl(std::unique_ptr<AssetsProvider> assets,
105 package_property_t property_flags,
106 std::unique_ptr<Asset> idmap_asset,
107 std::unique_ptr<LoadedIdmap> loaded_idmap) {
108 if (assets == nullptr) {
Ryan Mitchellef40d2e2020-03-11 10:26:08 -0700109 return {};
110 }
111
Ryan Mitchell4ea1e422020-03-11 13:15:28 -0700112 // Open the resource table via mmap unless it is compressed. This logic is taken care of by Open.
113 bool resources_asset_exists = false;
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800114 auto resources_asset = assets->Open(kResourcesArsc, Asset::AccessMode::ACCESS_BUFFER,
115 &resources_asset_exists);
116 if (resources_asset == nullptr && resources_asset_exists) {
117 LOG(ERROR) << "Failed to open '" << kResourcesArsc << "' in APK '" << assets->GetDebugName()
118 << "'.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700119 return {};
120 }
121
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800122 return LoadImpl(std::move(resources_asset), std::move(assets), property_flags,
123 std::move(idmap_asset), std::move(loaded_idmap));
Adam Lesinski7ad11102016-10-28 16:39:15 -0700124}
125
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800126std::unique_ptr<ApkAssets> ApkAssets::LoadImpl(std::unique_ptr<Asset> resources_asset,
127 std::unique_ptr<AssetsProvider> assets,
128 package_property_t property_flags,
129 std::unique_ptr<Asset> idmap_asset,
130 std::unique_ptr<LoadedIdmap> loaded_idmap) {
131 if (assets == nullptr ) {
Ryan Mitchelld338dfe2019-02-23 15:33:08 +0800132 return {};
133 }
134
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800135 std::unique_ptr<LoadedArsc> loaded_arsc;
136 if (resources_asset != nullptr) {
137 const auto data = resources_asset->getIncFsBuffer(true /* aligned */);
138 const size_t length = resources_asset->getLength();
139 if (!data || length == 0) {
140 LOG(ERROR) << "Failed to read resources table in APK '" << assets->GetDebugName() << "'.";
141 return {};
142 }
143 loaded_arsc = LoadedArsc::Load(data, length, loaded_idmap.get(), property_flags);
144 } else {
145 loaded_arsc = LoadedArsc::CreateEmpty();
146 }
147
148 if (loaded_arsc == nullptr) {
149 LOG(ERROR) << "Failed to load resources table in APK '" << assets->GetDebugName() << "'.";
Winson9947f1e2019-08-16 10:20:39 -0700150 return {};
151 }
152
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800153 return std::unique_ptr<ApkAssets>(new ApkAssets(std::move(resources_asset),
154 std::move(loaded_arsc), std::move(assets),
155 property_flags, std::move(idmap_asset),
156 std::move(loaded_idmap)));
157}
158
159const std::string& ApkAssets::GetPath() const {
160 return assets_provider_->GetDebugName();
Winson9947f1e2019-08-16 10:20:39 -0700161}
162
Winsonb0085ce2019-02-19 12:48:22 -0800163bool ApkAssets::IsUpToDate() const {
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800164 // Loaders are invalidated by the app, not the system, so assume they are up to date.
165 return IsLoader() || ((!loaded_idmap_ || loaded_idmap_->IsUpToDate())
166 && assets_provider_->IsUpToDate());
Winsonb0085ce2019-02-19 12:48:22 -0800167}
Adam Lesinski7ad11102016-10-28 16:39:15 -0700168} // namespace android