blob: ca5981c0dd5cb83076123ca821ef80706cfbeb06 [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 Mitchell1a48fa62021-01-10 08:36:36 -080086 const std::string overlay_path(loaded_idmap->OverlayApkPath());
87 auto overlay_assets = ZipAssetsProvider::Create(overlay_path);
88 if (overlay_assets == nullptr) {
Ryan Mitchellef40d2e2020-03-11 10:26:08 -070089 return {};
90 }
91
Ryan Mitchell1a48fa62021-01-10 08:36:36 -080092 return LoadImpl(std::move(overlay_assets), flags | PROPERTY_OVERLAY, std::move(idmap_asset),
93 std::move(loaded_idmap));
Ryan Mitchellef40d2e2020-03-11 10:26:08 -070094}
95
Ryan Mitchell1a48fa62021-01-10 08:36:36 -080096std::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 Mitchellef40d2e2020-03-11 10:26:08 -0700101 return {};
102 }
103
Ryan Mitchell4ea1e422020-03-11 13:15:28 -0700104 // 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 Mitchell1a48fa62021-01-10 08:36:36 -0800106 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 Lesinski7ad11102016-10-28 16:39:15 -0700111 return {};
112 }
113
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800114 return LoadImpl(std::move(resources_asset), std::move(assets), property_flags,
115 std::move(idmap_asset), std::move(loaded_idmap));
Adam Lesinski7ad11102016-10-28 16:39:15 -0700116}
117
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800118std::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 Mitchelld338dfe2019-02-23 15:33:08 +0800124 return {};
125 }
126
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800127 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() << "'.";
Winson9947f1e2019-08-16 10:20:39 -0700142 return {};
143 }
144
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800145 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
151const std::string& ApkAssets::GetPath() const {
152 return assets_provider_->GetDebugName();
Winson9947f1e2019-08-16 10:20:39 -0700153}
154
Winsonb0085ce2019-02-19 12:48:22 -0800155bool ApkAssets::IsUpToDate() const {
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800156 // 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());
Winsonb0085ce2019-02-19 12:48:22 -0800159}
Adam Lesinski7ad11102016-10-28 16:39:15 -0700160} // namespace android