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