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