Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 1 | /* |
| 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 Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 17 | #include "androidfw/ApkAssets.h" |
| 18 | |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 19 | #include <algorithm> |
| 20 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 21 | #include "android-base/errors.h" |
| 22 | #include "android-base/file.h" |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 23 | #include "android-base/logging.h" |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 24 | #include "android-base/unique_fd.h" |
| 25 | #include "android-base/utf8.h" |
| 26 | #include "utils/Compat.h" |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 27 | #include "utils/FileMap.h" |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 28 | #include "ziparchive/zip_archive.h" |
| 29 | |
| 30 | #include "androidfw/Asset.h" |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 31 | #include "androidfw/Idmap.h" |
Winson | b0085ce | 2019-02-19 12:48:22 -0800 | [diff] [blame] | 32 | #include "androidfw/misc.h" |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 33 | #include "androidfw/ResourceTypes.h" |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 34 | #include "androidfw/Util.h" |
| 35 | |
| 36 | namespace android { |
| 37 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 38 | using base::SystemErrorCodeToString; |
| 39 | using base::unique_fd; |
| 40 | |
| 41 | static const std::string kResourcesArsc("resources.arsc"); |
| 42 | |
Winson | b0085ce | 2019-02-19 12:48:22 -0800 | [diff] [blame] | 43 | ApkAssets::ApkAssets(ZipArchiveHandle unmanaged_handle, |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 44 | std::string path, |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 45 | time_t last_mod_time, |
Ryan Mitchell | 73bfe41 | 2019-11-12 16:22:04 -0800 | [diff] [blame] | 46 | package_property_t property_flags) |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 47 | : zip_handle_(unmanaged_handle, ::CloseArchive), |
| 48 | path_(std::move(path)), |
| 49 | last_mod_time_(last_mod_time), |
Ryan Mitchell | 73bfe41 | 2019-11-12 16:22:04 -0800 | [diff] [blame] | 50 | property_flags_(property_flags) { |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 51 | } |
Adam Lesinski | 03ebac8 | 2017-09-25 13:10:14 -0700 | [diff] [blame] | 52 | |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 53 | std::unique_ptr<const ApkAssets> ApkAssets::Load(const std::string& path, |
| 54 | const package_property_t flags) { |
| 55 | ::ZipArchiveHandle unmanaged_handle; |
| 56 | const int32_t result = ::OpenArchive(path.c_str(), &unmanaged_handle); |
| 57 | if (result != 0) { |
| 58 | LOG(ERROR) << "Failed to open APK '" << path << "' " << ::ErrorCodeString(result); |
| 59 | ::CloseArchive(unmanaged_handle); |
| 60 | return {}; |
| 61 | } |
| 62 | |
| 63 | return LoadImpl(unmanaged_handle, path, nullptr /*idmap_asset*/, nullptr /*loaded_idmap*/, |
| 64 | flags); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 65 | } |
| 66 | |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 67 | std::unique_ptr<const ApkAssets> ApkAssets::LoadFromFd(unique_fd fd, |
| 68 | const std::string& friendly_name, |
| 69 | const package_property_t flags, |
| 70 | const off64_t offset, |
| 71 | const off64_t length) { |
| 72 | CHECK(length >= kUnknownLength) << "length must be greater than or equal to " << kUnknownLength; |
| 73 | CHECK(length != kUnknownLength || offset == 0) << "offset must be 0 if length is " |
| 74 | << kUnknownLength; |
| 75 | |
| 76 | ::ZipArchiveHandle unmanaged_handle; |
| 77 | const int32_t result = (length == kUnknownLength) |
| 78 | ? ::OpenArchiveFd(fd.release(), friendly_name.c_str(), &unmanaged_handle) |
| 79 | : ::OpenArchiveFdRange(fd.release(), friendly_name.c_str(), &unmanaged_handle, length, |
| 80 | offset); |
| 81 | |
| 82 | if (result != 0) { |
| 83 | LOG(ERROR) << "Failed to open APK '" << friendly_name << "' through FD with offset " << offset |
| 84 | << " and length " << length << ": " << ::ErrorCodeString(result); |
| 85 | ::CloseArchive(unmanaged_handle); |
| 86 | return {}; |
| 87 | } |
| 88 | |
| 89 | return LoadImpl(unmanaged_handle, friendly_name, nullptr /*idmap_asset*/, |
| 90 | nullptr /*loaded_idmap*/, flags); |
| 91 | } |
| 92 | |
| 93 | std::unique_ptr<const ApkAssets> ApkAssets::LoadTable(const std::string& path, |
| 94 | const package_property_t flags) { |
| 95 | auto resources_asset = CreateAssetFromFile(path); |
| 96 | if (!resources_asset) { |
| 97 | LOG(ERROR) << "Failed to open ARSC '" << path; |
| 98 | return {}; |
| 99 | } |
| 100 | |
| 101 | return LoadTableImpl(std::move(resources_asset), path, flags); |
| 102 | } |
| 103 | |
| 104 | std::unique_ptr<const ApkAssets> ApkAssets::LoadTableFromFd(unique_fd fd, |
| 105 | const std::string& friendly_name, |
| 106 | const package_property_t flags, |
| 107 | const off64_t offset, |
| 108 | const off64_t length) { |
| 109 | auto resources_asset = CreateAssetFromFd(std::move(fd), nullptr /* path */, offset, length); |
| 110 | if (!resources_asset) { |
| 111 | LOG(ERROR) << "Failed to open ARSC '" << friendly_name << "' through FD with offset " << offset |
| 112 | << " and length " << length; |
| 113 | return {}; |
| 114 | } |
| 115 | |
| 116 | return LoadTableImpl(std::move(resources_asset), friendly_name, flags); |
Adam Lesinski | da431a2 | 2016-12-29 16:08:16 -0500 | [diff] [blame] | 117 | } |
| 118 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 119 | std::unique_ptr<const ApkAssets> ApkAssets::LoadOverlay(const std::string& idmap_path, |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 120 | const package_property_t flags) { |
| 121 | CHECK((flags & PROPERTY_LOADER) == 0U) << "Cannot load RROs through loaders"; |
| 122 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 123 | std::unique_ptr<Asset> idmap_asset = CreateAssetFromFile(idmap_path); |
| 124 | if (idmap_asset == nullptr) { |
| 125 | return {}; |
| 126 | } |
| 127 | |
| 128 | const StringPiece idmap_data( |
| 129 | reinterpret_cast<const char*>(idmap_asset->getBuffer(true /*wordAligned*/)), |
| 130 | static_cast<size_t>(idmap_asset->getLength())); |
| 131 | std::unique_ptr<const LoadedIdmap> loaded_idmap = LoadedIdmap::Load(idmap_data); |
| 132 | if (loaded_idmap == nullptr) { |
| 133 | LOG(ERROR) << "failed to load IDMAP " << idmap_path; |
| 134 | return {}; |
| 135 | } |
Ryan Mitchell | 73bfe41 | 2019-11-12 16:22:04 -0800 | [diff] [blame] | 136 | |
Adam Lesinski | 441500b | 2017-11-13 17:52:25 -0800 | [diff] [blame] | 137 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 138 | ::ZipArchiveHandle unmanaged_handle; |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 139 | auto overlay_path = loaded_idmap->OverlayApkPath(); |
| 140 | const int32_t result = ::OpenArchive(overlay_path.c_str(), &unmanaged_handle); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 141 | if (result != 0) { |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 142 | LOG(ERROR) << "Failed to open overlay APK '" << overlay_path << "' " |
| 143 | << ::ErrorCodeString(result); |
Songchun Fan | 898b316 | 2019-07-08 09:00:34 -0700 | [diff] [blame] | 144 | ::CloseArchive(unmanaged_handle); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 145 | return {}; |
| 146 | } |
| 147 | |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 148 | return LoadImpl(unmanaged_handle, overlay_path, std::move(idmap_asset), std::move(loaded_idmap), |
| 149 | flags | PROPERTY_OVERLAY); |
| 150 | } |
| 151 | |
| 152 | std::unique_ptr<const ApkAssets> ApkAssets::LoadEmpty(const package_property_t flags) { |
| 153 | std::unique_ptr<ApkAssets> loaded_apk(new ApkAssets(nullptr, "empty", -1, flags)); |
| 154 | loaded_apk->loaded_arsc_ = LoadedArsc::CreateEmpty(); |
| 155 | // Need to force a move for mingw32. |
| 156 | return std::move(loaded_apk); |
| 157 | } |
| 158 | |
| 159 | std::unique_ptr<Asset> ApkAssets::CreateAssetFromFile(const std::string& path) { |
| 160 | unique_fd fd(base::utf8::open(path.c_str(), O_RDONLY | O_BINARY | O_CLOEXEC)); |
| 161 | if (!fd.ok()) { |
| 162 | LOG(ERROR) << "Failed to open file '" << path << "': " << SystemErrorCodeToString(errno); |
| 163 | return {}; |
| 164 | } |
| 165 | |
| 166 | return CreateAssetFromFd(std::move(fd), path.c_str()); |
| 167 | } |
| 168 | |
| 169 | std::unique_ptr<Asset> ApkAssets::CreateAssetFromFd(base::unique_fd fd, |
| 170 | const char* path, |
| 171 | off64_t offset, |
| 172 | off64_t length) { |
| 173 | CHECK(length >= kUnknownLength) << "length must be greater than or equal to " << kUnknownLength; |
| 174 | CHECK(length != kUnknownLength || offset == 0) << "offset must be 0 if length is " |
| 175 | << kUnknownLength; |
| 176 | if (length == kUnknownLength) { |
| 177 | length = lseek64(fd, 0, SEEK_END); |
| 178 | if (length < 0) { |
| 179 | LOG(ERROR) << "Failed to get size of file '" << ((path) ? path : "anon") << "': " |
| 180 | << SystemErrorCodeToString(errno); |
| 181 | return {}; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | std::unique_ptr<FileMap> file_map = util::make_unique<FileMap>(); |
| 186 | if (!file_map->create(path, fd, offset, static_cast<size_t>(length), true /*readOnly*/)) { |
| 187 | LOG(ERROR) << "Failed to mmap file '" << ((path) ? path : "anon") << "': " |
| 188 | << SystemErrorCodeToString(errno); |
| 189 | return {}; |
| 190 | } |
| 191 | |
| 192 | // If `path` is set, do not pass ownership of the `fd` to the new Asset since |
| 193 | // Asset::openFileDescriptor can use `path` to create new file descriptors. |
| 194 | return Asset::createFromUncompressedMap(std::move(file_map), |
| 195 | (path) ? base::unique_fd(-1) : std::move(fd), |
| 196 | Asset::AccessMode::ACCESS_RANDOM); |
| 197 | } |
| 198 | |
| 199 | std::unique_ptr<const ApkAssets> ApkAssets::LoadImpl(ZipArchiveHandle unmanaged_handle, |
| 200 | const std::string& path, |
| 201 | std::unique_ptr<Asset> idmap_asset, |
| 202 | std::unique_ptr<const LoadedIdmap> idmap, |
| 203 | package_property_t property_flags) { |
| 204 | const time_t last_mod_time = getFileModDate(path.c_str()); |
Winson | b0085ce | 2019-02-19 12:48:22 -0800 | [diff] [blame] | 205 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 206 | // Wrap the handle in a unique_ptr so it gets automatically closed. |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 207 | std::unique_ptr<ApkAssets> |
Ryan Mitchell | 73bfe41 | 2019-11-12 16:22:04 -0800 | [diff] [blame] | 208 | loaded_apk(new ApkAssets(unmanaged_handle, path, last_mod_time, property_flags)); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 209 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 210 | // Find the resource table. |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 211 | ::ZipEntry entry; |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 212 | int32_t result = ::FindEntry(loaded_apk->zip_handle_.get(), kResourcesArsc, &entry); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 213 | if (result != 0) { |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 214 | // There is no resources.arsc, so create an empty LoadedArsc and return. |
| 215 | loaded_apk->loaded_arsc_ = LoadedArsc::CreateEmpty(); |
| 216 | return std::move(loaded_apk); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | if (entry.method == kCompressDeflated) { |
Ryan Mitchell | 31b1105 | 2019-06-13 13:47:26 -0700 | [diff] [blame] | 220 | ANDROID_LOG(WARNING) << kResourcesArsc << " in APK '" << path << "' is compressed."; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 223 | // Open the resource table via mmap unless it is compressed. This logic is taken care of by Open. |
| 224 | loaded_apk->resources_asset_ = loaded_apk->Open(kResourcesArsc, Asset::AccessMode::ACCESS_BUFFER); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 225 | if (loaded_apk->resources_asset_ == nullptr) { |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 226 | LOG(ERROR) << "Failed to open '" << kResourcesArsc << "' in APK '" << path << "'."; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 227 | return {}; |
| 228 | } |
| 229 | |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 230 | // Must retain ownership of the IDMAP Asset so that all pointers to its mmapped data remain valid. |
| 231 | loaded_apk->idmap_asset_ = std::move(idmap_asset); |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 232 | loaded_apk->loaded_idmap_ = std::move(idmap); |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 233 | |
| 234 | const StringPiece data( |
| 235 | reinterpret_cast<const char*>(loaded_apk->resources_asset_->getBuffer(true /*wordAligned*/)), |
| 236 | loaded_apk->resources_asset_->getLength()); |
Ryan Mitchell | 73bfe41 | 2019-11-12 16:22:04 -0800 | [diff] [blame] | 237 | loaded_apk->loaded_arsc_ = LoadedArsc::Load(data, loaded_apk->loaded_idmap_.get(), |
| 238 | property_flags); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 239 | if (loaded_apk->loaded_arsc_ == nullptr) { |
Adam Lesinski | 970bd8d | 2017-09-25 13:21:55 -0700 | [diff] [blame] | 240 | LOG(ERROR) << "Failed to load '" << kResourcesArsc << "' in APK '" << path << "'."; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 241 | return {}; |
| 242 | } |
Adam Lesinski | 0c40524 | 2017-01-13 20:47:26 -0800 | [diff] [blame] | 243 | |
| 244 | // Need to force a move for mingw32. |
| 245 | return std::move(loaded_apk); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 248 | std::unique_ptr<const ApkAssets> ApkAssets::LoadTableImpl(std::unique_ptr<Asset> resources_asset, |
| 249 | const std::string& path, |
| 250 | package_property_t property_flags) { |
| 251 | const time_t last_mod_time = getFileModDate(path.c_str()); |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 252 | |
Ryan Mitchell | 73bfe41 | 2019-11-12 16:22:04 -0800 | [diff] [blame] | 253 | std::unique_ptr<ApkAssets> loaded_apk( |
| 254 | new ApkAssets(nullptr, path, last_mod_time, property_flags)); |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 255 | loaded_apk->resources_asset_ = std::move(resources_asset); |
| 256 | |
| 257 | const StringPiece data( |
| 258 | reinterpret_cast<const char*>(loaded_apk->resources_asset_->getBuffer(true /*wordAligned*/)), |
| 259 | loaded_apk->resources_asset_->getLength()); |
Ryan Mitchell | 73bfe41 | 2019-11-12 16:22:04 -0800 | [diff] [blame] | 260 | loaded_apk->loaded_arsc_ = LoadedArsc::Load(data, nullptr, property_flags); |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 261 | if (loaded_apk->loaded_arsc_ == nullptr) { |
| 262 | LOG(ERROR) << "Failed to load '" << kResourcesArsc << path; |
| 263 | return {}; |
| 264 | } |
| 265 | |
| 266 | // Need to force a move for mingw32. |
| 267 | return std::move(loaded_apk); |
| 268 | } |
| 269 | |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 270 | std::unique_ptr<Asset> ApkAssets::Open(const std::string& path, Asset::AccessMode mode) const { |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 271 | // If this is a resource loader from an .arsc, there will be no zip handle |
| 272 | if (zip_handle_ == nullptr) { |
| 273 | return {}; |
| 274 | } |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 275 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 276 | ::ZipEntry entry; |
Elliott Hughes | b97e737 | 2019-05-03 22:42:31 -0700 | [diff] [blame] | 277 | int32_t result = ::FindEntry(zip_handle_.get(), path, &entry); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 278 | if (result != 0) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 279 | return {}; |
| 280 | } |
| 281 | |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 282 | const int fd = ::GetFileDescriptor(zip_handle_.get()); |
| 283 | const off64_t fd_offset = ::GetFileDescriptorOffset(zip_handle_.get()); |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 284 | if (entry.method == kCompressDeflated) { |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 285 | std::unique_ptr<FileMap> map = util::make_unique<FileMap>(); |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 286 | if (!map->create(path_.c_str(), fd, fd_offset + entry.offset, entry.compressed_length, |
| 287 | true /*readOnly*/)) { |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 288 | LOG(ERROR) << "Failed to mmap file '" << path << "' in APK '" << path_ << "'"; |
| 289 | return {}; |
| 290 | } |
| 291 | |
| 292 | std::unique_ptr<Asset> asset = |
| 293 | Asset::createFromCompressedMap(std::move(map), entry.uncompressed_length, mode); |
| 294 | if (asset == nullptr) { |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 295 | LOG(ERROR) << "Failed to decompress '" << path << "'."; |
| 296 | return {}; |
| 297 | } |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 298 | return asset; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 299 | } else { |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 300 | std::unique_ptr<FileMap> map = util::make_unique<FileMap>(); |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 301 | if (!map->create(path_.c_str(), fd, fd_offset + entry.offset, entry.uncompressed_length, |
| 302 | true /*readOnly*/)) { |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 303 | LOG(ERROR) << "Failed to mmap file '" << path << "' in APK '" << path_ << "'"; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 304 | return {}; |
| 305 | } |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 306 | |
Ryan Mitchell | ef40d2e | 2020-03-11 10:26:08 -0700 | [diff] [blame] | 307 | // TODO: apks created from file descriptors residing in RAM currently cannot open file |
| 308 | // descriptors to the assets they contain. This is because the Asset::openFileDeescriptor uses |
| 309 | // the zip path on disk to create a new file descriptor. This is fixed in a future change |
| 310 | // in the change topic. |
| 311 | std::unique_ptr<Asset> asset = Asset::createFromUncompressedMap(std::move(map), |
| 312 | unique_fd(-1) /* fd*/, mode); |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 313 | if (asset == nullptr) { |
| 314 | LOG(ERROR) << "Failed to mmap file '" << path << "' in APK '" << path_ << "'"; |
| 315 | return {}; |
| 316 | } |
| 317 | return asset; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 318 | } |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | bool ApkAssets::ForEachFile(const std::string& root_path, |
| 322 | const std::function<void(const StringPiece&, FileType)>& f) const { |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 323 | // If this is a resource loader from an .arsc, there will be no zip handle |
| 324 | if (zip_handle_ == nullptr) { |
| 325 | return false; |
| 326 | } |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 327 | |
| 328 | std::string root_path_full = root_path; |
| 329 | if (root_path_full.back() != '/') { |
| 330 | root_path_full += '/'; |
| 331 | } |
| 332 | |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 333 | void* cookie; |
Elliott Hughes | 7a6cc0c | 2019-05-08 12:12:39 -0700 | [diff] [blame] | 334 | if (::StartIteration(zip_handle_.get(), &cookie, root_path_full, "") != 0) { |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 335 | return false; |
| 336 | } |
| 337 | |
Elliott Hughes | 78de4f9 | 2019-06-14 15:28:38 -0700 | [diff] [blame] | 338 | std::string name; |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 339 | ::ZipEntry entry; |
| 340 | |
| 341 | // We need to hold back directories because many paths will contain them and we want to only |
| 342 | // surface one. |
| 343 | std::set<std::string> dirs; |
| 344 | |
| 345 | int32_t result; |
| 346 | while ((result = ::Next(cookie, &entry, &name)) == 0) { |
Elliott Hughes | 78de4f9 | 2019-06-14 15:28:38 -0700 | [diff] [blame] | 347 | StringPiece full_file_path(name); |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 348 | StringPiece leaf_file_path = full_file_path.substr(root_path_full.size()); |
Adam Lesinski | bebfcc4 | 2018-02-12 14:27:46 -0800 | [diff] [blame] | 349 | |
| 350 | if (!leaf_file_path.empty()) { |
| 351 | auto iter = std::find(leaf_file_path.begin(), leaf_file_path.end(), '/'); |
| 352 | if (iter != leaf_file_path.end()) { |
| 353 | std::string dir = |
| 354 | leaf_file_path.substr(0, std::distance(leaf_file_path.begin(), iter)).to_string(); |
| 355 | dirs.insert(std::move(dir)); |
| 356 | } else { |
| 357 | f(leaf_file_path, kFileTypeRegular); |
| 358 | } |
Adam Lesinski | d1ecd7a | 2017-01-23 12:58:11 -0800 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | ::EndIteration(cookie); |
| 362 | |
| 363 | // Now present the unique directories. |
| 364 | for (const std::string& dir : dirs) { |
| 365 | f(dir, kFileTypeDirectory); |
| 366 | } |
| 367 | |
| 368 | // -1 is end of iteration, anything else is an error. |
| 369 | return result == -1; |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Winson | b0085ce | 2019-02-19 12:48:22 -0800 | [diff] [blame] | 372 | bool ApkAssets::IsUpToDate() const { |
Ryan Mitchell | 73bfe41 | 2019-11-12 16:22:04 -0800 | [diff] [blame] | 373 | if (IsLoader()) { |
| 374 | // Loaders are invalidated by the app, not the system, so assume up to date. |
Winson | 9947f1e | 2019-08-16 10:20:39 -0700 | [diff] [blame] | 375 | return true; |
| 376 | } |
| 377 | |
Winson | b0085ce | 2019-02-19 12:48:22 -0800 | [diff] [blame] | 378 | return last_mod_time_ == getFileModDate(path_.c_str()); |
| 379 | } |
| 380 | |
Adam Lesinski | 7ad1110 | 2016-10-28 16:39:15 -0700 | [diff] [blame] | 381 | } // namespace android |