blob: 9f4a6193b434b9fa3638f6e7d48bb6f1f48b6b4c [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,
45 time_t last_mod_time)
46 : zip_handle_(unmanaged_handle, ::CloseArchive), path_(path), last_mod_time_(last_mod_time) {
Adam Lesinski970bd8d2017-09-25 13:21:55 -070047}
Adam Lesinski03ebac82017-09-25 13:10:14 -070048
Adam Lesinski0c405242017-01-13 20:47:26 -080049std::unique_ptr<const ApkAssets> ApkAssets::Load(const std::string& path, bool system) {
Adam Lesinski441500b2017-11-13 17:52:25 -080050 return LoadImpl({} /*fd*/, path, nullptr, nullptr, system, false /*load_as_shared_library*/);
Adam Lesinskida431a22016-12-29 16:08:16 -050051}
52
Adam Lesinski0c405242017-01-13 20:47:26 -080053std::unique_ptr<const ApkAssets> ApkAssets::LoadAsSharedLibrary(const std::string& path,
54 bool system) {
Adam Lesinski441500b2017-11-13 17:52:25 -080055 return LoadImpl({} /*fd*/, path, nullptr, nullptr, system, true /*load_as_shared_library*/);
Adam Lesinskida431a22016-12-29 16:08:16 -050056}
57
Adam Lesinski970bd8d2017-09-25 13:21:55 -070058std::unique_ptr<const ApkAssets> ApkAssets::LoadOverlay(const std::string& idmap_path,
59 bool system) {
60 std::unique_ptr<Asset> idmap_asset = CreateAssetFromFile(idmap_path);
61 if (idmap_asset == nullptr) {
62 return {};
63 }
64
65 const StringPiece idmap_data(
66 reinterpret_cast<const char*>(idmap_asset->getBuffer(true /*wordAligned*/)),
67 static_cast<size_t>(idmap_asset->getLength()));
68 std::unique_ptr<const LoadedIdmap> loaded_idmap = LoadedIdmap::Load(idmap_data);
69 if (loaded_idmap == nullptr) {
70 LOG(ERROR) << "failed to load IDMAP " << idmap_path;
71 return {};
72 }
Adam Lesinski441500b2017-11-13 17:52:25 -080073 return LoadImpl({} /*fd*/, loaded_idmap->OverlayApkPath(), std::move(idmap_asset),
74 std::move(loaded_idmap), system, false /*load_as_shared_library*/);
75}
76
77std::unique_ptr<const ApkAssets> ApkAssets::LoadFromFd(unique_fd fd,
78 const std::string& friendly_name,
79 bool system, bool force_shared_lib) {
80 return LoadImpl(std::move(fd), friendly_name, nullptr /*idmap_asset*/, nullptr /*loaded_idmap*/,
81 system, force_shared_lib);
Adam Lesinski970bd8d2017-09-25 13:21:55 -070082}
83
84std::unique_ptr<Asset> ApkAssets::CreateAssetFromFile(const std::string& path) {
85 unique_fd fd(base::utf8::open(path.c_str(), O_RDONLY | O_BINARY | O_CLOEXEC));
86 if (fd == -1) {
87 LOG(ERROR) << "Failed to open file '" << path << "': " << SystemErrorCodeToString(errno);
88 return {};
89 }
90
91 const off64_t file_len = lseek64(fd, 0, SEEK_END);
92 if (file_len < 0) {
93 LOG(ERROR) << "Failed to get size of file '" << path << "': " << SystemErrorCodeToString(errno);
94 return {};
95 }
96
97 std::unique_ptr<FileMap> file_map = util::make_unique<FileMap>();
98 if (!file_map->create(path.c_str(), fd, 0, static_cast<size_t>(file_len), true /*readOnly*/)) {
99 LOG(ERROR) << "Failed to mmap file '" << path << "': " << SystemErrorCodeToString(errno);
100 return {};
101 }
102 return Asset::createFromUncompressedMap(std::move(file_map), Asset::AccessMode::ACCESS_RANDOM);
103}
104
105std::unique_ptr<const ApkAssets> ApkAssets::LoadImpl(
Adam Lesinski441500b2017-11-13 17:52:25 -0800106 unique_fd fd, const std::string& path, std::unique_ptr<Asset> idmap_asset,
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700107 std::unique_ptr<const LoadedIdmap> loaded_idmap, bool system, bool load_as_shared_library) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700108 ::ZipArchiveHandle unmanaged_handle;
Adam Lesinski441500b2017-11-13 17:52:25 -0800109 int32_t result;
110 if (fd >= 0) {
111 result =
112 ::OpenArchiveFd(fd.release(), path.c_str(), &unmanaged_handle, true /*assume_ownership*/);
113 } else {
114 result = ::OpenArchive(path.c_str(), &unmanaged_handle);
115 }
116
Adam Lesinski7ad11102016-10-28 16:39:15 -0700117 if (result != 0) {
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700118 LOG(ERROR) << "Failed to open APK '" << path << "' " << ::ErrorCodeString(result);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700119 return {};
120 }
121
Winsonb0085ce2019-02-19 12:48:22 -0800122 time_t last_mod_time = getFileModDate(path.c_str());
123
Adam Lesinski7ad11102016-10-28 16:39:15 -0700124 // Wrap the handle in a unique_ptr so it gets automatically closed.
Winsonb0085ce2019-02-19 12:48:22 -0800125 std::unique_ptr<ApkAssets> loaded_apk(new ApkAssets(unmanaged_handle, path, last_mod_time));
Adam Lesinski7ad11102016-10-28 16:39:15 -0700126
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700127 // Find the resource table.
Adam Lesinski7ad11102016-10-28 16:39:15 -0700128 ::ZipEntry entry;
Elliott Hughesb97e7372019-05-03 22:42:31 -0700129 result = ::FindEntry(loaded_apk->zip_handle_.get(), kResourcesArsc, &entry);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700130 if (result != 0) {
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700131 // There is no resources.arsc, so create an empty LoadedArsc and return.
132 loaded_apk->loaded_arsc_ = LoadedArsc::CreateEmpty();
133 return std::move(loaded_apk);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700134 }
135
136 if (entry.method == kCompressDeflated) {
Ryan Mitchell31b11052019-06-13 13:47:26 -0700137 ANDROID_LOG(WARNING) << kResourcesArsc << " in APK '" << path << "' is compressed.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700138 }
139
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700140 // Open the resource table via mmap unless it is compressed. This logic is taken care of by Open.
141 loaded_apk->resources_asset_ = loaded_apk->Open(kResourcesArsc, Asset::AccessMode::ACCESS_BUFFER);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700142 if (loaded_apk->resources_asset_ == nullptr) {
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700143 LOG(ERROR) << "Failed to open '" << kResourcesArsc << "' in APK '" << path << "'.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700144 return {};
145 }
146
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700147 // Must retain ownership of the IDMAP Asset so that all pointers to its mmapped data remain valid.
148 loaded_apk->idmap_asset_ = std::move(idmap_asset);
149
150 const StringPiece data(
151 reinterpret_cast<const char*>(loaded_apk->resources_asset_->getBuffer(true /*wordAligned*/)),
152 loaded_apk->resources_asset_->getLength());
Adam Lesinski7ad11102016-10-28 16:39:15 -0700153 loaded_apk->loaded_arsc_ =
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700154 LoadedArsc::Load(data, loaded_idmap.get(), system, load_as_shared_library);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700155 if (loaded_apk->loaded_arsc_ == nullptr) {
Adam Lesinski970bd8d2017-09-25 13:21:55 -0700156 LOG(ERROR) << "Failed to load '" << kResourcesArsc << "' in APK '" << path << "'.";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700157 return {};
158 }
Adam Lesinski0c405242017-01-13 20:47:26 -0800159
160 // Need to force a move for mingw32.
161 return std::move(loaded_apk);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700162}
163
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800164std::unique_ptr<Asset> ApkAssets::Open(const std::string& path, Asset::AccessMode mode) const {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700165 CHECK(zip_handle_ != nullptr);
166
Adam Lesinski7ad11102016-10-28 16:39:15 -0700167 ::ZipEntry entry;
Elliott Hughesb97e7372019-05-03 22:42:31 -0700168 int32_t result = ::FindEntry(zip_handle_.get(), path, &entry);
Adam Lesinski7ad11102016-10-28 16:39:15 -0700169 if (result != 0) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700170 return {};
171 }
172
173 if (entry.method == kCompressDeflated) {
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800174 std::unique_ptr<FileMap> map = util::make_unique<FileMap>();
175 if (!map->create(path_.c_str(), ::GetFileDescriptor(zip_handle_.get()), entry.offset,
176 entry.compressed_length, true /*readOnly*/)) {
177 LOG(ERROR) << "Failed to mmap file '" << path << "' in APK '" << path_ << "'";
178 return {};
179 }
180
181 std::unique_ptr<Asset> asset =
182 Asset::createFromCompressedMap(std::move(map), entry.uncompressed_length, mode);
183 if (asset == nullptr) {
Adam Lesinski7ad11102016-10-28 16:39:15 -0700184 LOG(ERROR) << "Failed to decompress '" << path << "'.";
185 return {};
186 }
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800187 return asset;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700188 } else {
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800189 std::unique_ptr<FileMap> map = util::make_unique<FileMap>();
190 if (!map->create(path_.c_str(), ::GetFileDescriptor(zip_handle_.get()), entry.offset,
191 entry.uncompressed_length, true /*readOnly*/)) {
192 LOG(ERROR) << "Failed to mmap file '" << path << "' in APK '" << path_ << "'";
Adam Lesinski7ad11102016-10-28 16:39:15 -0700193 return {};
194 }
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800195
196 std::unique_ptr<Asset> asset = Asset::createFromUncompressedMap(std::move(map), mode);
197 if (asset == nullptr) {
198 LOG(ERROR) << "Failed to mmap file '" << path << "' in APK '" << path_ << "'";
199 return {};
200 }
201 return asset;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700202 }
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800203}
204
205bool ApkAssets::ForEachFile(const std::string& root_path,
206 const std::function<void(const StringPiece&, FileType)>& f) const {
207 CHECK(zip_handle_ != nullptr);
208
209 std::string root_path_full = root_path;
210 if (root_path_full.back() != '/') {
211 root_path_full += '/';
212 }
213
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800214 void* cookie;
Elliott Hughes7a6cc0c2019-05-08 12:12:39 -0700215 if (::StartIteration(zip_handle_.get(), &cookie, root_path_full, "") != 0) {
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800216 return false;
217 }
218
Elliott Hughes78de4f92019-06-14 15:28:38 -0700219 std::string name;
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800220 ::ZipEntry entry;
221
222 // We need to hold back directories because many paths will contain them and we want to only
223 // surface one.
224 std::set<std::string> dirs;
225
226 int32_t result;
227 while ((result = ::Next(cookie, &entry, &name)) == 0) {
Elliott Hughes78de4f92019-06-14 15:28:38 -0700228 StringPiece full_file_path(name);
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800229 StringPiece leaf_file_path = full_file_path.substr(root_path_full.size());
Adam Lesinskibebfcc42018-02-12 14:27:46 -0800230
231 if (!leaf_file_path.empty()) {
232 auto iter = std::find(leaf_file_path.begin(), leaf_file_path.end(), '/');
233 if (iter != leaf_file_path.end()) {
234 std::string dir =
235 leaf_file_path.substr(0, std::distance(leaf_file_path.begin(), iter)).to_string();
236 dirs.insert(std::move(dir));
237 } else {
238 f(leaf_file_path, kFileTypeRegular);
239 }
Adam Lesinskid1ecd7a2017-01-23 12:58:11 -0800240 }
241 }
242 ::EndIteration(cookie);
243
244 // Now present the unique directories.
245 for (const std::string& dir : dirs) {
246 f(dir, kFileTypeDirectory);
247 }
248
249 // -1 is end of iteration, anything else is an error.
250 return result == -1;
Adam Lesinski7ad11102016-10-28 16:39:15 -0700251}
252
Winsonb0085ce2019-02-19 12:48:22 -0800253bool ApkAssets::IsUpToDate() const {
254 return last_mod_time_ == getFileModDate(path_.c_str());
255}
256
Adam Lesinski7ad11102016-10-28 16:39:15 -0700257} // namespace android