Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 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 | // |
| 18 | // Provide access to read-only assets. |
| 19 | // |
| 20 | |
| 21 | #define LOG_TAG "asset" |
| 22 | #define ATRACE_TAG ATRACE_TAG_RESOURCES |
| 23 | //#define LOG_NDEBUG 0 |
| 24 | |
| 25 | #include <androidfw/Asset.h> |
| 26 | #include <androidfw/AssetDir.h> |
| 27 | #include <androidfw/AssetManager.h> |
| 28 | #include <androidfw/misc.h> |
Tomasz Wasilczyk | 804e819 | 2023-08-23 02:22:53 +0000 | [diff] [blame] | 29 | #include <androidfw/PathUtils.h> |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 30 | #include <androidfw/ResourceTypes.h> |
| 31 | #include <androidfw/ZipFileRO.h> |
Steven Moreland | fb7952f | 2018-02-23 14:58:50 -0800 | [diff] [blame] | 32 | #include <cutils/atomic.h> |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 33 | #include <utils/Log.h> |
| 34 | #include <utils/String8.h> |
| 35 | #include <utils/String8.h> |
| 36 | #include <utils/threads.h> |
| 37 | #include <utils/Timers.h> |
Adam Lesinski | b7e1ce0 | 2016-04-11 20:03:01 -0700 | [diff] [blame] | 38 | #include <utils/Trace.h> |
Martin Wallgren | 0fbb608 | 2015-08-11 15:10:31 +0200 | [diff] [blame] | 39 | #ifndef _WIN32 |
| 40 | #include <sys/file.h> |
| 41 | #endif |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 42 | |
| 43 | #include <assert.h> |
| 44 | #include <dirent.h> |
| 45 | #include <errno.h> |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 46 | #include <string.h> // strerror |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 47 | #include <strings.h> |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 48 | |
| 49 | #ifndef TEMP_FAILURE_RETRY |
| 50 | /* Used to retry syscalls that can return EINTR. */ |
| 51 | #define TEMP_FAILURE_RETRY(exp) ({ \ |
| 52 | typeof (exp) _rc; \ |
| 53 | do { \ |
| 54 | _rc = (exp); \ |
| 55 | } while (_rc == -1 && errno == EINTR); \ |
| 56 | _rc; }) |
| 57 | #endif |
| 58 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 59 | using namespace android; |
| 60 | |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 61 | static const bool kIsDebug = false; |
| 62 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 63 | static const char* kAssetsRoot = "assets"; |
| 64 | static const char* kAppZipName = NULL; //"classes.jar"; |
| 65 | static const char* kSystemAssets = "framework/framework-res.apk"; |
Adnan Begovic | 14e307c1 | 2015-07-06 20:06:36 -0700 | [diff] [blame^] | 66 | static const char* kOmniRomAssets = "framework/omnirom-res.apk"; |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 67 | static const char* kResourceCache = "resource-cache"; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 68 | |
| 69 | static const char* kExcludeExtension = ".EXCLUDE"; |
| 70 | |
| 71 | static Asset* const kExcludedAsset = (Asset*) 0xd000000d; |
| 72 | |
| 73 | static volatile int32_t gCount = 0; |
| 74 | |
Mårten Kongstad | 65a05fd | 2014-01-31 14:01:52 +0100 | [diff] [blame] | 75 | const char* AssetManager::RESOURCES_FILENAME = "resources.arsc"; |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 76 | const char* AssetManager::IDMAP_BIN = "/system/bin/idmap"; |
Mårten Kongstad | 06a1ac8 | 2018-09-20 13:09:47 +0200 | [diff] [blame] | 77 | const char* AssetManager::VENDOR_OVERLAY_DIR = "/vendor/overlay"; |
Jaekyun Seok | 1713d9e | 2018-01-12 21:47:26 +0900 | [diff] [blame] | 78 | const char* AssetManager::PRODUCT_OVERLAY_DIR = "/product/overlay"; |
Jeongik Cha | 3e725f2 | 2019-07-09 23:58:01 +0900 | [diff] [blame] | 79 | const char* AssetManager::SYSTEM_EXT_OVERLAY_DIR = "/system_ext/overlay"; |
Mårten Kongstad | 48c24cf | 2019-02-25 10:54:09 +0100 | [diff] [blame] | 80 | const char* AssetManager::ODM_OVERLAY_DIR = "/odm/overlay"; |
Mårten Kongstad | eb8a5c0 | 2019-02-25 14:18:17 +0100 | [diff] [blame] | 81 | const char* AssetManager::OEM_OVERLAY_DIR = "/oem/overlay"; |
Jakub Adamek | 54dcaab | 2016-10-19 11:46:13 +0100 | [diff] [blame] | 82 | const char* AssetManager::OVERLAY_THEME_DIR_PROPERTY = "ro.boot.vendor.overlay.theme"; |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 83 | const char* AssetManager::TARGET_PACKAGE_NAME = "android"; |
| 84 | const char* AssetManager::TARGET_APK_PATH = "/system/framework/framework-res.apk"; |
| 85 | const char* AssetManager::IDMAP_DIR = "/data/resource-cache"; |
Mårten Kongstad | 65a05fd | 2014-01-31 14:01:52 +0100 | [diff] [blame] | 86 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 87 | namespace { |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 88 | |
Adam Lesinski | a77685f | 2016-10-03 16:26:28 -0700 | [diff] [blame] | 89 | String8 idmapPathForPackagePath(const String8& pkgPath) { |
| 90 | const char* root = getenv("ANDROID_DATA"); |
| 91 | LOG_ALWAYS_FATAL_IF(root == NULL, "ANDROID_DATA not set"); |
| 92 | String8 path(root); |
Tomasz Wasilczyk | 804e819 | 2023-08-23 02:22:53 +0000 | [diff] [blame] | 93 | appendPath(path, kResourceCache); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 94 | |
Adam Lesinski | a77685f | 2016-10-03 16:26:28 -0700 | [diff] [blame] | 95 | char buf[256]; // 256 chars should be enough for anyone... |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 96 | strncpy(buf, pkgPath.c_str(), 255); |
Adam Lesinski | a77685f | 2016-10-03 16:26:28 -0700 | [diff] [blame] | 97 | buf[255] = '\0'; |
| 98 | char* filename = buf; |
| 99 | while (*filename && *filename == '/') { |
| 100 | ++filename; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 101 | } |
Adam Lesinski | a77685f | 2016-10-03 16:26:28 -0700 | [diff] [blame] | 102 | char* p = filename; |
| 103 | while (*p) { |
| 104 | if (*p == '/') { |
| 105 | *p = '@'; |
| 106 | } |
| 107 | ++p; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 108 | } |
Tomasz Wasilczyk | 804e819 | 2023-08-23 02:22:53 +0000 | [diff] [blame] | 109 | appendPath(path, filename); |
Adam Lesinski | a77685f | 2016-10-03 16:26:28 -0700 | [diff] [blame] | 110 | path.append("@idmap"); |
| 111 | |
| 112 | return path; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /* |
Adam Lesinski | a77685f | 2016-10-03 16:26:28 -0700 | [diff] [blame] | 116 | * Like strdup(), but uses C++ "new" operator instead of malloc. |
| 117 | */ |
| 118 | static char* strdupNew(const char* str) { |
| 119 | char* newStr; |
| 120 | int len; |
| 121 | |
| 122 | if (str == NULL) |
| 123 | return NULL; |
| 124 | |
| 125 | len = strlen(str); |
| 126 | newStr = new char[len+1]; |
| 127 | memcpy(newStr, str, len+1); |
| 128 | |
| 129 | return newStr; |
| 130 | } |
| 131 | |
| 132 | } // namespace |
| 133 | |
| 134 | /* |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 135 | * =========================================================================== |
| 136 | * AssetManager |
| 137 | * =========================================================================== |
| 138 | */ |
| 139 | |
Adam Lesinski | a77685f | 2016-10-03 16:26:28 -0700 | [diff] [blame] | 140 | int32_t AssetManager::getGlobalCount() { |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 141 | return gCount; |
| 142 | } |
| 143 | |
Adam Lesinski | a77685f | 2016-10-03 16:26:28 -0700 | [diff] [blame] | 144 | AssetManager::AssetManager() : |
| 145 | mLocale(NULL), mResources(NULL), mConfig(new ResTable_config) { |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 146 | int count = android_atomic_inc(&gCount) + 1; |
| 147 | if (kIsDebug) { |
| 148 | ALOGI("Creating AssetManager %p #%d\n", this, count); |
| 149 | } |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 150 | memset(mConfig, 0, sizeof(ResTable_config)); |
| 151 | } |
| 152 | |
Adam Lesinski | a77685f | 2016-10-03 16:26:28 -0700 | [diff] [blame] | 153 | AssetManager::~AssetManager() { |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 154 | int count = android_atomic_dec(&gCount); |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 155 | if (kIsDebug) { |
| 156 | ALOGI("Destroying AssetManager in %p #%d\n", this, count); |
Dianne Hackborn | 1704e3c | 2017-10-31 19:55:42 +0000 | [diff] [blame] | 157 | } else { |
| 158 | ALOGV("Destroying AssetManager in %p #%d\n", this, count); |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 159 | } |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 160 | |
Dianne Hackborn | ca3872c | 2017-10-30 14:19:32 -0700 | [diff] [blame] | 161 | // Manually close any fd paths for which we have not yet opened their zip (which |
| 162 | // will take ownership of the fd and close it when done). |
| 163 | for (size_t i=0; i<mAssetPaths.size(); i++) { |
Dianne Hackborn | 1704e3c | 2017-10-31 19:55:42 +0000 | [diff] [blame] | 164 | ALOGV("Cleaning path #%d: fd=%d, zip=%p", (int)i, mAssetPaths[i].rawFd, |
| 165 | mAssetPaths[i].zip.get()); |
Dianne Hackborn | ca3872c | 2017-10-30 14:19:32 -0700 | [diff] [blame] | 166 | if (mAssetPaths[i].rawFd >= 0 && mAssetPaths[i].zip == NULL) { |
| 167 | close(mAssetPaths[i].rawFd); |
| 168 | } |
| 169 | } |
| 170 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 171 | delete mConfig; |
| 172 | delete mResources; |
| 173 | |
| 174 | // don't have a String class yet, so make sure we clean up |
| 175 | delete[] mLocale; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 176 | } |
| 177 | |
Roozbeh Pournader | 1c686f2 | 2015-12-18 14:22:14 -0800 | [diff] [blame] | 178 | bool AssetManager::addAssetPath( |
Adam Lesinski | a77685f | 2016-10-03 16:26:28 -0700 | [diff] [blame] | 179 | const String8& path, int32_t* cookie, bool appAsLib, bool isSystemAsset) { |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 180 | AutoMutex _l(mLock); |
| 181 | |
| 182 | asset_path ap; |
| 183 | |
| 184 | String8 realPath(path); |
| 185 | if (kAppZipName) { |
Tomasz Wasilczyk | 804e819 | 2023-08-23 02:22:53 +0000 | [diff] [blame] | 186 | appendPath(realPath, kAppZipName); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 187 | } |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 188 | ap.type = ::getFileType(realPath.c_str()); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 189 | if (ap.type == kFileTypeRegular) { |
| 190 | ap.path = realPath; |
| 191 | } else { |
| 192 | ap.path = path; |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 193 | ap.type = ::getFileType(path.c_str()); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 194 | if (ap.type != kFileTypeDirectory && ap.type != kFileTypeRegular) { |
| 195 | ALOGW("Asset path %s is neither a directory nor file (type=%d).", |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 196 | path.c_str(), (int)ap.type); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 197 | return false; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | // Skip if we have it already. |
| 202 | for (size_t i=0; i<mAssetPaths.size(); i++) { |
| 203 | if (mAssetPaths[i].path == ap.path) { |
| 204 | if (cookie) { |
Narayan Kamath | a0c6260 | 2014-01-24 13:51:51 +0000 | [diff] [blame] | 205 | *cookie = static_cast<int32_t>(i+1); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 206 | } |
| 207 | return true; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | ALOGV("In %p Asset %s path: %s", this, |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 212 | ap.type == kFileTypeDirectory ? "dir" : "zip", ap.path.c_str()); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 213 | |
Roozbeh Pournader | 1c686f2 | 2015-12-18 14:22:14 -0800 | [diff] [blame] | 214 | ap.isSystemAsset = isSystemAsset; |
Dianne Hackborn | 1704e3c | 2017-10-31 19:55:42 +0000 | [diff] [blame] | 215 | ssize_t apPos = mAssetPaths.add(ap); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 216 | |
| 217 | // new paths are always added at the end |
| 218 | if (cookie) { |
Narayan Kamath | a0c6260 | 2014-01-24 13:51:51 +0000 | [diff] [blame] | 219 | *cookie = static_cast<int32_t>(mAssetPaths.size()); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 220 | } |
| 221 | |
Jaekyun Seok | 7de2f9c | 2017-03-02 12:45:10 +0900 | [diff] [blame] | 222 | #ifdef __ANDROID__ |
| 223 | // Load overlays, if any |
| 224 | asset_path oap; |
| 225 | for (size_t idx = 0; mZipSet.getOverlay(ap.path, idx, &oap); idx++) { |
| 226 | oap.isSystemAsset = isSystemAsset; |
| 227 | mAssetPaths.add(oap); |
| 228 | } |
| 229 | #endif |
| 230 | |
Martin Kosiba | 7df3625 | 2014-01-16 16:25:56 +0000 | [diff] [blame] | 231 | if (mResources != NULL) { |
Dianne Hackborn | 1704e3c | 2017-10-31 19:55:42 +0000 | [diff] [blame] | 232 | appendPathToResTable(mAssetPaths.editItemAt(apPos), appAsLib); |
Martin Kosiba | 7df3625 | 2014-01-16 16:25:56 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 235 | return true; |
| 236 | } |
| 237 | |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 238 | bool AssetManager::addOverlayPath(const String8& packagePath, int32_t* cookie) |
| 239 | { |
| 240 | const String8 idmapPath = idmapPathForPackagePath(packagePath); |
| 241 | |
| 242 | AutoMutex _l(mLock); |
| 243 | |
| 244 | for (size_t i = 0; i < mAssetPaths.size(); ++i) { |
| 245 | if (mAssetPaths[i].idmap == idmapPath) { |
| 246 | *cookie = static_cast<int32_t>(i + 1); |
| 247 | return true; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | Asset* idmap = NULL; |
| 252 | if ((idmap = openAssetFromFileLocked(idmapPath, Asset::ACCESS_BUFFER)) == NULL) { |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 253 | ALOGW("failed to open idmap file %s\n", idmapPath.c_str()); |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 254 | return false; |
| 255 | } |
| 256 | |
| 257 | String8 targetPath; |
| 258 | String8 overlayPath; |
| 259 | if (!ResTable::getIdmapInfo(idmap->getBuffer(false), idmap->getLength(), |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 260 | NULL, NULL, NULL, &targetPath, &overlayPath)) { |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 261 | ALOGW("failed to read idmap file %s\n", idmapPath.c_str()); |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 262 | delete idmap; |
| 263 | return false; |
| 264 | } |
| 265 | delete idmap; |
| 266 | |
| 267 | if (overlayPath != packagePath) { |
| 268 | ALOGW("idmap file %s inconcistent: expected path %s does not match actual path %s\n", |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 269 | idmapPath.c_str(), packagePath.c_str(), overlayPath.c_str()); |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 270 | return false; |
| 271 | } |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 272 | if (access(targetPath.c_str(), R_OK) != 0) { |
| 273 | ALOGW("failed to access file %s: %s\n", targetPath.c_str(), strerror(errno)); |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 274 | return false; |
| 275 | } |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 276 | if (access(idmapPath.c_str(), R_OK) != 0) { |
| 277 | ALOGW("failed to access file %s: %s\n", idmapPath.c_str(), strerror(errno)); |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 278 | return false; |
| 279 | } |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 280 | if (access(overlayPath.c_str(), R_OK) != 0) { |
| 281 | ALOGW("failed to access file %s: %s\n", overlayPath.c_str(), strerror(errno)); |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 282 | return false; |
| 283 | } |
| 284 | |
| 285 | asset_path oap; |
| 286 | oap.path = overlayPath; |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 287 | oap.type = ::getFileType(overlayPath.c_str()); |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 288 | oap.idmap = idmapPath; |
| 289 | #if 0 |
| 290 | ALOGD("Overlay added: targetPath=%s overlayPath=%s idmapPath=%s\n", |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 291 | targetPath.c_str(), overlayPath.c_str(), idmapPath.c_str()); |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 292 | #endif |
| 293 | mAssetPaths.add(oap); |
| 294 | *cookie = static_cast<int32_t>(mAssetPaths.size()); |
| 295 | |
Mårten Kongstad | 3011313 | 2014-11-07 10:52:17 +0100 | [diff] [blame] | 296 | if (mResources != NULL) { |
| 297 | appendPathToResTable(oap); |
| 298 | } |
| 299 | |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 300 | return true; |
Dianne Hackborn | ca3872c | 2017-10-30 14:19:32 -0700 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | bool AssetManager::addAssetFd( |
| 304 | int fd, const String8& debugPathName, int32_t* cookie, bool appAsLib, |
| 305 | bool assume_ownership) { |
| 306 | AutoMutex _l(mLock); |
| 307 | |
| 308 | asset_path ap; |
| 309 | |
| 310 | ap.path = debugPathName; |
| 311 | ap.rawFd = fd; |
| 312 | ap.type = kFileTypeRegular; |
| 313 | ap.assumeOwnership = assume_ownership; |
| 314 | |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 315 | ALOGV("In %p Asset fd %d name: %s", this, fd, ap.path.c_str()); |
Dianne Hackborn | ca3872c | 2017-10-30 14:19:32 -0700 | [diff] [blame] | 316 | |
Dianne Hackborn | 1704e3c | 2017-10-31 19:55:42 +0000 | [diff] [blame] | 317 | ssize_t apPos = mAssetPaths.add(ap); |
Dianne Hackborn | ca3872c | 2017-10-30 14:19:32 -0700 | [diff] [blame] | 318 | |
| 319 | // new paths are always added at the end |
| 320 | if (cookie) { |
| 321 | *cookie = static_cast<int32_t>(mAssetPaths.size()); |
| 322 | } |
| 323 | |
| 324 | if (mResources != NULL) { |
Dianne Hackborn | 1704e3c | 2017-10-31 19:55:42 +0000 | [diff] [blame] | 325 | appendPathToResTable(mAssetPaths.editItemAt(apPos), appAsLib); |
Dianne Hackborn | ca3872c | 2017-10-30 14:19:32 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | return true; |
| 329 | } |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 330 | |
Mårten Kongstad | 65a05fd | 2014-01-31 14:01:52 +0100 | [diff] [blame] | 331 | bool AssetManager::createIdmap(const char* targetApkPath, const char* overlayApkPath, |
Dianne Hackborn | 32bb5fa | 2014-02-11 13:56:21 -0800 | [diff] [blame] | 332 | uint32_t targetCrc, uint32_t overlayCrc, uint32_t** outData, size_t* outSize) |
Mårten Kongstad | 65a05fd | 2014-01-31 14:01:52 +0100 | [diff] [blame] | 333 | { |
| 334 | AutoMutex _l(mLock); |
| 335 | const String8 paths[2] = { String8(targetApkPath), String8(overlayApkPath) }; |
Mårten Kongstad | 6bb13da | 2016-06-02 09:34:36 +0200 | [diff] [blame] | 336 | Asset* assets[2] = {NULL, NULL}; |
| 337 | bool ret = false; |
| 338 | { |
| 339 | ResTable tables[2]; |
Mårten Kongstad | 65a05fd | 2014-01-31 14:01:52 +0100 | [diff] [blame] | 340 | |
Mårten Kongstad | 6bb13da | 2016-06-02 09:34:36 +0200 | [diff] [blame] | 341 | for (int i = 0; i < 2; ++i) { |
| 342 | asset_path ap; |
| 343 | ap.type = kFileTypeRegular; |
| 344 | ap.path = paths[i]; |
| 345 | assets[i] = openNonAssetInPathLocked("resources.arsc", |
| 346 | Asset::ACCESS_BUFFER, ap); |
| 347 | if (assets[i] == NULL) { |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 348 | ALOGW("failed to find resources.arsc in %s\n", ap.path.c_str()); |
Mårten Kongstad | 6bb13da | 2016-06-02 09:34:36 +0200 | [diff] [blame] | 349 | goto exit; |
| 350 | } |
| 351 | if (tables[i].add(assets[i]) != NO_ERROR) { |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 352 | ALOGW("failed to add %s to resource table", paths[i].c_str()); |
Mårten Kongstad | 6bb13da | 2016-06-02 09:34:36 +0200 | [diff] [blame] | 353 | goto exit; |
| 354 | } |
Mårten Kongstad | 65a05fd | 2014-01-31 14:01:52 +0100 | [diff] [blame] | 355 | } |
Mårten Kongstad | 67d5c93 | 2018-05-25 15:58:17 +0200 | [diff] [blame] | 356 | ret = tables[1].createIdmap(tables[0], targetCrc, overlayCrc, |
Mårten Kongstad | 6bb13da | 2016-06-02 09:34:36 +0200 | [diff] [blame] | 357 | targetApkPath, overlayApkPath, (void**)outData, outSize) == NO_ERROR; |
Mårten Kongstad | 65a05fd | 2014-01-31 14:01:52 +0100 | [diff] [blame] | 358 | } |
| 359 | |
Mårten Kongstad | 6bb13da | 2016-06-02 09:34:36 +0200 | [diff] [blame] | 360 | exit: |
| 361 | delete assets[0]; |
| 362 | delete assets[1]; |
| 363 | return ret; |
Mårten Kongstad | 65a05fd | 2014-01-31 14:01:52 +0100 | [diff] [blame] | 364 | } |
| 365 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 366 | bool AssetManager::addDefaultAssets() |
| 367 | { |
| 368 | const char* root = getenv("ANDROID_ROOT"); |
| 369 | LOG_ALWAYS_FATAL_IF(root == NULL, "ANDROID_ROOT not set"); |
| 370 | |
Adnan Begovic | 14e307c1 | 2015-07-06 20:06:36 -0700 | [diff] [blame^] | 371 | bool success = true; |
| 372 | { |
| 373 | String8 path(root); |
| 374 | appendPath(path, kSystemAssets); |
| 375 | success &= addAssetPath(path, NULL, false /* appAsLib */, true /* isSystemAsset */); |
| 376 | } |
| 377 | { |
| 378 | String8 path(root); |
| 379 | appendPath(path, kOmniRomAssets); |
| 380 | success &= addAssetPath(path, NULL, false /* appAsLib */, true /* isSystemAsset */); |
| 381 | } |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 382 | |
Adnan Begovic | 14e307c1 | 2015-07-06 20:06:36 -0700 | [diff] [blame^] | 383 | return success; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 384 | } |
| 385 | |
Narayan Kamath | a0c6260 | 2014-01-24 13:51:51 +0000 | [diff] [blame] | 386 | int32_t AssetManager::nextAssetPath(const int32_t cookie) const |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 387 | { |
| 388 | AutoMutex _l(mLock); |
Narayan Kamath | a0c6260 | 2014-01-24 13:51:51 +0000 | [diff] [blame] | 389 | const size_t next = static_cast<size_t>(cookie) + 1; |
| 390 | return next > mAssetPaths.size() ? -1 : next; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 391 | } |
| 392 | |
Narayan Kamath | a0c6260 | 2014-01-24 13:51:51 +0000 | [diff] [blame] | 393 | String8 AssetManager::getAssetPath(const int32_t cookie) const |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 394 | { |
| 395 | AutoMutex _l(mLock); |
Narayan Kamath | a0c6260 | 2014-01-24 13:51:51 +0000 | [diff] [blame] | 396 | const size_t which = static_cast<size_t>(cookie) - 1; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 397 | if (which < mAssetPaths.size()) { |
| 398 | return mAssetPaths[which].path; |
| 399 | } |
| 400 | return String8(); |
| 401 | } |
| 402 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 403 | void AssetManager::setLocaleLocked(const char* locale) |
| 404 | { |
| 405 | if (mLocale != NULL) { |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 406 | delete[] mLocale; |
| 407 | } |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 408 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 409 | mLocale = strdupNew(locale); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 410 | updateResourceParamsLocked(); |
| 411 | } |
| 412 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 413 | void AssetManager::setConfiguration(const ResTable_config& config, const char* locale) |
| 414 | { |
| 415 | AutoMutex _l(mLock); |
| 416 | *mConfig = config; |
| 417 | if (locale) { |
| 418 | setLocaleLocked(locale); |
| 419 | } else if (config.language[0] != 0) { |
Narayan Kamath | 91447d8 | 2014-01-21 15:32:36 +0000 | [diff] [blame] | 420 | char spec[RESTABLE_MAX_LOCALE_LEN]; |
| 421 | config.getBcp47Locale(spec); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 422 | setLocaleLocked(spec); |
| 423 | } else { |
| 424 | updateResourceParamsLocked(); |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | void AssetManager::getConfiguration(ResTable_config* outConfig) const |
| 429 | { |
| 430 | AutoMutex _l(mLock); |
| 431 | *outConfig = *mConfig; |
| 432 | } |
| 433 | |
| 434 | /* |
| 435 | * Open an asset. |
| 436 | * |
Adam Lesinski | fe90eaf | 2016-10-04 13:31:31 -0700 | [diff] [blame] | 437 | * The data could be in any asset path. Each asset path could be: |
| 438 | * - A directory on disk. |
| 439 | * - A Zip archive, uncompressed or compressed. |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 440 | * |
Adam Lesinski | fe90eaf | 2016-10-04 13:31:31 -0700 | [diff] [blame] | 441 | * If the file is in a directory, it could have a .gz suffix, meaning it is compressed. |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 442 | * |
| 443 | * We should probably reject requests for "illegal" filenames, e.g. those |
| 444 | * with illegal characters or "../" backward relative paths. |
| 445 | */ |
| 446 | Asset* AssetManager::open(const char* fileName, AccessMode mode) |
| 447 | { |
| 448 | AutoMutex _l(mLock); |
| 449 | |
| 450 | LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager"); |
| 451 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 452 | String8 assetName(kAssetsRoot); |
Tomasz Wasilczyk | 804e819 | 2023-08-23 02:22:53 +0000 | [diff] [blame] | 453 | appendPath(assetName, fileName); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 454 | |
| 455 | /* |
| 456 | * For each top-level asset path, search for the asset. |
| 457 | */ |
| 458 | |
| 459 | size_t i = mAssetPaths.size(); |
| 460 | while (i > 0) { |
| 461 | i--; |
| 462 | ALOGV("Looking for asset '%s' in '%s'\n", |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 463 | assetName.c_str(), mAssetPaths.itemAt(i).path.c_str()); |
| 464 | Asset* pAsset = openNonAssetInPathLocked(assetName.c_str(), mode, |
Dianne Hackborn | 1704e3c | 2017-10-31 19:55:42 +0000 | [diff] [blame] | 465 | mAssetPaths.editItemAt(i)); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 466 | if (pAsset != NULL) { |
| 467 | return pAsset != kExcludedAsset ? pAsset : NULL; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | return NULL; |
| 472 | } |
| 473 | |
| 474 | /* |
| 475 | * Open a non-asset file as if it were an asset. |
| 476 | * |
Adam Lesinski | fe90eaf | 2016-10-04 13:31:31 -0700 | [diff] [blame] | 477 | * The "fileName" is the partial path starting from the application name. |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 478 | */ |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 479 | Asset* AssetManager::openNonAsset(const char* fileName, AccessMode mode, int32_t* outCookie) |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 480 | { |
| 481 | AutoMutex _l(mLock); |
| 482 | |
| 483 | LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager"); |
| 484 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 485 | /* |
| 486 | * For each top-level asset path, search for the asset. |
| 487 | */ |
| 488 | |
| 489 | size_t i = mAssetPaths.size(); |
| 490 | while (i > 0) { |
| 491 | i--; |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 492 | ALOGV("Looking for non-asset '%s' in '%s'\n", fileName, mAssetPaths.itemAt(i).path.c_str()); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 493 | Asset* pAsset = openNonAssetInPathLocked( |
Dianne Hackborn | 1704e3c | 2017-10-31 19:55:42 +0000 | [diff] [blame] | 494 | fileName, mode, mAssetPaths.editItemAt(i)); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 495 | if (pAsset != NULL) { |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 496 | if (outCookie != NULL) *outCookie = static_cast<int32_t>(i + 1); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 497 | return pAsset != kExcludedAsset ? pAsset : NULL; |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | return NULL; |
| 502 | } |
| 503 | |
Narayan Kamath | a0c6260 | 2014-01-24 13:51:51 +0000 | [diff] [blame] | 504 | Asset* AssetManager::openNonAsset(const int32_t cookie, const char* fileName, AccessMode mode) |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 505 | { |
Narayan Kamath | a0c6260 | 2014-01-24 13:51:51 +0000 | [diff] [blame] | 506 | const size_t which = static_cast<size_t>(cookie) - 1; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 507 | |
| 508 | AutoMutex _l(mLock); |
| 509 | |
| 510 | LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager"); |
| 511 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 512 | if (which < mAssetPaths.size()) { |
| 513 | ALOGV("Looking for non-asset '%s' in '%s'\n", fileName, |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 514 | mAssetPaths.itemAt(which).path.c_str()); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 515 | Asset* pAsset = openNonAssetInPathLocked( |
Dianne Hackborn | 1704e3c | 2017-10-31 19:55:42 +0000 | [diff] [blame] | 516 | fileName, mode, mAssetPaths.editItemAt(which)); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 517 | if (pAsset != NULL) { |
| 518 | return pAsset != kExcludedAsset ? pAsset : NULL; |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | return NULL; |
| 523 | } |
| 524 | |
| 525 | /* |
| 526 | * Get the type of a file in the asset namespace. |
| 527 | * |
| 528 | * This currently only works for regular files. All others (including |
| 529 | * directories) will return kFileTypeNonexistent. |
| 530 | */ |
| 531 | FileType AssetManager::getFileType(const char* fileName) |
| 532 | { |
| 533 | Asset* pAsset = NULL; |
| 534 | |
| 535 | /* |
| 536 | * Open the asset. This is less efficient than simply finding the |
| 537 | * file, but it's not too bad (we don't uncompress or mmap data until |
| 538 | * the first read() call). |
| 539 | */ |
| 540 | pAsset = open(fileName, Asset::ACCESS_STREAMING); |
| 541 | delete pAsset; |
| 542 | |
Adam Lesinski | fe90eaf | 2016-10-04 13:31:31 -0700 | [diff] [blame] | 543 | if (pAsset == NULL) { |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 544 | return kFileTypeNonexistent; |
Adam Lesinski | fe90eaf | 2016-10-04 13:31:31 -0700 | [diff] [blame] | 545 | } else { |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 546 | return kFileTypeRegular; |
Adam Lesinski | fe90eaf | 2016-10-04 13:31:31 -0700 | [diff] [blame] | 547 | } |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 548 | } |
| 549 | |
Dianne Hackborn | 1704e3c | 2017-10-31 19:55:42 +0000 | [diff] [blame] | 550 | bool AssetManager::appendPathToResTable(asset_path& ap, bool appAsLib) const { |
Jaekyun Seok | 7de2f9c | 2017-03-02 12:45:10 +0900 | [diff] [blame] | 551 | // skip those ap's that correspond to system overlays |
| 552 | if (ap.isSystemOverlay) { |
| 553 | return true; |
| 554 | } |
| 555 | |
Martin Kosiba | 7df3625 | 2014-01-16 16:25:56 +0000 | [diff] [blame] | 556 | Asset* ass = NULL; |
| 557 | ResTable* sharedRes = NULL; |
| 558 | bool shared = true; |
| 559 | bool onlyEmptyResources = true; |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 560 | ATRACE_NAME(ap.path.c_str()); |
Martin Kosiba | 7df3625 | 2014-01-16 16:25:56 +0000 | [diff] [blame] | 561 | Asset* idmap = openIdmapLocked(ap); |
| 562 | size_t nextEntryIdx = mResources->getTableCount(); |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 563 | ALOGV("Looking for resource asset in '%s'\n", ap.path.c_str()); |
Dianne Hackborn | ca3872c | 2017-10-30 14:19:32 -0700 | [diff] [blame] | 564 | if (ap.type != kFileTypeDirectory && ap.rawFd < 0) { |
Martin Kosiba | 7df3625 | 2014-01-16 16:25:56 +0000 | [diff] [blame] | 565 | if (nextEntryIdx == 0) { |
| 566 | // The first item is typically the framework resources, |
| 567 | // which we want to avoid parsing every time. |
| 568 | sharedRes = const_cast<AssetManager*>(this)-> |
| 569 | mZipSet.getZipResourceTable(ap.path); |
| 570 | if (sharedRes != NULL) { |
| 571 | // skip ahead the number of system overlay packages preloaded |
| 572 | nextEntryIdx = sharedRes->getTableCount(); |
| 573 | } |
| 574 | } |
| 575 | if (sharedRes == NULL) { |
| 576 | ass = const_cast<AssetManager*>(this)-> |
| 577 | mZipSet.getZipResourceTableAsset(ap.path); |
| 578 | if (ass == NULL) { |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 579 | ALOGV("loading resource table %s\n", ap.path.c_str()); |
Martin Kosiba | 7df3625 | 2014-01-16 16:25:56 +0000 | [diff] [blame] | 580 | ass = const_cast<AssetManager*>(this)-> |
| 581 | openNonAssetInPathLocked("resources.arsc", |
| 582 | Asset::ACCESS_BUFFER, |
| 583 | ap); |
| 584 | if (ass != NULL && ass != kExcludedAsset) { |
| 585 | ass = const_cast<AssetManager*>(this)-> |
| 586 | mZipSet.setZipResourceTableAsset(ap.path, ass); |
| 587 | } |
| 588 | } |
Jeongik Cha | 3e725f2 | 2019-07-09 23:58:01 +0900 | [diff] [blame] | 589 | |
Martin Kosiba | 7df3625 | 2014-01-16 16:25:56 +0000 | [diff] [blame] | 590 | if (nextEntryIdx == 0 && ass != NULL) { |
| 591 | // If this is the first resource table in the asset |
| 592 | // manager, then we are going to cache it so that we |
| 593 | // can quickly copy it out for others. |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 594 | ALOGV("Creating shared resources for %s", ap.path.c_str()); |
Martin Kosiba | 7df3625 | 2014-01-16 16:25:56 +0000 | [diff] [blame] | 595 | sharedRes = new ResTable(); |
| 596 | sharedRes->add(ass, idmap, nextEntryIdx + 1, false); |
Jaekyun Seok | 7de2f9c | 2017-03-02 12:45:10 +0900 | [diff] [blame] | 597 | #ifdef __ANDROID__ |
| 598 | const char* data = getenv("ANDROID_DATA"); |
| 599 | LOG_ALWAYS_FATAL_IF(data == NULL, "ANDROID_DATA not set"); |
| 600 | String8 overlaysListPath(data); |
Tomasz Wasilczyk | 804e819 | 2023-08-23 02:22:53 +0000 | [diff] [blame] | 601 | appendPath(overlaysListPath, kResourceCache); |
| 602 | appendPath(overlaysListPath, "overlays.list"); |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 603 | addSystemOverlays(overlaysListPath.c_str(), ap.path, sharedRes, nextEntryIdx); |
Jaekyun Seok | 7de2f9c | 2017-03-02 12:45:10 +0900 | [diff] [blame] | 604 | #endif |
Martin Kosiba | 7df3625 | 2014-01-16 16:25:56 +0000 | [diff] [blame] | 605 | sharedRes = const_cast<AssetManager*>(this)-> |
| 606 | mZipSet.setZipResourceTable(ap.path, sharedRes); |
| 607 | } |
| 608 | } |
| 609 | } else { |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 610 | ALOGV("loading resource table %s\n", ap.path.c_str()); |
Martin Kosiba | 7df3625 | 2014-01-16 16:25:56 +0000 | [diff] [blame] | 611 | ass = const_cast<AssetManager*>(this)-> |
| 612 | openNonAssetInPathLocked("resources.arsc", |
| 613 | Asset::ACCESS_BUFFER, |
| 614 | ap); |
| 615 | shared = false; |
| 616 | } |
| 617 | |
| 618 | if ((ass != NULL || sharedRes != NULL) && ass != kExcludedAsset) { |
| 619 | ALOGV("Installing resource asset %p in to table %p\n", ass, mResources); |
| 620 | if (sharedRes != NULL) { |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 621 | ALOGV("Copying existing resources for %s", ap.path.c_str()); |
Roozbeh Pournader | 1c686f2 | 2015-12-18 14:22:14 -0800 | [diff] [blame] | 622 | mResources->add(sharedRes, ap.isSystemAsset); |
Martin Kosiba | 7df3625 | 2014-01-16 16:25:56 +0000 | [diff] [blame] | 623 | } else { |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 624 | ALOGV("Parsing resources for %s", ap.path.c_str()); |
Roozbeh Pournader | 1c686f2 | 2015-12-18 14:22:14 -0800 | [diff] [blame] | 625 | mResources->add(ass, idmap, nextEntryIdx + 1, !shared, appAsLib, ap.isSystemAsset); |
Martin Kosiba | 7df3625 | 2014-01-16 16:25:56 +0000 | [diff] [blame] | 626 | } |
| 627 | onlyEmptyResources = false; |
| 628 | |
| 629 | if (!shared) { |
| 630 | delete ass; |
| 631 | } |
| 632 | } else { |
| 633 | ALOGV("Installing empty resources in to table %p\n", mResources); |
| 634 | mResources->addEmpty(nextEntryIdx + 1); |
| 635 | } |
| 636 | |
| 637 | if (idmap != NULL) { |
| 638 | delete idmap; |
| 639 | } |
Martin Kosiba | 7df3625 | 2014-01-16 16:25:56 +0000 | [diff] [blame] | 640 | return onlyEmptyResources; |
| 641 | } |
| 642 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 643 | const ResTable* AssetManager::getResTable(bool required) const |
| 644 | { |
| 645 | ResTable* rt = mResources; |
| 646 | if (rt) { |
| 647 | return rt; |
| 648 | } |
| 649 | |
| 650 | // Iterate through all asset packages, collecting resources from each. |
| 651 | |
| 652 | AutoMutex _l(mLock); |
| 653 | |
| 654 | if (mResources != NULL) { |
| 655 | return mResources; |
| 656 | } |
| 657 | |
| 658 | if (required) { |
| 659 | LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager"); |
| 660 | } |
| 661 | |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 662 | mResources = new ResTable(); |
| 663 | updateResourceParamsLocked(); |
| 664 | |
| 665 | bool onlyEmptyResources = true; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 666 | const size_t N = mAssetPaths.size(); |
| 667 | for (size_t i=0; i<N; i++) { |
Dianne Hackborn | 1704e3c | 2017-10-31 19:55:42 +0000 | [diff] [blame] | 668 | bool empty = appendPathToResTable( |
| 669 | const_cast<AssetManager*>(this)->mAssetPaths.editItemAt(i)); |
Martin Kosiba | 7df3625 | 2014-01-16 16:25:56 +0000 | [diff] [blame] | 670 | onlyEmptyResources = onlyEmptyResources && empty; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 671 | } |
| 672 | |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 673 | if (required && onlyEmptyResources) { |
| 674 | ALOGW("Unable to find resources file resources.arsc"); |
| 675 | delete mResources; |
| 676 | mResources = NULL; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 677 | } |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 678 | |
| 679 | return mResources; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | void AssetManager::updateResourceParamsLocked() const |
| 683 | { |
Adam Lesinski | b7e1ce0 | 2016-04-11 20:03:01 -0700 | [diff] [blame] | 684 | ATRACE_CALL(); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 685 | ResTable* res = mResources; |
| 686 | if (!res) { |
| 687 | return; |
| 688 | } |
| 689 | |
Narayan Kamath | 91447d8 | 2014-01-21 15:32:36 +0000 | [diff] [blame] | 690 | if (mLocale) { |
| 691 | mConfig->setBcp47Locale(mLocale); |
| 692 | } else { |
| 693 | mConfig->clearLocale(); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 694 | } |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 695 | |
| 696 | res->setParameters(mConfig); |
| 697 | } |
| 698 | |
| 699 | Asset* AssetManager::openIdmapLocked(const struct asset_path& ap) const |
| 700 | { |
| 701 | Asset* ass = NULL; |
| 702 | if (ap.idmap.size() != 0) { |
| 703 | ass = const_cast<AssetManager*>(this)-> |
| 704 | openAssetFromFileLocked(ap.idmap, Asset::ACCESS_BUFFER); |
| 705 | if (ass) { |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 706 | ALOGV("loading idmap %s\n", ap.idmap.c_str()); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 707 | } else { |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 708 | ALOGW("failed to load idmap %s\n", ap.idmap.c_str()); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 709 | } |
| 710 | } |
| 711 | return ass; |
| 712 | } |
| 713 | |
Jaekyun Seok | 7de2f9c | 2017-03-02 12:45:10 +0900 | [diff] [blame] | 714 | void AssetManager::addSystemOverlays(const char* pathOverlaysList, |
| 715 | const String8& targetPackagePath, ResTable* sharedRes, size_t offset) const |
| 716 | { |
| 717 | FILE* fin = fopen(pathOverlaysList, "r"); |
| 718 | if (fin == NULL) { |
| 719 | return; |
| 720 | } |
| 721 | |
| 722 | #ifndef _WIN32 |
| 723 | if (TEMP_FAILURE_RETRY(flock(fileno(fin), LOCK_SH)) != 0) { |
| 724 | fclose(fin); |
| 725 | return; |
| 726 | } |
| 727 | #endif |
| 728 | char buf[1024]; |
| 729 | while (fgets(buf, sizeof(buf), fin)) { |
| 730 | // format of each line: |
| 731 | // <path to apk><space><path to idmap><newline> |
| 732 | char* space = strchr(buf, ' '); |
| 733 | char* newline = strchr(buf, '\n'); |
| 734 | asset_path oap; |
| 735 | |
| 736 | if (space == NULL || newline == NULL || newline < space) { |
| 737 | continue; |
| 738 | } |
| 739 | |
| 740 | oap.path = String8(buf, space - buf); |
| 741 | oap.type = kFileTypeRegular; |
| 742 | oap.idmap = String8(space + 1, newline - space - 1); |
| 743 | oap.isSystemOverlay = true; |
| 744 | |
| 745 | Asset* oass = const_cast<AssetManager*>(this)-> |
| 746 | openNonAssetInPathLocked("resources.arsc", |
| 747 | Asset::ACCESS_BUFFER, |
| 748 | oap); |
| 749 | |
| 750 | if (oass != NULL) { |
| 751 | Asset* oidmap = openIdmapLocked(oap); |
| 752 | offset++; |
| 753 | sharedRes->add(oass, oidmap, offset + 1, false); |
| 754 | const_cast<AssetManager*>(this)->mAssetPaths.add(oap); |
| 755 | const_cast<AssetManager*>(this)->mZipSet.addOverlay(targetPackagePath, oap); |
| 756 | delete oidmap; |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | #ifndef _WIN32 |
| 761 | TEMP_FAILURE_RETRY(flock(fileno(fin), LOCK_UN)); |
| 762 | #endif |
| 763 | fclose(fin); |
| 764 | } |
| 765 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 766 | const ResTable& AssetManager::getResources(bool required) const |
| 767 | { |
| 768 | const ResTable* rt = getResTable(required); |
| 769 | return *rt; |
| 770 | } |
| 771 | |
| 772 | bool AssetManager::isUpToDate() |
| 773 | { |
| 774 | AutoMutex _l(mLock); |
| 775 | return mZipSet.isUpToDate(); |
| 776 | } |
| 777 | |
Roozbeh Pournader | 1c686f2 | 2015-12-18 14:22:14 -0800 | [diff] [blame] | 778 | void AssetManager::getLocales(Vector<String8>* locales, bool includeSystemLocales) const |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 779 | { |
| 780 | ResTable* res = mResources; |
| 781 | if (res != NULL) { |
Roozbeh Pournader | 7e5f96f | 2016-06-13 18:10:49 -0700 | [diff] [blame] | 782 | res->getLocales(locales, includeSystemLocales, true /* mergeEquivalentLangs */); |
Narayan Kamath | e4345db | 2014-06-26 16:01:28 +0100 | [diff] [blame] | 783 | } |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | /* |
| 787 | * Open a non-asset file as if it were an asset, searching for it in the |
| 788 | * specified app. |
| 789 | * |
| 790 | * Pass in a NULL values for "appName" if the common app directory should |
| 791 | * be used. |
| 792 | */ |
| 793 | Asset* AssetManager::openNonAssetInPathLocked(const char* fileName, AccessMode mode, |
Dianne Hackborn | 1704e3c | 2017-10-31 19:55:42 +0000 | [diff] [blame] | 794 | asset_path& ap) |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 795 | { |
| 796 | Asset* pAsset = NULL; |
| 797 | |
Dianne Hackborn | ca3872c | 2017-10-30 14:19:32 -0700 | [diff] [blame] | 798 | ALOGV("openNonAssetInPath: name=%s type=%d fd=%d", fileName, ap.type, ap.rawFd); |
| 799 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 800 | /* look at the filesystem on disk */ |
| 801 | if (ap.type == kFileTypeDirectory) { |
| 802 | String8 path(ap.path); |
Tomasz Wasilczyk | 804e819 | 2023-08-23 02:22:53 +0000 | [diff] [blame] | 803 | appendPath(path, fileName); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 804 | |
| 805 | pAsset = openAssetFromFileLocked(path, mode); |
| 806 | |
| 807 | if (pAsset == NULL) { |
| 808 | /* try again, this time with ".gz" */ |
| 809 | path.append(".gz"); |
| 810 | pAsset = openAssetFromFileLocked(path, mode); |
| 811 | } |
| 812 | |
| 813 | if (pAsset != NULL) { |
Dianne Hackborn | ca3872c | 2017-10-30 14:19:32 -0700 | [diff] [blame] | 814 | ALOGV("FOUND NA '%s' on disk", fileName); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 815 | pAsset->setAssetSource(path); |
| 816 | } |
| 817 | |
| 818 | /* look inside the zip file */ |
| 819 | } else { |
| 820 | String8 path(fileName); |
| 821 | |
| 822 | /* check the appropriate Zip file */ |
Narayan Kamath | 560566d | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 823 | ZipFileRO* pZip = getZipFileLocked(ap); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 824 | if (pZip != NULL) { |
Tomasz Wasilczyk | 835dfe5 | 2023-08-17 16:27:22 +0000 | [diff] [blame] | 825 | ALOGV("GOT zip, checking NA '%s'", path.c_str()); |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 826 | ZipEntryRO entry = pZip->findEntryByName(path.c_str()); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 827 | if (entry != NULL) { |
Tomasz Wasilczyk | 835dfe5 | 2023-08-17 16:27:22 +0000 | [diff] [blame] | 828 | ALOGV("FOUND NA in Zip file for %s", path.c_str()); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 829 | pAsset = openAssetFromZipLocked(pZip, entry, mode, path); |
Narayan Kamath | 560566d | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 830 | pZip->releaseEntry(entry); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 831 | } |
| 832 | } |
| 833 | |
| 834 | if (pAsset != NULL) { |
| 835 | /* create a "source" name, for debug/display */ |
| 836 | pAsset->setAssetSource( |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 837 | createZipSourceNameLocked(ZipSet::getPathName(ap.path.c_str()), String8(""), |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 838 | String8(fileName))); |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | return pAsset; |
| 843 | } |
| 844 | |
| 845 | /* |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 846 | * Create a "source name" for a file from a Zip archive. |
| 847 | */ |
| 848 | String8 AssetManager::createZipSourceNameLocked(const String8& zipFileName, |
| 849 | const String8& dirName, const String8& fileName) |
| 850 | { |
| 851 | String8 sourceName("zip:"); |
| 852 | sourceName.append(zipFileName); |
| 853 | sourceName.append(":"); |
| 854 | if (dirName.length() > 0) { |
Tomasz Wasilczyk | 804e819 | 2023-08-23 02:22:53 +0000 | [diff] [blame] | 855 | appendPath(sourceName, dirName); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 856 | } |
Tomasz Wasilczyk | 804e819 | 2023-08-23 02:22:53 +0000 | [diff] [blame] | 857 | appendPath(sourceName, fileName); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 858 | return sourceName; |
| 859 | } |
| 860 | |
| 861 | /* |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 862 | * Create a path to a loose asset (asset-base/app/rootDir). |
| 863 | */ |
| 864 | String8 AssetManager::createPathNameLocked(const asset_path& ap, const char* rootDir) |
| 865 | { |
| 866 | String8 path(ap.path); |
Tomasz Wasilczyk | 804e819 | 2023-08-23 02:22:53 +0000 | [diff] [blame] | 867 | if (rootDir != NULL) appendPath(path, rootDir); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 868 | return path; |
| 869 | } |
| 870 | |
| 871 | /* |
| 872 | * Return a pointer to one of our open Zip archives. Returns NULL if no |
| 873 | * matching Zip file exists. |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 874 | */ |
Dianne Hackborn | 1704e3c | 2017-10-31 19:55:42 +0000 | [diff] [blame] | 875 | ZipFileRO* AssetManager::getZipFileLocked(asset_path& ap) |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 876 | { |
Dianne Hackborn | 1704e3c | 2017-10-31 19:55:42 +0000 | [diff] [blame] | 877 | ALOGV("getZipFileLocked() in %p: ap=%p zip=%p", this, &ap, ap.zip.get()); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 878 | |
Dianne Hackborn | ca3872c | 2017-10-30 14:19:32 -0700 | [diff] [blame] | 879 | if (ap.zip != NULL) { |
| 880 | return ap.zip->getZip(); |
| 881 | } |
| 882 | |
| 883 | if (ap.rawFd < 0) { |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 884 | ALOGV("getZipFileLocked: Creating new zip from path %s", ap.path.c_str()); |
Dianne Hackborn | ca3872c | 2017-10-30 14:19:32 -0700 | [diff] [blame] | 885 | ap.zip = mZipSet.getSharedZip(ap.path); |
| 886 | } else { |
Dianne Hackborn | 1704e3c | 2017-10-31 19:55:42 +0000 | [diff] [blame] | 887 | ALOGV("getZipFileLocked: Creating new zip from fd %d", ap.rawFd); |
Dianne Hackborn | ca3872c | 2017-10-30 14:19:32 -0700 | [diff] [blame] | 888 | ap.zip = SharedZip::create(ap.rawFd, ap.path); |
| 889 | |
| 890 | } |
| 891 | return ap.zip != NULL ? ap.zip->getZip() : NULL; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | /* |
| 895 | * Try to open an asset from a file on disk. |
| 896 | * |
| 897 | * If the file is compressed with gzip, we seek to the start of the |
| 898 | * deflated data and pass that in (just like we would for a Zip archive). |
| 899 | * |
| 900 | * For uncompressed data, we may already have an mmap()ed version sitting |
| 901 | * around. If so, we want to hand that to the Asset instead. |
| 902 | * |
| 903 | * This returns NULL if the file doesn't exist, couldn't be opened, or |
| 904 | * claims to be a ".gz" but isn't. |
| 905 | */ |
| 906 | Asset* AssetManager::openAssetFromFileLocked(const String8& pathName, |
| 907 | AccessMode mode) |
| 908 | { |
| 909 | Asset* pAsset = NULL; |
| 910 | |
Tomasz Wasilczyk | 804e819 | 2023-08-23 02:22:53 +0000 | [diff] [blame] | 911 | if (strcasecmp(getPathExtension(pathName).c_str(), ".gz") == 0) { |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 912 | //printf("TRYING '%s'\n", (const char*) pathName); |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 913 | pAsset = Asset::createFromCompressedFile(pathName.c_str(), mode); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 914 | } else { |
| 915 | //printf("TRYING '%s'\n", (const char*) pathName); |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 916 | pAsset = Asset::createFromFile(pathName.c_str(), mode); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | return pAsset; |
| 920 | } |
| 921 | |
| 922 | /* |
| 923 | * Given an entry in a Zip archive, create a new Asset object. |
| 924 | * |
| 925 | * If the entry is uncompressed, we may want to create or share a |
| 926 | * slice of shared memory. |
| 927 | */ |
| 928 | Asset* AssetManager::openAssetFromZipLocked(const ZipFileRO* pZipFile, |
| 929 | const ZipEntryRO entry, AccessMode mode, const String8& entryName) |
| 930 | { |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 931 | std::unique_ptr<Asset> pAsset; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 932 | |
| 933 | // TODO: look for previously-created shared memory slice? |
Narayan Kamath | 407753c | 2015-06-16 12:02:57 +0100 | [diff] [blame] | 934 | uint16_t method; |
| 935 | uint32_t uncompressedLen; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 936 | |
| 937 | //printf("USING Zip '%s'\n", pEntry->getFileName()); |
| 938 | |
Pawan Wagh | fbe2bd3 | 2024-06-03 21:39:07 +0000 | [diff] [blame] | 939 | if (!pZipFile->getEntryInfo(entry, &method, &uncompressedLen, nullptr, nullptr, |
| 940 | nullptr, nullptr, nullptr)) |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 941 | { |
| 942 | ALOGW("getEntryInfo failed\n"); |
| 943 | return NULL; |
| 944 | } |
| 945 | |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 946 | std::optional<incfs::IncFsFileMap> dataMap = pZipFile->createEntryIncFsFileMap(entry); |
| 947 | if (!dataMap.has_value()) { |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 948 | ALOGW("create map from entry failed\n"); |
| 949 | return NULL; |
| 950 | } |
| 951 | |
| 952 | if (method == ZipFileRO::kCompressStored) { |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 953 | pAsset = Asset::createFromUncompressedMap(std::move(*dataMap), mode); |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 954 | ALOGV("Opened uncompressed entry %s in zip %s mode %d: %p", entryName.c_str(), |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 955 | dataMap->file_name(), mode, pAsset.get()); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 956 | } else { |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 957 | pAsset = Asset::createFromCompressedMap(std::move(*dataMap), |
Narayan Kamath | 407753c | 2015-06-16 12:02:57 +0100 | [diff] [blame] | 958 | static_cast<size_t>(uncompressedLen), mode); |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 959 | ALOGV("Opened compressed entry %s in zip %s mode %d: %p", entryName.c_str(), |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 960 | dataMap->file_name(), mode, pAsset.get()); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 961 | } |
| 962 | if (pAsset == NULL) { |
| 963 | /* unexpected */ |
| 964 | ALOGW("create from segment failed\n"); |
| 965 | } |
| 966 | |
Ryan Mitchell | 80094e3 | 2020-11-16 23:08:18 +0000 | [diff] [blame] | 967 | return pAsset.release(); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 968 | } |
| 969 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 970 | /* |
| 971 | * Open a directory in the asset namespace. |
| 972 | * |
Adam Lesinski | fe90eaf | 2016-10-04 13:31:31 -0700 | [diff] [blame] | 973 | * An "asset directory" is simply the combination of all asset paths' "assets/" directories. |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 974 | * |
| 975 | * Pass in "" for the root dir. |
| 976 | */ |
| 977 | AssetDir* AssetManager::openDir(const char* dirName) |
| 978 | { |
| 979 | AutoMutex _l(mLock); |
| 980 | |
| 981 | AssetDir* pDir = NULL; |
| 982 | SortedVector<AssetDir::FileInfo>* pMergedInfo = NULL; |
| 983 | |
| 984 | LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager"); |
| 985 | assert(dirName != NULL); |
| 986 | |
| 987 | //printf("+++ openDir(%s) in '%s'\n", dirName, (const char*) mAssetBase); |
| 988 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 989 | pDir = new AssetDir; |
| 990 | |
| 991 | /* |
| 992 | * Scan the various directories, merging what we find into a single |
| 993 | * vector. We want to scan them in reverse priority order so that |
| 994 | * the ".EXCLUDE" processing works correctly. Also, if we decide we |
| 995 | * want to remember where the file is coming from, we'll get the right |
| 996 | * version. |
| 997 | * |
| 998 | * We start with Zip archives, then do loose files. |
| 999 | */ |
| 1000 | pMergedInfo = new SortedVector<AssetDir::FileInfo>; |
| 1001 | |
| 1002 | size_t i = mAssetPaths.size(); |
| 1003 | while (i > 0) { |
| 1004 | i--; |
| 1005 | const asset_path& ap = mAssetPaths.itemAt(i); |
| 1006 | if (ap.type == kFileTypeRegular) { |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 1007 | ALOGV("Adding directory %s from zip %s", dirName, ap.path.c_str()); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1008 | scanAndMergeZipLocked(pMergedInfo, ap, kAssetsRoot, dirName); |
| 1009 | } else { |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 1010 | ALOGV("Adding directory %s from dir %s", dirName, ap.path.c_str()); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1011 | scanAndMergeDirLocked(pMergedInfo, ap, kAssetsRoot, dirName); |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | #if 0 |
| 1016 | printf("FILE LIST:\n"); |
| 1017 | for (i = 0; i < (size_t) pMergedInfo->size(); i++) { |
| 1018 | printf(" %d: (%d) '%s'\n", i, |
| 1019 | pMergedInfo->itemAt(i).getFileType(), |
| 1020 | (const char*) pMergedInfo->itemAt(i).getFileName()); |
| 1021 | } |
| 1022 | #endif |
| 1023 | |
| 1024 | pDir->setFileList(pMergedInfo); |
| 1025 | return pDir; |
| 1026 | } |
| 1027 | |
| 1028 | /* |
| 1029 | * Open a directory in the non-asset namespace. |
| 1030 | * |
Adam Lesinski | fe90eaf | 2016-10-04 13:31:31 -0700 | [diff] [blame] | 1031 | * An "asset directory" is simply the combination of all asset paths' "assets/" directories. |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1032 | * |
| 1033 | * Pass in "" for the root dir. |
| 1034 | */ |
Narayan Kamath | a0c6260 | 2014-01-24 13:51:51 +0000 | [diff] [blame] | 1035 | AssetDir* AssetManager::openNonAssetDir(const int32_t cookie, const char* dirName) |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1036 | { |
| 1037 | AutoMutex _l(mLock); |
| 1038 | |
| 1039 | AssetDir* pDir = NULL; |
| 1040 | SortedVector<AssetDir::FileInfo>* pMergedInfo = NULL; |
| 1041 | |
| 1042 | LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager"); |
| 1043 | assert(dirName != NULL); |
| 1044 | |
| 1045 | //printf("+++ openDir(%s) in '%s'\n", dirName, (const char*) mAssetBase); |
| 1046 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1047 | pDir = new AssetDir; |
| 1048 | |
| 1049 | pMergedInfo = new SortedVector<AssetDir::FileInfo>; |
| 1050 | |
Narayan Kamath | a0c6260 | 2014-01-24 13:51:51 +0000 | [diff] [blame] | 1051 | const size_t which = static_cast<size_t>(cookie) - 1; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1052 | |
| 1053 | if (which < mAssetPaths.size()) { |
| 1054 | const asset_path& ap = mAssetPaths.itemAt(which); |
| 1055 | if (ap.type == kFileTypeRegular) { |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 1056 | ALOGV("Adding directory %s from zip %s", dirName, ap.path.c_str()); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1057 | scanAndMergeZipLocked(pMergedInfo, ap, NULL, dirName); |
| 1058 | } else { |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 1059 | ALOGV("Adding directory %s from dir %s", dirName, ap.path.c_str()); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1060 | scanAndMergeDirLocked(pMergedInfo, ap, NULL, dirName); |
| 1061 | } |
| 1062 | } |
| 1063 | |
| 1064 | #if 0 |
| 1065 | printf("FILE LIST:\n"); |
| 1066 | for (i = 0; i < (size_t) pMergedInfo->size(); i++) { |
| 1067 | printf(" %d: (%d) '%s'\n", i, |
| 1068 | pMergedInfo->itemAt(i).getFileType(), |
| 1069 | (const char*) pMergedInfo->itemAt(i).getFileName()); |
| 1070 | } |
| 1071 | #endif |
| 1072 | |
| 1073 | pDir->setFileList(pMergedInfo); |
| 1074 | return pDir; |
| 1075 | } |
| 1076 | |
| 1077 | /* |
| 1078 | * Scan the contents of the specified directory and merge them into the |
| 1079 | * "pMergedInfo" vector, removing previous entries if we find "exclude" |
| 1080 | * directives. |
| 1081 | * |
| 1082 | * Returns "false" if we found nothing to contribute. |
| 1083 | */ |
| 1084 | bool AssetManager::scanAndMergeDirLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo, |
| 1085 | const asset_path& ap, const char* rootDir, const char* dirName) |
| 1086 | { |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1087 | assert(pMergedInfo != NULL); |
| 1088 | |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 1089 | //printf("scanAndMergeDir: %s %s %s\n", ap.path.c_str(), rootDir, dirName); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1090 | |
Adam Lesinski | a77685f | 2016-10-03 16:26:28 -0700 | [diff] [blame] | 1091 | String8 path = createPathNameLocked(ap, rootDir); |
Tomasz Wasilczyk | 804e819 | 2023-08-23 02:22:53 +0000 | [diff] [blame] | 1092 | if (dirName[0] != '\0') appendPath(path, dirName); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1093 | |
Adam Lesinski | a77685f | 2016-10-03 16:26:28 -0700 | [diff] [blame] | 1094 | SortedVector<AssetDir::FileInfo>* pContents = scanDirLocked(path); |
| 1095 | if (pContents == NULL) |
| 1096 | return false; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1097 | |
| 1098 | // if we wanted to do an incremental cache fill, we would do it here |
| 1099 | |
| 1100 | /* |
| 1101 | * Process "exclude" directives. If we find a filename that ends with |
| 1102 | * ".EXCLUDE", we look for a matching entry in the "merged" set, and |
| 1103 | * remove it if we find it. We also delete the "exclude" entry. |
| 1104 | */ |
| 1105 | int i, count, exclExtLen; |
| 1106 | |
| 1107 | count = pContents->size(); |
| 1108 | exclExtLen = strlen(kExcludeExtension); |
| 1109 | for (i = 0; i < count; i++) { |
| 1110 | const char* name; |
| 1111 | int nameLen; |
| 1112 | |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 1113 | name = pContents->itemAt(i).getFileName().c_str(); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1114 | nameLen = strlen(name); |
| 1115 | if (nameLen > exclExtLen && |
| 1116 | strcmp(name + (nameLen - exclExtLen), kExcludeExtension) == 0) |
| 1117 | { |
| 1118 | String8 match(name, nameLen - exclExtLen); |
| 1119 | int matchIdx; |
| 1120 | |
| 1121 | matchIdx = AssetDir::FileInfo::findEntry(pMergedInfo, match); |
| 1122 | if (matchIdx > 0) { |
| 1123 | ALOGV("Excluding '%s' [%s]\n", |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 1124 | pMergedInfo->itemAt(matchIdx).getFileName().c_str(), |
| 1125 | pMergedInfo->itemAt(matchIdx).getSourceName().c_str()); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1126 | pMergedInfo->removeAt(matchIdx); |
| 1127 | } else { |
| 1128 | //printf("+++ no match on '%s'\n", (const char*) match); |
| 1129 | } |
| 1130 | |
| 1131 | ALOGD("HEY: size=%d removing %d\n", (int)pContents->size(), i); |
| 1132 | pContents->removeAt(i); |
| 1133 | i--; // adjust "for" loop |
| 1134 | count--; // and loop limit |
| 1135 | } |
| 1136 | } |
| 1137 | |
| 1138 | mergeInfoLocked(pMergedInfo, pContents); |
| 1139 | |
| 1140 | delete pContents; |
| 1141 | |
| 1142 | return true; |
| 1143 | } |
| 1144 | |
| 1145 | /* |
| 1146 | * Scan the contents of the specified directory, and stuff what we find |
| 1147 | * into a newly-allocated vector. |
| 1148 | * |
| 1149 | * Files ending in ".gz" will have their extensions removed. |
| 1150 | * |
| 1151 | * We should probably think about skipping files with "illegal" names, |
| 1152 | * e.g. illegal characters (/\:) or excessive length. |
| 1153 | * |
| 1154 | * Returns NULL if the specified directory doesn't exist. |
| 1155 | */ |
| 1156 | SortedVector<AssetDir::FileInfo>* AssetManager::scanDirLocked(const String8& path) |
| 1157 | { |
| 1158 | SortedVector<AssetDir::FileInfo>* pContents = NULL; |
| 1159 | DIR* dir; |
| 1160 | struct dirent* entry; |
| 1161 | FileType fileType; |
| 1162 | |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 1163 | ALOGV("Scanning dir '%s'\n", path.c_str()); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1164 | |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 1165 | dir = opendir(path.c_str()); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1166 | if (dir == NULL) |
| 1167 | return NULL; |
| 1168 | |
| 1169 | pContents = new SortedVector<AssetDir::FileInfo>; |
| 1170 | |
| 1171 | while (1) { |
| 1172 | entry = readdir(dir); |
| 1173 | if (entry == NULL) |
| 1174 | break; |
| 1175 | |
| 1176 | if (strcmp(entry->d_name, ".") == 0 || |
| 1177 | strcmp(entry->d_name, "..") == 0) |
| 1178 | continue; |
| 1179 | |
| 1180 | #ifdef _DIRENT_HAVE_D_TYPE |
| 1181 | if (entry->d_type == DT_REG) |
| 1182 | fileType = kFileTypeRegular; |
| 1183 | else if (entry->d_type == DT_DIR) |
| 1184 | fileType = kFileTypeDirectory; |
| 1185 | else |
| 1186 | fileType = kFileTypeUnknown; |
| 1187 | #else |
| 1188 | // stat the file |
Tomasz Wasilczyk | 804e819 | 2023-08-23 02:22:53 +0000 | [diff] [blame] | 1189 | fileType = ::getFileType(appendPathCopy(path, entry->d_name).c_str()); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1190 | #endif |
| 1191 | |
| 1192 | if (fileType != kFileTypeRegular && fileType != kFileTypeDirectory) |
| 1193 | continue; |
| 1194 | |
| 1195 | AssetDir::FileInfo info; |
| 1196 | info.set(String8(entry->d_name), fileType); |
Tomasz Wasilczyk | 804e819 | 2023-08-23 02:22:53 +0000 | [diff] [blame] | 1197 | if (strcasecmp(getPathExtension(info.getFileName()).c_str(), ".gz") == 0) |
| 1198 | info.setFileName(getBasePath(info.getFileName())); |
| 1199 | info.setSourceName(appendPathCopy(path, info.getFileName())); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1200 | pContents->add(info); |
| 1201 | } |
| 1202 | |
| 1203 | closedir(dir); |
| 1204 | return pContents; |
| 1205 | } |
| 1206 | |
| 1207 | /* |
| 1208 | * Scan the contents out of the specified Zip archive, and merge what we |
| 1209 | * find into "pMergedInfo". If the Zip archive in question doesn't exist, |
| 1210 | * we return immediately. |
| 1211 | * |
| 1212 | * Returns "false" if we found nothing to contribute. |
| 1213 | */ |
| 1214 | bool AssetManager::scanAndMergeZipLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo, |
| 1215 | const asset_path& ap, const char* rootDir, const char* baseDirName) |
| 1216 | { |
| 1217 | ZipFileRO* pZip; |
| 1218 | Vector<String8> dirs; |
| 1219 | AssetDir::FileInfo info; |
| 1220 | SortedVector<AssetDir::FileInfo> contents; |
| 1221 | String8 sourceName, zipName, dirName; |
| 1222 | |
| 1223 | pZip = mZipSet.getZip(ap.path); |
| 1224 | if (pZip == NULL) { |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 1225 | ALOGW("Failure opening zip %s\n", ap.path.c_str()); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1226 | return false; |
| 1227 | } |
| 1228 | |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 1229 | zipName = ZipSet::getPathName(ap.path.c_str()); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1230 | |
| 1231 | /* convert "sounds" to "rootDir/sounds" */ |
| 1232 | if (rootDir != NULL) dirName = rootDir; |
Tomasz Wasilczyk | 804e819 | 2023-08-23 02:22:53 +0000 | [diff] [blame] | 1233 | appendPath(dirName, baseDirName); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1234 | |
| 1235 | /* |
| 1236 | * Scan through the list of files, looking for a match. The files in |
| 1237 | * the Zip table of contents are not in sorted order, so we have to |
| 1238 | * process the entire list. We're looking for a string that begins |
| 1239 | * with the characters in "dirName", is followed by a '/', and has no |
| 1240 | * subsequent '/' in the stuff that follows. |
| 1241 | * |
| 1242 | * What makes this especially fun is that directories are not stored |
| 1243 | * explicitly in Zip archives, so we have to infer them from context. |
| 1244 | * When we see "sounds/foo.wav" we have to leave a note to ourselves |
| 1245 | * to insert a directory called "sounds" into the list. We store |
| 1246 | * these in temporary vector so that we only return each one once. |
| 1247 | * |
| 1248 | * Name comparisons are case-sensitive to match UNIX filesystem |
| 1249 | * semantics. |
| 1250 | */ |
| 1251 | int dirNameLen = dirName.length(); |
Narayan Kamath | 560566d | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 1252 | void *iterationCookie; |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 1253 | if (!pZip->startIteration(&iterationCookie, dirName.c_str(), NULL)) { |
Narayan Kamath | 560566d | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 1254 | ALOGW("ZipFileRO::startIteration returned false"); |
| 1255 | return false; |
| 1256 | } |
| 1257 | |
| 1258 | ZipEntryRO entry; |
| 1259 | while ((entry = pZip->nextEntry(iterationCookie)) != NULL) { |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1260 | char nameBuf[256]; |
| 1261 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1262 | if (pZip->getEntryFileName(entry, nameBuf, sizeof(nameBuf)) != 0) { |
| 1263 | // TODO: fix this if we expect to have long names |
| 1264 | ALOGE("ARGH: name too long?\n"); |
| 1265 | continue; |
| 1266 | } |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 1267 | //printf("Comparing %s in %s?\n", nameBuf, dirName.c_str()); |
Yusuke Sato | 05f648e | 2015-08-03 16:21:10 -0700 | [diff] [blame] | 1268 | if (dirNameLen == 0 || nameBuf[dirNameLen] == '/') |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1269 | { |
| 1270 | const char* cp; |
| 1271 | const char* nextSlash; |
| 1272 | |
| 1273 | cp = nameBuf + dirNameLen; |
| 1274 | if (dirNameLen != 0) |
| 1275 | cp++; // advance past the '/' |
| 1276 | |
| 1277 | nextSlash = strchr(cp, '/'); |
| 1278 | //xxx this may break if there are bare directory entries |
| 1279 | if (nextSlash == NULL) { |
| 1280 | /* this is a file in the requested directory */ |
| 1281 | |
Tomasz Wasilczyk | 804e819 | 2023-08-23 02:22:53 +0000 | [diff] [blame] | 1282 | info.set(getPathLeaf(String8(nameBuf)), kFileTypeRegular); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1283 | |
| 1284 | info.setSourceName( |
| 1285 | createZipSourceNameLocked(zipName, dirName, info.getFileName())); |
| 1286 | |
| 1287 | contents.add(info); |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 1288 | //printf("FOUND: file '%s'\n", info.getFileName().c_str()); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1289 | } else { |
| 1290 | /* this is a subdir; add it if we don't already have it*/ |
| 1291 | String8 subdirName(cp, nextSlash - cp); |
| 1292 | size_t j; |
| 1293 | size_t N = dirs.size(); |
| 1294 | |
| 1295 | for (j = 0; j < N; j++) { |
| 1296 | if (subdirName == dirs[j]) { |
| 1297 | break; |
| 1298 | } |
| 1299 | } |
| 1300 | if (j == N) { |
| 1301 | dirs.add(subdirName); |
| 1302 | } |
| 1303 | |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 1304 | //printf("FOUND: dir '%s'\n", subdirName.c_str()); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1305 | } |
| 1306 | } |
| 1307 | } |
| 1308 | |
Narayan Kamath | 560566d | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 1309 | pZip->endIteration(iterationCookie); |
| 1310 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1311 | /* |
| 1312 | * Add the set of unique directories. |
| 1313 | */ |
| 1314 | for (int i = 0; i < (int) dirs.size(); i++) { |
| 1315 | info.set(dirs[i], kFileTypeDirectory); |
| 1316 | info.setSourceName( |
| 1317 | createZipSourceNameLocked(zipName, dirName, info.getFileName())); |
| 1318 | contents.add(info); |
| 1319 | } |
| 1320 | |
| 1321 | mergeInfoLocked(pMergedInfo, &contents); |
| 1322 | |
| 1323 | return true; |
| 1324 | } |
| 1325 | |
| 1326 | |
| 1327 | /* |
| 1328 | * Merge two vectors of FileInfo. |
| 1329 | * |
| 1330 | * The merged contents will be stuffed into *pMergedInfo. |
| 1331 | * |
| 1332 | * If an entry for a file exists in both "pMergedInfo" and "pContents", |
| 1333 | * we use the newer "pContents" entry. |
| 1334 | */ |
| 1335 | void AssetManager::mergeInfoLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo, |
| 1336 | const SortedVector<AssetDir::FileInfo>* pContents) |
| 1337 | { |
| 1338 | /* |
| 1339 | * Merge what we found in this directory with what we found in |
| 1340 | * other places. |
| 1341 | * |
| 1342 | * Two basic approaches: |
| 1343 | * (1) Create a new array that holds the unique values of the two |
| 1344 | * arrays. |
| 1345 | * (2) Take the elements from pContents and shove them into pMergedInfo. |
| 1346 | * |
| 1347 | * Because these are vectors of complex objects, moving elements around |
| 1348 | * inside the vector requires constructing new objects and allocating |
| 1349 | * storage for members. With approach #1, we're always adding to the |
| 1350 | * end, whereas with #2 we could be inserting multiple elements at the |
| 1351 | * front of the vector. Approach #1 requires a full copy of the |
| 1352 | * contents of pMergedInfo, but approach #2 requires the same copy for |
| 1353 | * every insertion at the front of pMergedInfo. |
| 1354 | * |
| 1355 | * (We should probably use a SortedVector interface that allows us to |
| 1356 | * just stuff items in, trusting us to maintain the sort order.) |
| 1357 | */ |
| 1358 | SortedVector<AssetDir::FileInfo>* pNewSorted; |
| 1359 | int mergeMax, contMax; |
| 1360 | int mergeIdx, contIdx; |
| 1361 | |
| 1362 | pNewSorted = new SortedVector<AssetDir::FileInfo>; |
| 1363 | mergeMax = pMergedInfo->size(); |
| 1364 | contMax = pContents->size(); |
| 1365 | mergeIdx = contIdx = 0; |
| 1366 | |
| 1367 | while (mergeIdx < mergeMax || contIdx < contMax) { |
| 1368 | if (mergeIdx == mergeMax) { |
| 1369 | /* hit end of "merge" list, copy rest of "contents" */ |
| 1370 | pNewSorted->add(pContents->itemAt(contIdx)); |
| 1371 | contIdx++; |
| 1372 | } else if (contIdx == contMax) { |
| 1373 | /* hit end of "cont" list, copy rest of "merge" */ |
| 1374 | pNewSorted->add(pMergedInfo->itemAt(mergeIdx)); |
| 1375 | mergeIdx++; |
| 1376 | } else if (pMergedInfo->itemAt(mergeIdx) == pContents->itemAt(contIdx)) |
| 1377 | { |
| 1378 | /* items are identical, add newer and advance both indices */ |
| 1379 | pNewSorted->add(pContents->itemAt(contIdx)); |
| 1380 | mergeIdx++; |
| 1381 | contIdx++; |
| 1382 | } else if (pMergedInfo->itemAt(mergeIdx) < pContents->itemAt(contIdx)) |
| 1383 | { |
| 1384 | /* "merge" is lower, add that one */ |
| 1385 | pNewSorted->add(pMergedInfo->itemAt(mergeIdx)); |
| 1386 | mergeIdx++; |
| 1387 | } else { |
| 1388 | /* "cont" is lower, add that one */ |
| 1389 | assert(pContents->itemAt(contIdx) < pMergedInfo->itemAt(mergeIdx)); |
| 1390 | pNewSorted->add(pContents->itemAt(contIdx)); |
| 1391 | contIdx++; |
| 1392 | } |
| 1393 | } |
| 1394 | |
| 1395 | /* |
| 1396 | * Overwrite the "merged" list with the new stuff. |
| 1397 | */ |
| 1398 | *pMergedInfo = *pNewSorted; |
| 1399 | delete pNewSorted; |
| 1400 | |
| 1401 | #if 0 // for Vector, rather than SortedVector |
| 1402 | int i, j; |
| 1403 | for (i = pContents->size() -1; i >= 0; i--) { |
| 1404 | bool add = true; |
| 1405 | |
| 1406 | for (j = pMergedInfo->size() -1; j >= 0; j--) { |
| 1407 | /* case-sensitive comparisons, to behave like UNIX fs */ |
| 1408 | if (strcmp(pContents->itemAt(i).mFileName, |
| 1409 | pMergedInfo->itemAt(j).mFileName) == 0) |
| 1410 | { |
| 1411 | /* match, don't add this entry */ |
| 1412 | add = false; |
| 1413 | break; |
| 1414 | } |
| 1415 | } |
| 1416 | |
| 1417 | if (add) |
| 1418 | pMergedInfo->add(pContents->itemAt(i)); |
| 1419 | } |
| 1420 | #endif |
| 1421 | } |
| 1422 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1423 | /* |
| 1424 | * =========================================================================== |
| 1425 | * AssetManager::SharedZip |
| 1426 | * =========================================================================== |
| 1427 | */ |
| 1428 | |
| 1429 | |
| 1430 | Mutex AssetManager::SharedZip::gLock; |
| 1431 | DefaultKeyedVector<String8, wp<AssetManager::SharedZip> > AssetManager::SharedZip::gOpen; |
| 1432 | |
| 1433 | AssetManager::SharedZip::SharedZip(const String8& path, time_t modWhen) |
| 1434 | : mPath(path), mZipFile(NULL), mModWhen(modWhen), |
| 1435 | mResourceTableAsset(NULL), mResourceTable(NULL) |
| 1436 | { |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 1437 | if (kIsDebug) { |
Tomasz Wasilczyk | 835dfe5 | 2023-08-17 16:27:22 +0000 | [diff] [blame] | 1438 | ALOGI("Creating SharedZip %p %s\n", this, mPath.c_str()); |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 1439 | } |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 1440 | ALOGV("+++ opening zip '%s'\n", mPath.c_str()); |
| 1441 | mZipFile = ZipFileRO::open(mPath.c_str()); |
Narayan Kamath | 560566d | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 1442 | if (mZipFile == NULL) { |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 1443 | ALOGD("failed to open Zip archive '%s'\n", mPath.c_str()); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1444 | } |
| 1445 | } |
| 1446 | |
Dianne Hackborn | ca3872c | 2017-10-30 14:19:32 -0700 | [diff] [blame] | 1447 | AssetManager::SharedZip::SharedZip(int fd, const String8& path) |
| 1448 | : mPath(path), mZipFile(NULL), mModWhen(0), |
| 1449 | mResourceTableAsset(NULL), mResourceTable(NULL) |
| 1450 | { |
| 1451 | if (kIsDebug) { |
Tomasz Wasilczyk | 835dfe5 | 2023-08-17 16:27:22 +0000 | [diff] [blame] | 1452 | ALOGI("Creating SharedZip %p fd=%d %s\n", this, fd, mPath.c_str()); |
Dianne Hackborn | ca3872c | 2017-10-30 14:19:32 -0700 | [diff] [blame] | 1453 | } |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 1454 | ALOGV("+++ opening zip fd=%d '%s'\n", fd, mPath.c_str()); |
| 1455 | mZipFile = ZipFileRO::openFd(fd, mPath.c_str()); |
Dianne Hackborn | ca3872c | 2017-10-30 14:19:32 -0700 | [diff] [blame] | 1456 | if (mZipFile == NULL) { |
| 1457 | ::close(fd); |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 1458 | ALOGD("failed to open Zip archive fd=%d '%s'\n", fd, mPath.c_str()); |
Dianne Hackborn | ca3872c | 2017-10-30 14:19:32 -0700 | [diff] [blame] | 1459 | } |
| 1460 | } |
| 1461 | |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 1462 | sp<AssetManager::SharedZip> AssetManager::SharedZip::get(const String8& path, |
| 1463 | bool createIfNotPresent) |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1464 | { |
| 1465 | AutoMutex _l(gLock); |
Tomasz Wasilczyk | 835dfe5 | 2023-08-17 16:27:22 +0000 | [diff] [blame] | 1466 | time_t modWhen = getFileModDate(path.c_str()); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1467 | sp<SharedZip> zip = gOpen.valueFor(path).promote(); |
| 1468 | if (zip != NULL && zip->mModWhen == modWhen) { |
| 1469 | return zip; |
| 1470 | } |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 1471 | if (zip == NULL && !createIfNotPresent) { |
| 1472 | return NULL; |
| 1473 | } |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1474 | zip = new SharedZip(path, modWhen); |
| 1475 | gOpen.add(path, zip); |
| 1476 | return zip; |
Dianne Hackborn | ca3872c | 2017-10-30 14:19:32 -0700 | [diff] [blame] | 1477 | } |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1478 | |
Dianne Hackborn | ca3872c | 2017-10-30 14:19:32 -0700 | [diff] [blame] | 1479 | sp<AssetManager::SharedZip> AssetManager::SharedZip::create(int fd, const String8& path) |
| 1480 | { |
| 1481 | return new SharedZip(fd, path); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1482 | } |
| 1483 | |
| 1484 | ZipFileRO* AssetManager::SharedZip::getZip() |
| 1485 | { |
| 1486 | return mZipFile; |
| 1487 | } |
| 1488 | |
| 1489 | Asset* AssetManager::SharedZip::getResourceTableAsset() |
| 1490 | { |
songjinshi | 49921f2 | 2016-09-08 15:24:30 +0800 | [diff] [blame] | 1491 | AutoMutex _l(gLock); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1492 | ALOGV("Getting from SharedZip %p resource asset %p\n", this, mResourceTableAsset); |
| 1493 | return mResourceTableAsset; |
| 1494 | } |
| 1495 | |
| 1496 | Asset* AssetManager::SharedZip::setResourceTableAsset(Asset* asset) |
| 1497 | { |
| 1498 | { |
| 1499 | AutoMutex _l(gLock); |
| 1500 | if (mResourceTableAsset == NULL) { |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1501 | // This is not thread safe the first time it is called, so |
| 1502 | // do it here with the global lock held. |
| 1503 | asset->getBuffer(true); |
songjinshi | 49921f2 | 2016-09-08 15:24:30 +0800 | [diff] [blame] | 1504 | mResourceTableAsset = asset; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1505 | return asset; |
| 1506 | } |
| 1507 | } |
| 1508 | delete asset; |
| 1509 | return mResourceTableAsset; |
| 1510 | } |
| 1511 | |
| 1512 | ResTable* AssetManager::SharedZip::getResourceTable() |
| 1513 | { |
| 1514 | ALOGV("Getting from SharedZip %p resource table %p\n", this, mResourceTable); |
| 1515 | return mResourceTable; |
| 1516 | } |
| 1517 | |
| 1518 | ResTable* AssetManager::SharedZip::setResourceTable(ResTable* res) |
| 1519 | { |
| 1520 | { |
| 1521 | AutoMutex _l(gLock); |
| 1522 | if (mResourceTable == NULL) { |
| 1523 | mResourceTable = res; |
| 1524 | return res; |
| 1525 | } |
| 1526 | } |
| 1527 | delete res; |
| 1528 | return mResourceTable; |
| 1529 | } |
| 1530 | |
| 1531 | bool AssetManager::SharedZip::isUpToDate() |
| 1532 | { |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 1533 | time_t modWhen = getFileModDate(mPath.c_str()); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1534 | return mModWhen == modWhen; |
| 1535 | } |
| 1536 | |
Jaekyun Seok | 7de2f9c | 2017-03-02 12:45:10 +0900 | [diff] [blame] | 1537 | void AssetManager::SharedZip::addOverlay(const asset_path& ap) |
| 1538 | { |
| 1539 | mOverlays.add(ap); |
| 1540 | } |
| 1541 | |
| 1542 | bool AssetManager::SharedZip::getOverlay(size_t idx, asset_path* out) const |
| 1543 | { |
| 1544 | if (idx >= mOverlays.size()) { |
| 1545 | return false; |
| 1546 | } |
| 1547 | *out = mOverlays[idx]; |
| 1548 | return true; |
| 1549 | } |
| 1550 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1551 | AssetManager::SharedZip::~SharedZip() |
| 1552 | { |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 1553 | if (kIsDebug) { |
Tomasz Wasilczyk | 835dfe5 | 2023-08-17 16:27:22 +0000 | [diff] [blame] | 1554 | ALOGI("Destroying SharedZip %p %s\n", this, mPath.c_str()); |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 1555 | } |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1556 | if (mResourceTable != NULL) { |
| 1557 | delete mResourceTable; |
| 1558 | } |
| 1559 | if (mResourceTableAsset != NULL) { |
| 1560 | delete mResourceTableAsset; |
| 1561 | } |
| 1562 | if (mZipFile != NULL) { |
| 1563 | delete mZipFile; |
Tomasz Wasilczyk | d2a6983 | 2023-08-10 23:54:44 +0000 | [diff] [blame] | 1564 | ALOGV("Closed '%s'\n", mPath.c_str()); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1565 | } |
| 1566 | } |
| 1567 | |
| 1568 | /* |
| 1569 | * =========================================================================== |
| 1570 | * AssetManager::ZipSet |
| 1571 | * =========================================================================== |
| 1572 | */ |
| 1573 | |
| 1574 | /* |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1575 | * Destructor. Close any open archives. |
| 1576 | */ |
| 1577 | AssetManager::ZipSet::~ZipSet(void) |
| 1578 | { |
| 1579 | size_t N = mZipFile.size(); |
| 1580 | for (size_t i = 0; i < N; i++) |
| 1581 | closeZip(i); |
| 1582 | } |
| 1583 | |
| 1584 | /* |
| 1585 | * Close a Zip file and reset the entry. |
| 1586 | */ |
| 1587 | void AssetManager::ZipSet::closeZip(int idx) |
| 1588 | { |
| 1589 | mZipFile.editItemAt(idx) = NULL; |
| 1590 | } |
| 1591 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1592 | /* |
| 1593 | * Retrieve the appropriate Zip file from the set. |
| 1594 | */ |
| 1595 | ZipFileRO* AssetManager::ZipSet::getZip(const String8& path) |
| 1596 | { |
Dianne Hackborn | ca3872c | 2017-10-30 14:19:32 -0700 | [diff] [blame] | 1597 | return getSharedZip(path)->getZip(); |
| 1598 | } |
| 1599 | |
| 1600 | const sp<AssetManager::SharedZip> AssetManager::ZipSet::getSharedZip(const String8& path) |
| 1601 | { |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1602 | int idx = getIndex(path); |
| 1603 | sp<SharedZip> zip = mZipFile[idx]; |
| 1604 | if (zip == NULL) { |
| 1605 | zip = SharedZip::get(path); |
| 1606 | mZipFile.editItemAt(idx) = zip; |
| 1607 | } |
Dianne Hackborn | ca3872c | 2017-10-30 14:19:32 -0700 | [diff] [blame] | 1608 | return zip; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1609 | } |
| 1610 | |
| 1611 | Asset* AssetManager::ZipSet::getZipResourceTableAsset(const String8& path) |
| 1612 | { |
| 1613 | int idx = getIndex(path); |
| 1614 | sp<SharedZip> zip = mZipFile[idx]; |
| 1615 | if (zip == NULL) { |
| 1616 | zip = SharedZip::get(path); |
| 1617 | mZipFile.editItemAt(idx) = zip; |
| 1618 | } |
| 1619 | return zip->getResourceTableAsset(); |
| 1620 | } |
| 1621 | |
| 1622 | Asset* AssetManager::ZipSet::setZipResourceTableAsset(const String8& path, |
| 1623 | Asset* asset) |
| 1624 | { |
| 1625 | int idx = getIndex(path); |
| 1626 | sp<SharedZip> zip = mZipFile[idx]; |
| 1627 | // doesn't make sense to call before previously accessing. |
| 1628 | return zip->setResourceTableAsset(asset); |
| 1629 | } |
| 1630 | |
| 1631 | ResTable* AssetManager::ZipSet::getZipResourceTable(const String8& path) |
| 1632 | { |
| 1633 | int idx = getIndex(path); |
| 1634 | sp<SharedZip> zip = mZipFile[idx]; |
| 1635 | if (zip == NULL) { |
| 1636 | zip = SharedZip::get(path); |
| 1637 | mZipFile.editItemAt(idx) = zip; |
| 1638 | } |
| 1639 | return zip->getResourceTable(); |
| 1640 | } |
| 1641 | |
| 1642 | ResTable* AssetManager::ZipSet::setZipResourceTable(const String8& path, |
| 1643 | ResTable* res) |
| 1644 | { |
| 1645 | int idx = getIndex(path); |
| 1646 | sp<SharedZip> zip = mZipFile[idx]; |
| 1647 | // doesn't make sense to call before previously accessing. |
| 1648 | return zip->setResourceTable(res); |
| 1649 | } |
| 1650 | |
| 1651 | /* |
| 1652 | * Generate the partial pathname for the specified archive. The caller |
| 1653 | * gets to prepend the asset root directory. |
| 1654 | * |
| 1655 | * Returns something like "common/en-US-noogle.jar". |
| 1656 | */ |
| 1657 | /*static*/ String8 AssetManager::ZipSet::getPathName(const char* zipPath) |
| 1658 | { |
| 1659 | return String8(zipPath); |
| 1660 | } |
| 1661 | |
| 1662 | bool AssetManager::ZipSet::isUpToDate() |
| 1663 | { |
| 1664 | const size_t N = mZipFile.size(); |
| 1665 | for (size_t i=0; i<N; i++) { |
| 1666 | if (mZipFile[i] != NULL && !mZipFile[i]->isUpToDate()) { |
| 1667 | return false; |
| 1668 | } |
| 1669 | } |
| 1670 | return true; |
| 1671 | } |
| 1672 | |
Jaekyun Seok | 7de2f9c | 2017-03-02 12:45:10 +0900 | [diff] [blame] | 1673 | void AssetManager::ZipSet::addOverlay(const String8& path, const asset_path& overlay) |
| 1674 | { |
| 1675 | int idx = getIndex(path); |
| 1676 | sp<SharedZip> zip = mZipFile[idx]; |
| 1677 | zip->addOverlay(overlay); |
| 1678 | } |
| 1679 | |
| 1680 | bool AssetManager::ZipSet::getOverlay(const String8& path, size_t idx, asset_path* out) const |
| 1681 | { |
| 1682 | sp<SharedZip> zip = SharedZip::get(path, false); |
| 1683 | if (zip == NULL) { |
| 1684 | return false; |
| 1685 | } |
| 1686 | return zip->getOverlay(idx, out); |
| 1687 | } |
| 1688 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1689 | /* |
| 1690 | * Compute the zip file's index. |
| 1691 | * |
| 1692 | * "appName", "locale", and "vendor" should be set to NULL to indicate the |
| 1693 | * default directory. |
| 1694 | */ |
| 1695 | int AssetManager::ZipSet::getIndex(const String8& zip) const |
| 1696 | { |
| 1697 | const size_t N = mZipPath.size(); |
| 1698 | for (size_t i=0; i<N; i++) { |
| 1699 | if (mZipPath[i] == zip) { |
| 1700 | return i; |
| 1701 | } |
| 1702 | } |
| 1703 | |
| 1704 | mZipPath.add(zip); |
| 1705 | mZipFile.add(NULL); |
| 1706 | |
| 1707 | return mZipPath.size()-1; |
| 1708 | } |