blob: 2d3c06506a1fca16c4402ab762b18d5cbaaa67e5 [file] [log] [blame]
Ryan Mitchell1a48fa62021-01-10 08:36:36 -08001/*
2 * Copyright (C) 2021 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#include "androidfw/AssetsProvider.h"
18
19#include <sys/stat.h>
20
21#include <android-base/errors.h>
22#include <android-base/stringprintf.h>
23#include <android-base/utf8.h>
24#include <ziparchive/zip_archive.h>
25
26namespace android {
27namespace {
28constexpr const char* kEmptyDebugString = "<empty>";
29} // namespace
30
31std::unique_ptr<Asset> AssetsProvider::Open(const std::string& path, Asset::AccessMode mode,
32 bool* file_exists) const {
33 return OpenInternal(path, mode, file_exists);
34}
35
36std::unique_ptr<Asset> AssetsProvider::CreateAssetFromFile(const std::string& path) {
37 base::unique_fd fd(base::utf8::open(path.c_str(), O_RDONLY | O_CLOEXEC));
38 if (!fd.ok()) {
39 LOG(ERROR) << "Failed to open file '" << path << "': " << base::SystemErrorCodeToString(errno);
40 return {};
41 }
42
43 return CreateAssetFromFd(std::move(fd), path.c_str());
44}
45
46std::unique_ptr<Asset> AssetsProvider::CreateAssetFromFd(base::unique_fd fd,
47 const char* path,
48 off64_t offset,
49 off64_t length) {
50 CHECK(length >= kUnknownLength) << "length must be greater than or equal to " << kUnknownLength;
51 CHECK(length != kUnknownLength || offset == 0) << "offset must be 0 if length is "
52 << kUnknownLength;
53 if (length == kUnknownLength) {
54 length = lseek64(fd, 0, SEEK_END);
55 if (length < 0) {
56 LOG(ERROR) << "Failed to get size of file '" << ((path) ? path : "anon") << "': "
57 << base::SystemErrorCodeToString(errno);
58 return {};
59 }
60 }
61
62 incfs::IncFsFileMap file_map;
63 if (!file_map.Create(fd, offset, static_cast<size_t>(length), path)) {
64 LOG(ERROR) << "Failed to mmap file '" << ((path != nullptr) ? path : "anon") << "': "
65 << base::SystemErrorCodeToString(errno);
66 return {};
67 }
68
69 // If `path` is set, do not pass ownership of the `fd` to the new Asset since
70 // Asset::openFileDescriptor can use `path` to create new file descriptors.
71 return Asset::createFromUncompressedMap(std::move(file_map),
72 Asset::AccessMode::ACCESS_RANDOM,
73 (path != nullptr) ? base::unique_fd(-1) : std::move(fd));
74}
75
Ryan Mitchell1a48fa62021-01-10 08:36:36 -080076const std::string* ZipAssetsProvider::PathOrDebugName::GetPath() const {
77 return is_path_ ? &value_ : nullptr;
78}
79
80const std::string& ZipAssetsProvider::PathOrDebugName::GetDebugName() const {
81 return value_;
82}
83
Yurii Zubrytskyia5bc9582022-11-30 23:53:59 -080084void ZipAssetsProvider::ZipCloser::operator()(ZipArchive* a) const {
85 ::CloseArchive(a);
86}
87
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -080088ZipAssetsProvider::ZipAssetsProvider(ZipArchiveHandle handle, PathOrDebugName&& path,
Ryan Mitchellc0416692021-05-11 12:21:29 -070089 package_property_t flags, time_t last_mod_time)
Yurii Zubrytskyia5bc9582022-11-30 23:53:59 -080090 : zip_handle_(handle),
91 name_(std::move(path)),
Ryan Mitchellc0416692021-05-11 12:21:29 -070092 flags_(flags),
Ryan Mitchell1a48fa62021-01-10 08:36:36 -080093 last_mod_time_(last_mod_time) {}
94
Ryan Mitchellc0416692021-05-11 12:21:29 -070095std::unique_ptr<ZipAssetsProvider> ZipAssetsProvider::Create(std::string path,
Yurii Zubrytskyi801c4412022-11-30 16:47:23 -080096 package_property_t flags,
97 base::unique_fd fd) {
98 const auto released_fd = fd.ok() ? fd.release() : -1;
Ryan Mitchell1a48fa62021-01-10 08:36:36 -080099 ZipArchiveHandle handle;
Yurii Zubrytskyi801c4412022-11-30 16:47:23 -0800100 if (int32_t result = released_fd < 0 ? OpenArchive(path.c_str(), &handle)
101 : OpenArchiveFd(released_fd, path.c_str(), &handle)) {
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800102 LOG(ERROR) << "Failed to open APK '" << path << "': " << ::ErrorCodeString(result);
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800103 CloseArchive(handle);
104 return {};
105 }
106
107 struct stat sb{.st_mtime = -1};
Yurii Zubrytskyi2ab44472022-11-30 00:56:22 -0800108 // Skip all up-to-date checks if the file won't ever change.
109 if (!isReadonlyFilesystem(path.c_str())) {
110 if ((released_fd < 0 ? stat(path.c_str(), &sb) : fstat(released_fd, &sb)) < 0) {
111 // Stat requires execute permissions on all directories path to the file. If the process does
112 // not have execute permissions on this file, allow the zip to be opened but IsUpToDate() will
113 // always have to return true.
Yurii Zubrytskyia5bc9582022-11-30 23:53:59 -0800114 PLOG(WARNING) << "Failed to stat file '" << path << "'";
Yurii Zubrytskyi2ab44472022-11-30 00:56:22 -0800115 }
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800116 }
117
118 return std::unique_ptr<ZipAssetsProvider>(
Yurii Zubrytskyia5bc9582022-11-30 23:53:59 -0800119 new ZipAssetsProvider(handle, PathOrDebugName::Path(std::move(path)), flags, sb.st_mtime));
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800120}
121
122std::unique_ptr<ZipAssetsProvider> ZipAssetsProvider::Create(base::unique_fd fd,
123 std::string friendly_name,
Ryan Mitchellc0416692021-05-11 12:21:29 -0700124 package_property_t flags,
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800125 off64_t offset,
126 off64_t len) {
127 ZipArchiveHandle handle;
128 const int released_fd = fd.release();
129 const int32_t result = (len == AssetsProvider::kUnknownLength)
130 ? ::OpenArchiveFd(released_fd, friendly_name.c_str(), &handle)
131 : ::OpenArchiveFdRange(released_fd, friendly_name.c_str(), &handle, len, offset);
132
133 if (result != 0) {
134 LOG(ERROR) << "Failed to open APK '" << friendly_name << "' through FD with offset " << offset
135 << " and length " << len << ": " << ::ErrorCodeString(result);
136 CloseArchive(handle);
137 return {};
138 }
139
140 struct stat sb{.st_mtime = -1};
Yurii Zubrytskyi2ab44472022-11-30 00:56:22 -0800141 // Skip all up-to-date checks if the file won't ever change.
142 if (!isReadonlyFilesystem(released_fd)) {
143 if (fstat(released_fd, &sb) < 0) {
144 // Stat requires execute permissions on all directories path to the file. If the process does
145 // not have execute permissions on this file, allow the zip to be opened but IsUpToDate() will
146 // always have to return true.
147 LOG(WARNING) << "Failed to fstat file '" << friendly_name
148 << "': " << base::SystemErrorCodeToString(errno);
149 }
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800150 }
151
Yurii Zubrytskyia5bc9582022-11-30 23:53:59 -0800152 return std::unique_ptr<ZipAssetsProvider>(new ZipAssetsProvider(
153 handle, PathOrDebugName::DebugName(std::move(friendly_name)), flags, sb.st_mtime));
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800154}
155
156std::unique_ptr<Asset> ZipAssetsProvider::OpenInternal(const std::string& path,
157 Asset::AccessMode mode,
158 bool* file_exists) const {
159 if (file_exists != nullptr) {
160 *file_exists = false;
161 }
162
163 ZipEntry entry;
164 if (FindEntry(zip_handle_.get(), path, &entry) != 0) {
165 return {};
166 }
167
168 if (file_exists != nullptr) {
169 *file_exists = true;
170 }
171
172 const int fd = GetFileDescriptor(zip_handle_.get());
173 const off64_t fd_offset = GetFileDescriptorOffset(zip_handle_.get());
Ryan Mitchellc0416692021-05-11 12:21:29 -0700174 const bool incremental_hardening = (flags_ & PROPERTY_DISABLE_INCREMENTAL_HARDENING) == 0U;
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800175 incfs::IncFsFileMap asset_map;
176 if (entry.method == kCompressDeflated) {
177 if (!asset_map.Create(fd, entry.offset + fd_offset, entry.compressed_length,
Ryan Mitchellc0416692021-05-11 12:21:29 -0700178 name_.GetDebugName().c_str(), incremental_hardening)) {
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800179 LOG(ERROR) << "Failed to mmap file '" << path << "' in APK '" << name_.GetDebugName()
180 << "'";
181 return {};
182 }
183
184 std::unique_ptr<Asset> asset =
185 Asset::createFromCompressedMap(std::move(asset_map), entry.uncompressed_length, mode);
186 if (asset == nullptr) {
187 LOG(ERROR) << "Failed to decompress '" << path << "' in APK '" << name_.GetDebugName()
188 << "'";
189 return {};
190 }
191 return asset;
192 }
193
194 if (!asset_map.Create(fd, entry.offset + fd_offset, entry.uncompressed_length,
Ryan Mitchellc0416692021-05-11 12:21:29 -0700195 name_.GetDebugName().c_str(), incremental_hardening)) {
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800196 LOG(ERROR) << "Failed to mmap file '" << path << "' in APK '" << name_.GetDebugName() << "'";
197 return {};
198 }
199
200 base::unique_fd ufd;
201 if (name_.GetPath() == nullptr) {
202 // If the zip name does not represent a path, create a new `fd` for the new Asset to own in
203 // order to create new file descriptors using Asset::openFileDescriptor. If the zip name is a
204 // path, it will be used to create new file descriptors.
205 ufd = base::unique_fd(dup(fd));
206 if (!ufd.ok()) {
207 LOG(ERROR) << "Unable to dup fd '" << path << "' in APK '" << name_.GetDebugName() << "'";
208 return {};
209 }
210 }
211
212 auto asset = Asset::createFromUncompressedMap(std::move(asset_map), mode, std::move(ufd));
213 if (asset == nullptr) {
214 LOG(ERROR) << "Failed to mmap file '" << path << "' in APK '" << name_.GetDebugName() << "'";
215 return {};
216 }
217 return asset;
218}
219
Yurii Zubrytskyia5bc9582022-11-30 23:53:59 -0800220bool ZipAssetsProvider::ForEachFile(
221 const std::string& root_path,
222 base::function_ref<void(StringPiece, FileType)> f) const {
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800223 std::string root_path_full = root_path;
224 if (root_path_full.back() != '/') {
225 root_path_full += '/';
226 }
227
228 void* cookie;
229 if (StartIteration(zip_handle_.get(), &cookie, root_path_full, "") != 0) {
230 return false;
231 }
232
233 std::string name;
234 ::ZipEntry entry{};
235
236 // We need to hold back directories because many paths will contain them and we want to only
237 // surface one.
238 std::set<std::string> dirs{};
239
240 int32_t result;
241 while ((result = Next(cookie, &entry, &name)) == 0) {
242 StringPiece full_file_path(name);
243 StringPiece leaf_file_path = full_file_path.substr(root_path_full.size());
244
245 if (!leaf_file_path.empty()) {
246 auto iter = std::find(leaf_file_path.begin(), leaf_file_path.end(), '/');
247 if (iter != leaf_file_path.end()) {
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700248 std::string dir(leaf_file_path.substr(0, std::distance(leaf_file_path.begin(), iter)));
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800249 dirs.insert(std::move(dir));
250 } else {
251 f(leaf_file_path, kFileTypeRegular);
252 }
253 }
254 }
255 EndIteration(cookie);
256
257 // Now present the unique directories.
258 for (const std::string& dir : dirs) {
259 f(dir, kFileTypeDirectory);
260 }
261
262 // -1 is end of iteration, anything else is an error.
263 return result == -1;
264}
265
Ryan Mitchell2ed8bfa2021-01-08 13:34:28 -0800266std::optional<uint32_t> ZipAssetsProvider::GetCrc(std::string_view path) const {
267 ::ZipEntry entry;
268 if (FindEntry(zip_handle_.get(), path, &entry) != 0) {
269 return {};
270 }
271 return entry.crc32;
272}
273
Ryan Mitchellef538432021-03-01 14:52:14 -0800274std::optional<std::string_view> ZipAssetsProvider::GetPath() const {
275 if (name_.GetPath() != nullptr) {
276 return *name_.GetPath();
277 }
278 return {};
279}
280
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800281const std::string& ZipAssetsProvider::GetDebugName() const {
282 return name_.GetDebugName();
283}
284
285bool ZipAssetsProvider::IsUpToDate() const {
Yurii Zubrytskyi2ab44472022-11-30 00:56:22 -0800286 if (last_mod_time_ == -1) {
287 return true;
288 }
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800289 struct stat sb{};
290 if (fstat(GetFileDescriptor(zip_handle_.get()), &sb) < 0) {
291 // If fstat fails on the zip archive, return true so the zip archive the resource system does
292 // attempt to refresh the ApkAsset.
293 return true;
294 }
295 return last_mod_time_ == sb.st_mtime;
296}
297
298DirectoryAssetsProvider::DirectoryAssetsProvider(std::string&& path, time_t last_mod_time)
Yurii Zubrytskyia5bc9582022-11-30 23:53:59 -0800299 : dir_(std::move(path)), last_mod_time_(last_mod_time) {}
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800300
301std::unique_ptr<DirectoryAssetsProvider> DirectoryAssetsProvider::Create(std::string path) {
Yurii Zubrytskyi2ab44472022-11-30 00:56:22 -0800302 struct stat sb;
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800303 const int result = stat(path.c_str(), &sb);
304 if (result == -1) {
305 LOG(ERROR) << "Failed to find directory '" << path << "'.";
306 return nullptr;
307 }
308
309 if (!S_ISDIR(sb.st_mode)) {
310 LOG(ERROR) << "Path '" << path << "' is not a directory.";
311 return nullptr;
312 }
313
Yurii Zubrytskyia5bc9582022-11-30 23:53:59 -0800314 if (path.back() != OS_PATH_SEPARATOR) {
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800315 path += OS_PATH_SEPARATOR;
316 }
317
Yurii Zubrytskyi2ab44472022-11-30 00:56:22 -0800318 const bool isReadonly = isReadonlyFilesystem(path.c_str());
319 return std::unique_ptr<DirectoryAssetsProvider>(
320 new DirectoryAssetsProvider(std::move(path), isReadonly ? -1 : sb.st_mtime));
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800321}
322
323std::unique_ptr<Asset> DirectoryAssetsProvider::OpenInternal(const std::string& path,
324 Asset::AccessMode /* mode */,
325 bool* file_exists) const {
326 const std::string resolved_path = dir_ + path;
327 if (file_exists != nullptr) {
328 struct stat sb{};
329 *file_exists = (stat(resolved_path.c_str(), &sb) != -1) && S_ISREG(sb.st_mode);
330 }
331
332 return CreateAssetFromFile(resolved_path);
333}
334
335bool DirectoryAssetsProvider::ForEachFile(
336 const std::string& /* root_path */,
Yurii Zubrytskyia5bc9582022-11-30 23:53:59 -0800337 base::function_ref<void(StringPiece, FileType)> /* f */) const {
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800338 return true;
339}
340
Ryan Mitchellef538432021-03-01 14:52:14 -0800341std::optional<std::string_view> DirectoryAssetsProvider::GetPath() const {
342 return dir_;
343}
344
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800345const std::string& DirectoryAssetsProvider::GetDebugName() const {
346 return dir_;
347}
348
349bool DirectoryAssetsProvider::IsUpToDate() const {
Yurii Zubrytskyi2ab44472022-11-30 00:56:22 -0800350 if (last_mod_time_ == -1) {
351 return true;
352 }
353 struct stat sb;
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800354 if (stat(dir_.c_str(), &sb) < 0) {
355 // If stat fails on the zip archive, return true so the zip archive the resource system does
356 // attempt to refresh the ApkAsset.
357 return true;
358 }
359 return last_mod_time_ == sb.st_mtime;
360}
361
362MultiAssetsProvider::MultiAssetsProvider(std::unique_ptr<AssetsProvider>&& primary,
363 std::unique_ptr<AssetsProvider>&& secondary)
Yurii Zubrytskyia5bc9582022-11-30 23:53:59 -0800364 : primary_(std::move(primary)), secondary_(std::move(secondary)) {
Ryan Mitchellef538432021-03-01 14:52:14 -0800365 debug_name_ = primary_->GetDebugName() + " and " + secondary_->GetDebugName();
366 path_ = (primary_->GetDebugName() != kEmptyDebugString) ? primary_->GetPath()
367 : secondary_->GetPath();
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800368}
369
370std::unique_ptr<AssetsProvider> MultiAssetsProvider::Create(
371 std::unique_ptr<AssetsProvider>&& primary, std::unique_ptr<AssetsProvider>&& secondary) {
372 if (primary == nullptr || secondary == nullptr) {
373 return nullptr;
374 }
375 return std::unique_ptr<MultiAssetsProvider>(new MultiAssetsProvider(std::move(primary),
376 std::move(secondary)));
377}
378
379std::unique_ptr<Asset> MultiAssetsProvider::OpenInternal(const std::string& path,
380 Asset::AccessMode mode,
381 bool* file_exists) const {
382 auto asset = primary_->Open(path, mode, file_exists);
383 return (asset) ? std::move(asset) : secondary_->Open(path, mode, file_exists);
384}
385
Yurii Zubrytskyia5bc9582022-11-30 23:53:59 -0800386bool MultiAssetsProvider::ForEachFile(
387 const std::string& root_path,
388 base::function_ref<void(StringPiece, FileType)> f) const {
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800389 return primary_->ForEachFile(root_path, f) && secondary_->ForEachFile(root_path, f);
390}
391
Ryan Mitchellef538432021-03-01 14:52:14 -0800392std::optional<std::string_view> MultiAssetsProvider::GetPath() const {
393 return path_;
394}
395
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800396const std::string& MultiAssetsProvider::GetDebugName() const {
397 return debug_name_;
398}
399
400bool MultiAssetsProvider::IsUpToDate() const {
401 return primary_->IsUpToDate() && secondary_->IsUpToDate();
402}
403
Ryan Mitchellbdc0ae12021-03-01 15:18:15 -0800404EmptyAssetsProvider::EmptyAssetsProvider(std::optional<std::string>&& path) :
405 path_(std::move(path)) {}
406
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800407std::unique_ptr<AssetsProvider> EmptyAssetsProvider::Create() {
Ryan Mitchellbdc0ae12021-03-01 15:18:15 -0800408 return std::unique_ptr<EmptyAssetsProvider>(new EmptyAssetsProvider({}));
409}
410
Yurii Zubrytskyid0c22cc2022-11-10 14:11:05 -0800411std::unique_ptr<AssetsProvider> EmptyAssetsProvider::Create(std::string path) {
412 return std::unique_ptr<EmptyAssetsProvider>(new EmptyAssetsProvider(std::move(path)));
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800413}
414
415std::unique_ptr<Asset> EmptyAssetsProvider::OpenInternal(const std::string& /* path */,
416 Asset::AccessMode /* mode */,
417 bool* file_exists) const {
418 if (file_exists) {
419 *file_exists = false;
420 }
421 return nullptr;
422}
423
424bool EmptyAssetsProvider::ForEachFile(
425 const std::string& /* root_path */,
Yurii Zubrytskyia5bc9582022-11-30 23:53:59 -0800426 base::function_ref<void(StringPiece, FileType)> /* f */) const {
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800427 return true;
428}
429
Ryan Mitchellef538432021-03-01 14:52:14 -0800430std::optional<std::string_view> EmptyAssetsProvider::GetPath() const {
Ryan Mitchellbdc0ae12021-03-01 15:18:15 -0800431 if (path_.has_value()) {
432 return *path_;
433 }
Ryan Mitchellef538432021-03-01 14:52:14 -0800434 return {};
435}
436
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800437const std::string& EmptyAssetsProvider::GetDebugName() const {
Ryan Mitchellbdc0ae12021-03-01 15:18:15 -0800438 if (path_.has_value()) {
439 return *path_;
440 }
Ryan Mitchell1a48fa62021-01-10 08:36:36 -0800441 const static std::string kEmpty = kEmptyDebugString;
442 return kEmpty;
443}
444
445bool EmptyAssetsProvider::IsUpToDate() const {
446 return true;
447}
448
Yurii Zubrytskyia5bc9582022-11-30 23:53:59 -0800449} // namespace android