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 { |
Yurii Zubrytskyi | 36147b0 | 2025-02-06 15:41:54 -0800 | [diff] [blame] | 27 | |
| 28 | static constexpr std::string_view kEmptyDebugString = "<empty>"; |
| 29 | |
| 30 | std::unique_ptr<AssetsProvider> AssetsProvider::CreateWithOverride( |
| 31 | std::unique_ptr<AssetsProvider> provider, std::unique_ptr<AssetsProvider> override) { |
| 32 | if (provider == nullptr) { |
| 33 | return {}; |
| 34 | } |
| 35 | if (override == nullptr) { |
| 36 | return provider; |
| 37 | } |
| 38 | return MultiAssetsProvider::Create(std::move(override), std::move(provider)); |
| 39 | } |
| 40 | |
| 41 | std::unique_ptr<AssetsProvider> AssetsProvider::CreateFromNullable( |
| 42 | std::unique_ptr<AssetsProvider> nullable) { |
| 43 | if (nullable) { |
| 44 | return nullable; |
| 45 | } |
| 46 | return EmptyAssetsProvider::Create(); |
| 47 | } |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 48 | |
| 49 | std::unique_ptr<Asset> AssetsProvider::Open(const std::string& path, Asset::AccessMode mode, |
| 50 | bool* file_exists) const { |
| 51 | return OpenInternal(path, mode, file_exists); |
| 52 | } |
| 53 | |
| 54 | std::unique_ptr<Asset> AssetsProvider::CreateAssetFromFile(const std::string& path) { |
| 55 | base::unique_fd fd(base::utf8::open(path.c_str(), O_RDONLY | O_CLOEXEC)); |
| 56 | if (!fd.ok()) { |
| 57 | LOG(ERROR) << "Failed to open file '" << path << "': " << base::SystemErrorCodeToString(errno); |
| 58 | return {}; |
| 59 | } |
| 60 | |
| 61 | return CreateAssetFromFd(std::move(fd), path.c_str()); |
| 62 | } |
| 63 | |
| 64 | std::unique_ptr<Asset> AssetsProvider::CreateAssetFromFd(base::unique_fd fd, |
| 65 | const char* path, |
| 66 | off64_t offset, |
| 67 | off64_t length) { |
| 68 | CHECK(length >= kUnknownLength) << "length must be greater than or equal to " << kUnknownLength; |
| 69 | CHECK(length != kUnknownLength || offset == 0) << "offset must be 0 if length is " |
| 70 | << kUnknownLength; |
| 71 | if (length == kUnknownLength) { |
| 72 | length = lseek64(fd, 0, SEEK_END); |
| 73 | if (length < 0) { |
| 74 | LOG(ERROR) << "Failed to get size of file '" << ((path) ? path : "anon") << "': " |
| 75 | << base::SystemErrorCodeToString(errno); |
| 76 | return {}; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | incfs::IncFsFileMap file_map; |
| 81 | if (!file_map.Create(fd, offset, static_cast<size_t>(length), path)) { |
| 82 | LOG(ERROR) << "Failed to mmap file '" << ((path != nullptr) ? path : "anon") << "': " |
| 83 | << base::SystemErrorCodeToString(errno); |
| 84 | return {}; |
| 85 | } |
| 86 | |
| 87 | // If `path` is set, do not pass ownership of the `fd` to the new Asset since |
| 88 | // Asset::openFileDescriptor can use `path` to create new file descriptors. |
| 89 | return Asset::createFromUncompressedMap(std::move(file_map), |
| 90 | Asset::AccessMode::ACCESS_RANDOM, |
| 91 | (path != nullptr) ? base::unique_fd(-1) : std::move(fd)); |
| 92 | } |
| 93 | |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 94 | const std::string* ZipAssetsProvider::PathOrDebugName::GetPath() const { |
| 95 | return is_path_ ? &value_ : nullptr; |
| 96 | } |
| 97 | |
| 98 | const std::string& ZipAssetsProvider::PathOrDebugName::GetDebugName() const { |
| 99 | return value_; |
| 100 | } |
| 101 | |
Yurii Zubrytskyi | a5bc958 | 2022-11-30 23:53:59 -0800 | [diff] [blame] | 102 | void ZipAssetsProvider::ZipCloser::operator()(ZipArchive* a) const { |
| 103 | ::CloseArchive(a); |
| 104 | } |
| 105 | |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 106 | ZipAssetsProvider::ZipAssetsProvider(ZipArchiveHandle handle, PathOrDebugName&& path, |
Yurii Zubrytskyi | 1144bca | 2025-04-17 16:31:27 -0700 | [diff] [blame] | 107 | package_property_t flags, time_t last_mod_time) |
| 108 | : zip_handle_(handle), name_(std::move(path)), flags_(flags), last_mod_time_(last_mod_time) { |
| 109 | LOG(ERROR) << "This function is not supported and will result in " |
| 110 | "poor performance and/or crashes. Stop calling it."; |
| 111 | } |
| 112 | |
| 113 | ZipAssetsProvider::ZipAssetsProvider(ZipArchiveHandle handle, PathOrDebugName&& path, |
| 114 | ModDate last_mod_time, package_property_t flags) |
Yurii Zubrytskyi | 548c1ab | 2025-02-06 15:41:54 -0800 | [diff] [blame] | 115 | : zip_handle_(handle), name_(std::move(path)), flags_(flags), last_mod_time_(last_mod_time) { |
| 116 | } |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 117 | |
Ryan Mitchell | c041669 | 2021-05-11 12:21:29 -0700 | [diff] [blame] | 118 | std::unique_ptr<ZipAssetsProvider> ZipAssetsProvider::Create(std::string path, |
Yurii Zubrytskyi | 801c441 | 2022-11-30 16:47:23 -0800 | [diff] [blame] | 119 | package_property_t flags, |
| 120 | base::unique_fd fd) { |
| 121 | const auto released_fd = fd.ok() ? fd.release() : -1; |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 122 | ZipArchiveHandle handle; |
Yurii Zubrytskyi | 801c441 | 2022-11-30 16:47:23 -0800 | [diff] [blame] | 123 | if (int32_t result = released_fd < 0 ? OpenArchive(path.c_str(), &handle) |
| 124 | : OpenArchiveFd(released_fd, path.c_str(), &handle)) { |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 125 | LOG(ERROR) << "Failed to open APK '" << path << "': " << ::ErrorCodeString(result); |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 126 | CloseArchive(handle); |
| 127 | return {}; |
| 128 | } |
| 129 | |
Yurii Zubrytskyi | 548c1ab | 2025-02-06 15:41:54 -0800 | [diff] [blame] | 130 | ModDate mod_date = kInvalidModDate; |
Yurii Zubrytskyi | 2ab4447 | 2022-11-30 00:56:22 -0800 | [diff] [blame] | 131 | // Skip all up-to-date checks if the file won't ever change. |
Yurii Zubrytskyi | 548c1ab | 2025-02-06 15:41:54 -0800 | [diff] [blame] | 132 | if (isKnownWritablePath(path.c_str()) || !isReadonlyFilesystem(GetFileDescriptor(handle))) { |
| 133 | if (mod_date = getFileModDate(GetFileDescriptor(handle)); mod_date == kInvalidModDate) { |
Yurii Zubrytskyi | 2ab4447 | 2022-11-30 00:56:22 -0800 | [diff] [blame] | 134 | // Stat requires execute permissions on all directories path to the file. If the process does |
| 135 | // not have execute permissions on this file, allow the zip to be opened but IsUpToDate() will |
| 136 | // always have to return true. |
Yurii Zubrytskyi | a5bc958 | 2022-11-30 23:53:59 -0800 | [diff] [blame] | 137 | PLOG(WARNING) << "Failed to stat file '" << path << "'"; |
Yurii Zubrytskyi | 2ab4447 | 2022-11-30 00:56:22 -0800 | [diff] [blame] | 138 | } |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | return std::unique_ptr<ZipAssetsProvider>( |
Yurii Zubrytskyi | 1144bca | 2025-04-17 16:31:27 -0700 | [diff] [blame] | 142 | new ZipAssetsProvider(handle, PathOrDebugName::Path(std::move(path)), mod_date, flags)); |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | std::unique_ptr<ZipAssetsProvider> ZipAssetsProvider::Create(base::unique_fd fd, |
| 146 | std::string friendly_name, |
Ryan Mitchell | c041669 | 2021-05-11 12:21:29 -0700 | [diff] [blame] | 147 | package_property_t flags, |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 148 | off64_t offset, |
| 149 | off64_t len) { |
| 150 | ZipArchiveHandle handle; |
| 151 | const int released_fd = fd.release(); |
| 152 | const int32_t result = (len == AssetsProvider::kUnknownLength) |
| 153 | ? ::OpenArchiveFd(released_fd, friendly_name.c_str(), &handle) |
| 154 | : ::OpenArchiveFdRange(released_fd, friendly_name.c_str(), &handle, len, offset); |
| 155 | |
| 156 | if (result != 0) { |
| 157 | LOG(ERROR) << "Failed to open APK '" << friendly_name << "' through FD with offset " << offset |
| 158 | << " and length " << len << ": " << ::ErrorCodeString(result); |
| 159 | CloseArchive(handle); |
| 160 | return {}; |
| 161 | } |
| 162 | |
Yurii Zubrytskyi | 548c1ab | 2025-02-06 15:41:54 -0800 | [diff] [blame] | 163 | ModDate mod_date = kInvalidModDate; |
Yurii Zubrytskyi | 2ab4447 | 2022-11-30 00:56:22 -0800 | [diff] [blame] | 164 | // Skip all up-to-date checks if the file won't ever change. |
| 165 | if (!isReadonlyFilesystem(released_fd)) { |
Yurii Zubrytskyi | 548c1ab | 2025-02-06 15:41:54 -0800 | [diff] [blame] | 166 | if (mod_date = getFileModDate(released_fd); mod_date == kInvalidModDate) { |
Yurii Zubrytskyi | 2ab4447 | 2022-11-30 00:56:22 -0800 | [diff] [blame] | 167 | // Stat requires execute permissions on all directories path to the file. If the process does |
| 168 | // not have execute permissions on this file, allow the zip to be opened but IsUpToDate() will |
| 169 | // always have to return true. |
| 170 | LOG(WARNING) << "Failed to fstat file '" << friendly_name |
| 171 | << "': " << base::SystemErrorCodeToString(errno); |
| 172 | } |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 173 | } |
| 174 | |
Yurii Zubrytskyi | a5bc958 | 2022-11-30 23:53:59 -0800 | [diff] [blame] | 175 | return std::unique_ptr<ZipAssetsProvider>(new ZipAssetsProvider( |
Yurii Zubrytskyi | 1144bca | 2025-04-17 16:31:27 -0700 | [diff] [blame] | 176 | handle, PathOrDebugName::DebugName(std::move(friendly_name)), mod_date, flags)); |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | std::unique_ptr<Asset> ZipAssetsProvider::OpenInternal(const std::string& path, |
| 180 | Asset::AccessMode mode, |
| 181 | bool* file_exists) const { |
| 182 | if (file_exists != nullptr) { |
| 183 | *file_exists = false; |
| 184 | } |
| 185 | |
| 186 | ZipEntry entry; |
| 187 | if (FindEntry(zip_handle_.get(), path, &entry) != 0) { |
| 188 | return {}; |
| 189 | } |
| 190 | |
| 191 | if (file_exists != nullptr) { |
| 192 | *file_exists = true; |
| 193 | } |
| 194 | |
| 195 | const int fd = GetFileDescriptor(zip_handle_.get()); |
| 196 | const off64_t fd_offset = GetFileDescriptorOffset(zip_handle_.get()); |
Ryan Mitchell | c041669 | 2021-05-11 12:21:29 -0700 | [diff] [blame] | 197 | const bool incremental_hardening = (flags_ & PROPERTY_DISABLE_INCREMENTAL_HARDENING) == 0U; |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 198 | incfs::IncFsFileMap asset_map; |
| 199 | if (entry.method == kCompressDeflated) { |
| 200 | if (!asset_map.Create(fd, entry.offset + fd_offset, entry.compressed_length, |
Ryan Mitchell | c041669 | 2021-05-11 12:21:29 -0700 | [diff] [blame] | 201 | name_.GetDebugName().c_str(), incremental_hardening)) { |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 202 | LOG(ERROR) << "Failed to mmap file '" << path << "' in APK '" << name_.GetDebugName() |
| 203 | << "'"; |
| 204 | return {}; |
| 205 | } |
| 206 | |
| 207 | std::unique_ptr<Asset> asset = |
| 208 | Asset::createFromCompressedMap(std::move(asset_map), entry.uncompressed_length, mode); |
| 209 | if (asset == nullptr) { |
| 210 | LOG(ERROR) << "Failed to decompress '" << path << "' in APK '" << name_.GetDebugName() |
| 211 | << "'"; |
| 212 | return {}; |
| 213 | } |
| 214 | return asset; |
| 215 | } |
| 216 | |
| 217 | if (!asset_map.Create(fd, entry.offset + fd_offset, entry.uncompressed_length, |
Ryan Mitchell | c041669 | 2021-05-11 12:21:29 -0700 | [diff] [blame] | 218 | name_.GetDebugName().c_str(), incremental_hardening)) { |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 219 | LOG(ERROR) << "Failed to mmap file '" << path << "' in APK '" << name_.GetDebugName() << "'"; |
| 220 | return {}; |
| 221 | } |
| 222 | |
| 223 | base::unique_fd ufd; |
| 224 | if (name_.GetPath() == nullptr) { |
| 225 | // If the zip name does not represent a path, create a new `fd` for the new Asset to own in |
| 226 | // order to create new file descriptors using Asset::openFileDescriptor. If the zip name is a |
| 227 | // path, it will be used to create new file descriptors. |
| 228 | ufd = base::unique_fd(dup(fd)); |
| 229 | if (!ufd.ok()) { |
| 230 | LOG(ERROR) << "Unable to dup fd '" << path << "' in APK '" << name_.GetDebugName() << "'"; |
| 231 | return {}; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | auto asset = Asset::createFromUncompressedMap(std::move(asset_map), mode, std::move(ufd)); |
| 236 | if (asset == nullptr) { |
| 237 | LOG(ERROR) << "Failed to mmap file '" << path << "' in APK '" << name_.GetDebugName() << "'"; |
| 238 | return {}; |
| 239 | } |
| 240 | return asset; |
| 241 | } |
| 242 | |
Yurii Zubrytskyi | a5bc958 | 2022-11-30 23:53:59 -0800 | [diff] [blame] | 243 | bool ZipAssetsProvider::ForEachFile( |
| 244 | const std::string& root_path, |
| 245 | base::function_ref<void(StringPiece, FileType)> f) const { |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 246 | std::string root_path_full = root_path; |
| 247 | if (root_path_full.back() != '/') { |
| 248 | root_path_full += '/'; |
| 249 | } |
| 250 | |
| 251 | void* cookie; |
| 252 | if (StartIteration(zip_handle_.get(), &cookie, root_path_full, "") != 0) { |
| 253 | return false; |
| 254 | } |
| 255 | |
| 256 | std::string name; |
| 257 | ::ZipEntry entry{}; |
| 258 | |
| 259 | // We need to hold back directories because many paths will contain them and we want to only |
| 260 | // surface one. |
| 261 | std::set<std::string> dirs{}; |
| 262 | |
| 263 | int32_t result; |
| 264 | while ((result = Next(cookie, &entry, &name)) == 0) { |
| 265 | StringPiece full_file_path(name); |
| 266 | StringPiece leaf_file_path = full_file_path.substr(root_path_full.size()); |
| 267 | |
| 268 | if (!leaf_file_path.empty()) { |
| 269 | auto iter = std::find(leaf_file_path.begin(), leaf_file_path.end(), '/'); |
| 270 | if (iter != leaf_file_path.end()) { |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 271 | 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] | 272 | dirs.insert(std::move(dir)); |
| 273 | } else { |
| 274 | f(leaf_file_path, kFileTypeRegular); |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | EndIteration(cookie); |
| 279 | |
| 280 | // Now present the unique directories. |
| 281 | for (const std::string& dir : dirs) { |
| 282 | f(dir, kFileTypeDirectory); |
| 283 | } |
| 284 | |
| 285 | // -1 is end of iteration, anything else is an error. |
| 286 | return result == -1; |
| 287 | } |
| 288 | |
Ryan Mitchell | 2ed8bfa | 2021-01-08 13:34:28 -0800 | [diff] [blame] | 289 | std::optional<uint32_t> ZipAssetsProvider::GetCrc(std::string_view path) const { |
| 290 | ::ZipEntry entry; |
| 291 | if (FindEntry(zip_handle_.get(), path, &entry) != 0) { |
| 292 | return {}; |
| 293 | } |
| 294 | return entry.crc32; |
| 295 | } |
| 296 | |
Ryan Mitchell | ef53843 | 2021-03-01 14:52:14 -0800 | [diff] [blame] | 297 | std::optional<std::string_view> ZipAssetsProvider::GetPath() const { |
| 298 | if (name_.GetPath() != nullptr) { |
| 299 | return *name_.GetPath(); |
| 300 | } |
| 301 | return {}; |
| 302 | } |
| 303 | |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 304 | const std::string& ZipAssetsProvider::GetDebugName() const { |
| 305 | return name_.GetDebugName(); |
| 306 | } |
| 307 | |
Yurii Zubrytskyi | 548c1ab | 2025-02-06 15:41:54 -0800 | [diff] [blame] | 308 | UpToDate ZipAssetsProvider::IsUpToDate() const { |
| 309 | if (last_mod_time_ == kInvalidModDate) { |
| 310 | return UpToDate::Always; |
Yurii Zubrytskyi | 2ab4447 | 2022-11-30 00:56:22 -0800 | [diff] [blame] | 311 | } |
Yurii Zubrytskyi | 548c1ab | 2025-02-06 15:41:54 -0800 | [diff] [blame] | 312 | return fromBool(last_mod_time_ == getFileModDate(GetFileDescriptor(zip_handle_.get()))); |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 313 | } |
| 314 | |
Yurii Zubrytskyi | 548c1ab | 2025-02-06 15:41:54 -0800 | [diff] [blame] | 315 | DirectoryAssetsProvider::DirectoryAssetsProvider(std::string&& path, ModDate last_mod_time) |
| 316 | : dir_(std::move(path)), last_mod_time_(last_mod_time) { |
| 317 | } |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 318 | |
| 319 | std::unique_ptr<DirectoryAssetsProvider> DirectoryAssetsProvider::Create(std::string path) { |
Yurii Zubrytskyi | 2ab4447 | 2022-11-30 00:56:22 -0800 | [diff] [blame] | 320 | struct stat sb; |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 321 | const int result = stat(path.c_str(), &sb); |
| 322 | if (result == -1) { |
| 323 | LOG(ERROR) << "Failed to find directory '" << path << "'."; |
| 324 | return nullptr; |
| 325 | } |
| 326 | |
| 327 | if (!S_ISDIR(sb.st_mode)) { |
| 328 | LOG(ERROR) << "Path '" << path << "' is not a directory."; |
| 329 | return nullptr; |
| 330 | } |
| 331 | |
Yurii Zubrytskyi | a5bc958 | 2022-11-30 23:53:59 -0800 | [diff] [blame] | 332 | if (path.back() != OS_PATH_SEPARATOR) { |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 333 | path += OS_PATH_SEPARATOR; |
| 334 | } |
| 335 | |
Yurii Zubrytskyi | 2ab4447 | 2022-11-30 00:56:22 -0800 | [diff] [blame] | 336 | const bool isReadonly = isReadonlyFilesystem(path.c_str()); |
| 337 | return std::unique_ptr<DirectoryAssetsProvider>( |
Yurii Zubrytskyi | 548c1ab | 2025-02-06 15:41:54 -0800 | [diff] [blame] | 338 | new DirectoryAssetsProvider(std::move(path), isReadonly ? kInvalidModDate : getModDate(sb))); |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | std::unique_ptr<Asset> DirectoryAssetsProvider::OpenInternal(const std::string& path, |
| 342 | Asset::AccessMode /* mode */, |
| 343 | bool* file_exists) const { |
| 344 | const std::string resolved_path = dir_ + path; |
| 345 | if (file_exists != nullptr) { |
| 346 | struct stat sb{}; |
| 347 | *file_exists = (stat(resolved_path.c_str(), &sb) != -1) && S_ISREG(sb.st_mode); |
| 348 | } |
| 349 | |
| 350 | return CreateAssetFromFile(resolved_path); |
| 351 | } |
| 352 | |
| 353 | bool DirectoryAssetsProvider::ForEachFile( |
| 354 | const std::string& /* root_path */, |
Yurii Zubrytskyi | a5bc958 | 2022-11-30 23:53:59 -0800 | [diff] [blame] | 355 | base::function_ref<void(StringPiece, FileType)> /* f */) const { |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 356 | return true; |
| 357 | } |
| 358 | |
Ryan Mitchell | ef53843 | 2021-03-01 14:52:14 -0800 | [diff] [blame] | 359 | std::optional<std::string_view> DirectoryAssetsProvider::GetPath() const { |
| 360 | return dir_; |
| 361 | } |
| 362 | |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 363 | const std::string& DirectoryAssetsProvider::GetDebugName() const { |
| 364 | return dir_; |
| 365 | } |
| 366 | |
Yurii Zubrytskyi | 548c1ab | 2025-02-06 15:41:54 -0800 | [diff] [blame] | 367 | UpToDate DirectoryAssetsProvider::IsUpToDate() const { |
| 368 | if (last_mod_time_ == kInvalidModDate) { |
| 369 | return UpToDate::Always; |
Yurii Zubrytskyi | 2ab4447 | 2022-11-30 00:56:22 -0800 | [diff] [blame] | 370 | } |
Yurii Zubrytskyi | 548c1ab | 2025-02-06 15:41:54 -0800 | [diff] [blame] | 371 | return fromBool(last_mod_time_ == getFileModDate(dir_.c_str())); |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | MultiAssetsProvider::MultiAssetsProvider(std::unique_ptr<AssetsProvider>&& primary, |
| 375 | std::unique_ptr<AssetsProvider>&& secondary) |
Yurii Zubrytskyi | a5bc958 | 2022-11-30 23:53:59 -0800 | [diff] [blame] | 376 | : primary_(std::move(primary)), secondary_(std::move(secondary)) { |
Ryan Mitchell | ef53843 | 2021-03-01 14:52:14 -0800 | [diff] [blame] | 377 | debug_name_ = primary_->GetDebugName() + " and " + secondary_->GetDebugName(); |
| 378 | path_ = (primary_->GetDebugName() != kEmptyDebugString) ? primary_->GetPath() |
| 379 | : secondary_->GetPath(); |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | std::unique_ptr<AssetsProvider> MultiAssetsProvider::Create( |
| 383 | std::unique_ptr<AssetsProvider>&& primary, std::unique_ptr<AssetsProvider>&& secondary) { |
ESWAR MAGATAPALLI (xWF) | eb759ec | 2025-01-08 05:57:41 -0800 | [diff] [blame] | 384 | if (primary == nullptr || secondary == nullptr) { |
| 385 | return nullptr; |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 386 | } |
| 387 | return std::unique_ptr<MultiAssetsProvider>(new MultiAssetsProvider(std::move(primary), |
| 388 | std::move(secondary))); |
| 389 | } |
| 390 | |
| 391 | std::unique_ptr<Asset> MultiAssetsProvider::OpenInternal(const std::string& path, |
| 392 | Asset::AccessMode mode, |
| 393 | bool* file_exists) const { |
| 394 | auto asset = primary_->Open(path, mode, file_exists); |
| 395 | return (asset) ? std::move(asset) : secondary_->Open(path, mode, file_exists); |
| 396 | } |
| 397 | |
Yurii Zubrytskyi | a5bc958 | 2022-11-30 23:53:59 -0800 | [diff] [blame] | 398 | bool MultiAssetsProvider::ForEachFile( |
| 399 | const std::string& root_path, |
| 400 | base::function_ref<void(StringPiece, FileType)> f) const { |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 401 | return primary_->ForEachFile(root_path, f) && secondary_->ForEachFile(root_path, f); |
| 402 | } |
| 403 | |
Ryan Mitchell | ef53843 | 2021-03-01 14:52:14 -0800 | [diff] [blame] | 404 | std::optional<std::string_view> MultiAssetsProvider::GetPath() const { |
| 405 | return path_; |
| 406 | } |
| 407 | |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 408 | const std::string& MultiAssetsProvider::GetDebugName() const { |
| 409 | return debug_name_; |
| 410 | } |
| 411 | |
Yurii Zubrytskyi | 548c1ab | 2025-02-06 15:41:54 -0800 | [diff] [blame] | 412 | UpToDate MultiAssetsProvider::IsUpToDate() const { |
| 413 | return combine(primary_->IsUpToDate(), [this] { return secondary_->IsUpToDate(); }); |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 414 | } |
| 415 | |
Ryan Mitchell | bdc0ae1 | 2021-03-01 15:18:15 -0800 | [diff] [blame] | 416 | EmptyAssetsProvider::EmptyAssetsProvider(std::optional<std::string>&& path) : |
| 417 | path_(std::move(path)) {} |
| 418 | |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 419 | std::unique_ptr<AssetsProvider> EmptyAssetsProvider::Create() { |
Ryan Mitchell | bdc0ae1 | 2021-03-01 15:18:15 -0800 | [diff] [blame] | 420 | return std::unique_ptr<EmptyAssetsProvider>(new EmptyAssetsProvider({})); |
| 421 | } |
| 422 | |
Yurii Zubrytskyi | d0c22cc | 2022-11-10 14:11:05 -0800 | [diff] [blame] | 423 | std::unique_ptr<AssetsProvider> EmptyAssetsProvider::Create(std::string path) { |
| 424 | return std::unique_ptr<EmptyAssetsProvider>(new EmptyAssetsProvider(std::move(path))); |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | std::unique_ptr<Asset> EmptyAssetsProvider::OpenInternal(const std::string& /* path */, |
| 428 | Asset::AccessMode /* mode */, |
| 429 | bool* file_exists) const { |
| 430 | if (file_exists) { |
| 431 | *file_exists = false; |
| 432 | } |
| 433 | return nullptr; |
| 434 | } |
| 435 | |
| 436 | bool EmptyAssetsProvider::ForEachFile( |
| 437 | const std::string& /* root_path */, |
Yurii Zubrytskyi | a5bc958 | 2022-11-30 23:53:59 -0800 | [diff] [blame] | 438 | base::function_ref<void(StringPiece, FileType)> /* f */) const { |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 439 | return true; |
| 440 | } |
| 441 | |
Ryan Mitchell | ef53843 | 2021-03-01 14:52:14 -0800 | [diff] [blame] | 442 | std::optional<std::string_view> EmptyAssetsProvider::GetPath() const { |
Ryan Mitchell | bdc0ae1 | 2021-03-01 15:18:15 -0800 | [diff] [blame] | 443 | if (path_.has_value()) { |
| 444 | return *path_; |
| 445 | } |
Ryan Mitchell | ef53843 | 2021-03-01 14:52:14 -0800 | [diff] [blame] | 446 | return {}; |
| 447 | } |
| 448 | |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 449 | const std::string& EmptyAssetsProvider::GetDebugName() const { |
Ryan Mitchell | bdc0ae1 | 2021-03-01 15:18:15 -0800 | [diff] [blame] | 450 | if (path_.has_value()) { |
| 451 | return *path_; |
| 452 | } |
Yurii Zubrytskyi | 36147b0 | 2025-02-06 15:41:54 -0800 | [diff] [blame] | 453 | constexpr static std::string kEmpty{kEmptyDebugString}; |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 454 | return kEmpty; |
| 455 | } |
| 456 | |
Yurii Zubrytskyi | 548c1ab | 2025-02-06 15:41:54 -0800 | [diff] [blame] | 457 | UpToDate EmptyAssetsProvider::IsUpToDate() const { |
| 458 | return UpToDate::Always; |
Ryan Mitchell | 1a48fa6 | 2021-01-10 08:36:36 -0800 | [diff] [blame] | 459 | } |
| 460 | |
Yurii Zubrytskyi | a5bc958 | 2022-11-30 23:53:59 -0800 | [diff] [blame] | 461 | } // namespace android |