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