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