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