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