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