Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 26 | namespace android { |
| 27 | namespace { |
| 28 | constexpr const char* kEmptyDebugString = "<empty>"; |
| 29 | } // namespace |
| 30 | |
| 31 | std::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 | |
| 36 | std::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 | |
| 46 | std::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 Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 76 | const std::string* ZipAssetsProvider::PathOrDebugName::GetPath() const { |
| 77 | return is_path_ ? &value_ : nullptr; |
| 78 | } |
| 79 | |
| 80 | const std::string& ZipAssetsProvider::PathOrDebugName::GetDebugName() const { |
| 81 | return value_; |
| 82 | } |
| 83 | |
Yurii Zubrytskyi | a5bc958 | 2022-11-30 23:53:59 -0800 | [diff] [blame] | 84 | void ZipAssetsProvider::ZipCloser::operator()(ZipArchive* a) const { |
| 85 | ::CloseArchive(a); |
| 86 | } |
| 87 | |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 88 | ZipAssetsProvider::ZipAssetsProvider(ZipArchiveHandle handle, PathOrDebugName&& path, |
Ryan Mitchell | c041669 | 2021-05-11 12:21:29 -0700 | [diff] [blame] | 89 | package_property_t flags, time_t last_mod_time) |
Yurii Zubrytskyi | a5bc958 | 2022-11-30 23:53:59 -0800 | [diff] [blame] | 90 | : zip_handle_(handle), |
| 91 | name_(std::move(path)), |
Ryan Mitchell | c041669 | 2021-05-11 12:21:29 -0700 | [diff] [blame] | 92 | flags_(flags), |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 93 | last_mod_time_(last_mod_time) {} |
| 94 | |
Ryan Mitchell | c041669 | 2021-05-11 12:21:29 -0700 | [diff] [blame] | 95 | std::unique_ptr<ZipAssetsProvider> ZipAssetsProvider::Create(std::string path, |
Yurii Zubrytskyi | 801c441 | 2022-11-30 16:47:23 -0800 | [diff] [blame] | 96 | package_property_t flags, |
| 97 | base::unique_fd fd) { |
| 98 | const auto released_fd = fd.ok() ? fd.release() : -1; |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 99 | ZipArchiveHandle handle; |
Yurii Zubrytskyi | 801c441 | 2022-11-30 16:47:23 -0800 | [diff] [blame] | 100 | if (int32_t result = released_fd < 0 ? OpenArchive(path.c_str(), &handle) |
| 101 | : OpenArchiveFd(released_fd, path.c_str(), &handle)) { |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 102 | LOG(ERROR) << "Failed to open APK '" << path << "': " << ::ErrorCodeString(result); |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 103 | CloseArchive(handle); |
| 104 | return {}; |
| 105 | } |
| 106 | |
| 107 | struct stat sb{.st_mtime = -1}; |
Yurii Zubrytskyi | 2ab4447 | 2022-11-30 00:56:22 -0800 | [diff] [blame] | 108 | // 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 Zubrytskyi | a5bc958 | 2022-11-30 23:53:59 -0800 | [diff] [blame] | 114 | PLOG(WARNING) << "Failed to stat file '" << path << "'"; |
Yurii Zubrytskyi | 2ab4447 | 2022-11-30 00:56:22 -0800 | [diff] [blame] | 115 | } |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | return std::unique_ptr<ZipAssetsProvider>( |
Yurii Zubrytskyi | a5bc958 | 2022-11-30 23:53:59 -0800 | [diff] [blame] | 119 | new ZipAssetsProvider(handle, PathOrDebugName::Path(std::move(path)), flags, sb.st_mtime)); |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | std::unique_ptr<ZipAssetsProvider> ZipAssetsProvider::Create(base::unique_fd fd, |
| 123 | std::string friendly_name, |
Ryan Mitchell | c041669 | 2021-05-11 12:21:29 -0700 | [diff] [blame] | 124 | package_property_t flags, |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 125 | 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 Zubrytskyi | 2ab4447 | 2022-11-30 00:56:22 -0800 | [diff] [blame] | 141 | // 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 Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 150 | } |
| 151 | |
Yurii Zubrytskyi | a5bc958 | 2022-11-30 23:53:59 -0800 | [diff] [blame] | 152 | return std::unique_ptr<ZipAssetsProvider>(new ZipAssetsProvider( |
| 153 | handle, PathOrDebugName::DebugName(std::move(friendly_name)), flags, sb.st_mtime)); |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | std::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 Mitchell | c041669 | 2021-05-11 12:21:29 -0700 | [diff] [blame] | 174 | const bool incremental_hardening = (flags_ & PROPERTY_DISABLE_INCREMENTAL_HARDENING) == 0U; |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 175 | incfs::IncFsFileMap asset_map; |
| 176 | if (entry.method == kCompressDeflated) { |
| 177 | if (!asset_map.Create(fd, entry.offset + fd_offset, entry.compressed_length, |
Ryan Mitchell | c041669 | 2021-05-11 12:21:29 -0700 | [diff] [blame] | 178 | name_.GetDebugName().c_str(), incremental_hardening)) { |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 179 | 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 Mitchell | c041669 | 2021-05-11 12:21:29 -0700 | [diff] [blame] | 195 | name_.GetDebugName().c_str(), incremental_hardening)) { |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 196 | 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 Zubrytskyi | a5bc958 | 2022-11-30 23:53:59 -0800 | [diff] [blame] | 220 | bool ZipAssetsProvider::ForEachFile( |
| 221 | const std::string& root_path, |
| 222 | base::function_ref<void(StringPiece, FileType)> f) const { |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 223 | 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 Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 248 | std::string dir(leaf_file_path.substr(0, std::distance(leaf_file_path.begin(), iter))); |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 249 | 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 Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 266 | std::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 Mitchell | ef53843 | 2021-03-01 14:52:14 -0800 | [diff] [blame] | 274 | std::optional<std::string_view> ZipAssetsProvider::GetPath() const { |
| 275 | if (name_.GetPath() != nullptr) { |
| 276 | return *name_.GetPath(); |
| 277 | } |
| 278 | return {}; |
| 279 | } |
| 280 | |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 281 | const std::string& ZipAssetsProvider::GetDebugName() const { |
| 282 | return name_.GetDebugName(); |
| 283 | } |
| 284 | |
| 285 | bool ZipAssetsProvider::IsUpToDate() const { |
Yurii Zubrytskyi | 2ab4447 | 2022-11-30 00:56:22 -0800 | [diff] [blame] | 286 | if (last_mod_time_ == -1) { |
| 287 | return true; |
| 288 | } |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 289 | 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 | |
| 298 | DirectoryAssetsProvider::DirectoryAssetsProvider(std::string&& path, time_t last_mod_time) |
Yurii Zubrytskyi | a5bc958 | 2022-11-30 23:53:59 -0800 | [diff] [blame] | 299 | : dir_(std::move(path)), last_mod_time_(last_mod_time) {} |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 300 | |
| 301 | std::unique_ptr<DirectoryAssetsProvider> DirectoryAssetsProvider::Create(std::string path) { |
Yurii Zubrytskyi | 2ab4447 | 2022-11-30 00:56:22 -0800 | [diff] [blame] | 302 | struct stat sb; |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 303 | 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 Zubrytskyi | a5bc958 | 2022-11-30 23:53:59 -0800 | [diff] [blame] | 314 | if (path.back() != OS_PATH_SEPARATOR) { |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 315 | path += OS_PATH_SEPARATOR; |
| 316 | } |
| 317 | |
Yurii Zubrytskyi | 2ab4447 | 2022-11-30 00:56:22 -0800 | [diff] [blame] | 318 | 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 Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | std::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 | |
| 335 | bool DirectoryAssetsProvider::ForEachFile( |
| 336 | const std::string& /* root_path */, |
Yurii Zubrytskyi | a5bc958 | 2022-11-30 23:53:59 -0800 | [diff] [blame] | 337 | base::function_ref<void(StringPiece, FileType)> /* f */) const { |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 338 | return true; |
| 339 | } |
| 340 | |
Ryan Mitchell | ef53843 | 2021-03-01 14:52:14 -0800 | [diff] [blame] | 341 | std::optional<std::string_view> DirectoryAssetsProvider::GetPath() const { |
| 342 | return dir_; |
| 343 | } |
| 344 | |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 345 | const std::string& DirectoryAssetsProvider::GetDebugName() const { |
| 346 | return dir_; |
| 347 | } |
| 348 | |
| 349 | bool DirectoryAssetsProvider::IsUpToDate() const { |
Yurii Zubrytskyi | 2ab4447 | 2022-11-30 00:56:22 -0800 | [diff] [blame] | 350 | if (last_mod_time_ == -1) { |
| 351 | return true; |
| 352 | } |
| 353 | struct stat sb; |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 354 | 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 | |
| 362 | MultiAssetsProvider::MultiAssetsProvider(std::unique_ptr<AssetsProvider>&& primary, |
| 363 | std::unique_ptr<AssetsProvider>&& secondary) |
Yurii Zubrytskyi | a5bc958 | 2022-11-30 23:53:59 -0800 | [diff] [blame] | 364 | : primary_(std::move(primary)), secondary_(std::move(secondary)) { |
Ryan Mitchell | ef53843 | 2021-03-01 14:52:14 -0800 | [diff] [blame] | 365 | debug_name_ = primary_->GetDebugName() + " and " + secondary_->GetDebugName(); |
| 366 | path_ = (primary_->GetDebugName() != kEmptyDebugString) ? primary_->GetPath() |
| 367 | : secondary_->GetPath(); |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | std::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 | |
| 379 | std::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 Zubrytskyi | a5bc958 | 2022-11-30 23:53:59 -0800 | [diff] [blame] | 386 | bool MultiAssetsProvider::ForEachFile( |
| 387 | const std::string& root_path, |
| 388 | base::function_ref<void(StringPiece, FileType)> f) const { |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 389 | return primary_->ForEachFile(root_path, f) && secondary_->ForEachFile(root_path, f); |
| 390 | } |
| 391 | |
Ryan Mitchell | ef53843 | 2021-03-01 14:52:14 -0800 | [diff] [blame] | 392 | std::optional<std::string_view> MultiAssetsProvider::GetPath() const { |
| 393 | return path_; |
| 394 | } |
| 395 | |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 396 | const std::string& MultiAssetsProvider::GetDebugName() const { |
| 397 | return debug_name_; |
| 398 | } |
| 399 | |
| 400 | bool MultiAssetsProvider::IsUpToDate() const { |
| 401 | return primary_->IsUpToDate() && secondary_->IsUpToDate(); |
| 402 | } |
| 403 | |
Ryan Mitchell | bdc0ae1 | 2021-03-01 15:18:15 -0800 | [diff] [blame] | 404 | EmptyAssetsProvider::EmptyAssetsProvider(std::optional<std::string>&& path) : |
| 405 | path_(std::move(path)) {} |
| 406 | |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 407 | std::unique_ptr<AssetsProvider> EmptyAssetsProvider::Create() { |
Ryan Mitchell | bdc0ae1 | 2021-03-01 15:18:15 -0800 | [diff] [blame] | 408 | return std::unique_ptr<EmptyAssetsProvider>(new EmptyAssetsProvider({})); |
| 409 | } |
| 410 | |
Yurii Zubrytskyi | d0c22cc | 2022-11-10 14:11:05 -0800 | [diff] [blame] | 411 | std::unique_ptr<AssetsProvider> EmptyAssetsProvider::Create(std::string path) { |
| 412 | return std::unique_ptr<EmptyAssetsProvider>(new EmptyAssetsProvider(std::move(path))); |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | std::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 | |
| 424 | bool EmptyAssetsProvider::ForEachFile( |
| 425 | const std::string& /* root_path */, |
Yurii Zubrytskyi | a5bc958 | 2022-11-30 23:53:59 -0800 | [diff] [blame] | 426 | base::function_ref<void(StringPiece, FileType)> /* f */) const { |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 427 | return true; |
| 428 | } |
| 429 | |
Ryan Mitchell | ef53843 | 2021-03-01 14:52:14 -0800 | [diff] [blame] | 430 | std::optional<std::string_view> EmptyAssetsProvider::GetPath() const { |
Ryan Mitchell | bdc0ae1 | 2021-03-01 15:18:15 -0800 | [diff] [blame] | 431 | if (path_.has_value()) { |
| 432 | return *path_; |
| 433 | } |
Ryan Mitchell | ef53843 | 2021-03-01 14:52:14 -0800 | [diff] [blame] | 434 | return {}; |
| 435 | } |
| 436 | |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 437 | const std::string& EmptyAssetsProvider::GetDebugName() const { |
Ryan Mitchell | bdc0ae1 | 2021-03-01 15:18:15 -0800 | [diff] [blame] | 438 | if (path_.has_value()) { |
| 439 | return *path_; |
| 440 | } |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 441 | const static std::string kEmpty = kEmptyDebugString; |
| 442 | return kEmpty; |
| 443 | } |
| 444 | |
| 445 | bool EmptyAssetsProvider::IsUpToDate() const { |
| 446 | return true; |
| 447 | } |
| 448 | |
Yurii Zubrytskyi | a5bc958 | 2022-11-30 23:53:59 -0800 | [diff] [blame] | 449 | } // namespace android |