1 /* 2 * Copyright (C) 2006 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 // 18 // Provide access to read-only assets. 19 // 20 21 #define LOG_TAG "asset" 22 #define ATRACE_TAG ATRACE_TAG_RESOURCES 23 //#define LOG_NDEBUG 0 24 25 #include <androidfw/Asset.h> 26 #include <androidfw/AssetDir.h> 27 #include <androidfw/AssetManager.h> 28 #include <androidfw/misc.h> 29 #include <androidfw/ResourceTypes.h> 30 #include <androidfw/ZipFileRO.h> 31 #include <utils/Atomic.h> 32 #include <utils/Log.h> 33 #include <utils/String8.h> 34 #include <utils/String8.h> 35 #include <utils/threads.h> 36 #include <utils/Timers.h> 37 #include <utils/Trace.h> 38 #ifndef _WIN32 39 #include <sys/file.h> 40 #endif 41 42 #include <assert.h> 43 #include <dirent.h> 44 #include <errno.h> 45 #include <string.h> // strerror 46 #include <strings.h> 47 48 #ifndef TEMP_FAILURE_RETRY 49 /* Used to retry syscalls that can return EINTR. */ 50 #define TEMP_FAILURE_RETRY(exp) ({ \ 51 typeof (exp) _rc; \ 52 do { \ 53 _rc = (exp); \ 54 } while (_rc == -1 && errno == EINTR); \ 55 _rc; }) 56 #endif 57 58 using namespace android; 59 60 static const bool kIsDebug = false; 61 62 static const char* kAssetsRoot = "assets"; 63 static const char* kAppZipName = NULL; //"classes.jar"; 64 static const char* kSystemAssets = "framework/framework-res.apk"; 65 static const char* kResourceCache = "resource-cache"; 66 67 static const char* kExcludeExtension = ".EXCLUDE"; 68 69 static Asset* const kExcludedAsset = (Asset*) 0xd000000d; 70 71 static volatile int32_t gCount = 0; 72 73 const char* AssetManager::RESOURCES_FILENAME = "resources.arsc"; 74 const char* AssetManager::IDMAP_BIN = "/system/bin/idmap"; 75 const char* AssetManager::OVERLAY_DIR = "/vendor/overlay"; 76 const char* AssetManager::OVERLAY_THEME_DIR_PROPERTY = "ro.boot.vendor.overlay.theme"; 77 const char* AssetManager::TARGET_PACKAGE_NAME = "android"; 78 const char* AssetManager::TARGET_APK_PATH = "/system/framework/framework-res.apk"; 79 const char* AssetManager::IDMAP_DIR = "/data/resource-cache"; 80 81 namespace { 82 83 String8 idmapPathForPackagePath(const String8& pkgPath) { 84 const char* root = getenv("ANDROID_DATA"); 85 LOG_ALWAYS_FATAL_IF(root == NULL, "ANDROID_DATA not set"); 86 String8 path(root); 87 path.appendPath(kResourceCache); 88 89 char buf[256]; // 256 chars should be enough for anyone... 90 strncpy(buf, pkgPath.string(), 255); 91 buf[255] = '\0'; 92 char* filename = buf; 93 while (*filename && *filename == '/') { 94 ++filename; 95 } 96 char* p = filename; 97 while (*p) { 98 if (*p == '/') { 99 *p = '@'; 100 } 101 ++p; 102 } 103 path.appendPath(filename); 104 path.append("@idmap"); 105 106 return path; 107 } 108 109 /* 110 * Like strdup(), but uses C++ "new" operator instead of malloc. 111 */ 112 static char* strdupNew(const char* str) { 113 char* newStr; 114 int len; 115 116 if (str == NULL) 117 return NULL; 118 119 len = strlen(str); 120 newStr = new char[len+1]; 121 memcpy(newStr, str, len+1); 122 123 return newStr; 124 } 125 126 } // namespace 127 128 /* 129 * =========================================================================== 130 * AssetManager 131 * =========================================================================== 132 */ 133 134 int32_t AssetManager::getGlobalCount() { 135 return gCount; 136 } 137 138 AssetManager::AssetManager() : 139 mLocale(NULL), mResources(NULL), mConfig(new ResTable_config) { 140 int count = android_atomic_inc(&gCount) + 1; 141 if (kIsDebug) { 142 ALOGI("Creating AssetManager %p #%d\n", this, count); 143 } 144 memset(mConfig, 0, sizeof(ResTable_config)); 145 } 146 147 AssetManager::~AssetManager() { 148 int count = android_atomic_dec(&gCount); 149 if (kIsDebug) { 150 ALOGI("Destroying AssetManager in %p #%d\n", this, count); 151 } 152 153 delete mConfig; 154 delete mResources; 155 156 // don't have a String class yet, so make sure we clean up 157 delete[] mLocale; 158 } 159 160 bool AssetManager::addAssetPath( 161 const String8& path, int32_t* cookie, bool appAsLib, bool isSystemAsset) { 162 AutoMutex _l(mLock); 163 164 asset_path ap; 165 166 String8 realPath(path); 167 if (kAppZipName) { 168 realPath.appendPath(kAppZipName); 169 } 170 ap.type = ::getFileType(realPath.string()); 171 if (ap.type == kFileTypeRegular) { 172 ap.path = realPath; 173 } else { 174 ap.path = path; 175 ap.type = ::getFileType(path.string()); 176 if (ap.type != kFileTypeDirectory && ap.type != kFileTypeRegular) { 177 ALOGW("Asset path %s is neither a directory nor file (type=%d).", 178 path.string(), (int)ap.type); 179 return false; 180 } 181 } 182 183 // Skip if we have it already. 184 for (size_t i=0; i<mAssetPaths.size(); i++) { 185 if (mAssetPaths[i].path == ap.path) { 186 if (cookie) { 187 *cookie = static_cast<int32_t>(i+1); 188 } 189 return true; 190 } 191 } 192 193 ALOGV("In %p Asset %s path: %s", this, 194 ap.type == kFileTypeDirectory ? "dir" : "zip", ap.path.string()); 195 196 ap.isSystemAsset = isSystemAsset; 197 mAssetPaths.add(ap); 198 199 // new paths are always added at the end 200 if (cookie) { 201 *cookie = static_cast<int32_t>(mAssetPaths.size()); 202 } 203 204 #ifdef __ANDROID__ 205 // Load overlays, if any 206 asset_path oap; 207 for (size_t idx = 0; mZipSet.getOverlay(ap.path, idx, &oap); idx++) { 208 oap.isSystemAsset = isSystemAsset; 209 mAssetPaths.add(oap); 210 } 211 #endif 212 213 if (mResources != NULL) { 214 appendPathToResTable(ap, appAsLib); 215 } 216 217 return true; 218 } 219 220 bool AssetManager::addOverlayPath(const String8& packagePath, int32_t* cookie) 221 { 222 const String8 idmapPath = idmapPathForPackagePath(packagePath); 223 224 AutoMutex _l(mLock); 225 226 for (size_t i = 0; i < mAssetPaths.size(); ++i) { 227 if (mAssetPaths[i].idmap == idmapPath) { 228 *cookie = static_cast<int32_t>(i + 1); 229 return true; 230 } 231 } 232 233 Asset* idmap = NULL; 234 if ((idmap = openAssetFromFileLocked(idmapPath, Asset::ACCESS_BUFFER)) == NULL) { 235 ALOGW("failed to open idmap file %s\n", idmapPath.string()); 236 return false; 237 } 238 239 String8 targetPath; 240 String8 overlayPath; 241 if (!ResTable::getIdmapInfo(idmap->getBuffer(false), idmap->getLength(), 242 NULL, NULL, NULL, &targetPath, &overlayPath)) { 243 ALOGW("failed to read idmap file %s\n", idmapPath.string()); 244 delete idmap; 245 return false; 246 } 247 delete idmap; 248 249 if (overlayPath != packagePath) { 250 ALOGW("idmap file %s inconcistent: expected path %s does not match actual path %s\n", 251 idmapPath.string(), packagePath.string(), overlayPath.string()); 252 return false; 253 } 254 if (access(targetPath.string(), R_OK) != 0) { 255 ALOGW("failed to access file %s: %s\n", targetPath.string(), strerror(errno)); 256 return false; 257 } 258 if (access(idmapPath.string(), R_OK) != 0) { 259 ALOGW("failed to access file %s: %s\n", idmapPath.string(), strerror(errno)); 260 return false; 261 } 262 if (access(overlayPath.string(), R_OK) != 0) { 263 ALOGW("failed to access file %s: %s\n", overlayPath.string(), strerror(errno)); 264 return false; 265 } 266 267 asset_path oap; 268 oap.path = overlayPath; 269 oap.type = ::getFileType(overlayPath.string()); 270 oap.idmap = idmapPath; 271 #if 0 272 ALOGD("Overlay added: targetPath=%s overlayPath=%s idmapPath=%s\n", 273 targetPath.string(), overlayPath.string(), idmapPath.string()); 274 #endif 275 mAssetPaths.add(oap); 276 *cookie = static_cast<int32_t>(mAssetPaths.size()); 277 278 if (mResources != NULL) { 279 appendPathToResTable(oap); 280 } 281 282 return true; 283 } 284 285 bool AssetManager::createIdmap(const char* targetApkPath, const char* overlayApkPath, 286 uint32_t targetCrc, uint32_t overlayCrc, uint32_t** outData, size_t* outSize) 287 { 288 AutoMutex _l(mLock); 289 const String8 paths[2] = { String8(targetApkPath), String8(overlayApkPath) }; 290 Asset* assets[2] = {NULL, NULL}; 291 bool ret = false; 292 { 293 ResTable tables[2]; 294 295 for (int i = 0; i < 2; ++i) { 296 asset_path ap; 297 ap.type = kFileTypeRegular; 298 ap.path = paths[i]; 299 assets[i] = openNonAssetInPathLocked("resources.arsc", 300 Asset::ACCESS_BUFFER, ap); 301 if (assets[i] == NULL) { 302 ALOGW("failed to find resources.arsc in %s\n", ap.path.string()); 303 goto exit; 304 } 305 if (tables[i].add(assets[i]) != NO_ERROR) { 306 ALOGW("failed to add %s to resource table", paths[i].string()); 307 goto exit; 308 } 309 } 310 ret = tables[0].createIdmap(tables[1], targetCrc, overlayCrc, 311 targetApkPath, overlayApkPath, (void**)outData, outSize) == NO_ERROR; 312 } 313 314 exit: 315 delete assets[0]; 316 delete assets[1]; 317 return ret; 318 } 319 320 bool AssetManager::addDefaultAssets() 321 { 322 const char* root = getenv("ANDROID_ROOT"); 323 LOG_ALWAYS_FATAL_IF(root == NULL, "ANDROID_ROOT not set"); 324 325 String8 path(root); 326 path.appendPath(kSystemAssets); 327 328 return addAssetPath(path, NULL, false /* appAsLib */, true /* isSystemAsset */); 329 } 330 331 int32_t AssetManager::nextAssetPath(const int32_t cookie) const 332 { 333 AutoMutex _l(mLock); 334 const size_t next = static_cast<size_t>(cookie) + 1; 335 return next > mAssetPaths.size() ? -1 : next; 336 } 337 338 String8 AssetManager::getAssetPath(const int32_t cookie) const 339 { 340 AutoMutex _l(mLock); 341 const size_t which = static_cast<size_t>(cookie) - 1; 342 if (which < mAssetPaths.size()) { 343 return mAssetPaths[which].path; 344 } 345 return String8(); 346 } 347 348 void AssetManager::setLocaleLocked(const char* locale) 349 { 350 if (mLocale != NULL) { 351 delete[] mLocale; 352 } 353 354 mLocale = strdupNew(locale); 355 updateResourceParamsLocked(); 356 } 357 358 void AssetManager::setConfiguration(const ResTable_config& config, const char* locale) 359 { 360 AutoMutex _l(mLock); 361 *mConfig = config; 362 if (locale) { 363 setLocaleLocked(locale); 364 } else if (config.language[0] != 0) { 365 char spec[RESTABLE_MAX_LOCALE_LEN]; 366 config.getBcp47Locale(spec); 367 setLocaleLocked(spec); 368 } else { 369 updateResourceParamsLocked(); 370 } 371 } 372 373 void AssetManager::getConfiguration(ResTable_config* outConfig) const 374 { 375 AutoMutex _l(mLock); 376 *outConfig = *mConfig; 377 } 378 379 /* 380 * Open an asset. 381 * 382 * The data could be in any asset path. Each asset path could be: 383 * - A directory on disk. 384 * - A Zip archive, uncompressed or compressed. 385 * 386 * If the file is in a directory, it could have a .gz suffix, meaning it is compressed. 387 * 388 * We should probably reject requests for "illegal" filenames, e.g. those 389 * with illegal characters or "../" backward relative paths. 390 */ 391 Asset* AssetManager::open(const char* fileName, AccessMode mode) 392 { 393 AutoMutex _l(mLock); 394 395 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager"); 396 397 String8 assetName(kAssetsRoot); 398 assetName.appendPath(fileName); 399 400 /* 401 * For each top-level asset path, search for the asset. 402 */ 403 404 size_t i = mAssetPaths.size(); 405 while (i > 0) { 406 i--; 407 ALOGV("Looking for asset '%s' in '%s'\n", 408 assetName.string(), mAssetPaths.itemAt(i).path.string()); 409 Asset* pAsset = openNonAssetInPathLocked(assetName.string(), mode, mAssetPaths.itemAt(i)); 410 if (pAsset != NULL) { 411 return pAsset != kExcludedAsset ? pAsset : NULL; 412 } 413 } 414 415 return NULL; 416 } 417 418 /* 419 * Open a non-asset file as if it were an asset. 420 * 421 * The "fileName" is the partial path starting from the application name. 422 */ 423 Asset* AssetManager::openNonAsset(const char* fileName, AccessMode mode, int32_t* outCookie) 424 { 425 AutoMutex _l(mLock); 426 427 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager"); 428 429 /* 430 * For each top-level asset path, search for the asset. 431 */ 432 433 size_t i = mAssetPaths.size(); 434 while (i > 0) { 435 i--; 436 ALOGV("Looking for non-asset '%s' in '%s'\n", fileName, mAssetPaths.itemAt(i).path.string()); 437 Asset* pAsset = openNonAssetInPathLocked( 438 fileName, mode, mAssetPaths.itemAt(i)); 439 if (pAsset != NULL) { 440 if (outCookie != NULL) *outCookie = static_cast<int32_t>(i + 1); 441 return pAsset != kExcludedAsset ? pAsset : NULL; 442 } 443 } 444 445 return NULL; 446 } 447 448 Asset* AssetManager::openNonAsset(const int32_t cookie, const char* fileName, AccessMode mode) 449 { 450 const size_t which = static_cast<size_t>(cookie) - 1; 451 452 AutoMutex _l(mLock); 453 454 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager"); 455 456 if (which < mAssetPaths.size()) { 457 ALOGV("Looking for non-asset '%s' in '%s'\n", fileName, 458 mAssetPaths.itemAt(which).path.string()); 459 Asset* pAsset = openNonAssetInPathLocked( 460 fileName, mode, mAssetPaths.itemAt(which)); 461 if (pAsset != NULL) { 462 return pAsset != kExcludedAsset ? pAsset : NULL; 463 } 464 } 465 466 return NULL; 467 } 468 469 /* 470 * Get the type of a file in the asset namespace. 471 * 472 * This currently only works for regular files. All others (including 473 * directories) will return kFileTypeNonexistent. 474 */ 475 FileType AssetManager::getFileType(const char* fileName) 476 { 477 Asset* pAsset = NULL; 478 479 /* 480 * Open the asset. This is less efficient than simply finding the 481 * file, but it's not too bad (we don't uncompress or mmap data until 482 * the first read() call). 483 */ 484 pAsset = open(fileName, Asset::ACCESS_STREAMING); 485 delete pAsset; 486 487 if (pAsset == NULL) { 488 return kFileTypeNonexistent; 489 } else { 490 return kFileTypeRegular; 491 } 492 } 493 494 bool AssetManager::appendPathToResTable(const asset_path& ap, bool appAsLib) const { 495 // skip those ap's that correspond to system overlays 496 if (ap.isSystemOverlay) { 497 return true; 498 } 499 500 Asset* ass = NULL; 501 ResTable* sharedRes = NULL; 502 bool shared = true; 503 bool onlyEmptyResources = true; 504 ATRACE_NAME(ap.path.string()); 505 Asset* idmap = openIdmapLocked(ap); 506 size_t nextEntryIdx = mResources->getTableCount(); 507 ALOGV("Looking for resource asset in '%s'\n", ap.path.string()); 508 if (ap.type != kFileTypeDirectory) { 509 if (nextEntryIdx == 0) { 510 // The first item is typically the framework resources, 511 // which we want to avoid parsing every time. 512 sharedRes = const_cast<AssetManager*>(this)-> 513 mZipSet.getZipResourceTable(ap.path); 514 if (sharedRes != NULL) { 515 // skip ahead the number of system overlay packages preloaded 516 nextEntryIdx = sharedRes->getTableCount(); 517 } 518 } 519 if (sharedRes == NULL) { 520 ass = const_cast<AssetManager*>(this)-> 521 mZipSet.getZipResourceTableAsset(ap.path); 522 if (ass == NULL) { 523 ALOGV("loading resource table %s\n", ap.path.string()); 524 ass = const_cast<AssetManager*>(this)-> 525 openNonAssetInPathLocked("resources.arsc", 526 Asset::ACCESS_BUFFER, 527 ap); 528 if (ass != NULL && ass != kExcludedAsset) { 529 ass = const_cast<AssetManager*>(this)-> 530 mZipSet.setZipResourceTableAsset(ap.path, ass); 531 } 532 } 533 534 if (nextEntryIdx == 0 && ass != NULL) { 535 // If this is the first resource table in the asset 536 // manager, then we are going to cache it so that we 537 // can quickly copy it out for others. 538 ALOGV("Creating shared resources for %s", ap.path.string()); 539 sharedRes = new ResTable(); 540 sharedRes->add(ass, idmap, nextEntryIdx + 1, false); 541 #ifdef __ANDROID__ 542 const char* data = getenv("ANDROID_DATA"); 543 LOG_ALWAYS_FATAL_IF(data == NULL, "ANDROID_DATA not set"); 544 String8 overlaysListPath(data); 545 overlaysListPath.appendPath(kResourceCache); 546 overlaysListPath.appendPath("overlays.list"); 547 addSystemOverlays(overlaysListPath.string(), ap.path, sharedRes, nextEntryIdx); 548 #endif 549 sharedRes = const_cast<AssetManager*>(this)-> 550 mZipSet.setZipResourceTable(ap.path, sharedRes); 551 } 552 } 553 } else { 554 ALOGV("loading resource table %s\n", ap.path.string()); 555 ass = const_cast<AssetManager*>(this)-> 556 openNonAssetInPathLocked("resources.arsc", 557 Asset::ACCESS_BUFFER, 558 ap); 559 shared = false; 560 } 561 562 if ((ass != NULL || sharedRes != NULL) && ass != kExcludedAsset) { 563 ALOGV("Installing resource asset %p in to table %p\n", ass, mResources); 564 if (sharedRes != NULL) { 565 ALOGV("Copying existing resources for %s", ap.path.string()); 566 mResources->add(sharedRes, ap.isSystemAsset); 567 } else { 568 ALOGV("Parsing resources for %s", ap.path.string()); 569 mResources->add(ass, idmap, nextEntryIdx + 1, !shared, appAsLib, ap.isSystemAsset); 570 } 571 onlyEmptyResources = false; 572 573 if (!shared) { 574 delete ass; 575 } 576 } else { 577 ALOGV("Installing empty resources in to table %p\n", mResources); 578 mResources->addEmpty(nextEntryIdx + 1); 579 } 580 581 if (idmap != NULL) { 582 delete idmap; 583 } 584 return onlyEmptyResources; 585 } 586 587 const ResTable* AssetManager::getResTable(bool required) const 588 { 589 ResTable* rt = mResources; 590 if (rt) { 591 return rt; 592 } 593 594 // Iterate through all asset packages, collecting resources from each. 595 596 AutoMutex _l(mLock); 597 598 if (mResources != NULL) { 599 return mResources; 600 } 601 602 if (required) { 603 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager"); 604 } 605 606 mResources = new ResTable(); 607 updateResourceParamsLocked(); 608 609 bool onlyEmptyResources = true; 610 const size_t N = mAssetPaths.size(); 611 for (size_t i=0; i<N; i++) { 612 bool empty = appendPathToResTable(mAssetPaths.itemAt(i)); 613 onlyEmptyResources = onlyEmptyResources && empty; 614 } 615 616 if (required && onlyEmptyResources) { 617 ALOGW("Unable to find resources file resources.arsc"); 618 delete mResources; 619 mResources = NULL; 620 } 621 622 return mResources; 623 } 624 625 void AssetManager::updateResourceParamsLocked() const 626 { 627 ATRACE_CALL(); 628 ResTable* res = mResources; 629 if (!res) { 630 return; 631 } 632 633 if (mLocale) { 634 mConfig->setBcp47Locale(mLocale); 635 } else { 636 mConfig->clearLocale(); 637 } 638 639 res->setParameters(mConfig); 640 } 641 642 Asset* AssetManager::openIdmapLocked(const struct asset_path& ap) const 643 { 644 Asset* ass = NULL; 645 if (ap.idmap.size() != 0) { 646 ass = const_cast<AssetManager*>(this)-> 647 openAssetFromFileLocked(ap.idmap, Asset::ACCESS_BUFFER); 648 if (ass) { 649 ALOGV("loading idmap %s\n", ap.idmap.string()); 650 } else { 651 ALOGW("failed to load idmap %s\n", ap.idmap.string()); 652 } 653 } 654 return ass; 655 } 656 657 void AssetManager::addSystemOverlays(const char* pathOverlaysList, 658 const String8& targetPackagePath, ResTable* sharedRes, size_t offset) const 659 { 660 FILE* fin = fopen(pathOverlaysList, "r"); 661 if (fin == NULL) { 662 return; 663 } 664 665 #ifndef _WIN32 666 if (TEMP_FAILURE_RETRY(flock(fileno(fin), LOCK_SH)) != 0) { 667 fclose(fin); 668 return; 669 } 670 #endif 671 char buf[1024]; 672 while (fgets(buf, sizeof(buf), fin)) { 673 // format of each line: 674 // <path to apk><space><path to idmap><newline> 675 char* space = strchr(buf, ' '); 676 char* newline = strchr(buf, '\n'); 677 asset_path oap; 678 679 if (space == NULL || newline == NULL || newline < space) { 680 continue; 681 } 682 683 oap.path = String8(buf, space - buf); 684 oap.type = kFileTypeRegular; 685 oap.idmap = String8(space + 1, newline - space - 1); 686 oap.isSystemOverlay = true; 687 688 Asset* oass = const_cast<AssetManager*>(this)-> 689 openNonAssetInPathLocked("resources.arsc", 690 Asset::ACCESS_BUFFER, 691 oap); 692 693 if (oass != NULL) { 694 Asset* oidmap = openIdmapLocked(oap); 695 offset++; 696 sharedRes->add(oass, oidmap, offset + 1, false); 697 const_cast<AssetManager*>(this)->mAssetPaths.add(oap); 698 const_cast<AssetManager*>(this)->mZipSet.addOverlay(targetPackagePath, oap); 699 delete oidmap; 700 } 701 } 702 703 #ifndef _WIN32 704 TEMP_FAILURE_RETRY(flock(fileno(fin), LOCK_UN)); 705 #endif 706 fclose(fin); 707 } 708 709 const ResTable& AssetManager::getResources(bool required) const 710 { 711 const ResTable* rt = getResTable(required); 712 return *rt; 713 } 714 715 bool AssetManager::isUpToDate() 716 { 717 AutoMutex _l(mLock); 718 return mZipSet.isUpToDate(); 719 } 720 721 void AssetManager::getLocales(Vector<String8>* locales, bool includeSystemLocales) const 722 { 723 ResTable* res = mResources; 724 if (res != NULL) { 725 res->getLocales(locales, includeSystemLocales, true /* mergeEquivalentLangs */); 726 } 727 } 728 729 /* 730 * Open a non-asset file as if it were an asset, searching for it in the 731 * specified app. 732 * 733 * Pass in a NULL values for "appName" if the common app directory should 734 * be used. 735 */ 736 Asset* AssetManager::openNonAssetInPathLocked(const char* fileName, AccessMode mode, 737 const asset_path& ap) 738 { 739 Asset* pAsset = NULL; 740 741 /* look at the filesystem on disk */ 742 if (ap.type == kFileTypeDirectory) { 743 String8 path(ap.path); 744 path.appendPath(fileName); 745 746 pAsset = openAssetFromFileLocked(path, mode); 747 748 if (pAsset == NULL) { 749 /* try again, this time with ".gz" */ 750 path.append(".gz"); 751 pAsset = openAssetFromFileLocked(path, mode); 752 } 753 754 if (pAsset != NULL) { 755 //printf("FOUND NA '%s' on disk\n", fileName); 756 pAsset->setAssetSource(path); 757 } 758 759 /* look inside the zip file */ 760 } else { 761 String8 path(fileName); 762 763 /* check the appropriate Zip file */ 764 ZipFileRO* pZip = getZipFileLocked(ap); 765 if (pZip != NULL) { 766 //printf("GOT zip, checking NA '%s'\n", (const char*) path); 767 ZipEntryRO entry = pZip->findEntryByName(path.string()); 768 if (entry != NULL) { 769 //printf("FOUND NA in Zip file for %s\n", appName ? appName : kAppCommon); 770 pAsset = openAssetFromZipLocked(pZip, entry, mode, path); 771 pZip->releaseEntry(entry); 772 } 773 } 774 775 if (pAsset != NULL) { 776 /* create a "source" name, for debug/display */ 777 pAsset->setAssetSource( 778 createZipSourceNameLocked(ZipSet::getPathName(ap.path.string()), String8(""), 779 String8(fileName))); 780 } 781 } 782 783 return pAsset; 784 } 785 786 /* 787 * Create a "source name" for a file from a Zip archive. 788 */ 789 String8 AssetManager::createZipSourceNameLocked(const String8& zipFileName, 790 const String8& dirName, const String8& fileName) 791 { 792 String8 sourceName("zip:"); 793 sourceName.append(zipFileName); 794 sourceName.append(":"); 795 if (dirName.length() > 0) { 796 sourceName.appendPath(dirName); 797 } 798 sourceName.appendPath(fileName); 799 return sourceName; 800 } 801 802 /* 803 * Create a path to a loose asset (asset-base/app/rootDir). 804 */ 805 String8 AssetManager::createPathNameLocked(const asset_path& ap, const char* rootDir) 806 { 807 String8 path(ap.path); 808 if (rootDir != NULL) path.appendPath(rootDir); 809 return path; 810 } 811 812 /* 813 * Return a pointer to one of our open Zip archives. Returns NULL if no 814 * matching Zip file exists. 815 */ 816 ZipFileRO* AssetManager::getZipFileLocked(const asset_path& ap) 817 { 818 ALOGV("getZipFileLocked() in %p\n", this); 819 820 return mZipSet.getZip(ap.path); 821 } 822 823 /* 824 * Try to open an asset from a file on disk. 825 * 826 * If the file is compressed with gzip, we seek to the start of the 827 * deflated data and pass that in (just like we would for a Zip archive). 828 * 829 * For uncompressed data, we may already have an mmap()ed version sitting 830 * around. If so, we want to hand that to the Asset instead. 831 * 832 * This returns NULL if the file doesn't exist, couldn't be opened, or 833 * claims to be a ".gz" but isn't. 834 */ 835 Asset* AssetManager::openAssetFromFileLocked(const String8& pathName, 836 AccessMode mode) 837 { 838 Asset* pAsset = NULL; 839 840 if (strcasecmp(pathName.getPathExtension().string(), ".gz") == 0) { 841 //printf("TRYING '%s'\n", (const char*) pathName); 842 pAsset = Asset::createFromCompressedFile(pathName.string(), mode); 843 } else { 844 //printf("TRYING '%s'\n", (const char*) pathName); 845 pAsset = Asset::createFromFile(pathName.string(), mode); 846 } 847 848 return pAsset; 849 } 850 851 /* 852 * Given an entry in a Zip archive, create a new Asset object. 853 * 854 * If the entry is uncompressed, we may want to create or share a 855 * slice of shared memory. 856 */ 857 Asset* AssetManager::openAssetFromZipLocked(const ZipFileRO* pZipFile, 858 const ZipEntryRO entry, AccessMode mode, const String8& entryName) 859 { 860 Asset* pAsset = NULL; 861 862 // TODO: look for previously-created shared memory slice? 863 uint16_t method; 864 uint32_t uncompressedLen; 865 866 //printf("USING Zip '%s'\n", pEntry->getFileName()); 867 868 if (!pZipFile->getEntryInfo(entry, &method, &uncompressedLen, NULL, NULL, 869 NULL, NULL)) 870 { 871 ALOGW("getEntryInfo failed\n"); 872 return NULL; 873 } 874 875 FileMap* dataMap = pZipFile->createEntryFileMap(entry); 876 if (dataMap == NULL) { 877 ALOGW("create map from entry failed\n"); 878 return NULL; 879 } 880 881 if (method == ZipFileRO::kCompressStored) { 882 pAsset = Asset::createFromUncompressedMap(dataMap, mode); 883 ALOGV("Opened uncompressed entry %s in zip %s mode %d: %p", entryName.string(), 884 dataMap->getFileName(), mode, pAsset); 885 } else { 886 pAsset = Asset::createFromCompressedMap(dataMap, 887 static_cast<size_t>(uncompressedLen), mode); 888 ALOGV("Opened compressed entry %s in zip %s mode %d: %p", entryName.string(), 889 dataMap->getFileName(), mode, pAsset); 890 } 891 if (pAsset == NULL) { 892 /* unexpected */ 893 ALOGW("create from segment failed\n"); 894 } 895 896 return pAsset; 897 } 898 899 /* 900 * Open a directory in the asset namespace. 901 * 902 * An "asset directory" is simply the combination of all asset paths' "assets/" directories. 903 * 904 * Pass in "" for the root dir. 905 */ 906 AssetDir* AssetManager::openDir(const char* dirName) 907 { 908 AutoMutex _l(mLock); 909 910 AssetDir* pDir = NULL; 911 SortedVector<AssetDir::FileInfo>* pMergedInfo = NULL; 912 913 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager"); 914 assert(dirName != NULL); 915 916 //printf("+++ openDir(%s) in '%s'\n", dirName, (const char*) mAssetBase); 917 918 pDir = new AssetDir; 919 920 /* 921 * Scan the various directories, merging what we find into a single 922 * vector. We want to scan them in reverse priority order so that 923 * the ".EXCLUDE" processing works correctly. Also, if we decide we 924 * want to remember where the file is coming from, we'll get the right 925 * version. 926 * 927 * We start with Zip archives, then do loose files. 928 */ 929 pMergedInfo = new SortedVector<AssetDir::FileInfo>; 930 931 size_t i = mAssetPaths.size(); 932 while (i > 0) { 933 i--; 934 const asset_path& ap = mAssetPaths.itemAt(i); 935 if (ap.type == kFileTypeRegular) { 936 ALOGV("Adding directory %s from zip %s", dirName, ap.path.string()); 937 scanAndMergeZipLocked(pMergedInfo, ap, kAssetsRoot, dirName); 938 } else { 939 ALOGV("Adding directory %s from dir %s", dirName, ap.path.string()); 940 scanAndMergeDirLocked(pMergedInfo, ap, kAssetsRoot, dirName); 941 } 942 } 943 944 #if 0 945 printf("FILE LIST:\n"); 946 for (i = 0; i < (size_t) pMergedInfo->size(); i++) { 947 printf(" %d: (%d) '%s'\n", i, 948 pMergedInfo->itemAt(i).getFileType(), 949 (const char*) pMergedInfo->itemAt(i).getFileName()); 950 } 951 #endif 952 953 pDir->setFileList(pMergedInfo); 954 return pDir; 955 } 956 957 /* 958 * Open a directory in the non-asset namespace. 959 * 960 * An "asset directory" is simply the combination of all asset paths' "assets/" directories. 961 * 962 * Pass in "" for the root dir. 963 */ 964 AssetDir* AssetManager::openNonAssetDir(const int32_t cookie, const char* dirName) 965 { 966 AutoMutex _l(mLock); 967 968 AssetDir* pDir = NULL; 969 SortedVector<AssetDir::FileInfo>* pMergedInfo = NULL; 970 971 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager"); 972 assert(dirName != NULL); 973 974 //printf("+++ openDir(%s) in '%s'\n", dirName, (const char*) mAssetBase); 975 976 pDir = new AssetDir; 977 978 pMergedInfo = new SortedVector<AssetDir::FileInfo>; 979 980 const size_t which = static_cast<size_t>(cookie) - 1; 981 982 if (which < mAssetPaths.size()) { 983 const asset_path& ap = mAssetPaths.itemAt(which); 984 if (ap.type == kFileTypeRegular) { 985 ALOGV("Adding directory %s from zip %s", dirName, ap.path.string()); 986 scanAndMergeZipLocked(pMergedInfo, ap, NULL, dirName); 987 } else { 988 ALOGV("Adding directory %s from dir %s", dirName, ap.path.string()); 989 scanAndMergeDirLocked(pMergedInfo, ap, NULL, dirName); 990 } 991 } 992 993 #if 0 994 printf("FILE LIST:\n"); 995 for (i = 0; i < (size_t) pMergedInfo->size(); i++) { 996 printf(" %d: (%d) '%s'\n", i, 997 pMergedInfo->itemAt(i).getFileType(), 998 (const char*) pMergedInfo->itemAt(i).getFileName()); 999 } 1000 #endif 1001 1002 pDir->setFileList(pMergedInfo); 1003 return pDir; 1004 } 1005 1006 /* 1007 * Scan the contents of the specified directory and merge them into the 1008 * "pMergedInfo" vector, removing previous entries if we find "exclude" 1009 * directives. 1010 * 1011 * Returns "false" if we found nothing to contribute. 1012 */ 1013 bool AssetManager::scanAndMergeDirLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo, 1014 const asset_path& ap, const char* rootDir, const char* dirName) 1015 { 1016 assert(pMergedInfo != NULL); 1017 1018 //printf("scanAndMergeDir: %s %s %s\n", ap.path.string(), rootDir, dirName); 1019 1020 String8 path = createPathNameLocked(ap, rootDir); 1021 if (dirName[0] != '\0') 1022 path.appendPath(dirName); 1023 1024 SortedVector<AssetDir::FileInfo>* pContents = scanDirLocked(path); 1025 if (pContents == NULL) 1026 return false; 1027 1028 // if we wanted to do an incremental cache fill, we would do it here 1029 1030 /* 1031 * Process "exclude" directives. If we find a filename that ends with 1032 * ".EXCLUDE", we look for a matching entry in the "merged" set, and 1033 * remove it if we find it. We also delete the "exclude" entry. 1034 */ 1035 int i, count, exclExtLen; 1036 1037 count = pContents->size(); 1038 exclExtLen = strlen(kExcludeExtension); 1039 for (i = 0; i < count; i++) { 1040 const char* name; 1041 int nameLen; 1042 1043 name = pContents->itemAt(i).getFileName().string(); 1044 nameLen = strlen(name); 1045 if (nameLen > exclExtLen && 1046 strcmp(name + (nameLen - exclExtLen), kExcludeExtension) == 0) 1047 { 1048 String8 match(name, nameLen - exclExtLen); 1049 int matchIdx; 1050 1051 matchIdx = AssetDir::FileInfo::findEntry(pMergedInfo, match); 1052 if (matchIdx > 0) { 1053 ALOGV("Excluding '%s' [%s]\n", 1054 pMergedInfo->itemAt(matchIdx).getFileName().string(), 1055 pMergedInfo->itemAt(matchIdx).getSourceName().string()); 1056 pMergedInfo->removeAt(matchIdx); 1057 } else { 1058 //printf("+++ no match on '%s'\n", (const char*) match); 1059 } 1060 1061 ALOGD("HEY: size=%d removing %d\n", (int)pContents->size(), i); 1062 pContents->removeAt(i); 1063 i--; // adjust "for" loop 1064 count--; // and loop limit 1065 } 1066 } 1067 1068 mergeInfoLocked(pMergedInfo, pContents); 1069 1070 delete pContents; 1071 1072 return true; 1073 } 1074 1075 /* 1076 * Scan the contents of the specified directory, and stuff what we find 1077 * into a newly-allocated vector. 1078 * 1079 * Files ending in ".gz" will have their extensions removed. 1080 * 1081 * We should probably think about skipping files with "illegal" names, 1082 * e.g. illegal characters (/\:) or excessive length. 1083 * 1084 * Returns NULL if the specified directory doesn't exist. 1085 */ 1086 SortedVector<AssetDir::FileInfo>* AssetManager::scanDirLocked(const String8& path) 1087 { 1088 SortedVector<AssetDir::FileInfo>* pContents = NULL; 1089 DIR* dir; 1090 struct dirent* entry; 1091 FileType fileType; 1092 1093 ALOGV("Scanning dir '%s'\n", path.string()); 1094 1095 dir = opendir(path.string()); 1096 if (dir == NULL) 1097 return NULL; 1098 1099 pContents = new SortedVector<AssetDir::FileInfo>; 1100 1101 while (1) { 1102 entry = readdir(dir); 1103 if (entry == NULL) 1104 break; 1105 1106 if (strcmp(entry->d_name, ".") == 0 || 1107 strcmp(entry->d_name, "..") == 0) 1108 continue; 1109 1110 #ifdef _DIRENT_HAVE_D_TYPE 1111 if (entry->d_type == DT_REG) 1112 fileType = kFileTypeRegular; 1113 else if (entry->d_type == DT_DIR) 1114 fileType = kFileTypeDirectory; 1115 else 1116 fileType = kFileTypeUnknown; 1117 #else 1118 // stat the file 1119 fileType = ::getFileType(path.appendPathCopy(entry->d_name).string()); 1120 #endif 1121 1122 if (fileType != kFileTypeRegular && fileType != kFileTypeDirectory) 1123 continue; 1124 1125 AssetDir::FileInfo info; 1126 info.set(String8(entry->d_name), fileType); 1127 if (strcasecmp(info.getFileName().getPathExtension().string(), ".gz") == 0) 1128 info.setFileName(info.getFileName().getBasePath()); 1129 info.setSourceName(path.appendPathCopy(info.getFileName())); 1130 pContents->add(info); 1131 } 1132 1133 closedir(dir); 1134 return pContents; 1135 } 1136 1137 /* 1138 * Scan the contents out of the specified Zip archive, and merge what we 1139 * find into "pMergedInfo". If the Zip archive in question doesn't exist, 1140 * we return immediately. 1141 * 1142 * Returns "false" if we found nothing to contribute. 1143 */ 1144 bool AssetManager::scanAndMergeZipLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo, 1145 const asset_path& ap, const char* rootDir, const char* baseDirName) 1146 { 1147 ZipFileRO* pZip; 1148 Vector<String8> dirs; 1149 AssetDir::FileInfo info; 1150 SortedVector<AssetDir::FileInfo> contents; 1151 String8 sourceName, zipName, dirName; 1152 1153 pZip = mZipSet.getZip(ap.path); 1154 if (pZip == NULL) { 1155 ALOGW("Failure opening zip %s\n", ap.path.string()); 1156 return false; 1157 } 1158 1159 zipName = ZipSet::getPathName(ap.path.string()); 1160 1161 /* convert "sounds" to "rootDir/sounds" */ 1162 if (rootDir != NULL) dirName = rootDir; 1163 dirName.appendPath(baseDirName); 1164 1165 /* 1166 * Scan through the list of files, looking for a match. The files in 1167 * the Zip table of contents are not in sorted order, so we have to 1168 * process the entire list. We're looking for a string that begins 1169 * with the characters in "dirName", is followed by a '/', and has no 1170 * subsequent '/' in the stuff that follows. 1171 * 1172 * What makes this especially fun is that directories are not stored 1173 * explicitly in Zip archives, so we have to infer them from context. 1174 * When we see "sounds/foo.wav" we have to leave a note to ourselves 1175 * to insert a directory called "sounds" into the list. We store 1176 * these in temporary vector so that we only return each one once. 1177 * 1178 * Name comparisons are case-sensitive to match UNIX filesystem 1179 * semantics. 1180 */ 1181 int dirNameLen = dirName.length(); 1182 void *iterationCookie; 1183 if (!pZip->startIteration(&iterationCookie, dirName.string(), NULL)) { 1184 ALOGW("ZipFileRO::startIteration returned false"); 1185 return false; 1186 } 1187 1188 ZipEntryRO entry; 1189 while ((entry = pZip->nextEntry(iterationCookie)) != NULL) { 1190 char nameBuf[256]; 1191 1192 if (pZip->getEntryFileName(entry, nameBuf, sizeof(nameBuf)) != 0) { 1193 // TODO: fix this if we expect to have long names 1194 ALOGE("ARGH: name too long?\n"); 1195 continue; 1196 } 1197 //printf("Comparing %s in %s?\n", nameBuf, dirName.string()); 1198 if (dirNameLen == 0 || nameBuf[dirNameLen] == '/') 1199 { 1200 const char* cp; 1201 const char* nextSlash; 1202 1203 cp = nameBuf + dirNameLen; 1204 if (dirNameLen != 0) 1205 cp++; // advance past the '/' 1206 1207 nextSlash = strchr(cp, '/'); 1208 //xxx this may break if there are bare directory entries 1209 if (nextSlash == NULL) { 1210 /* this is a file in the requested directory */ 1211 1212 info.set(String8(nameBuf).getPathLeaf(), kFileTypeRegular); 1213 1214 info.setSourceName( 1215 createZipSourceNameLocked(zipName, dirName, info.getFileName())); 1216 1217 contents.add(info); 1218 //printf("FOUND: file '%s'\n", info.getFileName().string()); 1219 } else { 1220 /* this is a subdir; add it if we don't already have it*/ 1221 String8 subdirName(cp, nextSlash - cp); 1222 size_t j; 1223 size_t N = dirs.size(); 1224 1225 for (j = 0; j < N; j++) { 1226 if (subdirName == dirs[j]) { 1227 break; 1228 } 1229 } 1230 if (j == N) { 1231 dirs.add(subdirName); 1232 } 1233 1234 //printf("FOUND: dir '%s'\n", subdirName.string()); 1235 } 1236 } 1237 } 1238 1239 pZip->endIteration(iterationCookie); 1240 1241 /* 1242 * Add the set of unique directories. 1243 */ 1244 for (int i = 0; i < (int) dirs.size(); i++) { 1245 info.set(dirs[i], kFileTypeDirectory); 1246 info.setSourceName( 1247 createZipSourceNameLocked(zipName, dirName, info.getFileName())); 1248 contents.add(info); 1249 } 1250 1251 mergeInfoLocked(pMergedInfo, &contents); 1252 1253 return true; 1254 } 1255 1256 1257 /* 1258 * Merge two vectors of FileInfo. 1259 * 1260 * The merged contents will be stuffed into *pMergedInfo. 1261 * 1262 * If an entry for a file exists in both "pMergedInfo" and "pContents", 1263 * we use the newer "pContents" entry. 1264 */ 1265 void AssetManager::mergeInfoLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo, 1266 const SortedVector<AssetDir::FileInfo>* pContents) 1267 { 1268 /* 1269 * Merge what we found in this directory with what we found in 1270 * other places. 1271 * 1272 * Two basic approaches: 1273 * (1) Create a new array that holds the unique values of the two 1274 * arrays. 1275 * (2) Take the elements from pContents and shove them into pMergedInfo. 1276 * 1277 * Because these are vectors of complex objects, moving elements around 1278 * inside the vector requires constructing new objects and allocating 1279 * storage for members. With approach #1, we're always adding to the 1280 * end, whereas with #2 we could be inserting multiple elements at the 1281 * front of the vector. Approach #1 requires a full copy of the 1282 * contents of pMergedInfo, but approach #2 requires the same copy for 1283 * every insertion at the front of pMergedInfo. 1284 * 1285 * (We should probably use a SortedVector interface that allows us to 1286 * just stuff items in, trusting us to maintain the sort order.) 1287 */ 1288 SortedVector<AssetDir::FileInfo>* pNewSorted; 1289 int mergeMax, contMax; 1290 int mergeIdx, contIdx; 1291 1292 pNewSorted = new SortedVector<AssetDir::FileInfo>; 1293 mergeMax = pMergedInfo->size(); 1294 contMax = pContents->size(); 1295 mergeIdx = contIdx = 0; 1296 1297 while (mergeIdx < mergeMax || contIdx < contMax) { 1298 if (mergeIdx == mergeMax) { 1299 /* hit end of "merge" list, copy rest of "contents" */ 1300 pNewSorted->add(pContents->itemAt(contIdx)); 1301 contIdx++; 1302 } else if (contIdx == contMax) { 1303 /* hit end of "cont" list, copy rest of "merge" */ 1304 pNewSorted->add(pMergedInfo->itemAt(mergeIdx)); 1305 mergeIdx++; 1306 } else if (pMergedInfo->itemAt(mergeIdx) == pContents->itemAt(contIdx)) 1307 { 1308 /* items are identical, add newer and advance both indices */ 1309 pNewSorted->add(pContents->itemAt(contIdx)); 1310 mergeIdx++; 1311 contIdx++; 1312 } else if (pMergedInfo->itemAt(mergeIdx) < pContents->itemAt(contIdx)) 1313 { 1314 /* "merge" is lower, add that one */ 1315 pNewSorted->add(pMergedInfo->itemAt(mergeIdx)); 1316 mergeIdx++; 1317 } else { 1318 /* "cont" is lower, add that one */ 1319 assert(pContents->itemAt(contIdx) < pMergedInfo->itemAt(mergeIdx)); 1320 pNewSorted->add(pContents->itemAt(contIdx)); 1321 contIdx++; 1322 } 1323 } 1324 1325 /* 1326 * Overwrite the "merged" list with the new stuff. 1327 */ 1328 *pMergedInfo = *pNewSorted; 1329 delete pNewSorted; 1330 1331 #if 0 // for Vector, rather than SortedVector 1332 int i, j; 1333 for (i = pContents->size() -1; i >= 0; i--) { 1334 bool add = true; 1335 1336 for (j = pMergedInfo->size() -1; j >= 0; j--) { 1337 /* case-sensitive comparisons, to behave like UNIX fs */ 1338 if (strcmp(pContents->itemAt(i).mFileName, 1339 pMergedInfo->itemAt(j).mFileName) == 0) 1340 { 1341 /* match, don't add this entry */ 1342 add = false; 1343 break; 1344 } 1345 } 1346 1347 if (add) 1348 pMergedInfo->add(pContents->itemAt(i)); 1349 } 1350 #endif 1351 } 1352 1353 /* 1354 * =========================================================================== 1355 * AssetManager::SharedZip 1356 * =========================================================================== 1357 */ 1358 1359 1360 Mutex AssetManager::SharedZip::gLock; 1361 DefaultKeyedVector<String8, wp<AssetManager::SharedZip> > AssetManager::SharedZip::gOpen; 1362 1363 AssetManager::SharedZip::SharedZip(const String8& path, time_t modWhen) 1364 : mPath(path), mZipFile(NULL), mModWhen(modWhen), 1365 mResourceTableAsset(NULL), mResourceTable(NULL) 1366 { 1367 if (kIsDebug) { 1368 ALOGI("Creating SharedZip %p %s\n", this, (const char*)mPath); 1369 } 1370 ALOGV("+++ opening zip '%s'\n", mPath.string()); 1371 mZipFile = ZipFileRO::open(mPath.string()); 1372 if (mZipFile == NULL) { 1373 ALOGD("failed to open Zip archive '%s'\n", mPath.string()); 1374 } 1375 } 1376 1377 sp<AssetManager::SharedZip> AssetManager::SharedZip::get(const String8& path, 1378 bool createIfNotPresent) 1379 { 1380 AutoMutex _l(gLock); 1381 time_t modWhen = getFileModDate(path); 1382 sp<SharedZip> zip = gOpen.valueFor(path).promote(); 1383 if (zip != NULL && zip->mModWhen == modWhen) { 1384 return zip; 1385 } 1386 if (zip == NULL && !createIfNotPresent) { 1387 return NULL; 1388 } 1389 zip = new SharedZip(path, modWhen); 1390 gOpen.add(path, zip); 1391 return zip; 1392 1393 } 1394 1395 ZipFileRO* AssetManager::SharedZip::getZip() 1396 { 1397 return mZipFile; 1398 } 1399 1400 Asset* AssetManager::SharedZip::getResourceTableAsset() 1401 { 1402 AutoMutex _l(gLock); 1403 ALOGV("Getting from SharedZip %p resource asset %p\n", this, mResourceTableAsset); 1404 return mResourceTableAsset; 1405 } 1406 1407 Asset* AssetManager::SharedZip::setResourceTableAsset(Asset* asset) 1408 { 1409 { 1410 AutoMutex _l(gLock); 1411 if (mResourceTableAsset == NULL) { 1412 // This is not thread safe the first time it is called, so 1413 // do it here with the global lock held. 1414 asset->getBuffer(true); 1415 mResourceTableAsset = asset; 1416 return asset; 1417 } 1418 } 1419 delete asset; 1420 return mResourceTableAsset; 1421 } 1422 1423 ResTable* AssetManager::SharedZip::getResourceTable() 1424 { 1425 ALOGV("Getting from SharedZip %p resource table %p\n", this, mResourceTable); 1426 return mResourceTable; 1427 } 1428 1429 ResTable* AssetManager::SharedZip::setResourceTable(ResTable* res) 1430 { 1431 { 1432 AutoMutex _l(gLock); 1433 if (mResourceTable == NULL) { 1434 mResourceTable = res; 1435 return res; 1436 } 1437 } 1438 delete res; 1439 return mResourceTable; 1440 } 1441 1442 bool AssetManager::SharedZip::isUpToDate() 1443 { 1444 time_t modWhen = getFileModDate(mPath.string()); 1445 return mModWhen == modWhen; 1446 } 1447 1448 void AssetManager::SharedZip::addOverlay(const asset_path& ap) 1449 { 1450 mOverlays.add(ap); 1451 } 1452 1453 bool AssetManager::SharedZip::getOverlay(size_t idx, asset_path* out) const 1454 { 1455 if (idx >= mOverlays.size()) { 1456 return false; 1457 } 1458 *out = mOverlays[idx]; 1459 return true; 1460 } 1461 1462 AssetManager::SharedZip::~SharedZip() 1463 { 1464 if (kIsDebug) { 1465 ALOGI("Destroying SharedZip %p %s\n", this, (const char*)mPath); 1466 } 1467 if (mResourceTable != NULL) { 1468 delete mResourceTable; 1469 } 1470 if (mResourceTableAsset != NULL) { 1471 delete mResourceTableAsset; 1472 } 1473 if (mZipFile != NULL) { 1474 delete mZipFile; 1475 ALOGV("Closed '%s'\n", mPath.string()); 1476 } 1477 } 1478 1479 /* 1480 * =========================================================================== 1481 * AssetManager::ZipSet 1482 * =========================================================================== 1483 */ 1484 1485 /* 1486 * Destructor. Close any open archives. 1487 */ 1488 AssetManager::ZipSet::~ZipSet(void) 1489 { 1490 size_t N = mZipFile.size(); 1491 for (size_t i = 0; i < N; i++) 1492 closeZip(i); 1493 } 1494 1495 /* 1496 * Close a Zip file and reset the entry. 1497 */ 1498 void AssetManager::ZipSet::closeZip(int idx) 1499 { 1500 mZipFile.editItemAt(idx) = NULL; 1501 } 1502 1503 1504 /* 1505 * Retrieve the appropriate Zip file from the set. 1506 */ 1507 ZipFileRO* AssetManager::ZipSet::getZip(const String8& path) 1508 { 1509 int idx = getIndex(path); 1510 sp<SharedZip> zip = mZipFile[idx]; 1511 if (zip == NULL) { 1512 zip = SharedZip::get(path); 1513 mZipFile.editItemAt(idx) = zip; 1514 } 1515 return zip->getZip(); 1516 } 1517 1518 Asset* AssetManager::ZipSet::getZipResourceTableAsset(const String8& path) 1519 { 1520 int idx = getIndex(path); 1521 sp<SharedZip> zip = mZipFile[idx]; 1522 if (zip == NULL) { 1523 zip = SharedZip::get(path); 1524 mZipFile.editItemAt(idx) = zip; 1525 } 1526 return zip->getResourceTableAsset(); 1527 } 1528 1529 Asset* AssetManager::ZipSet::setZipResourceTableAsset(const String8& path, 1530 Asset* asset) 1531 { 1532 int idx = getIndex(path); 1533 sp<SharedZip> zip = mZipFile[idx]; 1534 // doesn't make sense to call before previously accessing. 1535 return zip->setResourceTableAsset(asset); 1536 } 1537 1538 ResTable* AssetManager::ZipSet::getZipResourceTable(const String8& path) 1539 { 1540 int idx = getIndex(path); 1541 sp<SharedZip> zip = mZipFile[idx]; 1542 if (zip == NULL) { 1543 zip = SharedZip::get(path); 1544 mZipFile.editItemAt(idx) = zip; 1545 } 1546 return zip->getResourceTable(); 1547 } 1548 1549 ResTable* AssetManager::ZipSet::setZipResourceTable(const String8& path, 1550 ResTable* res) 1551 { 1552 int idx = getIndex(path); 1553 sp<SharedZip> zip = mZipFile[idx]; 1554 // doesn't make sense to call before previously accessing. 1555 return zip->setResourceTable(res); 1556 } 1557 1558 /* 1559 * Generate the partial pathname for the specified archive. The caller 1560 * gets to prepend the asset root directory. 1561 * 1562 * Returns something like "common/en-US-noogle.jar". 1563 */ 1564 /*static*/ String8 AssetManager::ZipSet::getPathName(const char* zipPath) 1565 { 1566 return String8(zipPath); 1567 } 1568 1569 bool AssetManager::ZipSet::isUpToDate() 1570 { 1571 const size_t N = mZipFile.size(); 1572 for (size_t i=0; i<N; i++) { 1573 if (mZipFile[i] != NULL && !mZipFile[i]->isUpToDate()) { 1574 return false; 1575 } 1576 } 1577 return true; 1578 } 1579 1580 void AssetManager::ZipSet::addOverlay(const String8& path, const asset_path& overlay) 1581 { 1582 int idx = getIndex(path); 1583 sp<SharedZip> zip = mZipFile[idx]; 1584 zip->addOverlay(overlay); 1585 } 1586 1587 bool AssetManager::ZipSet::getOverlay(const String8& path, size_t idx, asset_path* out) const 1588 { 1589 sp<SharedZip> zip = SharedZip::get(path, false); 1590 if (zip == NULL) { 1591 return false; 1592 } 1593 return zip->getOverlay(idx, out); 1594 } 1595 1596 /* 1597 * Compute the zip file's index. 1598 * 1599 * "appName", "locale", and "vendor" should be set to NULL to indicate the 1600 * default directory. 1601 */ 1602 int AssetManager::ZipSet::getIndex(const String8& zip) const 1603 { 1604 const size_t N = mZipPath.size(); 1605 for (size_t i=0; i<N; i++) { 1606 if (mZipPath[i] == zip) { 1607 return i; 1608 } 1609 } 1610 1611 mZipPath.add(zip); 1612 mZipFile.add(NULL); 1613 1614 return mZipPath.size()-1; 1615 } 1616