blob: 6667dafe4521fc15c1f9c2d41cc8c96c7d137edd [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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"
Dianne Hackbornf7be4802013-04-12 14:52:58 -070022#define ATRACE_TAG ATRACE_TAG_RESOURCES
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023//#define LOG_NDEBUG 0
24
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080025#include <androidfw/Asset.h>
26#include <androidfw/AssetDir.h>
27#include <androidfw/AssetManager.h>
Mathias Agopian1f5762e2013-05-06 20:20:34 -070028#include <androidfw/misc.h>
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080029#include <androidfw/ResourceTypes.h>
Mathias Agopian1f5762e2013-05-06 20:20:34 -070030#include <androidfw/ZipFileRO.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031#include <utils/Atomic.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032#include <utils/Log.h>
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080033#include <utils/String8.h>
34#include <utils/String8.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035#include <utils/threads.h>
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080036#include <utils/Timers.h>
Dianne Hackbornc51d0502013-04-15 15:36:53 -070037#ifdef HAVE_ANDROID_OS
Dianne Hackbornf7be4802013-04-12 14:52:58 -070038#include <cutils/trace.h>
Dianne Hackbornc51d0502013-04-15 15:36:53 -070039#endif
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080041#include <assert.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042#include <dirent.h>
43#include <errno.h>
Mårten Kongstad57f4b772011-03-17 14:13:41 +010044#include <fcntl.h>
Mathias Agopianb13b9bd2012-02-17 18:27:36 -080045#include <strings.h>
Mårten Kongstad57f4b772011-03-17 14:13:41 +010046#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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058
Dianne Hackbornf7be4802013-04-12 14:52:58 -070059#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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067using 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 */
73static const char* kDefaultLocale = "default";
74static const char* kDefaultVendor = "default";
75static const char* kAssetsRoot = "assets";
76static const char* kAppZipName = NULL; //"classes.jar";
77static const char* kSystemAssets = "framework/framework-res.apk";
Mårten Kongstad57f4b772011-03-17 14:13:41 +010078static const char* kIdmapCacheDir = "resource-cache";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079
80static const char* kExcludeExtension = ".EXCLUDE";
81
82static Asset* const kExcludedAsset = (Asset*) 0xd000000d;
83
84static volatile int32_t gCount = 0;
85
Mårten Kongstad57f4b772011-03-17 14:13:41 +010086namespace {
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 }
Mathias Agopian69a3ce12012-08-05 12:38:51 -0700114
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 }
Mårten Kongstad57f4b772011-03-17 14:13:41 +0100132}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133
134/*
135 * ===========================================================================
136 * AssetManager
137 * ===========================================================================
138 */
139
140int32_t AssetManager::getGlobalCount()
141{
142 return gCount;
143}
144
145AssetManager::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;
Steve Block6215d3f2012-01-04 20:05:49 +0000151 //ALOGI("Creating AssetManager %p #%d\n", this, count);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 memset(mConfig, 0, sizeof(ResTable_config));
153}
154
155AssetManager::~AssetManager(void)
156{
157 int count = android_atomic_dec(&gCount);
Steve Block6215d3f2012-01-04 20:05:49 +0000158 //ALOGI("Destroying AssetManager in %p #%d\n", this, count);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159
160 delete mConfig;
161 delete mResources;
162
163 // don't have a String class yet, so make sure we clean up
164 delete[] mLocale;
165 delete[] mVendor;
166}
167
168bool AssetManager::addAssetPath(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) {
Steve Block8564c8d2012-01-05 23:22:43 +0000185 ALOGW("Asset path %s is neither a directory nor file (type=%d).",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 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 }
Mårten Kongstad57f4b772011-03-17 14:13:41 +0100200
Steve Block71f2cf12011-10-20 11:56:00 +0100201 ALOGV("In %p Asset %s path: %s", this,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 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
Mårten Kongstad57f4b772011-03-17 14:13:41 +0100211 // 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 {
Steve Block8564c8d2012-01-05 23:22:43 +0000234 ALOGW("failed to add overlay package %s\n", overlayPath.string());
Mårten Kongstad57f4b772011-03-17 14:13:41 +0100235 }
236 }
237 }
238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 return true;
240}
241
Mårten Kongstad57f4b772011-03-17 14:13:41 +0100242bool 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 {
Steve Block8564c8d2012-01-05 23:22:43 +0000250 ALOGW("failed to stat file %s: %s\n", idmapPath.string(), strerror(errno));
Mårten Kongstad57f4b772011-03-17 14:13:41 +0100251 return false;
252 }
253 }
254 if (st.st_size < ResTable::IDMAP_HEADER_SIZE_BYTES) {
Steve Block8564c8d2012-01-05 23:22:43 +0000255 ALOGW("file %s has unexpectedly small size=%zd\n", idmapPath.string(), (size_t)st.st_size);
Mårten Kongstad57f4b772011-03-17 14:13:41 +0100256 return false;
257 }
258 int fd = TEMP_FAILURE_RETRY(::open(idmapPath.string(), O_RDONLY));
259 if (fd == -1) {
Steve Block8564c8d2012-01-05 23:22:43 +0000260 ALOGW("failed to open file %s: %s\n", idmapPath.string(), strerror(errno));
Mårten Kongstad57f4b772011-03-17 14:13:41 +0100261 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
295bool 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 }
308 if (!zip->getEntryInfo(entry, NULL, NULL, NULL, NULL, NULL, (long*)pCrc)) {
309 return false;
310 }
311 return true;
312}
313
314bool AssetManager::createIdmapFileLocked(const String8& originalPath, const String8& overlayPath,
315 const String8& idmapPath)
316{
Steve Block5baa3a62011-12-20 16:23:08 +0000317 ALOGD("%s: originalPath=%s overlayPath=%s idmapPath=%s\n",
Mårten Kongstad57f4b772011-03-17 14:13:41 +0100318 __FUNCTION__, originalPath.string(), overlayPath.string(), idmapPath.string());
319 ResTable tables[2];
320 const String8* paths[2] = { &originalPath, &overlayPath };
321 uint32_t originalCrc, overlayCrc;
322 bool retval = false;
323 ssize_t offset = 0;
324 int fd = 0;
325 uint32_t* data = NULL;
326 size_t size;
327
328 for (int i = 0; i < 2; ++i) {
329 asset_path ap;
330 ap.type = kFileTypeRegular;
331 ap.path = *paths[i];
332 Asset* ass = openNonAssetInPathLocked("resources.arsc", Asset::ACCESS_BUFFER, ap);
333 if (ass == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +0000334 ALOGW("failed to find resources.arsc in %s\n", ap.path.string());
Mårten Kongstad57f4b772011-03-17 14:13:41 +0100335 goto error;
336 }
337 tables[i].add(ass, (void*)1, false);
338 }
339
340 if (!getZipEntryCrcLocked(originalPath, "resources.arsc", &originalCrc)) {
Steve Block8564c8d2012-01-05 23:22:43 +0000341 ALOGW("failed to retrieve crc for resources.arsc in %s\n", originalPath.string());
Mårten Kongstad57f4b772011-03-17 14:13:41 +0100342 goto error;
343 }
344 if (!getZipEntryCrcLocked(overlayPath, "resources.arsc", &overlayCrc)) {
Steve Block8564c8d2012-01-05 23:22:43 +0000345 ALOGW("failed to retrieve crc for resources.arsc in %s\n", overlayPath.string());
Mårten Kongstad57f4b772011-03-17 14:13:41 +0100346 goto error;
347 }
348
349 if (tables[0].createIdmap(tables[1], originalCrc, overlayCrc,
350 (void**)&data, &size) != NO_ERROR) {
Steve Block8564c8d2012-01-05 23:22:43 +0000351 ALOGW("failed to generate idmap data for file %s\n", idmapPath.string());
Mårten Kongstad57f4b772011-03-17 14:13:41 +0100352 goto error;
353 }
354
355 // This should be abstracted (eg replaced by a stand-alone
356 // application like dexopt, triggered by something equivalent to
357 // installd).
358 fd = TEMP_FAILURE_RETRY(::open(idmapPath.string(), O_WRONLY | O_CREAT | O_TRUNC, 0644));
359 if (fd == -1) {
Steve Block8564c8d2012-01-05 23:22:43 +0000360 ALOGW("failed to write idmap file %s (open: %s)\n", idmapPath.string(), strerror(errno));
Mårten Kongstad57f4b772011-03-17 14:13:41 +0100361 goto error_free;
362 }
363 for (;;) {
364 ssize_t written = TEMP_FAILURE_RETRY(write(fd, data + offset, size));
365 if (written < 0) {
Steve Block8564c8d2012-01-05 23:22:43 +0000366 ALOGW("failed to write idmap file %s (write: %s)\n", idmapPath.string(),
Mårten Kongstad57f4b772011-03-17 14:13:41 +0100367 strerror(errno));
368 goto error_close;
369 }
370 size -= (size_t)written;
371 offset += written;
372 if (size == 0) {
373 break;
374 }
375 }
376
377 retval = true;
378error_close:
379 TEMP_FAILURE_RETRY(close(fd));
380error_free:
381 free(data);
382error:
383 return retval;
384}
385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386bool AssetManager::addDefaultAssets()
387{
388 const char* root = getenv("ANDROID_ROOT");
389 LOG_ALWAYS_FATAL_IF(root == NULL, "ANDROID_ROOT not set");
390
391 String8 path(root);
392 path.appendPath(kSystemAssets);
393
394 return addAssetPath(path, NULL);
395}
396
397void* AssetManager::nextAssetPath(void* cookie) const
398{
399 AutoMutex _l(mLock);
400 size_t next = ((size_t)cookie)+1;
401 return next > mAssetPaths.size() ? NULL : (void*)next;
402}
403
404String8 AssetManager::getAssetPath(void* cookie) const
405{
406 AutoMutex _l(mLock);
407 const size_t which = ((size_t)cookie)-1;
408 if (which < mAssetPaths.size()) {
409 return mAssetPaths[which].path;
410 }
411 return String8();
412}
413
414/*
415 * Set the current locale. Use NULL to indicate no locale.
416 *
417 * Close and reopen Zip archives as appropriate, and reset cached
418 * information in the locale-specific sections of the tree.
419 */
420void AssetManager::setLocale(const char* locale)
421{
422 AutoMutex _l(mLock);
423 setLocaleLocked(locale);
424}
425
426void AssetManager::setLocaleLocked(const char* locale)
427{
428 if (mLocale != NULL) {
429 /* previously set, purge cached data */
430 purgeFileNameCacheLocked();
431 //mZipSet.purgeLocale();
432 delete[] mLocale;
433 }
434 mLocale = strdupNew(locale);
435
436 updateResourceParamsLocked();
437}
438
439/*
440 * Set the current vendor. Use NULL to indicate no vendor.
441 *
442 * Close and reopen Zip archives as appropriate, and reset cached
443 * information in the vendor-specific sections of the tree.
444 */
445void AssetManager::setVendor(const char* vendor)
446{
447 AutoMutex _l(mLock);
448
449 if (mVendor != NULL) {
450 /* previously set, purge cached data */
451 purgeFileNameCacheLocked();
452 //mZipSet.purgeVendor();
453 delete[] mVendor;
454 }
455 mVendor = strdupNew(vendor);
456}
457
458void AssetManager::setConfiguration(const ResTable_config& config, const char* locale)
459{
460 AutoMutex _l(mLock);
461 *mConfig = config;
462 if (locale) {
463 setLocaleLocked(locale);
464 } else if (config.language[0] != 0) {
465 char spec[9];
466 spec[0] = config.language[0];
467 spec[1] = config.language[1];
468 if (config.country[0] != 0) {
469 spec[2] = '_';
470 spec[3] = config.country[0];
471 spec[4] = config.country[1];
472 spec[5] = 0;
473 } else {
474 spec[3] = 0;
475 }
476 setLocaleLocked(spec);
477 } else {
478 updateResourceParamsLocked();
479 }
480}
481
Dianne Hackborn08d5b8f2010-08-04 11:12:40 -0700482void AssetManager::getConfiguration(ResTable_config* outConfig) const
483{
484 AutoMutex _l(mLock);
485 *outConfig = *mConfig;
486}
487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800488/*
489 * Open an asset.
490 *
491 * The data could be;
492 * - In a file on disk (assetBase + fileName).
493 * - In a compressed file on disk (assetBase + fileName.gz).
494 * - In a Zip archive, uncompressed or compressed.
495 *
496 * It can be in a number of different directories and Zip archives.
497 * The search order is:
498 * - [appname]
499 * - locale + vendor
500 * - "default" + vendor
501 * - locale + "default"
502 * - "default + "default"
503 * - "common"
504 * - (same as above)
505 *
506 * To find a particular file, we have to try up to eight paths with
507 * all three forms of data.
508 *
509 * We should probably reject requests for "illegal" filenames, e.g. those
510 * with illegal characters or "../" backward relative paths.
511 */
512Asset* AssetManager::open(const char* fileName, AccessMode mode)
513{
514 AutoMutex _l(mLock);
515
516 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
517
518
519 if (mCacheMode != CACHE_OFF && !mCacheValid)
520 loadFileNameCacheLocked();
521
522 String8 assetName(kAssetsRoot);
523 assetName.appendPath(fileName);
524
525 /*
526 * For each top-level asset path, search for the asset.
527 */
528
529 size_t i = mAssetPaths.size();
530 while (i > 0) {
531 i--;
Steve Block71f2cf12011-10-20 11:56:00 +0100532 ALOGV("Looking for asset '%s' in '%s'\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800533 assetName.string(), mAssetPaths.itemAt(i).path.string());
534 Asset* pAsset = openNonAssetInPathLocked(assetName.string(), mode, mAssetPaths.itemAt(i));
535 if (pAsset != NULL) {
536 return pAsset != kExcludedAsset ? pAsset : NULL;
537 }
538 }
539
540 return NULL;
541}
542
543/*
544 * Open a non-asset file as if it were an asset.
545 *
546 * The "fileName" is the partial path starting from the application
547 * name.
548 */
549Asset* AssetManager::openNonAsset(const char* fileName, AccessMode mode)
550{
551 AutoMutex _l(mLock);
552
553 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
554
555
556 if (mCacheMode != CACHE_OFF && !mCacheValid)
557 loadFileNameCacheLocked();
558
559 /*
560 * For each top-level asset path, search for the asset.
561 */
562
563 size_t i = mAssetPaths.size();
564 while (i > 0) {
565 i--;
Steve Block71f2cf12011-10-20 11:56:00 +0100566 ALOGV("Looking for non-asset '%s' in '%s'\n", fileName, mAssetPaths.itemAt(i).path.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 Asset* pAsset = openNonAssetInPathLocked(
568 fileName, mode, mAssetPaths.itemAt(i));
569 if (pAsset != NULL) {
570 return pAsset != kExcludedAsset ? pAsset : NULL;
571 }
572 }
573
574 return NULL;
575}
576
577Asset* AssetManager::openNonAsset(void* cookie, const char* fileName, AccessMode mode)
578{
579 const size_t which = ((size_t)cookie)-1;
580
581 AutoMutex _l(mLock);
582
583 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
584
585
586 if (mCacheMode != CACHE_OFF && !mCacheValid)
587 loadFileNameCacheLocked();
588
589 if (which < mAssetPaths.size()) {
Steve Block71f2cf12011-10-20 11:56:00 +0100590 ALOGV("Looking for non-asset '%s' in '%s'\n", fileName,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 mAssetPaths.itemAt(which).path.string());
592 Asset* pAsset = openNonAssetInPathLocked(
593 fileName, mode, mAssetPaths.itemAt(which));
594 if (pAsset != NULL) {
595 return pAsset != kExcludedAsset ? pAsset : NULL;
596 }
597 }
598
599 return NULL;
600}
601
602/*
603 * Get the type of a file in the asset namespace.
604 *
605 * This currently only works for regular files. All others (including
606 * directories) will return kFileTypeNonexistent.
607 */
608FileType AssetManager::getFileType(const char* fileName)
609{
610 Asset* pAsset = NULL;
611
612 /*
613 * Open the asset. This is less efficient than simply finding the
614 * file, but it's not too bad (we don't uncompress or mmap data until
615 * the first read() call).
616 */
617 pAsset = open(fileName, Asset::ACCESS_STREAMING);
618 delete pAsset;
619
620 if (pAsset == NULL)
621 return kFileTypeNonexistent;
622 else
623 return kFileTypeRegular;
624}
625
626const ResTable* AssetManager::getResTable(bool required) const
627{
628 ResTable* rt = mResources;
629 if (rt) {
630 return rt;
631 }
632
633 // Iterate through all asset packages, collecting resources from each.
634
635 AutoMutex _l(mLock);
636
637 if (mResources != NULL) {
638 return mResources;
639 }
640
641 if (required) {
642 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
643 }
644
645 if (mCacheMode != CACHE_OFF && !mCacheValid)
646 const_cast<AssetManager*>(this)->loadFileNameCacheLocked();
647
648 const size_t N = mAssetPaths.size();
649 for (size_t i=0; i<N; i++) {
650 Asset* ass = NULL;
Dianne Hackborn78c40512009-07-06 11:07:40 -0700651 ResTable* sharedRes = NULL;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800652 bool shared = true;
653 const asset_path& ap = mAssetPaths.itemAt(i);
Dianne Hackbornf7be4802013-04-12 14:52:58 -0700654 MY_TRACE_BEGIN(ap.path.string());
Mårten Kongstad57f4b772011-03-17 14:13:41 +0100655 Asset* idmap = openIdmapLocked(ap);
Steve Block71f2cf12011-10-20 11:56:00 +0100656 ALOGV("Looking for resource asset in '%s'\n", ap.path.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657 if (ap.type != kFileTypeDirectory) {
Dianne Hackborn78c40512009-07-06 11:07:40 -0700658 if (i == 0) {
659 // The first item is typically the framework resources,
660 // which we want to avoid parsing every time.
661 sharedRes = const_cast<AssetManager*>(this)->
662 mZipSet.getZipResourceTable(ap.path);
663 }
664 if (sharedRes == NULL) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 ass = const_cast<AssetManager*>(this)->
Dianne Hackborn78c40512009-07-06 11:07:40 -0700666 mZipSet.getZipResourceTableAsset(ap.path);
667 if (ass == NULL) {
Steve Block71f2cf12011-10-20 11:56:00 +0100668 ALOGV("loading resource table %s\n", ap.path.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 ass = const_cast<AssetManager*>(this)->
Dianne Hackborn78c40512009-07-06 11:07:40 -0700670 openNonAssetInPathLocked("resources.arsc",
671 Asset::ACCESS_BUFFER,
672 ap);
673 if (ass != NULL && ass != kExcludedAsset) {
674 ass = const_cast<AssetManager*>(this)->
675 mZipSet.setZipResourceTableAsset(ap.path, ass);
676 }
677 }
678
679 if (i == 0 && ass != NULL) {
680 // If this is the first resource table in the asset
681 // manager, then we are going to cache it so that we
682 // can quickly copy it out for others.
Steve Block71f2cf12011-10-20 11:56:00 +0100683 ALOGV("Creating shared resources for %s", ap.path.string());
Dianne Hackborn78c40512009-07-06 11:07:40 -0700684 sharedRes = new ResTable();
Mårten Kongstad57f4b772011-03-17 14:13:41 +0100685 sharedRes->add(ass, (void*)(i+1), false, idmap);
Dianne Hackborn78c40512009-07-06 11:07:40 -0700686 sharedRes = const_cast<AssetManager*>(this)->
687 mZipSet.setZipResourceTable(ap.path, sharedRes);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 }
689 }
690 } else {
Steve Block71f2cf12011-10-20 11:56:00 +0100691 ALOGV("loading resource table %s\n", ap.path.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 Asset* ass = const_cast<AssetManager*>(this)->
693 openNonAssetInPathLocked("resources.arsc",
694 Asset::ACCESS_BUFFER,
695 ap);
696 shared = false;
697 }
Dianne Hackborn78c40512009-07-06 11:07:40 -0700698 if ((ass != NULL || sharedRes != NULL) && ass != kExcludedAsset) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 if (rt == NULL) {
700 mResources = rt = new ResTable();
701 updateResourceParamsLocked();
702 }
Steve Block71f2cf12011-10-20 11:56:00 +0100703 ALOGV("Installing resource asset %p in to table %p\n", ass, mResources);
Dianne Hackborn78c40512009-07-06 11:07:40 -0700704 if (sharedRes != NULL) {
Steve Block71f2cf12011-10-20 11:56:00 +0100705 ALOGV("Copying existing resources for %s", ap.path.string());
Dianne Hackborn78c40512009-07-06 11:07:40 -0700706 rt->add(sharedRes);
707 } else {
Steve Block71f2cf12011-10-20 11:56:00 +0100708 ALOGV("Parsing resources for %s", ap.path.string());
Mårten Kongstad57f4b772011-03-17 14:13:41 +0100709 rt->add(ass, (void*)(i+1), !shared, idmap);
Dianne Hackborn78c40512009-07-06 11:07:40 -0700710 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711
712 if (!shared) {
713 delete ass;
714 }
715 }
Mårten Kongstad2b91c672011-05-05 10:40:42 +0200716 if (idmap != NULL) {
717 delete idmap;
718 }
Dianne Hackbornf7be4802013-04-12 14:52:58 -0700719 MY_TRACE_END();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720 }
721
Steve Block8564c8d2012-01-05 23:22:43 +0000722 if (required && !rt) ALOGW("Unable to find resources file resources.arsc");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800723 if (!rt) {
724 mResources = rt = new ResTable();
725 }
726 return rt;
727}
728
729void AssetManager::updateResourceParamsLocked() const
730{
731 ResTable* res = mResources;
732 if (!res) {
733 return;
734 }
735
736 size_t llen = mLocale ? strlen(mLocale) : 0;
737 mConfig->language[0] = 0;
738 mConfig->language[1] = 0;
739 mConfig->country[0] = 0;
740 mConfig->country[1] = 0;
741 if (llen >= 2) {
742 mConfig->language[0] = mLocale[0];
743 mConfig->language[1] = mLocale[1];
744 }
745 if (llen >= 5) {
746 mConfig->country[0] = mLocale[3];
747 mConfig->country[1] = mLocale[4];
748 }
749 mConfig->size = sizeof(*mConfig);
750
751 res->setParameters(mConfig);
752}
753
Mårten Kongstad57f4b772011-03-17 14:13:41 +0100754Asset* AssetManager::openIdmapLocked(const struct asset_path& ap) const
755{
756 Asset* ass = NULL;
757 if (ap.idmap.size() != 0) {
758 ass = const_cast<AssetManager*>(this)->
759 openAssetFromFileLocked(ap.idmap, Asset::ACCESS_BUFFER);
760 if (ass) {
Steve Block71f2cf12011-10-20 11:56:00 +0100761 ALOGV("loading idmap %s\n", ap.idmap.string());
Mårten Kongstad57f4b772011-03-17 14:13:41 +0100762 } else {
Steve Block8564c8d2012-01-05 23:22:43 +0000763 ALOGW("failed to load idmap %s\n", ap.idmap.string());
Mårten Kongstad57f4b772011-03-17 14:13:41 +0100764 }
765 }
766 return ass;
767}
768
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800769const ResTable& AssetManager::getResources(bool required) const
770{
771 const ResTable* rt = getResTable(required);
772 return *rt;
773}
774
775bool AssetManager::isUpToDate()
776{
777 AutoMutex _l(mLock);
778 return mZipSet.isUpToDate();
779}
780
781void AssetManager::getLocales(Vector<String8>* locales) const
782{
783 ResTable* res = mResources;
784 if (res != NULL) {
785 res->getLocales(locales);
786 }
787}
788
789/*
790 * Open a non-asset file as if it were an asset, searching for it in the
791 * specified app.
792 *
793 * Pass in a NULL values for "appName" if the common app directory should
794 * be used.
795 */
796Asset* AssetManager::openNonAssetInPathLocked(const char* fileName, AccessMode mode,
797 const asset_path& ap)
798{
799 Asset* pAsset = NULL;
800
801 /* look at the filesystem on disk */
802 if (ap.type == kFileTypeDirectory) {
803 String8 path(ap.path);
804 path.appendPath(fileName);
805
806 pAsset = openAssetFromFileLocked(path, mode);
807
808 if (pAsset == NULL) {
809 /* try again, this time with ".gz" */
810 path.append(".gz");
811 pAsset = openAssetFromFileLocked(path, mode);
812 }
813
814 if (pAsset != NULL) {
815 //printf("FOUND NA '%s' on disk\n", fileName);
816 pAsset->setAssetSource(path);
817 }
818
819 /* look inside the zip file */
820 } else {
821 String8 path(fileName);
822
823 /* check the appropriate Zip file */
824 ZipFileRO* pZip;
825 ZipEntryRO entry;
826
827 pZip = getZipFileLocked(ap);
828 if (pZip != NULL) {
829 //printf("GOT zip, checking NA '%s'\n", (const char*) path);
830 entry = pZip->findEntryByName(path.string());
831 if (entry != NULL) {
832 //printf("FOUND NA in Zip file for %s\n", appName ? appName : kAppCommon);
833 pAsset = openAssetFromZipLocked(pZip, entry, mode, path);
834 }
835 }
836
837 if (pAsset != NULL) {
838 /* create a "source" name, for debug/display */
839 pAsset->setAssetSource(
840 createZipSourceNameLocked(ZipSet::getPathName(ap.path.string()), String8(""),
841 String8(fileName)));
842 }
843 }
844
845 return pAsset;
846}
847
848/*
849 * Open an asset, searching for it in the directory hierarchy for the
850 * specified app.
851 *
852 * Pass in a NULL values for "appName" if the common app directory should
853 * be used.
854 */
855Asset* AssetManager::openInPathLocked(const char* fileName, AccessMode mode,
856 const asset_path& ap)
857{
858 Asset* pAsset = NULL;
859
860 /*
861 * Try various combinations of locale and vendor.
862 */
863 if (mLocale != NULL && mVendor != NULL)
864 pAsset = openInLocaleVendorLocked(fileName, mode, ap, mLocale, mVendor);
865 if (pAsset == NULL && mVendor != NULL)
866 pAsset = openInLocaleVendorLocked(fileName, mode, ap, NULL, mVendor);
867 if (pAsset == NULL && mLocale != NULL)
868 pAsset = openInLocaleVendorLocked(fileName, mode, ap, mLocale, NULL);
869 if (pAsset == NULL)
870 pAsset = openInLocaleVendorLocked(fileName, mode, ap, NULL, NULL);
871
872 return pAsset;
873}
874
875/*
876 * Open an asset, searching for it in the directory hierarchy for the
877 * specified locale and vendor.
878 *
879 * We also search in "app.jar".
880 *
881 * Pass in NULL values for "appName", "locale", and "vendor" if the
882 * defaults should be used.
883 */
884Asset* AssetManager::openInLocaleVendorLocked(const char* fileName, AccessMode mode,
885 const asset_path& ap, const char* locale, const char* vendor)
886{
887 Asset* pAsset = NULL;
888
889 if (ap.type == kFileTypeDirectory) {
890 if (mCacheMode == CACHE_OFF) {
891 /* look at the filesystem on disk */
892 String8 path(createPathNameLocked(ap, locale, vendor));
893 path.appendPath(fileName);
894
895 String8 excludeName(path);
896 excludeName.append(kExcludeExtension);
897 if (::getFileType(excludeName.string()) != kFileTypeNonexistent) {
898 /* say no more */
899 //printf("+++ excluding '%s'\n", (const char*) excludeName);
900 return kExcludedAsset;
901 }
902
903 pAsset = openAssetFromFileLocked(path, mode);
904
905 if (pAsset == NULL) {
906 /* try again, this time with ".gz" */
907 path.append(".gz");
908 pAsset = openAssetFromFileLocked(path, mode);
909 }
910
911 if (pAsset != NULL)
912 pAsset->setAssetSource(path);
913 } else {
914 /* find in cache */
915 String8 path(createPathNameLocked(ap, locale, vendor));
916 path.appendPath(fileName);
917
918 AssetDir::FileInfo tmpInfo;
919 bool found = false;
920
921 String8 excludeName(path);
922 excludeName.append(kExcludeExtension);
923
924 if (mCache.indexOf(excludeName) != NAME_NOT_FOUND) {
925 /* go no farther */
926 //printf("+++ Excluding '%s'\n", (const char*) excludeName);
927 return kExcludedAsset;
928 }
929
930 /*
931 * File compression extensions (".gz") don't get stored in the
932 * name cache, so we have to try both here.
933 */
934 if (mCache.indexOf(path) != NAME_NOT_FOUND) {
935 found = true;
936 pAsset = openAssetFromFileLocked(path, mode);
937 if (pAsset == NULL) {
938 /* try again, this time with ".gz" */
939 path.append(".gz");
940 pAsset = openAssetFromFileLocked(path, mode);
941 }
942 }
943
944 if (pAsset != NULL)
945 pAsset->setAssetSource(path);
946
947 /*
948 * Don't continue the search into the Zip files. Our cached info
949 * said it was a file on disk; to be consistent with openDir()
950 * we want to return the loose asset. If the cached file gets
951 * removed, we fail.
952 *
953 * The alternative is to update our cache when files get deleted,
954 * or make some sort of "best effort" promise, but for now I'm
955 * taking the hard line.
956 */
957 if (found) {
958 if (pAsset == NULL)
Steve Block5baa3a62011-12-20 16:23:08 +0000959 ALOGD("Expected file not found: '%s'\n", path.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800960 return pAsset;
961 }
962 }
963 }
964
965 /*
966 * Either it wasn't found on disk or on the cached view of the disk.
967 * Dig through the currently-opened set of Zip files. If caching
968 * is disabled, the Zip file may get reopened.
969 */
970 if (pAsset == NULL && ap.type == kFileTypeRegular) {
971 String8 path;
972
973 path.appendPath((locale != NULL) ? locale : kDefaultLocale);
974 path.appendPath((vendor != NULL) ? vendor : kDefaultVendor);
975 path.appendPath(fileName);
976
977 /* check the appropriate Zip file */
978 ZipFileRO* pZip;
979 ZipEntryRO entry;
980
981 pZip = getZipFileLocked(ap);
982 if (pZip != NULL) {
983 //printf("GOT zip, checking '%s'\n", (const char*) path);
984 entry = pZip->findEntryByName(path.string());
985 if (entry != NULL) {
986 //printf("FOUND in Zip file for %s/%s-%s\n",
987 // appName, locale, vendor);
988 pAsset = openAssetFromZipLocked(pZip, entry, mode, path);
989 }
990 }
991
992 if (pAsset != NULL) {
993 /* create a "source" name, for debug/display */
994 pAsset->setAssetSource(createZipSourceNameLocked(ZipSet::getPathName(ap.path.string()),
995 String8(""), String8(fileName)));
996 }
997 }
998
999 return pAsset;
1000}
1001
1002/*
1003 * Create a "source name" for a file from a Zip archive.
1004 */
1005String8 AssetManager::createZipSourceNameLocked(const String8& zipFileName,
1006 const String8& dirName, const String8& fileName)
1007{
1008 String8 sourceName("zip:");
1009 sourceName.append(zipFileName);
1010 sourceName.append(":");
1011 if (dirName.length() > 0) {
1012 sourceName.appendPath(dirName);
1013 }
1014 sourceName.appendPath(fileName);
1015 return sourceName;
1016}
1017
1018/*
1019 * Create a path to a loose asset (asset-base/app/locale/vendor).
1020 */
1021String8 AssetManager::createPathNameLocked(const asset_path& ap, const char* locale,
1022 const char* vendor)
1023{
1024 String8 path(ap.path);
1025 path.appendPath((locale != NULL) ? locale : kDefaultLocale);
1026 path.appendPath((vendor != NULL) ? vendor : kDefaultVendor);
1027 return path;
1028}
1029
1030/*
1031 * Create a path to a loose asset (asset-base/app/rootDir).
1032 */
1033String8 AssetManager::createPathNameLocked(const asset_path& ap, const char* rootDir)
1034{
1035 String8 path(ap.path);
1036 if (rootDir != NULL) path.appendPath(rootDir);
1037 return path;
1038}
1039
1040/*
1041 * Return a pointer to one of our open Zip archives. Returns NULL if no
1042 * matching Zip file exists.
1043 *
1044 * Right now we have 2 possible Zip files (1 each in app/"common").
1045 *
1046 * If caching is set to CACHE_OFF, to get the expected behavior we
1047 * need to reopen the Zip file on every request. That would be silly
1048 * and expensive, so instead we just check the file modification date.
1049 *
1050 * Pass in NULL values for "appName", "locale", and "vendor" if the
1051 * generics should be used.
1052 */
1053ZipFileRO* AssetManager::getZipFileLocked(const asset_path& ap)
1054{
Steve Block71f2cf12011-10-20 11:56:00 +01001055 ALOGV("getZipFileLocked() in %p\n", this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056
1057 return mZipSet.getZip(ap.path);
1058}
1059
1060/*
1061 * Try to open an asset from a file on disk.
1062 *
1063 * If the file is compressed with gzip, we seek to the start of the
1064 * deflated data and pass that in (just like we would for a Zip archive).
1065 *
1066 * For uncompressed data, we may already have an mmap()ed version sitting
1067 * around. If so, we want to hand that to the Asset instead.
1068 *
1069 * This returns NULL if the file doesn't exist, couldn't be opened, or
1070 * claims to be a ".gz" but isn't.
1071 */
1072Asset* AssetManager::openAssetFromFileLocked(const String8& pathName,
1073 AccessMode mode)
1074{
1075 Asset* pAsset = NULL;
1076
1077 if (strcasecmp(pathName.getPathExtension().string(), ".gz") == 0) {
1078 //printf("TRYING '%s'\n", (const char*) pathName);
1079 pAsset = Asset::createFromCompressedFile(pathName.string(), mode);
1080 } else {
1081 //printf("TRYING '%s'\n", (const char*) pathName);
1082 pAsset = Asset::createFromFile(pathName.string(), mode);
1083 }
1084
1085 return pAsset;
1086}
1087
1088/*
1089 * Given an entry in a Zip archive, create a new Asset object.
1090 *
1091 * If the entry is uncompressed, we may want to create or share a
1092 * slice of shared memory.
1093 */
1094Asset* AssetManager::openAssetFromZipLocked(const ZipFileRO* pZipFile,
1095 const ZipEntryRO entry, AccessMode mode, const String8& entryName)
1096{
1097 Asset* pAsset = NULL;
1098
1099 // TODO: look for previously-created shared memory slice?
1100 int method;
Kenny Root68246dc2010-04-22 18:28:29 -07001101 size_t uncompressedLen;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102
1103 //printf("USING Zip '%s'\n", pEntry->getFileName());
1104
1105 //pZipFile->getEntryInfo(entry, &method, &uncompressedLen, &compressedLen,
1106 // &offset);
1107 if (!pZipFile->getEntryInfo(entry, &method, &uncompressedLen, NULL, NULL,
1108 NULL, NULL))
1109 {
Steve Block8564c8d2012-01-05 23:22:43 +00001110 ALOGW("getEntryInfo failed\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 return NULL;
1112 }
1113
1114 FileMap* dataMap = pZipFile->createEntryFileMap(entry);
1115 if (dataMap == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +00001116 ALOGW("create map from entry failed\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 return NULL;
1118 }
1119
1120 if (method == ZipFileRO::kCompressStored) {
1121 pAsset = Asset::createFromUncompressedMap(dataMap, mode);
Steve Block71f2cf12011-10-20 11:56:00 +01001122 ALOGV("Opened uncompressed entry %s in zip %s mode %d: %p", entryName.string(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 dataMap->getFileName(), mode, pAsset);
1124 } else {
1125 pAsset = Asset::createFromCompressedMap(dataMap, method,
1126 uncompressedLen, mode);
Steve Block71f2cf12011-10-20 11:56:00 +01001127 ALOGV("Opened compressed entry %s in zip %s mode %d: %p", entryName.string(),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 dataMap->getFileName(), mode, pAsset);
1129 }
1130 if (pAsset == NULL) {
1131 /* unexpected */
Steve Block8564c8d2012-01-05 23:22:43 +00001132 ALOGW("create from segment failed\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 }
1134
1135 return pAsset;
1136}
1137
1138
1139
1140/*
1141 * Open a directory in the asset namespace.
1142 *
1143 * An "asset directory" is simply the combination of all files in all
1144 * locations, with ".gz" stripped for loose files. With app, locale, and
1145 * vendor defined, we have 8 directories and 2 Zip archives to scan.
1146 *
1147 * Pass in "" for the root dir.
1148 */
1149AssetDir* AssetManager::openDir(const char* dirName)
1150{
1151 AutoMutex _l(mLock);
1152
1153 AssetDir* pDir = NULL;
1154 SortedVector<AssetDir::FileInfo>* pMergedInfo = NULL;
1155
1156 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
1157 assert(dirName != NULL);
1158
1159 //printf("+++ openDir(%s) in '%s'\n", dirName, (const char*) mAssetBase);
1160
1161 if (mCacheMode != CACHE_OFF && !mCacheValid)
1162 loadFileNameCacheLocked();
1163
1164 pDir = new AssetDir;
1165
1166 /*
1167 * Scan the various directories, merging what we find into a single
1168 * vector. We want to scan them in reverse priority order so that
1169 * the ".EXCLUDE" processing works correctly. Also, if we decide we
1170 * want to remember where the file is coming from, we'll get the right
1171 * version.
1172 *
1173 * We start with Zip archives, then do loose files.
1174 */
1175 pMergedInfo = new SortedVector<AssetDir::FileInfo>;
1176
1177 size_t i = mAssetPaths.size();
1178 while (i > 0) {
1179 i--;
1180 const asset_path& ap = mAssetPaths.itemAt(i);
1181 if (ap.type == kFileTypeRegular) {
Steve Block71f2cf12011-10-20 11:56:00 +01001182 ALOGV("Adding directory %s from zip %s", dirName, ap.path.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 scanAndMergeZipLocked(pMergedInfo, ap, kAssetsRoot, dirName);
1184 } else {
Steve Block71f2cf12011-10-20 11:56:00 +01001185 ALOGV("Adding directory %s from dir %s", dirName, ap.path.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 scanAndMergeDirLocked(pMergedInfo, ap, kAssetsRoot, dirName);
1187 }
1188 }
1189
1190#if 0
1191 printf("FILE LIST:\n");
1192 for (i = 0; i < (size_t) pMergedInfo->size(); i++) {
1193 printf(" %d: (%d) '%s'\n", i,
1194 pMergedInfo->itemAt(i).getFileType(),
1195 (const char*) pMergedInfo->itemAt(i).getFileName());
1196 }
1197#endif
1198
1199 pDir->setFileList(pMergedInfo);
1200 return pDir;
1201}
1202
1203/*
Dianne Hackbornbb9ea302009-05-18 15:22:00 -07001204 * Open a directory in the non-asset namespace.
1205 *
1206 * An "asset directory" is simply the combination of all files in all
1207 * locations, with ".gz" stripped for loose files. With app, locale, and
1208 * vendor defined, we have 8 directories and 2 Zip archives to scan.
1209 *
1210 * Pass in "" for the root dir.
1211 */
1212AssetDir* AssetManager::openNonAssetDir(void* cookie, const char* dirName)
1213{
1214 AutoMutex _l(mLock);
1215
1216 AssetDir* pDir = NULL;
1217 SortedVector<AssetDir::FileInfo>* pMergedInfo = NULL;
1218
1219 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
1220 assert(dirName != NULL);
1221
1222 //printf("+++ openDir(%s) in '%s'\n", dirName, (const char*) mAssetBase);
1223
1224 if (mCacheMode != CACHE_OFF && !mCacheValid)
1225 loadFileNameCacheLocked();
1226
1227 pDir = new AssetDir;
1228
1229 pMergedInfo = new SortedVector<AssetDir::FileInfo>;
1230
1231 const size_t which = ((size_t)cookie)-1;
1232
1233 if (which < mAssetPaths.size()) {
1234 const asset_path& ap = mAssetPaths.itemAt(which);
1235 if (ap.type == kFileTypeRegular) {
Steve Block71f2cf12011-10-20 11:56:00 +01001236 ALOGV("Adding directory %s from zip %s", dirName, ap.path.string());
Dianne Hackbornbb9ea302009-05-18 15:22:00 -07001237 scanAndMergeZipLocked(pMergedInfo, ap, NULL, dirName);
1238 } else {
Steve Block71f2cf12011-10-20 11:56:00 +01001239 ALOGV("Adding directory %s from dir %s", dirName, ap.path.string());
Dianne Hackbornbb9ea302009-05-18 15:22:00 -07001240 scanAndMergeDirLocked(pMergedInfo, ap, NULL, dirName);
1241 }
1242 }
1243
1244#if 0
1245 printf("FILE LIST:\n");
1246 for (i = 0; i < (size_t) pMergedInfo->size(); i++) {
1247 printf(" %d: (%d) '%s'\n", i,
1248 pMergedInfo->itemAt(i).getFileType(),
1249 (const char*) pMergedInfo->itemAt(i).getFileName());
1250 }
1251#endif
1252
1253 pDir->setFileList(pMergedInfo);
1254 return pDir;
1255}
1256
1257/*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 * Scan the contents of the specified directory and merge them into the
1259 * "pMergedInfo" vector, removing previous entries if we find "exclude"
1260 * directives.
1261 *
1262 * Returns "false" if we found nothing to contribute.
1263 */
1264bool AssetManager::scanAndMergeDirLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
1265 const asset_path& ap, const char* rootDir, const char* dirName)
1266{
1267 SortedVector<AssetDir::FileInfo>* pContents;
1268 String8 path;
1269
1270 assert(pMergedInfo != NULL);
1271
1272 //printf("scanAndMergeDir: %s %s %s %s\n", appName, locale, vendor,dirName);
1273
1274 if (mCacheValid) {
1275 int i, start, count;
1276
1277 pContents = new SortedVector<AssetDir::FileInfo>;
1278
1279 /*
1280 * Get the basic partial path and find it in the cache. That's
1281 * the start point for the search.
1282 */
1283 path = createPathNameLocked(ap, rootDir);
1284 if (dirName[0] != '\0')
1285 path.appendPath(dirName);
1286
1287 start = mCache.indexOf(path);
1288 if (start == NAME_NOT_FOUND) {
1289 //printf("+++ not found in cache: dir '%s'\n", (const char*) path);
1290 delete pContents;
1291 return false;
1292 }
1293
1294 /*
1295 * The match string looks like "common/default/default/foo/bar/".
1296 * The '/' on the end ensures that we don't match on the directory
1297 * itself or on ".../foo/barfy/".
1298 */
1299 path.append("/");
1300
1301 count = mCache.size();
1302
1303 /*
1304 * Pick out the stuff in the current dir by examining the pathname.
1305 * It needs to match the partial pathname prefix, and not have a '/'
1306 * (fssep) anywhere after the prefix.
1307 */
1308 for (i = start+1; i < count; i++) {
1309 if (mCache[i].getFileName().length() > path.length() &&
1310 strncmp(mCache[i].getFileName().string(), path.string(), path.length()) == 0)
1311 {
1312 const char* name = mCache[i].getFileName().string();
1313 // XXX THIS IS BROKEN! Looks like we need to store the full
1314 // path prefix separately from the file path.
1315 if (strchr(name + path.length(), '/') == NULL) {
1316 /* grab it, reducing path to just the filename component */
1317 AssetDir::FileInfo tmp = mCache[i];
1318 tmp.setFileName(tmp.getFileName().getPathLeaf());
1319 pContents->add(tmp);
1320 }
1321 } else {
1322 /* no longer in the dir or its subdirs */
1323 break;
1324 }
1325
1326 }
1327 } else {
1328 path = createPathNameLocked(ap, rootDir);
1329 if (dirName[0] != '\0')
1330 path.appendPath(dirName);
1331 pContents = scanDirLocked(path);
1332 if (pContents == NULL)
1333 return false;
1334 }
1335
1336 // if we wanted to do an incremental cache fill, we would do it here
1337
1338 /*
1339 * Process "exclude" directives. If we find a filename that ends with
1340 * ".EXCLUDE", we look for a matching entry in the "merged" set, and
1341 * remove it if we find it. We also delete the "exclude" entry.
1342 */
1343 int i, count, exclExtLen;
1344
1345 count = pContents->size();
1346 exclExtLen = strlen(kExcludeExtension);
1347 for (i = 0; i < count; i++) {
1348 const char* name;
1349 int nameLen;
1350
1351 name = pContents->itemAt(i).getFileName().string();
1352 nameLen = strlen(name);
1353 if (nameLen > exclExtLen &&
1354 strcmp(name + (nameLen - exclExtLen), kExcludeExtension) == 0)
1355 {
1356 String8 match(name, nameLen - exclExtLen);
1357 int matchIdx;
1358
1359 matchIdx = AssetDir::FileInfo::findEntry(pMergedInfo, match);
1360 if (matchIdx > 0) {
Steve Block71f2cf12011-10-20 11:56:00 +01001361 ALOGV("Excluding '%s' [%s]\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362 pMergedInfo->itemAt(matchIdx).getFileName().string(),
1363 pMergedInfo->itemAt(matchIdx).getSourceName().string());
1364 pMergedInfo->removeAt(matchIdx);
1365 } else {
1366 //printf("+++ no match on '%s'\n", (const char*) match);
1367 }
1368
Steve Block5baa3a62011-12-20 16:23:08 +00001369 ALOGD("HEY: size=%d removing %d\n", (int)pContents->size(), i);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001370 pContents->removeAt(i);
1371 i--; // adjust "for" loop
1372 count--; // and loop limit
1373 }
1374 }
1375
1376 mergeInfoLocked(pMergedInfo, pContents);
1377
1378 delete pContents;
1379
1380 return true;
1381}
1382
1383/*
1384 * Scan the contents of the specified directory, and stuff what we find
1385 * into a newly-allocated vector.
1386 *
1387 * Files ending in ".gz" will have their extensions removed.
1388 *
1389 * We should probably think about skipping files with "illegal" names,
1390 * e.g. illegal characters (/\:) or excessive length.
1391 *
1392 * Returns NULL if the specified directory doesn't exist.
1393 */
1394SortedVector<AssetDir::FileInfo>* AssetManager::scanDirLocked(const String8& path)
1395{
1396 SortedVector<AssetDir::FileInfo>* pContents = NULL;
1397 DIR* dir;
1398 struct dirent* entry;
1399 FileType fileType;
1400
Steve Block71f2cf12011-10-20 11:56:00 +01001401 ALOGV("Scanning dir '%s'\n", path.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001402
1403 dir = opendir(path.string());
1404 if (dir == NULL)
1405 return NULL;
1406
1407 pContents = new SortedVector<AssetDir::FileInfo>;
1408
1409 while (1) {
1410 entry = readdir(dir);
1411 if (entry == NULL)
1412 break;
1413
1414 if (strcmp(entry->d_name, ".") == 0 ||
1415 strcmp(entry->d_name, "..") == 0)
1416 continue;
1417
1418#ifdef _DIRENT_HAVE_D_TYPE
1419 if (entry->d_type == DT_REG)
1420 fileType = kFileTypeRegular;
1421 else if (entry->d_type == DT_DIR)
1422 fileType = kFileTypeDirectory;
1423 else
1424 fileType = kFileTypeUnknown;
1425#else
1426 // stat the file
1427 fileType = ::getFileType(path.appendPathCopy(entry->d_name).string());
1428#endif
1429
1430 if (fileType != kFileTypeRegular && fileType != kFileTypeDirectory)
1431 continue;
1432
1433 AssetDir::FileInfo info;
1434 info.set(String8(entry->d_name), fileType);
1435 if (strcasecmp(info.getFileName().getPathExtension().string(), ".gz") == 0)
1436 info.setFileName(info.getFileName().getBasePath());
1437 info.setSourceName(path.appendPathCopy(info.getFileName()));
1438 pContents->add(info);
1439 }
1440
1441 closedir(dir);
1442 return pContents;
1443}
1444
1445/*
1446 * Scan the contents out of the specified Zip archive, and merge what we
1447 * find into "pMergedInfo". If the Zip archive in question doesn't exist,
1448 * we return immediately.
1449 *
1450 * Returns "false" if we found nothing to contribute.
1451 */
1452bool AssetManager::scanAndMergeZipLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
1453 const asset_path& ap, const char* rootDir, const char* baseDirName)
1454{
1455 ZipFileRO* pZip;
1456 Vector<String8> dirs;
1457 AssetDir::FileInfo info;
1458 SortedVector<AssetDir::FileInfo> contents;
1459 String8 sourceName, zipName, dirName;
1460
1461 pZip = mZipSet.getZip(ap.path);
1462 if (pZip == NULL) {
Steve Block8564c8d2012-01-05 23:22:43 +00001463 ALOGW("Failure opening zip %s\n", ap.path.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001464 return false;
1465 }
1466
1467 zipName = ZipSet::getPathName(ap.path.string());
1468
1469 /* convert "sounds" to "rootDir/sounds" */
1470 if (rootDir != NULL) dirName = rootDir;
1471 dirName.appendPath(baseDirName);
1472
1473 /*
1474 * Scan through the list of files, looking for a match. The files in
1475 * the Zip table of contents are not in sorted order, so we have to
1476 * process the entire list. We're looking for a string that begins
1477 * with the characters in "dirName", is followed by a '/', and has no
1478 * subsequent '/' in the stuff that follows.
1479 *
1480 * What makes this especially fun is that directories are not stored
1481 * explicitly in Zip archives, so we have to infer them from context.
1482 * When we see "sounds/foo.wav" we have to leave a note to ourselves
1483 * to insert a directory called "sounds" into the list. We store
1484 * these in temporary vector so that we only return each one once.
1485 *
1486 * Name comparisons are case-sensitive to match UNIX filesystem
1487 * semantics.
1488 */
1489 int dirNameLen = dirName.length();
1490 for (int i = 0; i < pZip->getNumEntries(); i++) {
1491 ZipEntryRO entry;
1492 char nameBuf[256];
1493
1494 entry = pZip->findEntryByIndex(i);
1495 if (pZip->getEntryFileName(entry, nameBuf, sizeof(nameBuf)) != 0) {
1496 // TODO: fix this if we expect to have long names
Steve Block3762c312012-01-06 19:20:56 +00001497 ALOGE("ARGH: name too long?\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 continue;
1499 }
Dianne Hackbornbb9ea302009-05-18 15:22:00 -07001500 //printf("Comparing %s in %s?\n", nameBuf, dirName.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 if (dirNameLen == 0 ||
1502 (strncmp(nameBuf, dirName.string(), dirNameLen) == 0 &&
1503 nameBuf[dirNameLen] == '/'))
1504 {
1505 const char* cp;
1506 const char* nextSlash;
1507
1508 cp = nameBuf + dirNameLen;
1509 if (dirNameLen != 0)
1510 cp++; // advance past the '/'
1511
1512 nextSlash = strchr(cp, '/');
1513//xxx this may break if there are bare directory entries
1514 if (nextSlash == NULL) {
1515 /* this is a file in the requested directory */
1516
1517 info.set(String8(nameBuf).getPathLeaf(), kFileTypeRegular);
1518
1519 info.setSourceName(
1520 createZipSourceNameLocked(zipName, dirName, info.getFileName()));
1521
1522 contents.add(info);
Dianne Hackbornbb9ea302009-05-18 15:22:00 -07001523 //printf("FOUND: file '%s'\n", info.getFileName().string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 } else {
1525 /* this is a subdir; add it if we don't already have it*/
1526 String8 subdirName(cp, nextSlash - cp);
1527 size_t j;
1528 size_t N = dirs.size();
1529
1530 for (j = 0; j < N; j++) {
1531 if (subdirName == dirs[j]) {
1532 break;
1533 }
1534 }
1535 if (j == N) {
1536 dirs.add(subdirName);
1537 }
1538
Dianne Hackbornbb9ea302009-05-18 15:22:00 -07001539 //printf("FOUND: dir '%s'\n", subdirName.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 }
1541 }
1542 }
1543
1544 /*
1545 * Add the set of unique directories.
1546 */
1547 for (int i = 0; i < (int) dirs.size(); i++) {
1548 info.set(dirs[i], kFileTypeDirectory);
1549 info.setSourceName(
1550 createZipSourceNameLocked(zipName, dirName, info.getFileName()));
1551 contents.add(info);
1552 }
1553
1554 mergeInfoLocked(pMergedInfo, &contents);
1555
1556 return true;
1557}
1558
1559
1560/*
1561 * Merge two vectors of FileInfo.
1562 *
1563 * The merged contents will be stuffed into *pMergedInfo.
1564 *
1565 * If an entry for a file exists in both "pMergedInfo" and "pContents",
1566 * we use the newer "pContents" entry.
1567 */
1568void AssetManager::mergeInfoLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
1569 const SortedVector<AssetDir::FileInfo>* pContents)
1570{
1571 /*
1572 * Merge what we found in this directory with what we found in
1573 * other places.
1574 *
1575 * Two basic approaches:
1576 * (1) Create a new array that holds the unique values of the two
1577 * arrays.
1578 * (2) Take the elements from pContents and shove them into pMergedInfo.
1579 *
1580 * Because these are vectors of complex objects, moving elements around
1581 * inside the vector requires constructing new objects and allocating
1582 * storage for members. With approach #1, we're always adding to the
1583 * end, whereas with #2 we could be inserting multiple elements at the
1584 * front of the vector. Approach #1 requires a full copy of the
1585 * contents of pMergedInfo, but approach #2 requires the same copy for
1586 * every insertion at the front of pMergedInfo.
1587 *
1588 * (We should probably use a SortedVector interface that allows us to
1589 * just stuff items in, trusting us to maintain the sort order.)
1590 */
1591 SortedVector<AssetDir::FileInfo>* pNewSorted;
1592 int mergeMax, contMax;
1593 int mergeIdx, contIdx;
1594
1595 pNewSorted = new SortedVector<AssetDir::FileInfo>;
1596 mergeMax = pMergedInfo->size();
1597 contMax = pContents->size();
1598 mergeIdx = contIdx = 0;
1599
1600 while (mergeIdx < mergeMax || contIdx < contMax) {
1601 if (mergeIdx == mergeMax) {
1602 /* hit end of "merge" list, copy rest of "contents" */
1603 pNewSorted->add(pContents->itemAt(contIdx));
1604 contIdx++;
1605 } else if (contIdx == contMax) {
1606 /* hit end of "cont" list, copy rest of "merge" */
1607 pNewSorted->add(pMergedInfo->itemAt(mergeIdx));
1608 mergeIdx++;
1609 } else if (pMergedInfo->itemAt(mergeIdx) == pContents->itemAt(contIdx))
1610 {
1611 /* items are identical, add newer and advance both indices */
1612 pNewSorted->add(pContents->itemAt(contIdx));
1613 mergeIdx++;
1614 contIdx++;
1615 } else if (pMergedInfo->itemAt(mergeIdx) < pContents->itemAt(contIdx))
1616 {
1617 /* "merge" is lower, add that one */
1618 pNewSorted->add(pMergedInfo->itemAt(mergeIdx));
1619 mergeIdx++;
1620 } else {
1621 /* "cont" is lower, add that one */
1622 assert(pContents->itemAt(contIdx) < pMergedInfo->itemAt(mergeIdx));
1623 pNewSorted->add(pContents->itemAt(contIdx));
1624 contIdx++;
1625 }
1626 }
1627
1628 /*
1629 * Overwrite the "merged" list with the new stuff.
1630 */
1631 *pMergedInfo = *pNewSorted;
1632 delete pNewSorted;
1633
1634#if 0 // for Vector, rather than SortedVector
1635 int i, j;
1636 for (i = pContents->size() -1; i >= 0; i--) {
1637 bool add = true;
1638
1639 for (j = pMergedInfo->size() -1; j >= 0; j--) {
1640 /* case-sensitive comparisons, to behave like UNIX fs */
1641 if (strcmp(pContents->itemAt(i).mFileName,
1642 pMergedInfo->itemAt(j).mFileName) == 0)
1643 {
1644 /* match, don't add this entry */
1645 add = false;
1646 break;
1647 }
1648 }
1649
1650 if (add)
1651 pMergedInfo->add(pContents->itemAt(i));
1652 }
1653#endif
1654}
1655
1656
1657/*
1658 * Load all files into the file name cache. We want to do this across
1659 * all combinations of { appname, locale, vendor }, performing a recursive
1660 * directory traversal.
1661 *
1662 * This is not the most efficient data structure. Also, gathering the
1663 * information as we needed it (file-by-file or directory-by-directory)
1664 * would be faster. However, on the actual device, 99% of the files will
1665 * live in Zip archives, so this list will be very small. The trouble
1666 * is that we have to check the "loose" files first, so it's important
1667 * that we don't beat the filesystem silly looking for files that aren't
1668 * there.
1669 *
1670 * Note on thread safety: this is the only function that causes updates
1671 * to mCache, and anybody who tries to use it will call here if !mCacheValid,
1672 * so we need to employ a mutex here.
1673 */
1674void AssetManager::loadFileNameCacheLocked(void)
1675{
1676 assert(!mCacheValid);
1677 assert(mCache.size() == 0);
1678
1679#ifdef DO_TIMINGS // need to link against -lrt for this now
1680 DurationTimer timer;
1681 timer.start();
1682#endif
1683
1684 fncScanLocked(&mCache, "");
1685
1686#ifdef DO_TIMINGS
1687 timer.stop();
Steve Block5baa3a62011-12-20 16:23:08 +00001688 ALOGD("Cache scan took %.3fms\n",
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 timer.durationUsecs() / 1000.0);
1690#endif
1691
1692#if 0
1693 int i;
1694 printf("CACHED FILE LIST (%d entries):\n", mCache.size());
1695 for (i = 0; i < (int) mCache.size(); i++) {
1696 printf(" %d: (%d) '%s'\n", i,
1697 mCache.itemAt(i).getFileType(),
1698 (const char*) mCache.itemAt(i).getFileName());
1699 }
1700#endif
1701
1702 mCacheValid = true;
1703}
1704
1705/*
1706 * Scan up to 8 versions of the specified directory.
1707 */
1708void AssetManager::fncScanLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
1709 const char* dirName)
1710{
1711 size_t i = mAssetPaths.size();
1712 while (i > 0) {
1713 i--;
1714 const asset_path& ap = mAssetPaths.itemAt(i);
1715 fncScanAndMergeDirLocked(pMergedInfo, ap, NULL, NULL, dirName);
1716 if (mLocale != NULL)
1717 fncScanAndMergeDirLocked(pMergedInfo, ap, mLocale, NULL, dirName);
1718 if (mVendor != NULL)
1719 fncScanAndMergeDirLocked(pMergedInfo, ap, NULL, mVendor, dirName);
1720 if (mLocale != NULL && mVendor != NULL)
1721 fncScanAndMergeDirLocked(pMergedInfo, ap, mLocale, mVendor, dirName);
1722 }
1723}
1724
1725/*
1726 * Recursively scan this directory and all subdirs.
1727 *
1728 * This is similar to scanAndMergeDir, but we don't remove the .EXCLUDE
1729 * files, and we prepend the extended partial path to the filenames.
1730 */
1731bool AssetManager::fncScanAndMergeDirLocked(
1732 SortedVector<AssetDir::FileInfo>* pMergedInfo,
1733 const asset_path& ap, const char* locale, const char* vendor,
1734 const char* dirName)
1735{
1736 SortedVector<AssetDir::FileInfo>* pContents;
1737 String8 partialPath;
1738 String8 fullPath;
1739
1740 // XXX This is broken -- the filename cache needs to hold the base
1741 // asset path separately from its filename.
1742
1743 partialPath = createPathNameLocked(ap, locale, vendor);
1744 if (dirName[0] != '\0') {
1745 partialPath.appendPath(dirName);
1746 }
1747
1748 fullPath = partialPath;
1749 pContents = scanDirLocked(fullPath);
1750 if (pContents == NULL) {
1751 return false; // directory did not exist
1752 }
1753
1754 /*
1755 * Scan all subdirectories of the current dir, merging what we find
1756 * into "pMergedInfo".
1757 */
1758 for (int i = 0; i < (int) pContents->size(); i++) {
1759 if (pContents->itemAt(i).getFileType() == kFileTypeDirectory) {
1760 String8 subdir(dirName);
1761 subdir.appendPath(pContents->itemAt(i).getFileName());
1762
1763 fncScanAndMergeDirLocked(pMergedInfo, ap, locale, vendor, subdir.string());
1764 }
1765 }
1766
1767 /*
1768 * To be consistent, we want entries for the root directory. If
1769 * we're the root, add one now.
1770 */
1771 if (dirName[0] == '\0') {
1772 AssetDir::FileInfo tmpInfo;
1773
1774 tmpInfo.set(String8(""), kFileTypeDirectory);
1775 tmpInfo.setSourceName(createPathNameLocked(ap, locale, vendor));
1776 pContents->add(tmpInfo);
1777 }
1778
1779 /*
1780 * We want to prepend the extended partial path to every entry in
1781 * "pContents". It's the same value for each entry, so this will
1782 * not change the sorting order of the vector contents.
1783 */
1784 for (int i = 0; i < (int) pContents->size(); i++) {
1785 const AssetDir::FileInfo& info = pContents->itemAt(i);
1786 pContents->editItemAt(i).setFileName(partialPath.appendPathCopy(info.getFileName()));
1787 }
1788
1789 mergeInfoLocked(pMergedInfo, pContents);
1790 return true;
1791}
1792
1793/*
1794 * Trash the cache.
1795 */
1796void AssetManager::purgeFileNameCacheLocked(void)
1797{
1798 mCacheValid = false;
1799 mCache.clear();
1800}
1801
1802/*
1803 * ===========================================================================
1804 * AssetManager::SharedZip
1805 * ===========================================================================
1806 */
1807
1808
1809Mutex AssetManager::SharedZip::gLock;
1810DefaultKeyedVector<String8, wp<AssetManager::SharedZip> > AssetManager::SharedZip::gOpen;
1811
1812AssetManager::SharedZip::SharedZip(const String8& path, time_t modWhen)
Dianne Hackborn78c40512009-07-06 11:07:40 -07001813 : mPath(path), mZipFile(NULL), mModWhen(modWhen),
1814 mResourceTableAsset(NULL), mResourceTable(NULL)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001815{
Steve Block6215d3f2012-01-04 20:05:49 +00001816 //ALOGI("Creating SharedZip %p %s\n", this, (const char*)mPath);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001817 mZipFile = new ZipFileRO;
Steve Block71f2cf12011-10-20 11:56:00 +01001818 ALOGV("+++ opening zip '%s'\n", mPath.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001819 if (mZipFile->open(mPath.string()) != NO_ERROR) {
Steve Block5baa3a62011-12-20 16:23:08 +00001820 ALOGD("failed to open Zip archive '%s'\n", mPath.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001821 delete mZipFile;
1822 mZipFile = NULL;
1823 }
1824}
1825
1826sp<AssetManager::SharedZip> AssetManager::SharedZip::get(const String8& path)
1827{
1828 AutoMutex _l(gLock);
1829 time_t modWhen = getFileModDate(path);
1830 sp<SharedZip> zip = gOpen.valueFor(path).promote();
1831 if (zip != NULL && zip->mModWhen == modWhen) {
1832 return zip;
1833 }
1834 zip = new SharedZip(path, modWhen);
1835 gOpen.add(path, zip);
1836 return zip;
1837
1838}
1839
1840ZipFileRO* AssetManager::SharedZip::getZip()
1841{
1842 return mZipFile;
1843}
1844
1845Asset* AssetManager::SharedZip::getResourceTableAsset()
1846{
Steve Block71f2cf12011-10-20 11:56:00 +01001847 ALOGV("Getting from SharedZip %p resource asset %p\n", this, mResourceTableAsset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001848 return mResourceTableAsset;
1849}
1850
1851Asset* AssetManager::SharedZip::setResourceTableAsset(Asset* asset)
1852{
1853 {
1854 AutoMutex _l(gLock);
1855 if (mResourceTableAsset == NULL) {
1856 mResourceTableAsset = asset;
1857 // This is not thread safe the first time it is called, so
1858 // do it here with the global lock held.
1859 asset->getBuffer(true);
1860 return asset;
1861 }
1862 }
1863 delete asset;
1864 return mResourceTableAsset;
1865}
1866
Dianne Hackborn78c40512009-07-06 11:07:40 -07001867ResTable* AssetManager::SharedZip::getResourceTable()
1868{
Steve Block71f2cf12011-10-20 11:56:00 +01001869 ALOGV("Getting from SharedZip %p resource table %p\n", this, mResourceTable);
Dianne Hackborn78c40512009-07-06 11:07:40 -07001870 return mResourceTable;
1871}
1872
1873ResTable* AssetManager::SharedZip::setResourceTable(ResTable* res)
1874{
1875 {
1876 AutoMutex _l(gLock);
1877 if (mResourceTable == NULL) {
1878 mResourceTable = res;
1879 return res;
1880 }
1881 }
1882 delete res;
1883 return mResourceTable;
1884}
1885
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001886bool AssetManager::SharedZip::isUpToDate()
1887{
1888 time_t modWhen = getFileModDate(mPath.string());
1889 return mModWhen == modWhen;
1890}
1891
1892AssetManager::SharedZip::~SharedZip()
1893{
Steve Block6215d3f2012-01-04 20:05:49 +00001894 //ALOGI("Destroying SharedZip %p %s\n", this, (const char*)mPath);
Dianne Hackborn78c40512009-07-06 11:07:40 -07001895 if (mResourceTable != NULL) {
1896 delete mResourceTable;
1897 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001898 if (mResourceTableAsset != NULL) {
1899 delete mResourceTableAsset;
1900 }
1901 if (mZipFile != NULL) {
1902 delete mZipFile;
Steve Block71f2cf12011-10-20 11:56:00 +01001903 ALOGV("Closed '%s'\n", mPath.string());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001904 }
1905}
1906
1907/*
1908 * ===========================================================================
1909 * AssetManager::ZipSet
1910 * ===========================================================================
1911 */
1912
1913/*
1914 * Constructor.
1915 */
1916AssetManager::ZipSet::ZipSet(void)
1917{
1918}
1919
1920/*
1921 * Destructor. Close any open archives.
1922 */
1923AssetManager::ZipSet::~ZipSet(void)
1924{
1925 size_t N = mZipFile.size();
1926 for (size_t i = 0; i < N; i++)
1927 closeZip(i);
1928}
1929
1930/*
1931 * Close a Zip file and reset the entry.
1932 */
1933void AssetManager::ZipSet::closeZip(int idx)
1934{
1935 mZipFile.editItemAt(idx) = NULL;
1936}
1937
1938
1939/*
1940 * Retrieve the appropriate Zip file from the set.
1941 */
1942ZipFileRO* AssetManager::ZipSet::getZip(const String8& path)
1943{
1944 int idx = getIndex(path);
1945 sp<SharedZip> zip = mZipFile[idx];
1946 if (zip == NULL) {
1947 zip = SharedZip::get(path);
1948 mZipFile.editItemAt(idx) = zip;
1949 }
1950 return zip->getZip();
1951}
1952
Dianne Hackborn78c40512009-07-06 11:07:40 -07001953Asset* AssetManager::ZipSet::getZipResourceTableAsset(const String8& path)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954{
1955 int idx = getIndex(path);
1956 sp<SharedZip> zip = mZipFile[idx];
1957 if (zip == NULL) {
1958 zip = SharedZip::get(path);
1959 mZipFile.editItemAt(idx) = zip;
1960 }
1961 return zip->getResourceTableAsset();
1962}
1963
Dianne Hackborn78c40512009-07-06 11:07:40 -07001964Asset* AssetManager::ZipSet::setZipResourceTableAsset(const String8& path,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965 Asset* asset)
1966{
1967 int idx = getIndex(path);
1968 sp<SharedZip> zip = mZipFile[idx];
1969 // doesn't make sense to call before previously accessing.
1970 return zip->setResourceTableAsset(asset);
1971}
1972
Dianne Hackborn78c40512009-07-06 11:07:40 -07001973ResTable* AssetManager::ZipSet::getZipResourceTable(const String8& path)
1974{
1975 int idx = getIndex(path);
1976 sp<SharedZip> zip = mZipFile[idx];
1977 if (zip == NULL) {
1978 zip = SharedZip::get(path);
1979 mZipFile.editItemAt(idx) = zip;
1980 }
1981 return zip->getResourceTable();
1982}
1983
1984ResTable* AssetManager::ZipSet::setZipResourceTable(const String8& path,
1985 ResTable* res)
1986{
1987 int idx = getIndex(path);
1988 sp<SharedZip> zip = mZipFile[idx];
1989 // doesn't make sense to call before previously accessing.
1990 return zip->setResourceTable(res);
1991}
1992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001993/*
1994 * Generate the partial pathname for the specified archive. The caller
1995 * gets to prepend the asset root directory.
1996 *
1997 * Returns something like "common/en-US-noogle.jar".
1998 */
1999/*static*/ String8 AssetManager::ZipSet::getPathName(const char* zipPath)
2000{
2001 return String8(zipPath);
2002}
2003
2004bool AssetManager::ZipSet::isUpToDate()
2005{
2006 const size_t N = mZipFile.size();
2007 for (size_t i=0; i<N; i++) {
2008 if (mZipFile[i] != NULL && !mZipFile[i]->isUpToDate()) {
2009 return false;
2010 }
2011 }
2012 return true;
2013}
2014
2015/*
2016 * Compute the zip file's index.
2017 *
2018 * "appName", "locale", and "vendor" should be set to NULL to indicate the
2019 * default directory.
2020 */
2021int AssetManager::ZipSet::getIndex(const String8& zip) const
2022{
2023 const size_t N = mZipPath.size();
2024 for (size_t i=0; i<N; i++) {
2025 if (mZipPath[i] == zip) {
2026 return i;
2027 }
2028 }
2029
2030 mZipPath.add(zip);
2031 mZipFile.add(NULL);
2032
2033 return mZipPath.size()-1;
2034}