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