blob: 16dbbf61351ad3975f299f8bcda076aca784e469 [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 Lesinskid1ecd7a2017-01-23 12:58:11 -080019#include <algorithm>
20
Adam Lesinski970bd8d2017-09-25 13:21:55 -070021#include "android-base/errors.h"
22#include "android-base/file.h"
Adam Lesinski7ad11102016-10-28 16:39:15 -070023#include "android-base/logging.h"
Adam Lesinski970bd8d2017-09-25 13:21:55 -070024#include "android-base/unique_fd.h"
25#include "android-base/utf8.h"
26#include "utils/Compat.h"
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -080027#include "utils/FileMap.h"
Adam Lesinski7ad11102016-10-28 16:39:15 -070028#include "ziparchive/zip_archive.h"
29
30#include "androidfw/Asset.h"
Adam Lesinski970bd8d2017-09-25 13:21:55 -070031#include "androidfw/Idmap.h"
Winsonb0085ce2019-02-19 12:48:22 -080032#include "androidfw/misc.h"
Adam Lesinski970bd8d2017-09-25 13:21:55 -070033#include "androidfw/ResourceTypes.h"
Adam Lesinski7ad11102016-10-28 16:39:15 -070034#include "androidfw/Util.h"
35
36namespace android {
37
Adam Lesinski970bd8d2017-09-25 13:21:55 -070038using base::SystemErrorCodeToString;
39using base::unique_fd;
40
41static const std::string kResourcesArsc("resources.arsc");
42
Winsonb0085ce2019-02-19 12:48:22 -080043ApkAssets::ApkAssets(ZipArchiveHandle unmanaged_handle,
44 const std::string& path,
Winson9947f1e2019-08-16 10:20:39 -070045 time_t last_mod_time,
46 bool for_loader)
47 : zip_handle_(unmanaged_handle, ::CloseArchive), path_(path), last_mod_time_(last_mod_time),
Ryan Mitchell8a891d82019-07-01 09:48:23 -070048 for_loader_(for_loader) {
Adam Lesinski970bd8d2017-09-25 13:21:55 -070049}
Adam Lesinski03ebac82017-09-25 13:10:14 -070050
Winson9947f1e2019-08-16 10:20:39 -070051std::unique_ptr<const ApkAssets> ApkAssets::Load(const std::string& path, bool system,
52 bool for_loader) {
53 return LoadImpl({} /*fd*/, path, nullptr, nullptr, system, false /*load_as_shared_library*/,
54 for_loader);
Adam Lesinskida431a22016-12-29 16:08:16 -050055}
56
Adam Lesinski0c405242017-01-13 20:47:26 -080057std::unique_ptr<const ApkAssets> ApkAssets::LoadAsSharedLibrary(const std::string& path,
58 bool system) {
Adam Lesinski441500b2017-11-13 17:52:25 -080059 return LoadImpl({} /*fd*/, path, nullptr, nullptr, system, true /*load_as_shared_library*/);
Adam Lesinskida431a22016-12-29 16:08:16 -050060}
61
Adam Lesinski970bd8d2017-09-25 13:21:55 -070062std::unique_ptr<const ApkAssets> ApkAssets::LoadOverlay(const std::string& idmap_path,
63 bool system) {
64 std::unique_ptr<Asset> idmap_asset = CreateAssetFromFile(idmap_path);
65 if (idmap_asset == nullptr) {
66 return {};
67 }
68
69 const StringPiece idmap_data(
70 reinterpret_cast<const char*>(idmap_asset->getBuffer(true /*wordAligned*/)),
71 static_cast<size_t>(idmap_asset->getLength()));
72 std::unique_ptr<const LoadedIdmap> loaded_idmap = LoadedIdmap::Load(idmap_data);
73 if (loaded_idmap == nullptr) {
74 LOG(ERROR) << "failed to load IDMAP " << idmap_path;
75 return {};
76 }
Adam Lesinski441500b2017-11-13 17:52:25 -080077 return LoadImpl({} /*fd*/, loaded_idmap->OverlayApkPath(), std::move(idmap_asset),
Ryan Mitchell8a891d82019-07-01 09:48:23 -070078 std::move(loaded_idmap), system, true /*load_as_shared_library*/);
Adam Lesinski441500b2017-11-13 17:52:25 -080079}
80
81std::unique_ptr<const ApkAssets> ApkAssets::LoadFromFd(unique_fd fd,
82 const std::string& friendly_name,
Winson9947f1e2019-08-16 10:20:39 -070083 bool system, bool force_shared_lib,
84 bool for_loader) {
Adam Lesinski441500b2017-11-13 17:52:25 -080085 return LoadImpl(std::move(fd), friendly_name, nullptr /*idmap_asset*/, nullptr /*loaded_idmap*/,
Winson9947f1e2019-08-16 10:20:39 -070086 system, force_shared_lib, for_loader);
87}
88
89std::unique_ptr<const ApkAssets> ApkAssets::LoadArsc(const std::string& path,
90 bool for_loader) {
91 return LoadArscImpl({} /*fd*/, path, for_loader);
92}
93
94std::unique_ptr<const ApkAssets> ApkAssets::LoadArsc(unique_fd fd,
95 const std::string& friendly_name,
96 bool for_loader) {
97 return LoadArscImpl(std::move(fd), friendly_name, for_loader);
Adam Lesinski970bd8d2017-09-25 13:21:55 -070098}
99
100std::unique_ptr<Asset> ApkAssets::CreateAssetFromFile(const std::string& path) {
101 unique_fd fd(base::utf8::open(path.c_str(), O_RDONLY | O_BINARY | O_CLOEXEC));
102 if (fd == -1) {
103 LOG(ERROR) << "Failed to open file '" << path << "': " << SystemErrorCodeToString(errno);
104 return {};
105 }
106
107 const off64_t file_len = lseek64(fd, 0, SEEK_END);
108 if (file_len < 0) {
109 LOG(ERROR) << "Failed to get size of file '" << path << "': " << SystemErrorCodeToString(errno);
110 return {};
111 }
112
113 std::unique_ptr<FileMap> file_map = util::make_unique<FileMap>();
114 if (!file_map->create(path.c_str(), fd, 0, static_cast<size_t>(file_len), true /*readOnly*/)) {
115 LOG(ERROR) << "Failed to mmap file '" << path << "': " << SystemErrorCodeToString(errno);
116 return {};
117 }
118 return Asset::createFromUncompressedMap(std::move(file_map), Asset::AccessMode::ACCESS_RANDOM);
119}
120
121std::unique_ptr<const ApkAssets> ApkAssets::LoadImpl(
Adam Lesinski441500b2017-11-13 17:52:25 -0800122 unique_fd fd, const std::string& path, std::unique_ptr<Asset> idmap_asset,
Winson9947f1e2019-08-16 10:20:39 -0700123 std::unique_ptr<const LoadedIdmap> loaded_idmap, bool system, bool load_as_shared_library,
124 bool for_loader) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700125 ::ZipArchiveHandle unmanaged_handle;
Adam Lesinski441500b2017-11-13 17:52:25 -0800126 int32_t result;
127 if (fd >= 0) {
128 result =
129 ::OpenArchiveFd(fd.release(), path.c_str(), &unmanaged_handle, true /*assume_ownership*/);
130 } else {
131 result = ::OpenArchive(path.c_str(), &unmanaged_handle);
132 }
133
Adam Lesinski7ad11102016-10-28 16:39:15 -0700134 if (result != 0) {
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700135 LOG(ERROR) << "Failed to open APK '" << path << "' " << ::ErrorCodeString(result);
Songchun Fan898b3162019-07-08 09:00:34 -0700136 ::CloseArchive(unmanaged_handle);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700137 return {};
138 }
139
Winsonb0085ce2019-02-19 12:48:22 -0800140 time_t last_mod_time = getFileModDate(path.c_str());
141
Adam Lesinski7ad11102016-10-28 16:39:15 -0700142 // Wrap the handle in a unique_ptr so it gets automatically closed.
Winson9947f1e2019-08-16 10:20:39 -0700143 std::unique_ptr<ApkAssets>
144 loaded_apk(new ApkAssets(unmanaged_handle, path, last_mod_time, for_loader));
Adam Lesinski7ad11102016-10-28 16:39:15 -0700145
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700146 // Find the resource table.
Adam Lesinski7ad11102016-10-28 16:39:15 -0700147 ::ZipEntry entry;
Elliott Hughesb97e7372019-05-03 22:42:31 -0700148 result = ::FindEntry(loaded_apk->zip_handle_.get(), kResourcesArsc, &entry);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700149 if (result != 0) {
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700150 // There is no resources.arsc, so create an empty LoadedArsc and return.
151 loaded_apk->loaded_arsc_ = LoadedArsc::CreateEmpty();
152 return std::move(loaded_apk);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700153 }
154
155 if (entry.method == kCompressDeflated) {
Ryan Mitchell31b11052019-06-13 13:47:26 -0700156 ANDROID_LOG(WARNING) << kResourcesArsc << " in APK '" << path << "' is compressed.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700157 }
158
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700159 // Open the resource table via mmap unless it is compressed. This logic is taken care of by Open.
160 loaded_apk->resources_asset_ = loaded_apk->Open(kResourcesArsc, Asset::AccessMode::ACCESS_BUFFER);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700161 if (loaded_apk->resources_asset_ == nullptr) {
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700162 LOG(ERROR) << "Failed to open '" << kResourcesArsc << "' in APK '" << path << "'.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700163 return {};
164 }
165
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700166 // Must retain ownership of the IDMAP Asset so that all pointers to its mmapped data remain valid.
167 loaded_apk->idmap_asset_ = std::move(idmap_asset);
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700168 loaded_apk->loaded_idmap_ = std::move(loaded_idmap);
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700169
170 const StringPiece data(
171 reinterpret_cast<const char*>(loaded_apk->resources_asset_->getBuffer(true /*wordAligned*/)),
172 loaded_apk->resources_asset_->getLength());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700173 loaded_apk->loaded_arsc_ =
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700174 LoadedArsc::Load(data, loaded_apk->loaded_idmap_.get(), system, load_as_shared_library,
175 for_loader);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700176 if (loaded_apk->loaded_arsc_ == nullptr) {
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700177 LOG(ERROR) << "Failed to load '" << kResourcesArsc << "' in APK '" << path << "'.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700178 return {};
179 }
Adam Lesinski0c405242017-01-13 20:47:26 -0800180
181 // Need to force a move for mingw32.
182 return std::move(loaded_apk);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700183}
184
Winson9947f1e2019-08-16 10:20:39 -0700185std::unique_ptr<const ApkAssets> ApkAssets::LoadArscImpl(unique_fd fd,
186 const std::string& path,
187 bool for_loader) {
188 std::unique_ptr<Asset> resources_asset;
189
190 if (fd >= 0) {
191 resources_asset = std::unique_ptr<Asset>(Asset::createFromFd(fd.release(), nullptr,
192 Asset::AccessMode::ACCESS_BUFFER));
193 } else {
194 resources_asset = CreateAssetFromFile(path);
195 }
196
197 if (resources_asset == nullptr) {
198 LOG(ERROR) << "Failed to open ARSC '" << path;
199 return {};
200 }
201
202 time_t last_mod_time = getFileModDate(path.c_str());
203
204 std::unique_ptr<ApkAssets> loaded_apk(new ApkAssets(nullptr, path, last_mod_time, for_loader));
205 loaded_apk->resources_asset_ = std::move(resources_asset);
206
207 const StringPiece data(
208 reinterpret_cast<const char*>(loaded_apk->resources_asset_->getBuffer(true /*wordAligned*/)),
209 loaded_apk->resources_asset_->getLength());
210 loaded_apk->loaded_arsc_ = LoadedArsc::Load(data, nullptr, false, false, for_loader);
211 if (loaded_apk->loaded_arsc_ == nullptr) {
212 LOG(ERROR) << "Failed to load '" << kResourcesArsc << path;
213 return {};
214 }
215
216 // Need to force a move for mingw32.
217 return std::move(loaded_apk);
218}
219
220std::unique_ptr<const ApkAssets> ApkAssets::LoadEmpty(bool for_loader) {
221 std::unique_ptr<ApkAssets> loaded_apk(new ApkAssets(nullptr, "", -1, for_loader));
222 loaded_apk->loaded_arsc_ = LoadedArsc::CreateEmpty();
223 // Need to force a move for mingw32.
224 return std::move(loaded_apk);
225}
226
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800227std::unique_ptr<Asset> ApkAssets::Open(const std::string& path, Asset::AccessMode mode) const {
Winson9947f1e2019-08-16 10:20:39 -0700228 // If this is a resource loader from an .arsc, there will be no zip handle
229 if (zip_handle_ == nullptr) {
230 return {};
231 }
Adam Lesinski7ad11102016-10-28 16:39:15 -0700232
Adam Lesinski7ad11102016-10-28 16:39:15 -0700233 ::ZipEntry entry;
Elliott Hughesb97e7372019-05-03 22:42:31 -0700234 int32_t result = ::FindEntry(zip_handle_.get(), path, &entry);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700235 if (result != 0) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700236 return {};
237 }
238
239 if (entry.method == kCompressDeflated) {
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800240 std::unique_ptr<FileMap> map = util::make_unique<FileMap>();
241 if (!map->create(path_.c_str(), ::GetFileDescriptor(zip_handle_.get()), entry.offset,
242 entry.compressed_length, true /*readOnly*/)) {
243 LOG(ERROR) << "Failed to mmap file '" << path << "' in APK '" << path_ << "'";
244 return {};
245 }
246
247 std::unique_ptr<Asset> asset =
248 Asset::createFromCompressedMap(std::move(map), entry.uncompressed_length, mode);
249 if (asset == nullptr) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700250 LOG(ERROR) << "Failed to decompress '" << path << "'.";
251 return {};
252 }
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800253 return asset;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700254 } else {
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800255 std::unique_ptr<FileMap> map = util::make_unique<FileMap>();
256 if (!map->create(path_.c_str(), ::GetFileDescriptor(zip_handle_.get()), entry.offset,
257 entry.uncompressed_length, true /*readOnly*/)) {
258 LOG(ERROR) << "Failed to mmap file '" << path << "' in APK '" << path_ << "'";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700259 return {};
260 }
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800261
262 std::unique_ptr<Asset> asset = Asset::createFromUncompressedMap(std::move(map), mode);
263 if (asset == nullptr) {
264 LOG(ERROR) << "Failed to mmap file '" << path << "' in APK '" << path_ << "'";
265 return {};
266 }
267 return asset;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700268 }
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800269}
270
271bool ApkAssets::ForEachFile(const std::string& root_path,
272 const std::function<void(const StringPiece&, FileType)>& f) const {
Winson9947f1e2019-08-16 10:20:39 -0700273 // If this is a resource loader from an .arsc, there will be no zip handle
274 if (zip_handle_ == nullptr) {
275 return false;
276 }
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800277
278 std::string root_path_full = root_path;
279 if (root_path_full.back() != '/') {
280 root_path_full += '/';
281 }
282
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800283 void* cookie;
Elliott Hughes7a6cc0c2019-05-08 12:12:39 -0700284 if (::StartIteration(zip_handle_.get(), &cookie, root_path_full, "") != 0) {
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800285 return false;
286 }
287
Elliott Hughes78de4f92019-06-14 15:28:38 -0700288 std::string name;
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800289 ::ZipEntry entry;
290
291 // We need to hold back directories because many paths will contain them and we want to only
292 // surface one.
293 std::set<std::string> dirs;
294
295 int32_t result;
296 while ((result = ::Next(cookie, &entry, &name)) == 0) {
Elliott Hughes78de4f92019-06-14 15:28:38 -0700297 StringPiece full_file_path(name);
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800298 StringPiece leaf_file_path = full_file_path.substr(root_path_full.size());
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800299
300 if (!leaf_file_path.empty()) {
301 auto iter = std::find(leaf_file_path.begin(), leaf_file_path.end(), '/');
302 if (iter != leaf_file_path.end()) {
303 std::string dir =
304 leaf_file_path.substr(0, std::distance(leaf_file_path.begin(), iter)).to_string();
305 dirs.insert(std::move(dir));
306 } else {
307 f(leaf_file_path, kFileTypeRegular);
308 }
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800309 }
310 }
311 ::EndIteration(cookie);
312
313 // Now present the unique directories.
314 for (const std::string& dir : dirs) {
315 f(dir, kFileTypeDirectory);
316 }
317
318 // -1 is end of iteration, anything else is an error.
319 return result == -1;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700320}
321
Winsonb0085ce2019-02-19 12:48:22 -0800322bool ApkAssets::IsUpToDate() const {
Winson9947f1e2019-08-16 10:20:39 -0700323 // Loaders are invalidated by the app, not the system, so assume up to date
Ryan Mitchell8a891d82019-07-01 09:48:23 -0700324 if (for_loader_) {
Winson9947f1e2019-08-16 10:20:39 -0700325 return true;
326 }
327
Winsonb0085ce2019-02-19 12:48:22 -0800328 return last_mod_time_ == getFileModDate(path_.c_str());
329}
330
Adam Lesinski7ad11102016-10-28 16:39:15 -0700331} // namespace android