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> |
| 31 | #include <utils/Atomic.h> |
| 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> |
| 37 | #ifdef HAVE_ANDROID_OS |
| 38 | #include <cutils/trace.h> |
| 39 | #endif |
| 40 | |
| 41 | #include <assert.h> |
| 42 | #include <dirent.h> |
| 43 | #include <errno.h> |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 44 | #include <string.h> // strerror |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 45 | #include <strings.h> |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 46 | |
| 47 | #ifndef TEMP_FAILURE_RETRY |
| 48 | /* Used to retry syscalls that can return EINTR. */ |
| 49 | #define TEMP_FAILURE_RETRY(exp) ({ \ |
| 50 | typeof (exp) _rc; \ |
| 51 | do { \ |
| 52 | _rc = (exp); \ |
| 53 | } while (_rc == -1 && errno == EINTR); \ |
| 54 | _rc; }) |
| 55 | #endif |
| 56 | |
| 57 | #ifdef HAVE_ANDROID_OS |
| 58 | #define MY_TRACE_BEGIN(x) ATRACE_BEGIN(x) |
| 59 | #define MY_TRACE_END() ATRACE_END() |
| 60 | #else |
| 61 | #define MY_TRACE_BEGIN(x) |
| 62 | #define MY_TRACE_END() |
| 63 | #endif |
| 64 | |
| 65 | using namespace android; |
| 66 | |
| 67 | /* |
| 68 | * Names for default app, locale, and vendor. We might want to change |
| 69 | * these to be an actual locale, e.g. always use en-US as the default. |
| 70 | */ |
| 71 | static const char* kDefaultLocale = "default"; |
| 72 | static const char* kDefaultVendor = "default"; |
| 73 | static const char* kAssetsRoot = "assets"; |
| 74 | static const char* kAppZipName = NULL; //"classes.jar"; |
| 75 | static const char* kSystemAssets = "framework/framework-res.apk"; |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 76 | static const char* kResourceCache = "resource-cache"; |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 77 | static const char* kAndroidManifest = "AndroidManifest.xml"; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 78 | |
| 79 | static const char* kExcludeExtension = ".EXCLUDE"; |
| 80 | |
| 81 | static Asset* const kExcludedAsset = (Asset*) 0xd000000d; |
| 82 | |
| 83 | static volatile int32_t gCount = 0; |
| 84 | |
Mårten Kongstad | 65a05fd | 2014-01-31 14:01:52 +0100 | [diff] [blame] | 85 | const char* AssetManager::RESOURCES_FILENAME = "resources.arsc"; |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 86 | const char* AssetManager::IDMAP_BIN = "/system/bin/idmap"; |
| 87 | const char* AssetManager::OVERLAY_DIR = "/vendor/overlay"; |
| 88 | const char* AssetManager::TARGET_PACKAGE_NAME = "android"; |
| 89 | const char* AssetManager::TARGET_APK_PATH = "/system/framework/framework-res.apk"; |
| 90 | const char* AssetManager::IDMAP_DIR = "/data/resource-cache"; |
Mårten Kongstad | 65a05fd | 2014-01-31 14:01:52 +0100 | [diff] [blame] | 91 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 92 | namespace { |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 93 | String8 idmapPathForPackagePath(const String8& pkgPath) |
| 94 | { |
| 95 | const char* root = getenv("ANDROID_DATA"); |
| 96 | LOG_ALWAYS_FATAL_IF(root == NULL, "ANDROID_DATA not set"); |
| 97 | String8 path(root); |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 98 | path.appendPath(kResourceCache); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 99 | |
| 100 | char buf[256]; // 256 chars should be enough for anyone... |
| 101 | strncpy(buf, pkgPath.string(), 255); |
| 102 | buf[255] = '\0'; |
| 103 | char* filename = buf; |
| 104 | while (*filename && *filename == '/') { |
| 105 | ++filename; |
| 106 | } |
| 107 | char* p = filename; |
| 108 | while (*p) { |
| 109 | if (*p == '/') { |
| 110 | *p = '@'; |
| 111 | } |
| 112 | ++p; |
| 113 | } |
| 114 | path.appendPath(filename); |
| 115 | path.append("@idmap"); |
| 116 | |
| 117 | return path; |
| 118 | } |
| 119 | |
| 120 | /* |
| 121 | * Like strdup(), but uses C++ "new" operator instead of malloc. |
| 122 | */ |
| 123 | static char* strdupNew(const char* str) |
| 124 | { |
| 125 | char* newStr; |
| 126 | int len; |
| 127 | |
| 128 | if (str == NULL) |
| 129 | return NULL; |
| 130 | |
| 131 | len = strlen(str); |
| 132 | newStr = new char[len+1]; |
| 133 | memcpy(newStr, str, len+1); |
| 134 | |
| 135 | return newStr; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | * =========================================================================== |
| 141 | * AssetManager |
| 142 | * =========================================================================== |
| 143 | */ |
| 144 | |
| 145 | int32_t AssetManager::getGlobalCount() |
| 146 | { |
| 147 | return gCount; |
| 148 | } |
| 149 | |
| 150 | AssetManager::AssetManager(CacheMode cacheMode) |
| 151 | : mLocale(NULL), mVendor(NULL), |
| 152 | mResources(NULL), mConfig(new ResTable_config), |
| 153 | mCacheMode(cacheMode), mCacheValid(false) |
| 154 | { |
| 155 | int count = android_atomic_inc(&gCount)+1; |
| 156 | //ALOGI("Creating AssetManager %p #%d\n", this, count); |
| 157 | memset(mConfig, 0, sizeof(ResTable_config)); |
| 158 | } |
| 159 | |
| 160 | AssetManager::~AssetManager(void) |
| 161 | { |
| 162 | int count = android_atomic_dec(&gCount); |
| 163 | //ALOGI("Destroying AssetManager in %p #%d\n", this, count); |
| 164 | |
| 165 | delete mConfig; |
| 166 | delete mResources; |
| 167 | |
| 168 | // don't have a String class yet, so make sure we clean up |
| 169 | delete[] mLocale; |
| 170 | delete[] mVendor; |
| 171 | } |
| 172 | |
Narayan Kamath | a0c6260 | 2014-01-24 13:51:51 +0000 | [diff] [blame] | 173 | bool AssetManager::addAssetPath(const String8& path, int32_t* cookie) |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 174 | { |
| 175 | AutoMutex _l(mLock); |
| 176 | |
| 177 | asset_path ap; |
| 178 | |
| 179 | String8 realPath(path); |
| 180 | if (kAppZipName) { |
| 181 | realPath.appendPath(kAppZipName); |
| 182 | } |
| 183 | ap.type = ::getFileType(realPath.string()); |
| 184 | if (ap.type == kFileTypeRegular) { |
| 185 | ap.path = realPath; |
| 186 | } else { |
| 187 | ap.path = path; |
| 188 | ap.type = ::getFileType(path.string()); |
| 189 | if (ap.type != kFileTypeDirectory && ap.type != kFileTypeRegular) { |
| 190 | ALOGW("Asset path %s is neither a directory nor file (type=%d).", |
| 191 | path.string(), (int)ap.type); |
| 192 | return false; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | // Skip if we have it already. |
| 197 | for (size_t i=0; i<mAssetPaths.size(); i++) { |
| 198 | if (mAssetPaths[i].path == ap.path) { |
| 199 | if (cookie) { |
Narayan Kamath | a0c6260 | 2014-01-24 13:51:51 +0000 | [diff] [blame] | 200 | *cookie = static_cast<int32_t>(i+1); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 201 | } |
| 202 | return true; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | ALOGV("In %p Asset %s path: %s", this, |
| 207 | ap.type == kFileTypeDirectory ? "dir" : "zip", ap.path.string()); |
| 208 | |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 209 | // Check that the path has an AndroidManifest.xml |
| 210 | Asset* manifestAsset = const_cast<AssetManager*>(this)->openNonAssetInPathLocked( |
| 211 | kAndroidManifest, Asset::ACCESS_BUFFER, ap); |
| 212 | if (manifestAsset == NULL) { |
| 213 | // This asset path does not contain any resources. |
| 214 | delete manifestAsset; |
| 215 | return false; |
| 216 | } |
| 217 | delete manifestAsset; |
| 218 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 219 | mAssetPaths.add(ap); |
| 220 | |
| 221 | // new paths are always added at the end |
| 222 | if (cookie) { |
Narayan Kamath | a0c6260 | 2014-01-24 13:51:51 +0000 | [diff] [blame] | 223 | *cookie = static_cast<int32_t>(mAssetPaths.size()); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 224 | } |
| 225 | |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 226 | #ifdef HAVE_ANDROID_OS |
| 227 | // Load overlays, if any |
| 228 | asset_path oap; |
| 229 | for (size_t idx = 0; mZipSet.getOverlay(ap.path, idx, &oap); idx++) { |
| 230 | mAssetPaths.add(oap); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 231 | } |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 232 | #endif |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 233 | |
| 234 | return true; |
| 235 | } |
| 236 | |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 237 | bool AssetManager::addOverlayPath(const String8& packagePath, int32_t* cookie) |
| 238 | { |
| 239 | const String8 idmapPath = idmapPathForPackagePath(packagePath); |
| 240 | |
| 241 | AutoMutex _l(mLock); |
| 242 | |
| 243 | for (size_t i = 0; i < mAssetPaths.size(); ++i) { |
| 244 | if (mAssetPaths[i].idmap == idmapPath) { |
| 245 | *cookie = static_cast<int32_t>(i + 1); |
| 246 | return true; |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | Asset* idmap = NULL; |
| 251 | if ((idmap = openAssetFromFileLocked(idmapPath, Asset::ACCESS_BUFFER)) == NULL) { |
| 252 | ALOGW("failed to open idmap file %s\n", idmapPath.string()); |
| 253 | return false; |
| 254 | } |
| 255 | |
| 256 | String8 targetPath; |
| 257 | String8 overlayPath; |
| 258 | if (!ResTable::getIdmapInfo(idmap->getBuffer(false), idmap->getLength(), |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 259 | NULL, NULL, NULL, &targetPath, &overlayPath)) { |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 260 | ALOGW("failed to read idmap file %s\n", idmapPath.string()); |
| 261 | delete idmap; |
| 262 | return false; |
| 263 | } |
| 264 | delete idmap; |
| 265 | |
| 266 | if (overlayPath != packagePath) { |
| 267 | ALOGW("idmap file %s inconcistent: expected path %s does not match actual path %s\n", |
| 268 | idmapPath.string(), packagePath.string(), overlayPath.string()); |
| 269 | return false; |
| 270 | } |
| 271 | if (access(targetPath.string(), R_OK) != 0) { |
| 272 | ALOGW("failed to access file %s: %s\n", targetPath.string(), strerror(errno)); |
| 273 | return false; |
| 274 | } |
| 275 | if (access(idmapPath.string(), R_OK) != 0) { |
| 276 | ALOGW("failed to access file %s: %s\n", idmapPath.string(), strerror(errno)); |
| 277 | return false; |
| 278 | } |
| 279 | if (access(overlayPath.string(), R_OK) != 0) { |
| 280 | ALOGW("failed to access file %s: %s\n", overlayPath.string(), strerror(errno)); |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | asset_path oap; |
| 285 | oap.path = overlayPath; |
| 286 | oap.type = ::getFileType(overlayPath.string()); |
| 287 | oap.idmap = idmapPath; |
| 288 | #if 0 |
| 289 | ALOGD("Overlay added: targetPath=%s overlayPath=%s idmapPath=%s\n", |
| 290 | targetPath.string(), overlayPath.string(), idmapPath.string()); |
| 291 | #endif |
| 292 | mAssetPaths.add(oap); |
| 293 | *cookie = static_cast<int32_t>(mAssetPaths.size()); |
| 294 | |
| 295 | return true; |
| 296 | } |
| 297 | |
Mårten Kongstad | 65a05fd | 2014-01-31 14:01:52 +0100 | [diff] [blame] | 298 | bool AssetManager::createIdmap(const char* targetApkPath, const char* overlayApkPath, |
Dianne Hackborn | 32bb5fa | 2014-02-11 13:56:21 -0800 | [diff] [blame] | 299 | 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] | 300 | { |
| 301 | AutoMutex _l(mLock); |
| 302 | const String8 paths[2] = { String8(targetApkPath), String8(overlayApkPath) }; |
| 303 | ResTable tables[2]; |
| 304 | |
| 305 | for (int i = 0; i < 2; ++i) { |
| 306 | asset_path ap; |
| 307 | ap.type = kFileTypeRegular; |
| 308 | ap.path = paths[i]; |
| 309 | Asset* ass = openNonAssetInPathLocked("resources.arsc", Asset::ACCESS_BUFFER, ap); |
| 310 | if (ass == NULL) { |
| 311 | ALOGW("failed to find resources.arsc in %s\n", ap.path.string()); |
| 312 | return false; |
| 313 | } |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 314 | tables[i].add(ass); |
Mårten Kongstad | 65a05fd | 2014-01-31 14:01:52 +0100 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | return tables[0].createIdmap(tables[1], targetCrc, overlayCrc, |
| 318 | targetApkPath, overlayApkPath, (void**)outData, outSize) == NO_ERROR; |
| 319 | } |
| 320 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 321 | bool AssetManager::addDefaultAssets() |
| 322 | { |
| 323 | const char* root = getenv("ANDROID_ROOT"); |
| 324 | LOG_ALWAYS_FATAL_IF(root == NULL, "ANDROID_ROOT not set"); |
| 325 | |
| 326 | String8 path(root); |
| 327 | path.appendPath(kSystemAssets); |
| 328 | |
| 329 | return addAssetPath(path, NULL); |
| 330 | } |
| 331 | |
Narayan Kamath | a0c6260 | 2014-01-24 13:51:51 +0000 | [diff] [blame] | 332 | int32_t AssetManager::nextAssetPath(const int32_t cookie) const |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 333 | { |
| 334 | AutoMutex _l(mLock); |
Narayan Kamath | a0c6260 | 2014-01-24 13:51:51 +0000 | [diff] [blame] | 335 | const size_t next = static_cast<size_t>(cookie) + 1; |
| 336 | return next > mAssetPaths.size() ? -1 : next; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 337 | } |
| 338 | |
Narayan Kamath | a0c6260 | 2014-01-24 13:51:51 +0000 | [diff] [blame] | 339 | String8 AssetManager::getAssetPath(const int32_t cookie) const |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 340 | { |
| 341 | AutoMutex _l(mLock); |
Narayan Kamath | a0c6260 | 2014-01-24 13:51:51 +0000 | [diff] [blame] | 342 | const size_t which = static_cast<size_t>(cookie) - 1; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 343 | if (which < mAssetPaths.size()) { |
| 344 | return mAssetPaths[which].path; |
| 345 | } |
| 346 | return String8(); |
| 347 | } |
| 348 | |
| 349 | /* |
| 350 | * Set the current locale. Use NULL to indicate no locale. |
| 351 | * |
| 352 | * Close and reopen Zip archives as appropriate, and reset cached |
| 353 | * information in the locale-specific sections of the tree. |
| 354 | */ |
| 355 | void AssetManager::setLocale(const char* locale) |
| 356 | { |
| 357 | AutoMutex _l(mLock); |
| 358 | setLocaleLocked(locale); |
| 359 | } |
| 360 | |
Narayan Kamath | e4345db | 2014-06-26 16:01:28 +0100 | [diff] [blame] | 361 | |
| 362 | static const char kFilPrefix[] = "fil"; |
| 363 | static const char kTlPrefix[] = "tl"; |
| 364 | |
| 365 | // The sizes of the prefixes, excluding the 0 suffix. |
| 366 | // char. |
| 367 | static const int kFilPrefixLen = sizeof(kFilPrefix) - 1; |
| 368 | static const int kTlPrefixLen = sizeof(kTlPrefix) - 1; |
| 369 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 370 | void AssetManager::setLocaleLocked(const char* locale) |
| 371 | { |
| 372 | if (mLocale != NULL) { |
| 373 | /* previously set, purge cached data */ |
| 374 | purgeFileNameCacheLocked(); |
| 375 | //mZipSet.purgeLocale(); |
| 376 | delete[] mLocale; |
| 377 | } |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 378 | |
Narayan Kamath | e4345db | 2014-06-26 16:01:28 +0100 | [diff] [blame] | 379 | |
| 380 | // If we're attempting to set a locale that starts with "fil", |
| 381 | // we should convert it to "tl" for backwards compatibility since |
| 382 | // we've been using "tl" instead of "fil" prior to L. |
| 383 | // |
| 384 | // If the resource table already has entries for "fil", we use that |
| 385 | // instead of attempting a fallback. |
| 386 | if (strncmp(locale, kFilPrefix, kFilPrefixLen) == 0) { |
| 387 | Vector<String8> locales; |
| 388 | getLocales(&locales); |
| 389 | const size_t localesSize = locales.size(); |
| 390 | bool hasFil = false; |
| 391 | for (size_t i = 0; i < localesSize; ++i) { |
| 392 | if (locales[i].find(kFilPrefix) == 0) { |
| 393 | hasFil = true; |
| 394 | break; |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | |
| 399 | if (!hasFil) { |
| 400 | const size_t newLocaleLen = strlen(locale); |
| 401 | // This isn't a bug. We really do want mLocale to be 1 byte |
| 402 | // shorter than locale, because we're replacing "fil-" with |
| 403 | // "tl-". |
| 404 | mLocale = new char[newLocaleLen]; |
| 405 | // Copy over "tl". |
| 406 | memcpy(mLocale, kTlPrefix, kTlPrefixLen); |
| 407 | // Copy the rest of |locale|, including the terminating '\0'. |
| 408 | memcpy(mLocale + kTlPrefixLen, locale + kFilPrefixLen, |
| 409 | newLocaleLen - kFilPrefixLen + 1); |
| 410 | updateResourceParamsLocked(); |
| 411 | return; |
| 412 | } |
| 413 | } |
| 414 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 415 | mLocale = strdupNew(locale); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 416 | updateResourceParamsLocked(); |
| 417 | } |
| 418 | |
| 419 | /* |
| 420 | * Set the current vendor. Use NULL to indicate no vendor. |
| 421 | * |
| 422 | * Close and reopen Zip archives as appropriate, and reset cached |
| 423 | * information in the vendor-specific sections of the tree. |
| 424 | */ |
| 425 | void AssetManager::setVendor(const char* vendor) |
| 426 | { |
| 427 | AutoMutex _l(mLock); |
| 428 | |
| 429 | if (mVendor != NULL) { |
| 430 | /* previously set, purge cached data */ |
| 431 | purgeFileNameCacheLocked(); |
| 432 | //mZipSet.purgeVendor(); |
| 433 | delete[] mVendor; |
| 434 | } |
| 435 | mVendor = strdupNew(vendor); |
| 436 | } |
| 437 | |
| 438 | void AssetManager::setConfiguration(const ResTable_config& config, const char* locale) |
| 439 | { |
| 440 | AutoMutex _l(mLock); |
| 441 | *mConfig = config; |
| 442 | if (locale) { |
| 443 | setLocaleLocked(locale); |
| 444 | } else if (config.language[0] != 0) { |
Narayan Kamath | 91447d8 | 2014-01-21 15:32:36 +0000 | [diff] [blame] | 445 | char spec[RESTABLE_MAX_LOCALE_LEN]; |
| 446 | config.getBcp47Locale(spec); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 447 | setLocaleLocked(spec); |
| 448 | } else { |
| 449 | updateResourceParamsLocked(); |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | void AssetManager::getConfiguration(ResTable_config* outConfig) const |
| 454 | { |
| 455 | AutoMutex _l(mLock); |
| 456 | *outConfig = *mConfig; |
| 457 | } |
| 458 | |
| 459 | /* |
| 460 | * Open an asset. |
| 461 | * |
| 462 | * The data could be; |
| 463 | * - In a file on disk (assetBase + fileName). |
| 464 | * - In a compressed file on disk (assetBase + fileName.gz). |
| 465 | * - In a Zip archive, uncompressed or compressed. |
| 466 | * |
| 467 | * It can be in a number of different directories and Zip archives. |
| 468 | * The search order is: |
| 469 | * - [appname] |
| 470 | * - locale + vendor |
| 471 | * - "default" + vendor |
| 472 | * - locale + "default" |
| 473 | * - "default + "default" |
| 474 | * - "common" |
| 475 | * - (same as above) |
| 476 | * |
| 477 | * To find a particular file, we have to try up to eight paths with |
| 478 | * all three forms of data. |
| 479 | * |
| 480 | * We should probably reject requests for "illegal" filenames, e.g. those |
| 481 | * with illegal characters or "../" backward relative paths. |
| 482 | */ |
| 483 | Asset* AssetManager::open(const char* fileName, AccessMode mode) |
| 484 | { |
| 485 | AutoMutex _l(mLock); |
| 486 | |
| 487 | LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager"); |
| 488 | |
| 489 | |
| 490 | if (mCacheMode != CACHE_OFF && !mCacheValid) |
| 491 | loadFileNameCacheLocked(); |
| 492 | |
| 493 | String8 assetName(kAssetsRoot); |
| 494 | assetName.appendPath(fileName); |
| 495 | |
| 496 | /* |
| 497 | * For each top-level asset path, search for the asset. |
| 498 | */ |
| 499 | |
| 500 | size_t i = mAssetPaths.size(); |
| 501 | while (i > 0) { |
| 502 | i--; |
| 503 | ALOGV("Looking for asset '%s' in '%s'\n", |
| 504 | assetName.string(), mAssetPaths.itemAt(i).path.string()); |
| 505 | Asset* pAsset = openNonAssetInPathLocked(assetName.string(), mode, mAssetPaths.itemAt(i)); |
| 506 | if (pAsset != NULL) { |
| 507 | return pAsset != kExcludedAsset ? pAsset : NULL; |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | return NULL; |
| 512 | } |
| 513 | |
| 514 | /* |
| 515 | * Open a non-asset file as if it were an asset. |
| 516 | * |
| 517 | * The "fileName" is the partial path starting from the application |
| 518 | * name. |
| 519 | */ |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 520 | Asset* AssetManager::openNonAsset(const char* fileName, AccessMode mode, int32_t* outCookie) |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 521 | { |
| 522 | AutoMutex _l(mLock); |
| 523 | |
| 524 | LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager"); |
| 525 | |
| 526 | |
| 527 | if (mCacheMode != CACHE_OFF && !mCacheValid) |
| 528 | loadFileNameCacheLocked(); |
| 529 | |
| 530 | /* |
| 531 | * For each top-level asset path, search for the asset. |
| 532 | */ |
| 533 | |
| 534 | size_t i = mAssetPaths.size(); |
| 535 | while (i > 0) { |
| 536 | i--; |
| 537 | ALOGV("Looking for non-asset '%s' in '%s'\n", fileName, mAssetPaths.itemAt(i).path.string()); |
| 538 | Asset* pAsset = openNonAssetInPathLocked( |
| 539 | fileName, mode, mAssetPaths.itemAt(i)); |
| 540 | if (pAsset != NULL) { |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 541 | if (outCookie != NULL) *outCookie = static_cast<int32_t>(i + 1); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 542 | return pAsset != kExcludedAsset ? pAsset : NULL; |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | return NULL; |
| 547 | } |
| 548 | |
Narayan Kamath | a0c6260 | 2014-01-24 13:51:51 +0000 | [diff] [blame] | 549 | Asset* AssetManager::openNonAsset(const int32_t cookie, const char* fileName, AccessMode mode) |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 550 | { |
Narayan Kamath | a0c6260 | 2014-01-24 13:51:51 +0000 | [diff] [blame] | 551 | const size_t which = static_cast<size_t>(cookie) - 1; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 552 | |
| 553 | AutoMutex _l(mLock); |
| 554 | |
| 555 | LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager"); |
| 556 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 557 | if (mCacheMode != CACHE_OFF && !mCacheValid) |
| 558 | loadFileNameCacheLocked(); |
| 559 | |
| 560 | if (which < mAssetPaths.size()) { |
| 561 | ALOGV("Looking for non-asset '%s' in '%s'\n", fileName, |
| 562 | mAssetPaths.itemAt(which).path.string()); |
| 563 | Asset* pAsset = openNonAssetInPathLocked( |
| 564 | fileName, mode, mAssetPaths.itemAt(which)); |
| 565 | if (pAsset != NULL) { |
| 566 | return pAsset != kExcludedAsset ? pAsset : NULL; |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | return NULL; |
| 571 | } |
| 572 | |
| 573 | /* |
| 574 | * Get the type of a file in the asset namespace. |
| 575 | * |
| 576 | * This currently only works for regular files. All others (including |
| 577 | * directories) will return kFileTypeNonexistent. |
| 578 | */ |
| 579 | FileType AssetManager::getFileType(const char* fileName) |
| 580 | { |
| 581 | Asset* pAsset = NULL; |
| 582 | |
| 583 | /* |
| 584 | * Open the asset. This is less efficient than simply finding the |
| 585 | * file, but it's not too bad (we don't uncompress or mmap data until |
| 586 | * the first read() call). |
| 587 | */ |
| 588 | pAsset = open(fileName, Asset::ACCESS_STREAMING); |
| 589 | delete pAsset; |
| 590 | |
| 591 | if (pAsset == NULL) |
| 592 | return kFileTypeNonexistent; |
| 593 | else |
| 594 | return kFileTypeRegular; |
| 595 | } |
| 596 | |
| 597 | const ResTable* AssetManager::getResTable(bool required) const |
| 598 | { |
| 599 | ResTable* rt = mResources; |
| 600 | if (rt) { |
| 601 | return rt; |
| 602 | } |
| 603 | |
| 604 | // Iterate through all asset packages, collecting resources from each. |
| 605 | |
| 606 | AutoMutex _l(mLock); |
| 607 | |
| 608 | if (mResources != NULL) { |
| 609 | return mResources; |
| 610 | } |
| 611 | |
| 612 | if (required) { |
| 613 | LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager"); |
| 614 | } |
| 615 | |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 616 | if (mCacheMode != CACHE_OFF && !mCacheValid) { |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 617 | const_cast<AssetManager*>(this)->loadFileNameCacheLocked(); |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 618 | } |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 619 | |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 620 | mResources = new ResTable(); |
| 621 | updateResourceParamsLocked(); |
| 622 | |
| 623 | bool onlyEmptyResources = true; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 624 | const size_t N = mAssetPaths.size(); |
| 625 | for (size_t i=0; i<N; i++) { |
| 626 | Asset* ass = NULL; |
| 627 | ResTable* sharedRes = NULL; |
| 628 | bool shared = true; |
| 629 | const asset_path& ap = mAssetPaths.itemAt(i); |
| 630 | MY_TRACE_BEGIN(ap.path.string()); |
| 631 | Asset* idmap = openIdmapLocked(ap); |
| 632 | ALOGV("Looking for resource asset in '%s'\n", ap.path.string()); |
| 633 | if (ap.type != kFileTypeDirectory) { |
| 634 | if (i == 0) { |
| 635 | // The first item is typically the framework resources, |
| 636 | // which we want to avoid parsing every time. |
| 637 | sharedRes = const_cast<AssetManager*>(this)-> |
| 638 | mZipSet.getZipResourceTable(ap.path); |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 639 | if (sharedRes != NULL) { |
| 640 | // skip ahead the number of system overlay packages preloaded |
| 641 | i += sharedRes->getTableCount() - 1; |
| 642 | } |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 643 | } |
| 644 | if (sharedRes == NULL) { |
| 645 | ass = const_cast<AssetManager*>(this)-> |
| 646 | mZipSet.getZipResourceTableAsset(ap.path); |
| 647 | if (ass == NULL) { |
| 648 | ALOGV("loading resource table %s\n", ap.path.string()); |
| 649 | ass = const_cast<AssetManager*>(this)-> |
| 650 | openNonAssetInPathLocked("resources.arsc", |
| 651 | Asset::ACCESS_BUFFER, |
| 652 | ap); |
| 653 | if (ass != NULL && ass != kExcludedAsset) { |
| 654 | ass = const_cast<AssetManager*>(this)-> |
| 655 | mZipSet.setZipResourceTableAsset(ap.path, ass); |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | if (i == 0 && ass != NULL) { |
| 660 | // If this is the first resource table in the asset |
| 661 | // manager, then we are going to cache it so that we |
| 662 | // can quickly copy it out for others. |
| 663 | ALOGV("Creating shared resources for %s", ap.path.string()); |
| 664 | sharedRes = new ResTable(); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 665 | sharedRes->add(ass, idmap, i + 1, false); |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 666 | #ifdef HAVE_ANDROID_OS |
| 667 | const char* data = getenv("ANDROID_DATA"); |
| 668 | LOG_ALWAYS_FATAL_IF(data == NULL, "ANDROID_DATA not set"); |
| 669 | String8 overlaysListPath(data); |
| 670 | overlaysListPath.appendPath(kResourceCache); |
| 671 | overlaysListPath.appendPath("overlays.list"); |
| 672 | addSystemOverlays(overlaysListPath.string(), ap.path, sharedRes, i); |
| 673 | #endif |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 674 | sharedRes = const_cast<AssetManager*>(this)-> |
| 675 | mZipSet.setZipResourceTable(ap.path, sharedRes); |
| 676 | } |
| 677 | } |
| 678 | } else { |
| 679 | ALOGV("loading resource table %s\n", ap.path.string()); |
Elliott Hughes | e1aa223 | 2013-10-29 15:28:08 -0700 | [diff] [blame] | 680 | ass = const_cast<AssetManager*>(this)-> |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 681 | openNonAssetInPathLocked("resources.arsc", |
| 682 | Asset::ACCESS_BUFFER, |
| 683 | ap); |
| 684 | shared = false; |
| 685 | } |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 686 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 687 | if ((ass != NULL || sharedRes != NULL) && ass != kExcludedAsset) { |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 688 | ALOGV("Installing resource asset %p in to table %p\n", ass, mResources); |
| 689 | if (sharedRes != NULL) { |
| 690 | ALOGV("Copying existing resources for %s", ap.path.string()); |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 691 | mResources->add(sharedRes); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 692 | } else { |
| 693 | ALOGV("Parsing resources for %s", ap.path.string()); |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 694 | mResources->add(ass, idmap, i + 1, !shared); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 695 | } |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 696 | onlyEmptyResources = false; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 697 | |
| 698 | if (!shared) { |
| 699 | delete ass; |
| 700 | } |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 701 | } else { |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 702 | ALOGV("Installing empty resources in to table %p\n", mResources); |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 703 | mResources->addEmpty(i + 1); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 704 | } |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 705 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 706 | if (idmap != NULL) { |
| 707 | delete idmap; |
| 708 | } |
| 709 | MY_TRACE_END(); |
| 710 | } |
| 711 | |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 712 | if (required && onlyEmptyResources) { |
| 713 | ALOGW("Unable to find resources file resources.arsc"); |
| 714 | delete mResources; |
| 715 | mResources = NULL; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 716 | } |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 717 | |
| 718 | return mResources; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | void AssetManager::updateResourceParamsLocked() const |
| 722 | { |
| 723 | ResTable* res = mResources; |
| 724 | if (!res) { |
| 725 | return; |
| 726 | } |
| 727 | |
Narayan Kamath | 91447d8 | 2014-01-21 15:32:36 +0000 | [diff] [blame] | 728 | if (mLocale) { |
| 729 | mConfig->setBcp47Locale(mLocale); |
| 730 | } else { |
| 731 | mConfig->clearLocale(); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 732 | } |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 733 | |
| 734 | res->setParameters(mConfig); |
| 735 | } |
| 736 | |
| 737 | Asset* AssetManager::openIdmapLocked(const struct asset_path& ap) const |
| 738 | { |
| 739 | Asset* ass = NULL; |
| 740 | if (ap.idmap.size() != 0) { |
| 741 | ass = const_cast<AssetManager*>(this)-> |
| 742 | openAssetFromFileLocked(ap.idmap, Asset::ACCESS_BUFFER); |
| 743 | if (ass) { |
| 744 | ALOGV("loading idmap %s\n", ap.idmap.string()); |
| 745 | } else { |
| 746 | ALOGW("failed to load idmap %s\n", ap.idmap.string()); |
| 747 | } |
| 748 | } |
| 749 | return ass; |
| 750 | } |
| 751 | |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 752 | void AssetManager::addSystemOverlays(const char* pathOverlaysList, |
| 753 | const String8& targetPackagePath, ResTable* sharedRes, size_t offset) const |
| 754 | { |
| 755 | FILE* fin = fopen(pathOverlaysList, "r"); |
| 756 | if (fin == NULL) { |
| 757 | return; |
| 758 | } |
| 759 | |
| 760 | char buf[1024]; |
| 761 | while (fgets(buf, sizeof(buf), fin)) { |
| 762 | // format of each line: |
| 763 | // <path to apk><space><path to idmap><newline> |
| 764 | char* space = strchr(buf, ' '); |
| 765 | char* newline = strchr(buf, '\n'); |
| 766 | asset_path oap; |
| 767 | |
| 768 | if (space == NULL || newline == NULL || newline < space) { |
| 769 | continue; |
| 770 | } |
| 771 | |
| 772 | oap.path = String8(buf, space - buf); |
| 773 | oap.type = kFileTypeRegular; |
| 774 | oap.idmap = String8(space + 1, newline - space - 1); |
| 775 | |
| 776 | Asset* oass = const_cast<AssetManager*>(this)-> |
| 777 | openNonAssetInPathLocked("resources.arsc", |
| 778 | Asset::ACCESS_BUFFER, |
| 779 | oap); |
| 780 | |
| 781 | if (oass != NULL) { |
| 782 | Asset* oidmap = openIdmapLocked(oap); |
| 783 | offset++; |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 784 | sharedRes->add(oass, oidmap, offset + 1, false); |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 785 | const_cast<AssetManager*>(this)->mAssetPaths.add(oap); |
| 786 | const_cast<AssetManager*>(this)->mZipSet.addOverlay(targetPackagePath, oap); |
| 787 | } |
| 788 | } |
| 789 | fclose(fin); |
| 790 | } |
| 791 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 792 | const ResTable& AssetManager::getResources(bool required) const |
| 793 | { |
| 794 | const ResTable* rt = getResTable(required); |
| 795 | return *rt; |
| 796 | } |
| 797 | |
| 798 | bool AssetManager::isUpToDate() |
| 799 | { |
| 800 | AutoMutex _l(mLock); |
| 801 | return mZipSet.isUpToDate(); |
| 802 | } |
| 803 | |
| 804 | void AssetManager::getLocales(Vector<String8>* locales) const |
| 805 | { |
| 806 | ResTable* res = mResources; |
| 807 | if (res != NULL) { |
| 808 | res->getLocales(locales); |
| 809 | } |
Narayan Kamath | e4345db | 2014-06-26 16:01:28 +0100 | [diff] [blame] | 810 | |
| 811 | const size_t numLocales = locales->size(); |
| 812 | for (size_t i = 0; i < numLocales; ++i) { |
| 813 | const String8& localeStr = locales->itemAt(i); |
| 814 | if (localeStr.find(kTlPrefix) == 0) { |
| 815 | String8 replaced("fil"); |
| 816 | replaced += (localeStr.string() + kTlPrefixLen); |
| 817 | locales->editItemAt(i) = replaced; |
| 818 | } |
| 819 | } |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 820 | } |
| 821 | |
| 822 | /* |
| 823 | * Open a non-asset file as if it were an asset, searching for it in the |
| 824 | * specified app. |
| 825 | * |
| 826 | * Pass in a NULL values for "appName" if the common app directory should |
| 827 | * be used. |
| 828 | */ |
| 829 | Asset* AssetManager::openNonAssetInPathLocked(const char* fileName, AccessMode mode, |
| 830 | const asset_path& ap) |
| 831 | { |
| 832 | Asset* pAsset = NULL; |
| 833 | |
| 834 | /* look at the filesystem on disk */ |
| 835 | if (ap.type == kFileTypeDirectory) { |
| 836 | String8 path(ap.path); |
| 837 | path.appendPath(fileName); |
| 838 | |
| 839 | pAsset = openAssetFromFileLocked(path, mode); |
| 840 | |
| 841 | if (pAsset == NULL) { |
| 842 | /* try again, this time with ".gz" */ |
| 843 | path.append(".gz"); |
| 844 | pAsset = openAssetFromFileLocked(path, mode); |
| 845 | } |
| 846 | |
| 847 | if (pAsset != NULL) { |
| 848 | //printf("FOUND NA '%s' on disk\n", fileName); |
| 849 | pAsset->setAssetSource(path); |
| 850 | } |
| 851 | |
| 852 | /* look inside the zip file */ |
| 853 | } else { |
| 854 | String8 path(fileName); |
| 855 | |
| 856 | /* check the appropriate Zip file */ |
Narayan Kamath | 560566d | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 857 | ZipFileRO* pZip = getZipFileLocked(ap); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 858 | if (pZip != NULL) { |
| 859 | //printf("GOT zip, checking NA '%s'\n", (const char*) path); |
Narayan Kamath | 560566d | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 860 | ZipEntryRO entry = pZip->findEntryByName(path.string()); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 861 | if (entry != NULL) { |
| 862 | //printf("FOUND NA in Zip file for %s\n", appName ? appName : kAppCommon); |
| 863 | pAsset = openAssetFromZipLocked(pZip, entry, mode, path); |
Narayan Kamath | 560566d | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 864 | pZip->releaseEntry(entry); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 865 | } |
| 866 | } |
| 867 | |
| 868 | if (pAsset != NULL) { |
| 869 | /* create a "source" name, for debug/display */ |
| 870 | pAsset->setAssetSource( |
| 871 | createZipSourceNameLocked(ZipSet::getPathName(ap.path.string()), String8(""), |
| 872 | String8(fileName))); |
| 873 | } |
| 874 | } |
| 875 | |
| 876 | return pAsset; |
| 877 | } |
| 878 | |
| 879 | /* |
| 880 | * Open an asset, searching for it in the directory hierarchy for the |
| 881 | * specified app. |
| 882 | * |
| 883 | * Pass in a NULL values for "appName" if the common app directory should |
| 884 | * be used. |
| 885 | */ |
| 886 | Asset* AssetManager::openInPathLocked(const char* fileName, AccessMode mode, |
| 887 | const asset_path& ap) |
| 888 | { |
| 889 | Asset* pAsset = NULL; |
| 890 | |
| 891 | /* |
| 892 | * Try various combinations of locale and vendor. |
| 893 | */ |
| 894 | if (mLocale != NULL && mVendor != NULL) |
| 895 | pAsset = openInLocaleVendorLocked(fileName, mode, ap, mLocale, mVendor); |
| 896 | if (pAsset == NULL && mVendor != NULL) |
| 897 | pAsset = openInLocaleVendorLocked(fileName, mode, ap, NULL, mVendor); |
| 898 | if (pAsset == NULL && mLocale != NULL) |
| 899 | pAsset = openInLocaleVendorLocked(fileName, mode, ap, mLocale, NULL); |
| 900 | if (pAsset == NULL) |
| 901 | pAsset = openInLocaleVendorLocked(fileName, mode, ap, NULL, NULL); |
| 902 | |
| 903 | return pAsset; |
| 904 | } |
| 905 | |
| 906 | /* |
| 907 | * Open an asset, searching for it in the directory hierarchy for the |
| 908 | * specified locale and vendor. |
| 909 | * |
| 910 | * We also search in "app.jar". |
| 911 | * |
| 912 | * Pass in NULL values for "appName", "locale", and "vendor" if the |
| 913 | * defaults should be used. |
| 914 | */ |
| 915 | Asset* AssetManager::openInLocaleVendorLocked(const char* fileName, AccessMode mode, |
| 916 | const asset_path& ap, const char* locale, const char* vendor) |
| 917 | { |
| 918 | Asset* pAsset = NULL; |
| 919 | |
| 920 | if (ap.type == kFileTypeDirectory) { |
| 921 | if (mCacheMode == CACHE_OFF) { |
| 922 | /* look at the filesystem on disk */ |
| 923 | String8 path(createPathNameLocked(ap, locale, vendor)); |
| 924 | path.appendPath(fileName); |
| 925 | |
| 926 | String8 excludeName(path); |
| 927 | excludeName.append(kExcludeExtension); |
| 928 | if (::getFileType(excludeName.string()) != kFileTypeNonexistent) { |
| 929 | /* say no more */ |
| 930 | //printf("+++ excluding '%s'\n", (const char*) excludeName); |
| 931 | return kExcludedAsset; |
| 932 | } |
| 933 | |
| 934 | pAsset = openAssetFromFileLocked(path, mode); |
| 935 | |
| 936 | if (pAsset == NULL) { |
| 937 | /* try again, this time with ".gz" */ |
| 938 | path.append(".gz"); |
| 939 | pAsset = openAssetFromFileLocked(path, mode); |
| 940 | } |
| 941 | |
| 942 | if (pAsset != NULL) |
| 943 | pAsset->setAssetSource(path); |
| 944 | } else { |
| 945 | /* find in cache */ |
| 946 | String8 path(createPathNameLocked(ap, locale, vendor)); |
| 947 | path.appendPath(fileName); |
| 948 | |
| 949 | AssetDir::FileInfo tmpInfo; |
| 950 | bool found = false; |
| 951 | |
| 952 | String8 excludeName(path); |
| 953 | excludeName.append(kExcludeExtension); |
| 954 | |
| 955 | if (mCache.indexOf(excludeName) != NAME_NOT_FOUND) { |
| 956 | /* go no farther */ |
| 957 | //printf("+++ Excluding '%s'\n", (const char*) excludeName); |
| 958 | return kExcludedAsset; |
| 959 | } |
| 960 | |
| 961 | /* |
| 962 | * File compression extensions (".gz") don't get stored in the |
| 963 | * name cache, so we have to try both here. |
| 964 | */ |
| 965 | if (mCache.indexOf(path) != NAME_NOT_FOUND) { |
| 966 | found = true; |
| 967 | pAsset = openAssetFromFileLocked(path, mode); |
| 968 | if (pAsset == NULL) { |
| 969 | /* try again, this time with ".gz" */ |
| 970 | path.append(".gz"); |
| 971 | pAsset = openAssetFromFileLocked(path, mode); |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | if (pAsset != NULL) |
| 976 | pAsset->setAssetSource(path); |
| 977 | |
| 978 | /* |
| 979 | * Don't continue the search into the Zip files. Our cached info |
| 980 | * said it was a file on disk; to be consistent with openDir() |
| 981 | * we want to return the loose asset. If the cached file gets |
| 982 | * removed, we fail. |
| 983 | * |
| 984 | * The alternative is to update our cache when files get deleted, |
| 985 | * or make some sort of "best effort" promise, but for now I'm |
| 986 | * taking the hard line. |
| 987 | */ |
| 988 | if (found) { |
| 989 | if (pAsset == NULL) |
| 990 | ALOGD("Expected file not found: '%s'\n", path.string()); |
| 991 | return pAsset; |
| 992 | } |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | /* |
| 997 | * Either it wasn't found on disk or on the cached view of the disk. |
| 998 | * Dig through the currently-opened set of Zip files. If caching |
| 999 | * is disabled, the Zip file may get reopened. |
| 1000 | */ |
| 1001 | if (pAsset == NULL && ap.type == kFileTypeRegular) { |
| 1002 | String8 path; |
| 1003 | |
| 1004 | path.appendPath((locale != NULL) ? locale : kDefaultLocale); |
| 1005 | path.appendPath((vendor != NULL) ? vendor : kDefaultVendor); |
| 1006 | path.appendPath(fileName); |
| 1007 | |
| 1008 | /* check the appropriate Zip file */ |
Narayan Kamath | 560566d | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 1009 | ZipFileRO* pZip = getZipFileLocked(ap); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1010 | if (pZip != NULL) { |
| 1011 | //printf("GOT zip, checking '%s'\n", (const char*) path); |
Narayan Kamath | 560566d | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 1012 | ZipEntryRO entry = pZip->findEntryByName(path.string()); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1013 | if (entry != NULL) { |
| 1014 | //printf("FOUND in Zip file for %s/%s-%s\n", |
| 1015 | // appName, locale, vendor); |
| 1016 | pAsset = openAssetFromZipLocked(pZip, entry, mode, path); |
Narayan Kamath | 560566d | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 1017 | pZip->releaseEntry(entry); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1018 | } |
| 1019 | } |
| 1020 | |
| 1021 | if (pAsset != NULL) { |
| 1022 | /* create a "source" name, for debug/display */ |
| 1023 | pAsset->setAssetSource(createZipSourceNameLocked(ZipSet::getPathName(ap.path.string()), |
| 1024 | String8(""), String8(fileName))); |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | return pAsset; |
| 1029 | } |
| 1030 | |
| 1031 | /* |
| 1032 | * Create a "source name" for a file from a Zip archive. |
| 1033 | */ |
| 1034 | String8 AssetManager::createZipSourceNameLocked(const String8& zipFileName, |
| 1035 | const String8& dirName, const String8& fileName) |
| 1036 | { |
| 1037 | String8 sourceName("zip:"); |
| 1038 | sourceName.append(zipFileName); |
| 1039 | sourceName.append(":"); |
| 1040 | if (dirName.length() > 0) { |
| 1041 | sourceName.appendPath(dirName); |
| 1042 | } |
| 1043 | sourceName.appendPath(fileName); |
| 1044 | return sourceName; |
| 1045 | } |
| 1046 | |
| 1047 | /* |
| 1048 | * Create a path to a loose asset (asset-base/app/locale/vendor). |
| 1049 | */ |
| 1050 | String8 AssetManager::createPathNameLocked(const asset_path& ap, const char* locale, |
| 1051 | const char* vendor) |
| 1052 | { |
| 1053 | String8 path(ap.path); |
| 1054 | path.appendPath((locale != NULL) ? locale : kDefaultLocale); |
| 1055 | path.appendPath((vendor != NULL) ? vendor : kDefaultVendor); |
| 1056 | return path; |
| 1057 | } |
| 1058 | |
| 1059 | /* |
| 1060 | * Create a path to a loose asset (asset-base/app/rootDir). |
| 1061 | */ |
| 1062 | String8 AssetManager::createPathNameLocked(const asset_path& ap, const char* rootDir) |
| 1063 | { |
| 1064 | String8 path(ap.path); |
| 1065 | if (rootDir != NULL) path.appendPath(rootDir); |
| 1066 | return path; |
| 1067 | } |
| 1068 | |
| 1069 | /* |
| 1070 | * Return a pointer to one of our open Zip archives. Returns NULL if no |
| 1071 | * matching Zip file exists. |
| 1072 | * |
| 1073 | * Right now we have 2 possible Zip files (1 each in app/"common"). |
| 1074 | * |
| 1075 | * If caching is set to CACHE_OFF, to get the expected behavior we |
| 1076 | * need to reopen the Zip file on every request. That would be silly |
| 1077 | * and expensive, so instead we just check the file modification date. |
| 1078 | * |
| 1079 | * Pass in NULL values for "appName", "locale", and "vendor" if the |
| 1080 | * generics should be used. |
| 1081 | */ |
| 1082 | ZipFileRO* AssetManager::getZipFileLocked(const asset_path& ap) |
| 1083 | { |
| 1084 | ALOGV("getZipFileLocked() in %p\n", this); |
| 1085 | |
| 1086 | return mZipSet.getZip(ap.path); |
| 1087 | } |
| 1088 | |
| 1089 | /* |
| 1090 | * Try to open an asset from a file on disk. |
| 1091 | * |
| 1092 | * If the file is compressed with gzip, we seek to the start of the |
| 1093 | * deflated data and pass that in (just like we would for a Zip archive). |
| 1094 | * |
| 1095 | * For uncompressed data, we may already have an mmap()ed version sitting |
| 1096 | * around. If so, we want to hand that to the Asset instead. |
| 1097 | * |
| 1098 | * This returns NULL if the file doesn't exist, couldn't be opened, or |
| 1099 | * claims to be a ".gz" but isn't. |
| 1100 | */ |
| 1101 | Asset* AssetManager::openAssetFromFileLocked(const String8& pathName, |
| 1102 | AccessMode mode) |
| 1103 | { |
| 1104 | Asset* pAsset = NULL; |
| 1105 | |
| 1106 | if (strcasecmp(pathName.getPathExtension().string(), ".gz") == 0) { |
| 1107 | //printf("TRYING '%s'\n", (const char*) pathName); |
| 1108 | pAsset = Asset::createFromCompressedFile(pathName.string(), mode); |
| 1109 | } else { |
| 1110 | //printf("TRYING '%s'\n", (const char*) pathName); |
| 1111 | pAsset = Asset::createFromFile(pathName.string(), mode); |
| 1112 | } |
| 1113 | |
| 1114 | return pAsset; |
| 1115 | } |
| 1116 | |
| 1117 | /* |
| 1118 | * Given an entry in a Zip archive, create a new Asset object. |
| 1119 | * |
| 1120 | * If the entry is uncompressed, we may want to create or share a |
| 1121 | * slice of shared memory. |
| 1122 | */ |
| 1123 | Asset* AssetManager::openAssetFromZipLocked(const ZipFileRO* pZipFile, |
| 1124 | const ZipEntryRO entry, AccessMode mode, const String8& entryName) |
| 1125 | { |
| 1126 | Asset* pAsset = NULL; |
| 1127 | |
| 1128 | // TODO: look for previously-created shared memory slice? |
| 1129 | int method; |
| 1130 | size_t uncompressedLen; |
| 1131 | |
| 1132 | //printf("USING Zip '%s'\n", pEntry->getFileName()); |
| 1133 | |
| 1134 | //pZipFile->getEntryInfo(entry, &method, &uncompressedLen, &compressedLen, |
| 1135 | // &offset); |
| 1136 | if (!pZipFile->getEntryInfo(entry, &method, &uncompressedLen, NULL, NULL, |
| 1137 | NULL, NULL)) |
| 1138 | { |
| 1139 | ALOGW("getEntryInfo failed\n"); |
| 1140 | return NULL; |
| 1141 | } |
| 1142 | |
| 1143 | FileMap* dataMap = pZipFile->createEntryFileMap(entry); |
| 1144 | if (dataMap == NULL) { |
| 1145 | ALOGW("create map from entry failed\n"); |
| 1146 | return NULL; |
| 1147 | } |
| 1148 | |
| 1149 | if (method == ZipFileRO::kCompressStored) { |
| 1150 | pAsset = Asset::createFromUncompressedMap(dataMap, mode); |
| 1151 | ALOGV("Opened uncompressed entry %s in zip %s mode %d: %p", entryName.string(), |
| 1152 | dataMap->getFileName(), mode, pAsset); |
| 1153 | } else { |
| 1154 | pAsset = Asset::createFromCompressedMap(dataMap, method, |
| 1155 | uncompressedLen, mode); |
| 1156 | ALOGV("Opened compressed entry %s in zip %s mode %d: %p", entryName.string(), |
| 1157 | dataMap->getFileName(), mode, pAsset); |
| 1158 | } |
| 1159 | if (pAsset == NULL) { |
| 1160 | /* unexpected */ |
| 1161 | ALOGW("create from segment failed\n"); |
| 1162 | } |
| 1163 | |
| 1164 | return pAsset; |
| 1165 | } |
| 1166 | |
| 1167 | |
| 1168 | |
| 1169 | /* |
| 1170 | * Open a directory in the asset namespace. |
| 1171 | * |
| 1172 | * An "asset directory" is simply the combination of all files in all |
| 1173 | * locations, with ".gz" stripped for loose files. With app, locale, and |
| 1174 | * vendor defined, we have 8 directories and 2 Zip archives to scan. |
| 1175 | * |
| 1176 | * Pass in "" for the root dir. |
| 1177 | */ |
| 1178 | AssetDir* AssetManager::openDir(const char* dirName) |
| 1179 | { |
| 1180 | AutoMutex _l(mLock); |
| 1181 | |
| 1182 | AssetDir* pDir = NULL; |
| 1183 | SortedVector<AssetDir::FileInfo>* pMergedInfo = NULL; |
| 1184 | |
| 1185 | LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager"); |
| 1186 | assert(dirName != NULL); |
| 1187 | |
| 1188 | //printf("+++ openDir(%s) in '%s'\n", dirName, (const char*) mAssetBase); |
| 1189 | |
| 1190 | if (mCacheMode != CACHE_OFF && !mCacheValid) |
| 1191 | loadFileNameCacheLocked(); |
| 1192 | |
| 1193 | pDir = new AssetDir; |
| 1194 | |
| 1195 | /* |
| 1196 | * Scan the various directories, merging what we find into a single |
| 1197 | * vector. We want to scan them in reverse priority order so that |
| 1198 | * the ".EXCLUDE" processing works correctly. Also, if we decide we |
| 1199 | * want to remember where the file is coming from, we'll get the right |
| 1200 | * version. |
| 1201 | * |
| 1202 | * We start with Zip archives, then do loose files. |
| 1203 | */ |
| 1204 | pMergedInfo = new SortedVector<AssetDir::FileInfo>; |
| 1205 | |
| 1206 | size_t i = mAssetPaths.size(); |
| 1207 | while (i > 0) { |
| 1208 | i--; |
| 1209 | const asset_path& ap = mAssetPaths.itemAt(i); |
| 1210 | if (ap.type == kFileTypeRegular) { |
| 1211 | ALOGV("Adding directory %s from zip %s", dirName, ap.path.string()); |
| 1212 | scanAndMergeZipLocked(pMergedInfo, ap, kAssetsRoot, dirName); |
| 1213 | } else { |
| 1214 | ALOGV("Adding directory %s from dir %s", dirName, ap.path.string()); |
| 1215 | scanAndMergeDirLocked(pMergedInfo, ap, kAssetsRoot, dirName); |
| 1216 | } |
| 1217 | } |
| 1218 | |
| 1219 | #if 0 |
| 1220 | printf("FILE LIST:\n"); |
| 1221 | for (i = 0; i < (size_t) pMergedInfo->size(); i++) { |
| 1222 | printf(" %d: (%d) '%s'\n", i, |
| 1223 | pMergedInfo->itemAt(i).getFileType(), |
| 1224 | (const char*) pMergedInfo->itemAt(i).getFileName()); |
| 1225 | } |
| 1226 | #endif |
| 1227 | |
| 1228 | pDir->setFileList(pMergedInfo); |
| 1229 | return pDir; |
| 1230 | } |
| 1231 | |
| 1232 | /* |
| 1233 | * Open a directory in the non-asset namespace. |
| 1234 | * |
| 1235 | * An "asset directory" is simply the combination of all files in all |
| 1236 | * locations, with ".gz" stripped for loose files. With app, locale, and |
| 1237 | * vendor defined, we have 8 directories and 2 Zip archives to scan. |
| 1238 | * |
| 1239 | * Pass in "" for the root dir. |
| 1240 | */ |
Narayan Kamath | a0c6260 | 2014-01-24 13:51:51 +0000 | [diff] [blame] | 1241 | AssetDir* AssetManager::openNonAssetDir(const int32_t cookie, const char* dirName) |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1242 | { |
| 1243 | AutoMutex _l(mLock); |
| 1244 | |
| 1245 | AssetDir* pDir = NULL; |
| 1246 | SortedVector<AssetDir::FileInfo>* pMergedInfo = NULL; |
| 1247 | |
| 1248 | LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager"); |
| 1249 | assert(dirName != NULL); |
| 1250 | |
| 1251 | //printf("+++ openDir(%s) in '%s'\n", dirName, (const char*) mAssetBase); |
| 1252 | |
| 1253 | if (mCacheMode != CACHE_OFF && !mCacheValid) |
| 1254 | loadFileNameCacheLocked(); |
| 1255 | |
| 1256 | pDir = new AssetDir; |
| 1257 | |
| 1258 | pMergedInfo = new SortedVector<AssetDir::FileInfo>; |
| 1259 | |
Narayan Kamath | a0c6260 | 2014-01-24 13:51:51 +0000 | [diff] [blame] | 1260 | const size_t which = static_cast<size_t>(cookie) - 1; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1261 | |
| 1262 | if (which < mAssetPaths.size()) { |
| 1263 | const asset_path& ap = mAssetPaths.itemAt(which); |
| 1264 | if (ap.type == kFileTypeRegular) { |
| 1265 | ALOGV("Adding directory %s from zip %s", dirName, ap.path.string()); |
| 1266 | scanAndMergeZipLocked(pMergedInfo, ap, NULL, dirName); |
| 1267 | } else { |
| 1268 | ALOGV("Adding directory %s from dir %s", dirName, ap.path.string()); |
| 1269 | scanAndMergeDirLocked(pMergedInfo, ap, NULL, dirName); |
| 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | #if 0 |
| 1274 | printf("FILE LIST:\n"); |
| 1275 | for (i = 0; i < (size_t) pMergedInfo->size(); i++) { |
| 1276 | printf(" %d: (%d) '%s'\n", i, |
| 1277 | pMergedInfo->itemAt(i).getFileType(), |
| 1278 | (const char*) pMergedInfo->itemAt(i).getFileName()); |
| 1279 | } |
| 1280 | #endif |
| 1281 | |
| 1282 | pDir->setFileList(pMergedInfo); |
| 1283 | return pDir; |
| 1284 | } |
| 1285 | |
| 1286 | /* |
| 1287 | * Scan the contents of the specified directory and merge them into the |
| 1288 | * "pMergedInfo" vector, removing previous entries if we find "exclude" |
| 1289 | * directives. |
| 1290 | * |
| 1291 | * Returns "false" if we found nothing to contribute. |
| 1292 | */ |
| 1293 | bool AssetManager::scanAndMergeDirLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo, |
| 1294 | const asset_path& ap, const char* rootDir, const char* dirName) |
| 1295 | { |
| 1296 | SortedVector<AssetDir::FileInfo>* pContents; |
| 1297 | String8 path; |
| 1298 | |
| 1299 | assert(pMergedInfo != NULL); |
| 1300 | |
| 1301 | //printf("scanAndMergeDir: %s %s %s %s\n", appName, locale, vendor,dirName); |
| 1302 | |
| 1303 | if (mCacheValid) { |
| 1304 | int i, start, count; |
| 1305 | |
| 1306 | pContents = new SortedVector<AssetDir::FileInfo>; |
| 1307 | |
| 1308 | /* |
| 1309 | * Get the basic partial path and find it in the cache. That's |
| 1310 | * the start point for the search. |
| 1311 | */ |
| 1312 | path = createPathNameLocked(ap, rootDir); |
| 1313 | if (dirName[0] != '\0') |
| 1314 | path.appendPath(dirName); |
| 1315 | |
| 1316 | start = mCache.indexOf(path); |
| 1317 | if (start == NAME_NOT_FOUND) { |
| 1318 | //printf("+++ not found in cache: dir '%s'\n", (const char*) path); |
| 1319 | delete pContents; |
| 1320 | return false; |
| 1321 | } |
| 1322 | |
| 1323 | /* |
| 1324 | * The match string looks like "common/default/default/foo/bar/". |
| 1325 | * The '/' on the end ensures that we don't match on the directory |
| 1326 | * itself or on ".../foo/barfy/". |
| 1327 | */ |
| 1328 | path.append("/"); |
| 1329 | |
| 1330 | count = mCache.size(); |
| 1331 | |
| 1332 | /* |
| 1333 | * Pick out the stuff in the current dir by examining the pathname. |
| 1334 | * It needs to match the partial pathname prefix, and not have a '/' |
| 1335 | * (fssep) anywhere after the prefix. |
| 1336 | */ |
| 1337 | for (i = start+1; i < count; i++) { |
| 1338 | if (mCache[i].getFileName().length() > path.length() && |
| 1339 | strncmp(mCache[i].getFileName().string(), path.string(), path.length()) == 0) |
| 1340 | { |
| 1341 | const char* name = mCache[i].getFileName().string(); |
| 1342 | // XXX THIS IS BROKEN! Looks like we need to store the full |
| 1343 | // path prefix separately from the file path. |
| 1344 | if (strchr(name + path.length(), '/') == NULL) { |
| 1345 | /* grab it, reducing path to just the filename component */ |
| 1346 | AssetDir::FileInfo tmp = mCache[i]; |
| 1347 | tmp.setFileName(tmp.getFileName().getPathLeaf()); |
| 1348 | pContents->add(tmp); |
| 1349 | } |
| 1350 | } else { |
| 1351 | /* no longer in the dir or its subdirs */ |
| 1352 | break; |
| 1353 | } |
| 1354 | |
| 1355 | } |
| 1356 | } else { |
| 1357 | path = createPathNameLocked(ap, rootDir); |
| 1358 | if (dirName[0] != '\0') |
| 1359 | path.appendPath(dirName); |
| 1360 | pContents = scanDirLocked(path); |
| 1361 | if (pContents == NULL) |
| 1362 | return false; |
| 1363 | } |
| 1364 | |
| 1365 | // if we wanted to do an incremental cache fill, we would do it here |
| 1366 | |
| 1367 | /* |
| 1368 | * Process "exclude" directives. If we find a filename that ends with |
| 1369 | * ".EXCLUDE", we look for a matching entry in the "merged" set, and |
| 1370 | * remove it if we find it. We also delete the "exclude" entry. |
| 1371 | */ |
| 1372 | int i, count, exclExtLen; |
| 1373 | |
| 1374 | count = pContents->size(); |
| 1375 | exclExtLen = strlen(kExcludeExtension); |
| 1376 | for (i = 0; i < count; i++) { |
| 1377 | const char* name; |
| 1378 | int nameLen; |
| 1379 | |
| 1380 | name = pContents->itemAt(i).getFileName().string(); |
| 1381 | nameLen = strlen(name); |
| 1382 | if (nameLen > exclExtLen && |
| 1383 | strcmp(name + (nameLen - exclExtLen), kExcludeExtension) == 0) |
| 1384 | { |
| 1385 | String8 match(name, nameLen - exclExtLen); |
| 1386 | int matchIdx; |
| 1387 | |
| 1388 | matchIdx = AssetDir::FileInfo::findEntry(pMergedInfo, match); |
| 1389 | if (matchIdx > 0) { |
| 1390 | ALOGV("Excluding '%s' [%s]\n", |
| 1391 | pMergedInfo->itemAt(matchIdx).getFileName().string(), |
| 1392 | pMergedInfo->itemAt(matchIdx).getSourceName().string()); |
| 1393 | pMergedInfo->removeAt(matchIdx); |
| 1394 | } else { |
| 1395 | //printf("+++ no match on '%s'\n", (const char*) match); |
| 1396 | } |
| 1397 | |
| 1398 | ALOGD("HEY: size=%d removing %d\n", (int)pContents->size(), i); |
| 1399 | pContents->removeAt(i); |
| 1400 | i--; // adjust "for" loop |
| 1401 | count--; // and loop limit |
| 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | mergeInfoLocked(pMergedInfo, pContents); |
| 1406 | |
| 1407 | delete pContents; |
| 1408 | |
| 1409 | return true; |
| 1410 | } |
| 1411 | |
| 1412 | /* |
| 1413 | * Scan the contents of the specified directory, and stuff what we find |
| 1414 | * into a newly-allocated vector. |
| 1415 | * |
| 1416 | * Files ending in ".gz" will have their extensions removed. |
| 1417 | * |
| 1418 | * We should probably think about skipping files with "illegal" names, |
| 1419 | * e.g. illegal characters (/\:) or excessive length. |
| 1420 | * |
| 1421 | * Returns NULL if the specified directory doesn't exist. |
| 1422 | */ |
| 1423 | SortedVector<AssetDir::FileInfo>* AssetManager::scanDirLocked(const String8& path) |
| 1424 | { |
| 1425 | SortedVector<AssetDir::FileInfo>* pContents = NULL; |
| 1426 | DIR* dir; |
| 1427 | struct dirent* entry; |
| 1428 | FileType fileType; |
| 1429 | |
| 1430 | ALOGV("Scanning dir '%s'\n", path.string()); |
| 1431 | |
| 1432 | dir = opendir(path.string()); |
| 1433 | if (dir == NULL) |
| 1434 | return NULL; |
| 1435 | |
| 1436 | pContents = new SortedVector<AssetDir::FileInfo>; |
| 1437 | |
| 1438 | while (1) { |
| 1439 | entry = readdir(dir); |
| 1440 | if (entry == NULL) |
| 1441 | break; |
| 1442 | |
| 1443 | if (strcmp(entry->d_name, ".") == 0 || |
| 1444 | strcmp(entry->d_name, "..") == 0) |
| 1445 | continue; |
| 1446 | |
| 1447 | #ifdef _DIRENT_HAVE_D_TYPE |
| 1448 | if (entry->d_type == DT_REG) |
| 1449 | fileType = kFileTypeRegular; |
| 1450 | else if (entry->d_type == DT_DIR) |
| 1451 | fileType = kFileTypeDirectory; |
| 1452 | else |
| 1453 | fileType = kFileTypeUnknown; |
| 1454 | #else |
| 1455 | // stat the file |
| 1456 | fileType = ::getFileType(path.appendPathCopy(entry->d_name).string()); |
| 1457 | #endif |
| 1458 | |
| 1459 | if (fileType != kFileTypeRegular && fileType != kFileTypeDirectory) |
| 1460 | continue; |
| 1461 | |
| 1462 | AssetDir::FileInfo info; |
| 1463 | info.set(String8(entry->d_name), fileType); |
| 1464 | if (strcasecmp(info.getFileName().getPathExtension().string(), ".gz") == 0) |
| 1465 | info.setFileName(info.getFileName().getBasePath()); |
| 1466 | info.setSourceName(path.appendPathCopy(info.getFileName())); |
| 1467 | pContents->add(info); |
| 1468 | } |
| 1469 | |
| 1470 | closedir(dir); |
| 1471 | return pContents; |
| 1472 | } |
| 1473 | |
| 1474 | /* |
| 1475 | * Scan the contents out of the specified Zip archive, and merge what we |
| 1476 | * find into "pMergedInfo". If the Zip archive in question doesn't exist, |
| 1477 | * we return immediately. |
| 1478 | * |
| 1479 | * Returns "false" if we found nothing to contribute. |
| 1480 | */ |
| 1481 | bool AssetManager::scanAndMergeZipLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo, |
| 1482 | const asset_path& ap, const char* rootDir, const char* baseDirName) |
| 1483 | { |
| 1484 | ZipFileRO* pZip; |
| 1485 | Vector<String8> dirs; |
| 1486 | AssetDir::FileInfo info; |
| 1487 | SortedVector<AssetDir::FileInfo> contents; |
| 1488 | String8 sourceName, zipName, dirName; |
| 1489 | |
| 1490 | pZip = mZipSet.getZip(ap.path); |
| 1491 | if (pZip == NULL) { |
| 1492 | ALOGW("Failure opening zip %s\n", ap.path.string()); |
| 1493 | return false; |
| 1494 | } |
| 1495 | |
| 1496 | zipName = ZipSet::getPathName(ap.path.string()); |
| 1497 | |
| 1498 | /* convert "sounds" to "rootDir/sounds" */ |
| 1499 | if (rootDir != NULL) dirName = rootDir; |
| 1500 | dirName.appendPath(baseDirName); |
| 1501 | |
| 1502 | /* |
| 1503 | * Scan through the list of files, looking for a match. The files in |
| 1504 | * the Zip table of contents are not in sorted order, so we have to |
| 1505 | * process the entire list. We're looking for a string that begins |
| 1506 | * with the characters in "dirName", is followed by a '/', and has no |
| 1507 | * subsequent '/' in the stuff that follows. |
| 1508 | * |
| 1509 | * What makes this especially fun is that directories are not stored |
| 1510 | * explicitly in Zip archives, so we have to infer them from context. |
| 1511 | * When we see "sounds/foo.wav" we have to leave a note to ourselves |
| 1512 | * to insert a directory called "sounds" into the list. We store |
| 1513 | * these in temporary vector so that we only return each one once. |
| 1514 | * |
| 1515 | * Name comparisons are case-sensitive to match UNIX filesystem |
| 1516 | * semantics. |
| 1517 | */ |
| 1518 | int dirNameLen = dirName.length(); |
Narayan Kamath | 560566d | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 1519 | void *iterationCookie; |
| 1520 | if (!pZip->startIteration(&iterationCookie)) { |
| 1521 | ALOGW("ZipFileRO::startIteration returned false"); |
| 1522 | return false; |
| 1523 | } |
| 1524 | |
| 1525 | ZipEntryRO entry; |
| 1526 | while ((entry = pZip->nextEntry(iterationCookie)) != NULL) { |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1527 | char nameBuf[256]; |
| 1528 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1529 | if (pZip->getEntryFileName(entry, nameBuf, sizeof(nameBuf)) != 0) { |
| 1530 | // TODO: fix this if we expect to have long names |
| 1531 | ALOGE("ARGH: name too long?\n"); |
| 1532 | continue; |
| 1533 | } |
| 1534 | //printf("Comparing %s in %s?\n", nameBuf, dirName.string()); |
| 1535 | if (dirNameLen == 0 || |
| 1536 | (strncmp(nameBuf, dirName.string(), dirNameLen) == 0 && |
| 1537 | nameBuf[dirNameLen] == '/')) |
| 1538 | { |
| 1539 | const char* cp; |
| 1540 | const char* nextSlash; |
| 1541 | |
| 1542 | cp = nameBuf + dirNameLen; |
| 1543 | if (dirNameLen != 0) |
| 1544 | cp++; // advance past the '/' |
| 1545 | |
| 1546 | nextSlash = strchr(cp, '/'); |
| 1547 | //xxx this may break if there are bare directory entries |
| 1548 | if (nextSlash == NULL) { |
| 1549 | /* this is a file in the requested directory */ |
| 1550 | |
| 1551 | info.set(String8(nameBuf).getPathLeaf(), kFileTypeRegular); |
| 1552 | |
| 1553 | info.setSourceName( |
| 1554 | createZipSourceNameLocked(zipName, dirName, info.getFileName())); |
| 1555 | |
| 1556 | contents.add(info); |
| 1557 | //printf("FOUND: file '%s'\n", info.getFileName().string()); |
| 1558 | } else { |
| 1559 | /* this is a subdir; add it if we don't already have it*/ |
| 1560 | String8 subdirName(cp, nextSlash - cp); |
| 1561 | size_t j; |
| 1562 | size_t N = dirs.size(); |
| 1563 | |
| 1564 | for (j = 0; j < N; j++) { |
| 1565 | if (subdirName == dirs[j]) { |
| 1566 | break; |
| 1567 | } |
| 1568 | } |
| 1569 | if (j == N) { |
| 1570 | dirs.add(subdirName); |
| 1571 | } |
| 1572 | |
| 1573 | //printf("FOUND: dir '%s'\n", subdirName.string()); |
| 1574 | } |
| 1575 | } |
| 1576 | } |
| 1577 | |
Narayan Kamath | 560566d | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 1578 | pZip->endIteration(iterationCookie); |
| 1579 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1580 | /* |
| 1581 | * Add the set of unique directories. |
| 1582 | */ |
| 1583 | for (int i = 0; i < (int) dirs.size(); i++) { |
| 1584 | info.set(dirs[i], kFileTypeDirectory); |
| 1585 | info.setSourceName( |
| 1586 | createZipSourceNameLocked(zipName, dirName, info.getFileName())); |
| 1587 | contents.add(info); |
| 1588 | } |
| 1589 | |
| 1590 | mergeInfoLocked(pMergedInfo, &contents); |
| 1591 | |
| 1592 | return true; |
| 1593 | } |
| 1594 | |
| 1595 | |
| 1596 | /* |
| 1597 | * Merge two vectors of FileInfo. |
| 1598 | * |
| 1599 | * The merged contents will be stuffed into *pMergedInfo. |
| 1600 | * |
| 1601 | * If an entry for a file exists in both "pMergedInfo" and "pContents", |
| 1602 | * we use the newer "pContents" entry. |
| 1603 | */ |
| 1604 | void AssetManager::mergeInfoLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo, |
| 1605 | const SortedVector<AssetDir::FileInfo>* pContents) |
| 1606 | { |
| 1607 | /* |
| 1608 | * Merge what we found in this directory with what we found in |
| 1609 | * other places. |
| 1610 | * |
| 1611 | * Two basic approaches: |
| 1612 | * (1) Create a new array that holds the unique values of the two |
| 1613 | * arrays. |
| 1614 | * (2) Take the elements from pContents and shove them into pMergedInfo. |
| 1615 | * |
| 1616 | * Because these are vectors of complex objects, moving elements around |
| 1617 | * inside the vector requires constructing new objects and allocating |
| 1618 | * storage for members. With approach #1, we're always adding to the |
| 1619 | * end, whereas with #2 we could be inserting multiple elements at the |
| 1620 | * front of the vector. Approach #1 requires a full copy of the |
| 1621 | * contents of pMergedInfo, but approach #2 requires the same copy for |
| 1622 | * every insertion at the front of pMergedInfo. |
| 1623 | * |
| 1624 | * (We should probably use a SortedVector interface that allows us to |
| 1625 | * just stuff items in, trusting us to maintain the sort order.) |
| 1626 | */ |
| 1627 | SortedVector<AssetDir::FileInfo>* pNewSorted; |
| 1628 | int mergeMax, contMax; |
| 1629 | int mergeIdx, contIdx; |
| 1630 | |
| 1631 | pNewSorted = new SortedVector<AssetDir::FileInfo>; |
| 1632 | mergeMax = pMergedInfo->size(); |
| 1633 | contMax = pContents->size(); |
| 1634 | mergeIdx = contIdx = 0; |
| 1635 | |
| 1636 | while (mergeIdx < mergeMax || contIdx < contMax) { |
| 1637 | if (mergeIdx == mergeMax) { |
| 1638 | /* hit end of "merge" list, copy rest of "contents" */ |
| 1639 | pNewSorted->add(pContents->itemAt(contIdx)); |
| 1640 | contIdx++; |
| 1641 | } else if (contIdx == contMax) { |
| 1642 | /* hit end of "cont" list, copy rest of "merge" */ |
| 1643 | pNewSorted->add(pMergedInfo->itemAt(mergeIdx)); |
| 1644 | mergeIdx++; |
| 1645 | } else if (pMergedInfo->itemAt(mergeIdx) == pContents->itemAt(contIdx)) |
| 1646 | { |
| 1647 | /* items are identical, add newer and advance both indices */ |
| 1648 | pNewSorted->add(pContents->itemAt(contIdx)); |
| 1649 | mergeIdx++; |
| 1650 | contIdx++; |
| 1651 | } else if (pMergedInfo->itemAt(mergeIdx) < pContents->itemAt(contIdx)) |
| 1652 | { |
| 1653 | /* "merge" is lower, add that one */ |
| 1654 | pNewSorted->add(pMergedInfo->itemAt(mergeIdx)); |
| 1655 | mergeIdx++; |
| 1656 | } else { |
| 1657 | /* "cont" is lower, add that one */ |
| 1658 | assert(pContents->itemAt(contIdx) < pMergedInfo->itemAt(mergeIdx)); |
| 1659 | pNewSorted->add(pContents->itemAt(contIdx)); |
| 1660 | contIdx++; |
| 1661 | } |
| 1662 | } |
| 1663 | |
| 1664 | /* |
| 1665 | * Overwrite the "merged" list with the new stuff. |
| 1666 | */ |
| 1667 | *pMergedInfo = *pNewSorted; |
| 1668 | delete pNewSorted; |
| 1669 | |
| 1670 | #if 0 // for Vector, rather than SortedVector |
| 1671 | int i, j; |
| 1672 | for (i = pContents->size() -1; i >= 0; i--) { |
| 1673 | bool add = true; |
| 1674 | |
| 1675 | for (j = pMergedInfo->size() -1; j >= 0; j--) { |
| 1676 | /* case-sensitive comparisons, to behave like UNIX fs */ |
| 1677 | if (strcmp(pContents->itemAt(i).mFileName, |
| 1678 | pMergedInfo->itemAt(j).mFileName) == 0) |
| 1679 | { |
| 1680 | /* match, don't add this entry */ |
| 1681 | add = false; |
| 1682 | break; |
| 1683 | } |
| 1684 | } |
| 1685 | |
| 1686 | if (add) |
| 1687 | pMergedInfo->add(pContents->itemAt(i)); |
| 1688 | } |
| 1689 | #endif |
| 1690 | } |
| 1691 | |
| 1692 | |
| 1693 | /* |
| 1694 | * Load all files into the file name cache. We want to do this across |
| 1695 | * all combinations of { appname, locale, vendor }, performing a recursive |
| 1696 | * directory traversal. |
| 1697 | * |
| 1698 | * This is not the most efficient data structure. Also, gathering the |
| 1699 | * information as we needed it (file-by-file or directory-by-directory) |
| 1700 | * would be faster. However, on the actual device, 99% of the files will |
| 1701 | * live in Zip archives, so this list will be very small. The trouble |
| 1702 | * is that we have to check the "loose" files first, so it's important |
| 1703 | * that we don't beat the filesystem silly looking for files that aren't |
| 1704 | * there. |
| 1705 | * |
| 1706 | * Note on thread safety: this is the only function that causes updates |
| 1707 | * to mCache, and anybody who tries to use it will call here if !mCacheValid, |
| 1708 | * so we need to employ a mutex here. |
| 1709 | */ |
| 1710 | void AssetManager::loadFileNameCacheLocked(void) |
| 1711 | { |
| 1712 | assert(!mCacheValid); |
| 1713 | assert(mCache.size() == 0); |
| 1714 | |
| 1715 | #ifdef DO_TIMINGS // need to link against -lrt for this now |
| 1716 | DurationTimer timer; |
| 1717 | timer.start(); |
| 1718 | #endif |
| 1719 | |
| 1720 | fncScanLocked(&mCache, ""); |
| 1721 | |
| 1722 | #ifdef DO_TIMINGS |
| 1723 | timer.stop(); |
| 1724 | ALOGD("Cache scan took %.3fms\n", |
| 1725 | timer.durationUsecs() / 1000.0); |
| 1726 | #endif |
| 1727 | |
| 1728 | #if 0 |
| 1729 | int i; |
| 1730 | printf("CACHED FILE LIST (%d entries):\n", mCache.size()); |
| 1731 | for (i = 0; i < (int) mCache.size(); i++) { |
| 1732 | printf(" %d: (%d) '%s'\n", i, |
| 1733 | mCache.itemAt(i).getFileType(), |
| 1734 | (const char*) mCache.itemAt(i).getFileName()); |
| 1735 | } |
| 1736 | #endif |
| 1737 | |
| 1738 | mCacheValid = true; |
| 1739 | } |
| 1740 | |
| 1741 | /* |
| 1742 | * Scan up to 8 versions of the specified directory. |
| 1743 | */ |
| 1744 | void AssetManager::fncScanLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo, |
| 1745 | const char* dirName) |
| 1746 | { |
| 1747 | size_t i = mAssetPaths.size(); |
| 1748 | while (i > 0) { |
| 1749 | i--; |
| 1750 | const asset_path& ap = mAssetPaths.itemAt(i); |
| 1751 | fncScanAndMergeDirLocked(pMergedInfo, ap, NULL, NULL, dirName); |
| 1752 | if (mLocale != NULL) |
| 1753 | fncScanAndMergeDirLocked(pMergedInfo, ap, mLocale, NULL, dirName); |
| 1754 | if (mVendor != NULL) |
| 1755 | fncScanAndMergeDirLocked(pMergedInfo, ap, NULL, mVendor, dirName); |
| 1756 | if (mLocale != NULL && mVendor != NULL) |
| 1757 | fncScanAndMergeDirLocked(pMergedInfo, ap, mLocale, mVendor, dirName); |
| 1758 | } |
| 1759 | } |
| 1760 | |
| 1761 | /* |
| 1762 | * Recursively scan this directory and all subdirs. |
| 1763 | * |
| 1764 | * This is similar to scanAndMergeDir, but we don't remove the .EXCLUDE |
| 1765 | * files, and we prepend the extended partial path to the filenames. |
| 1766 | */ |
| 1767 | bool AssetManager::fncScanAndMergeDirLocked( |
| 1768 | SortedVector<AssetDir::FileInfo>* pMergedInfo, |
| 1769 | const asset_path& ap, const char* locale, const char* vendor, |
| 1770 | const char* dirName) |
| 1771 | { |
| 1772 | SortedVector<AssetDir::FileInfo>* pContents; |
| 1773 | String8 partialPath; |
| 1774 | String8 fullPath; |
| 1775 | |
| 1776 | // XXX This is broken -- the filename cache needs to hold the base |
| 1777 | // asset path separately from its filename. |
| 1778 | |
| 1779 | partialPath = createPathNameLocked(ap, locale, vendor); |
| 1780 | if (dirName[0] != '\0') { |
| 1781 | partialPath.appendPath(dirName); |
| 1782 | } |
| 1783 | |
| 1784 | fullPath = partialPath; |
| 1785 | pContents = scanDirLocked(fullPath); |
| 1786 | if (pContents == NULL) { |
| 1787 | return false; // directory did not exist |
| 1788 | } |
| 1789 | |
| 1790 | /* |
| 1791 | * Scan all subdirectories of the current dir, merging what we find |
| 1792 | * into "pMergedInfo". |
| 1793 | */ |
| 1794 | for (int i = 0; i < (int) pContents->size(); i++) { |
| 1795 | if (pContents->itemAt(i).getFileType() == kFileTypeDirectory) { |
| 1796 | String8 subdir(dirName); |
| 1797 | subdir.appendPath(pContents->itemAt(i).getFileName()); |
| 1798 | |
| 1799 | fncScanAndMergeDirLocked(pMergedInfo, ap, locale, vendor, subdir.string()); |
| 1800 | } |
| 1801 | } |
| 1802 | |
| 1803 | /* |
| 1804 | * To be consistent, we want entries for the root directory. If |
| 1805 | * we're the root, add one now. |
| 1806 | */ |
| 1807 | if (dirName[0] == '\0') { |
| 1808 | AssetDir::FileInfo tmpInfo; |
| 1809 | |
| 1810 | tmpInfo.set(String8(""), kFileTypeDirectory); |
| 1811 | tmpInfo.setSourceName(createPathNameLocked(ap, locale, vendor)); |
| 1812 | pContents->add(tmpInfo); |
| 1813 | } |
| 1814 | |
| 1815 | /* |
| 1816 | * We want to prepend the extended partial path to every entry in |
| 1817 | * "pContents". It's the same value for each entry, so this will |
| 1818 | * not change the sorting order of the vector contents. |
| 1819 | */ |
| 1820 | for (int i = 0; i < (int) pContents->size(); i++) { |
| 1821 | const AssetDir::FileInfo& info = pContents->itemAt(i); |
| 1822 | pContents->editItemAt(i).setFileName(partialPath.appendPathCopy(info.getFileName())); |
| 1823 | } |
| 1824 | |
| 1825 | mergeInfoLocked(pMergedInfo, pContents); |
sean_lu | 7c57d23 | 2014-06-16 15:11:29 +0800 | [diff] [blame] | 1826 | delete pContents; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1827 | return true; |
| 1828 | } |
| 1829 | |
| 1830 | /* |
| 1831 | * Trash the cache. |
| 1832 | */ |
| 1833 | void AssetManager::purgeFileNameCacheLocked(void) |
| 1834 | { |
| 1835 | mCacheValid = false; |
| 1836 | mCache.clear(); |
| 1837 | } |
| 1838 | |
| 1839 | /* |
| 1840 | * =========================================================================== |
| 1841 | * AssetManager::SharedZip |
| 1842 | * =========================================================================== |
| 1843 | */ |
| 1844 | |
| 1845 | |
| 1846 | Mutex AssetManager::SharedZip::gLock; |
| 1847 | DefaultKeyedVector<String8, wp<AssetManager::SharedZip> > AssetManager::SharedZip::gOpen; |
| 1848 | |
| 1849 | AssetManager::SharedZip::SharedZip(const String8& path, time_t modWhen) |
| 1850 | : mPath(path), mZipFile(NULL), mModWhen(modWhen), |
| 1851 | mResourceTableAsset(NULL), mResourceTable(NULL) |
| 1852 | { |
| 1853 | //ALOGI("Creating SharedZip %p %s\n", this, (const char*)mPath); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1854 | ALOGV("+++ opening zip '%s'\n", mPath.string()); |
Narayan Kamath | 560566d | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 1855 | mZipFile = ZipFileRO::open(mPath.string()); |
| 1856 | if (mZipFile == NULL) { |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1857 | ALOGD("failed to open Zip archive '%s'\n", mPath.string()); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1858 | } |
| 1859 | } |
| 1860 | |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 1861 | sp<AssetManager::SharedZip> AssetManager::SharedZip::get(const String8& path, |
| 1862 | bool createIfNotPresent) |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1863 | { |
| 1864 | AutoMutex _l(gLock); |
| 1865 | time_t modWhen = getFileModDate(path); |
| 1866 | sp<SharedZip> zip = gOpen.valueFor(path).promote(); |
| 1867 | if (zip != NULL && zip->mModWhen == modWhen) { |
| 1868 | return zip; |
| 1869 | } |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 1870 | if (zip == NULL && !createIfNotPresent) { |
| 1871 | return NULL; |
| 1872 | } |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1873 | zip = new SharedZip(path, modWhen); |
| 1874 | gOpen.add(path, zip); |
| 1875 | return zip; |
| 1876 | |
| 1877 | } |
| 1878 | |
| 1879 | ZipFileRO* AssetManager::SharedZip::getZip() |
| 1880 | { |
| 1881 | return mZipFile; |
| 1882 | } |
| 1883 | |
| 1884 | Asset* AssetManager::SharedZip::getResourceTableAsset() |
| 1885 | { |
| 1886 | ALOGV("Getting from SharedZip %p resource asset %p\n", this, mResourceTableAsset); |
| 1887 | return mResourceTableAsset; |
| 1888 | } |
| 1889 | |
| 1890 | Asset* AssetManager::SharedZip::setResourceTableAsset(Asset* asset) |
| 1891 | { |
| 1892 | { |
| 1893 | AutoMutex _l(gLock); |
| 1894 | if (mResourceTableAsset == NULL) { |
| 1895 | mResourceTableAsset = asset; |
| 1896 | // This is not thread safe the first time it is called, so |
| 1897 | // do it here with the global lock held. |
| 1898 | asset->getBuffer(true); |
| 1899 | return asset; |
| 1900 | } |
| 1901 | } |
| 1902 | delete asset; |
| 1903 | return mResourceTableAsset; |
| 1904 | } |
| 1905 | |
| 1906 | ResTable* AssetManager::SharedZip::getResourceTable() |
| 1907 | { |
| 1908 | ALOGV("Getting from SharedZip %p resource table %p\n", this, mResourceTable); |
| 1909 | return mResourceTable; |
| 1910 | } |
| 1911 | |
| 1912 | ResTable* AssetManager::SharedZip::setResourceTable(ResTable* res) |
| 1913 | { |
| 1914 | { |
| 1915 | AutoMutex _l(gLock); |
| 1916 | if (mResourceTable == NULL) { |
| 1917 | mResourceTable = res; |
| 1918 | return res; |
| 1919 | } |
| 1920 | } |
| 1921 | delete res; |
| 1922 | return mResourceTable; |
| 1923 | } |
| 1924 | |
| 1925 | bool AssetManager::SharedZip::isUpToDate() |
| 1926 | { |
| 1927 | time_t modWhen = getFileModDate(mPath.string()); |
| 1928 | return mModWhen == modWhen; |
| 1929 | } |
| 1930 | |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 1931 | void AssetManager::SharedZip::addOverlay(const asset_path& ap) |
| 1932 | { |
| 1933 | mOverlays.add(ap); |
| 1934 | } |
| 1935 | |
| 1936 | bool AssetManager::SharedZip::getOverlay(size_t idx, asset_path* out) const |
| 1937 | { |
| 1938 | if (idx >= mOverlays.size()) { |
| 1939 | return false; |
| 1940 | } |
| 1941 | *out = mOverlays[idx]; |
| 1942 | return true; |
| 1943 | } |
| 1944 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1945 | AssetManager::SharedZip::~SharedZip() |
| 1946 | { |
| 1947 | //ALOGI("Destroying SharedZip %p %s\n", this, (const char*)mPath); |
| 1948 | if (mResourceTable != NULL) { |
| 1949 | delete mResourceTable; |
| 1950 | } |
| 1951 | if (mResourceTableAsset != NULL) { |
| 1952 | delete mResourceTableAsset; |
| 1953 | } |
| 1954 | if (mZipFile != NULL) { |
| 1955 | delete mZipFile; |
| 1956 | ALOGV("Closed '%s'\n", mPath.string()); |
| 1957 | } |
| 1958 | } |
| 1959 | |
| 1960 | /* |
| 1961 | * =========================================================================== |
| 1962 | * AssetManager::ZipSet |
| 1963 | * =========================================================================== |
| 1964 | */ |
| 1965 | |
| 1966 | /* |
| 1967 | * Constructor. |
| 1968 | */ |
| 1969 | AssetManager::ZipSet::ZipSet(void) |
| 1970 | { |
| 1971 | } |
| 1972 | |
| 1973 | /* |
| 1974 | * Destructor. Close any open archives. |
| 1975 | */ |
| 1976 | AssetManager::ZipSet::~ZipSet(void) |
| 1977 | { |
| 1978 | size_t N = mZipFile.size(); |
| 1979 | for (size_t i = 0; i < N; i++) |
| 1980 | closeZip(i); |
| 1981 | } |
| 1982 | |
| 1983 | /* |
| 1984 | * Close a Zip file and reset the entry. |
| 1985 | */ |
| 1986 | void AssetManager::ZipSet::closeZip(int idx) |
| 1987 | { |
| 1988 | mZipFile.editItemAt(idx) = NULL; |
| 1989 | } |
| 1990 | |
| 1991 | |
| 1992 | /* |
| 1993 | * Retrieve the appropriate Zip file from the set. |
| 1994 | */ |
| 1995 | ZipFileRO* AssetManager::ZipSet::getZip(const String8& path) |
| 1996 | { |
| 1997 | int idx = getIndex(path); |
| 1998 | sp<SharedZip> zip = mZipFile[idx]; |
| 1999 | if (zip == NULL) { |
| 2000 | zip = SharedZip::get(path); |
| 2001 | mZipFile.editItemAt(idx) = zip; |
| 2002 | } |
| 2003 | return zip->getZip(); |
| 2004 | } |
| 2005 | |
| 2006 | Asset* AssetManager::ZipSet::getZipResourceTableAsset(const String8& path) |
| 2007 | { |
| 2008 | int idx = getIndex(path); |
| 2009 | sp<SharedZip> zip = mZipFile[idx]; |
| 2010 | if (zip == NULL) { |
| 2011 | zip = SharedZip::get(path); |
| 2012 | mZipFile.editItemAt(idx) = zip; |
| 2013 | } |
| 2014 | return zip->getResourceTableAsset(); |
| 2015 | } |
| 2016 | |
| 2017 | Asset* AssetManager::ZipSet::setZipResourceTableAsset(const String8& path, |
| 2018 | Asset* asset) |
| 2019 | { |
| 2020 | int idx = getIndex(path); |
| 2021 | sp<SharedZip> zip = mZipFile[idx]; |
| 2022 | // doesn't make sense to call before previously accessing. |
| 2023 | return zip->setResourceTableAsset(asset); |
| 2024 | } |
| 2025 | |
| 2026 | ResTable* AssetManager::ZipSet::getZipResourceTable(const String8& path) |
| 2027 | { |
| 2028 | int idx = getIndex(path); |
| 2029 | sp<SharedZip> zip = mZipFile[idx]; |
| 2030 | if (zip == NULL) { |
| 2031 | zip = SharedZip::get(path); |
| 2032 | mZipFile.editItemAt(idx) = zip; |
| 2033 | } |
| 2034 | return zip->getResourceTable(); |
| 2035 | } |
| 2036 | |
| 2037 | ResTable* AssetManager::ZipSet::setZipResourceTable(const String8& path, |
| 2038 | ResTable* res) |
| 2039 | { |
| 2040 | int idx = getIndex(path); |
| 2041 | sp<SharedZip> zip = mZipFile[idx]; |
| 2042 | // doesn't make sense to call before previously accessing. |
| 2043 | return zip->setResourceTable(res); |
| 2044 | } |
| 2045 | |
| 2046 | /* |
| 2047 | * Generate the partial pathname for the specified archive. The caller |
| 2048 | * gets to prepend the asset root directory. |
| 2049 | * |
| 2050 | * Returns something like "common/en-US-noogle.jar". |
| 2051 | */ |
| 2052 | /*static*/ String8 AssetManager::ZipSet::getPathName(const char* zipPath) |
| 2053 | { |
| 2054 | return String8(zipPath); |
| 2055 | } |
| 2056 | |
| 2057 | bool AssetManager::ZipSet::isUpToDate() |
| 2058 | { |
| 2059 | const size_t N = mZipFile.size(); |
| 2060 | for (size_t i=0; i<N; i++) { |
| 2061 | if (mZipFile[i] != NULL && !mZipFile[i]->isUpToDate()) { |
| 2062 | return false; |
| 2063 | } |
| 2064 | } |
| 2065 | return true; |
| 2066 | } |
| 2067 | |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 2068 | void AssetManager::ZipSet::addOverlay(const String8& path, const asset_path& overlay) |
| 2069 | { |
| 2070 | int idx = getIndex(path); |
| 2071 | sp<SharedZip> zip = mZipFile[idx]; |
| 2072 | zip->addOverlay(overlay); |
| 2073 | } |
| 2074 | |
| 2075 | bool AssetManager::ZipSet::getOverlay(const String8& path, size_t idx, asset_path* out) const |
| 2076 | { |
| 2077 | sp<SharedZip> zip = SharedZip::get(path, false); |
| 2078 | if (zip == NULL) { |
| 2079 | return false; |
| 2080 | } |
| 2081 | return zip->getOverlay(idx, out); |
| 2082 | } |
| 2083 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 2084 | /* |
| 2085 | * Compute the zip file's index. |
| 2086 | * |
| 2087 | * "appName", "locale", and "vendor" should be set to NULL to indicate the |
| 2088 | * default directory. |
| 2089 | */ |
| 2090 | int AssetManager::ZipSet::getIndex(const String8& zip) const |
| 2091 | { |
| 2092 | const size_t N = mZipPath.size(); |
| 2093 | for (size_t i=0; i<N; i++) { |
| 2094 | if (mZipPath[i] == zip) { |
| 2095 | return i; |
| 2096 | } |
| 2097 | } |
| 2098 | |
| 2099 | mZipPath.add(zip); |
| 2100 | mZipFile.add(NULL); |
| 2101 | |
| 2102 | return mZipPath.size()-1; |
| 2103 | } |