blob: 4a42e2b542c85c485420a4a1048647d19d8c4efc [file] [log] [blame]
Adam Lesinski16c4d152014-01-24 13:27:13 -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"
22#define ATRACE_TAG ATRACE_TAG_RESOURCES
23//#define LOG_NDEBUG 0
24
25#include <androidfw/Asset.h>
26#include <androidfw/AssetDir.h>
27#include <androidfw/AssetManager.h>
28#include <androidfw/misc.h>
29#include <androidfw/ResourceTypes.h>
30#include <androidfw/ZipFileRO.h>
31#include <utils/Atomic.h>
32#include <utils/Log.h>
33#include <utils/String8.h>
34#include <utils/String8.h>
35#include <utils/threads.h>
36#include <utils/Timers.h>
Adam Lesinskib7e1ce02016-04-11 20:03:01 -070037#include <utils/Trace.h>
Adam Lesinski16c4d152014-01-24 13:27:13 -080038
39#include <assert.h>
40#include <dirent.h>
41#include <errno.h>
Mårten Kongstad48d22322014-01-31 14:43:27 +010042#include <string.h> // strerror
Adam Lesinski16c4d152014-01-24 13:27:13 -080043#include <strings.h>
Adam Lesinski16c4d152014-01-24 13:27:13 -080044
45#ifndef TEMP_FAILURE_RETRY
46/* Used to retry syscalls that can return EINTR. */
47#define TEMP_FAILURE_RETRY(exp) ({ \
48 typeof (exp) _rc; \
49 do { \
50 _rc = (exp); \
51 } while (_rc == -1 && errno == EINTR); \
52 _rc; })
53#endif
54
Adam Lesinski16c4d152014-01-24 13:27:13 -080055using namespace android;
56
Andreas Gampe2204f0b2014-10-21 23:04:54 -070057static const bool kIsDebug = false;
58
Adam Lesinski16c4d152014-01-24 13:27:13 -080059/*
60 * Names for default app, locale, and vendor. We might want to change
61 * these to be an actual locale, e.g. always use en-US as the default.
62 */
63static const char* kDefaultLocale = "default";
64static const char* kDefaultVendor = "default";
65static const char* kAssetsRoot = "assets";
66static const char* kAppZipName = NULL; //"classes.jar";
67static const char* kSystemAssets = "framework/framework-res.apk";
Mårten Kongstad48d22322014-01-31 14:43:27 +010068static const char* kResourceCache = "resource-cache";
Adam Lesinski16c4d152014-01-24 13:27:13 -080069
70static const char* kExcludeExtension = ".EXCLUDE";
71
72static Asset* const kExcludedAsset = (Asset*) 0xd000000d;
73
74static volatile int32_t gCount = 0;
75
Mårten Kongstad65a05fd2014-01-31 14:01:52 +010076const char* AssetManager::RESOURCES_FILENAME = "resources.arsc";
Mårten Kongstad48d22322014-01-31 14:43:27 +010077const char* AssetManager::IDMAP_BIN = "/system/bin/idmap";
78const char* AssetManager::OVERLAY_DIR = "/vendor/overlay";
Jakub Adamekc03d9482016-09-30 09:19:09 +010079const char* AssetManager::OVERLAY_SKU_DIR_PROPERTY = "ro.boot.vendor.overlay.sku";
Mårten Kongstad48d22322014-01-31 14:43:27 +010080const char* AssetManager::TARGET_PACKAGE_NAME = "android";
81const char* AssetManager::TARGET_APK_PATH = "/system/framework/framework-res.apk";
82const char* AssetManager::IDMAP_DIR = "/data/resource-cache";
Mårten Kongstad65a05fd2014-01-31 14:01:52 +010083
Adam Lesinski16c4d152014-01-24 13:27:13 -080084namespace {
Adam Lesinski16c4d152014-01-24 13:27:13 -080085 String8 idmapPathForPackagePath(const String8& pkgPath)
86 {
87 const char* root = getenv("ANDROID_DATA");
88 LOG_ALWAYS_FATAL_IF(root == NULL, "ANDROID_DATA not set");
89 String8 path(root);
Mårten Kongstad48d22322014-01-31 14:43:27 +010090 path.appendPath(kResourceCache);
Adam Lesinski16c4d152014-01-24 13:27:13 -080091
92 char buf[256]; // 256 chars should be enough for anyone...
93 strncpy(buf, pkgPath.string(), 255);
94 buf[255] = '\0';
95 char* filename = buf;
96 while (*filename && *filename == '/') {
97 ++filename;
98 }
99 char* p = filename;
100 while (*p) {
101 if (*p == '/') {
102 *p = '@';
103 }
104 ++p;
105 }
106 path.appendPath(filename);
107 path.append("@idmap");
108
109 return path;
110 }
111
112 /*
113 * Like strdup(), but uses C++ "new" operator instead of malloc.
114 */
115 static char* strdupNew(const char* str)
116 {
117 char* newStr;
118 int len;
119
120 if (str == NULL)
121 return NULL;
122
123 len = strlen(str);
124 newStr = new char[len+1];
125 memcpy(newStr, str, len+1);
126
127 return newStr;
128 }
129}
130
131/*
132 * ===========================================================================
133 * AssetManager
134 * ===========================================================================
135 */
136
137int32_t AssetManager::getGlobalCount()
138{
139 return gCount;
140}
141
142AssetManager::AssetManager(CacheMode cacheMode)
143 : mLocale(NULL), mVendor(NULL),
144 mResources(NULL), mConfig(new ResTable_config),
145 mCacheMode(cacheMode), mCacheValid(false)
146{
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700147 int count = android_atomic_inc(&gCount) + 1;
148 if (kIsDebug) {
149 ALOGI("Creating AssetManager %p #%d\n", this, count);
150 }
Adam Lesinski16c4d152014-01-24 13:27:13 -0800151 memset(mConfig, 0, sizeof(ResTable_config));
152}
153
154AssetManager::~AssetManager(void)
155{
156 int count = android_atomic_dec(&gCount);
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700157 if (kIsDebug) {
158 ALOGI("Destroying AssetManager in %p #%d\n", this, count);
159 }
Adam Lesinski16c4d152014-01-24 13:27:13 -0800160
161 delete mConfig;
162 delete mResources;
163
164 // don't have a String class yet, so make sure we clean up
165 delete[] mLocale;
166 delete[] mVendor;
167}
168
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800169bool AssetManager::addAssetPath(
170 const String8& path, int32_t* cookie, bool appAsLib, bool isSystemAsset)
Adam Lesinski16c4d152014-01-24 13:27:13 -0800171{
172 AutoMutex _l(mLock);
173
174 asset_path ap;
175
176 String8 realPath(path);
177 if (kAppZipName) {
178 realPath.appendPath(kAppZipName);
179 }
180 ap.type = ::getFileType(realPath.string());
181 if (ap.type == kFileTypeRegular) {
182 ap.path = realPath;
183 } else {
184 ap.path = path;
185 ap.type = ::getFileType(path.string());
186 if (ap.type != kFileTypeDirectory && ap.type != kFileTypeRegular) {
187 ALOGW("Asset path %s is neither a directory nor file (type=%d).",
188 path.string(), (int)ap.type);
189 return false;
190 }
191 }
192
193 // Skip if we have it already.
194 for (size_t i=0; i<mAssetPaths.size(); i++) {
195 if (mAssetPaths[i].path == ap.path) {
196 if (cookie) {
Narayan Kamatha0c62602014-01-24 13:51:51 +0000197 *cookie = static_cast<int32_t>(i+1);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800198 }
199 return true;
200 }
201 }
202
203 ALOGV("In %p Asset %s path: %s", this,
204 ap.type == kFileTypeDirectory ? "dir" : "zip", ap.path.string());
205
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800206 ap.isSystemAsset = isSystemAsset;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800207 mAssetPaths.add(ap);
208
209 // new paths are always added at the end
210 if (cookie) {
Narayan Kamatha0c62602014-01-24 13:51:51 +0000211 *cookie = static_cast<int32_t>(mAssetPaths.size());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800212 }
213
Elliott Hughesba3fe562015-08-12 14:49:53 -0700214#ifdef __ANDROID__
Mårten Kongstad48d22322014-01-31 14:43:27 +0100215 // Load overlays, if any
216 asset_path oap;
217 for (size_t idx = 0; mZipSet.getOverlay(ap.path, idx, &oap); idx++) {
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800218 oap.isSystemAsset = isSystemAsset;
Mårten Kongstad48d22322014-01-31 14:43:27 +0100219 mAssetPaths.add(oap);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800220 }
Mårten Kongstad48d22322014-01-31 14:43:27 +0100221#endif
Adam Lesinski16c4d152014-01-24 13:27:13 -0800222
Martin Kosiba7df36252014-01-16 16:25:56 +0000223 if (mResources != NULL) {
Tao Baia6d7e3f2015-09-01 18:49:54 -0700224 appendPathToResTable(ap, appAsLib);
Martin Kosiba7df36252014-01-16 16:25:56 +0000225 }
226
Adam Lesinski16c4d152014-01-24 13:27:13 -0800227 return true;
228}
229
Mårten Kongstad48d22322014-01-31 14:43:27 +0100230bool AssetManager::addOverlayPath(const String8& packagePath, int32_t* cookie)
231{
232 const String8 idmapPath = idmapPathForPackagePath(packagePath);
233
234 AutoMutex _l(mLock);
235
236 for (size_t i = 0; i < mAssetPaths.size(); ++i) {
237 if (mAssetPaths[i].idmap == idmapPath) {
238 *cookie = static_cast<int32_t>(i + 1);
239 return true;
240 }
241 }
242
243 Asset* idmap = NULL;
244 if ((idmap = openAssetFromFileLocked(idmapPath, Asset::ACCESS_BUFFER)) == NULL) {
245 ALOGW("failed to open idmap file %s\n", idmapPath.string());
246 return false;
247 }
248
249 String8 targetPath;
250 String8 overlayPath;
251 if (!ResTable::getIdmapInfo(idmap->getBuffer(false), idmap->getLength(),
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700252 NULL, NULL, NULL, &targetPath, &overlayPath)) {
Mårten Kongstad48d22322014-01-31 14:43:27 +0100253 ALOGW("failed to read idmap file %s\n", idmapPath.string());
254 delete idmap;
255 return false;
256 }
257 delete idmap;
258
259 if (overlayPath != packagePath) {
260 ALOGW("idmap file %s inconcistent: expected path %s does not match actual path %s\n",
261 idmapPath.string(), packagePath.string(), overlayPath.string());
262 return false;
263 }
264 if (access(targetPath.string(), R_OK) != 0) {
265 ALOGW("failed to access file %s: %s\n", targetPath.string(), strerror(errno));
266 return false;
267 }
268 if (access(idmapPath.string(), R_OK) != 0) {
269 ALOGW("failed to access file %s: %s\n", idmapPath.string(), strerror(errno));
270 return false;
271 }
272 if (access(overlayPath.string(), R_OK) != 0) {
273 ALOGW("failed to access file %s: %s\n", overlayPath.string(), strerror(errno));
274 return false;
275 }
276
277 asset_path oap;
278 oap.path = overlayPath;
279 oap.type = ::getFileType(overlayPath.string());
280 oap.idmap = idmapPath;
281#if 0
282 ALOGD("Overlay added: targetPath=%s overlayPath=%s idmapPath=%s\n",
283 targetPath.string(), overlayPath.string(), idmapPath.string());
284#endif
285 mAssetPaths.add(oap);
286 *cookie = static_cast<int32_t>(mAssetPaths.size());
287
Mårten Kongstad30113132014-11-07 10:52:17 +0100288 if (mResources != NULL) {
289 appendPathToResTable(oap);
290 }
291
Mårten Kongstad48d22322014-01-31 14:43:27 +0100292 return true;
293 }
294
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100295bool AssetManager::createIdmap(const char* targetApkPath, const char* overlayApkPath,
Dianne Hackborn32bb5fa2014-02-11 13:56:21 -0800296 uint32_t targetCrc, uint32_t overlayCrc, uint32_t** outData, size_t* outSize)
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100297{
298 AutoMutex _l(mLock);
299 const String8 paths[2] = { String8(targetApkPath), String8(overlayApkPath) };
300 ResTable tables[2];
301
302 for (int i = 0; i < 2; ++i) {
303 asset_path ap;
304 ap.type = kFileTypeRegular;
305 ap.path = paths[i];
306 Asset* ass = openNonAssetInPathLocked("resources.arsc", Asset::ACCESS_BUFFER, ap);
307 if (ass == NULL) {
308 ALOGW("failed to find resources.arsc in %s\n", ap.path.string());
309 return false;
310 }
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700311 tables[i].add(ass);
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100312 }
313
314 return tables[0].createIdmap(tables[1], targetCrc, overlayCrc,
315 targetApkPath, overlayApkPath, (void**)outData, outSize) == NO_ERROR;
316}
317
Adam Lesinski16c4d152014-01-24 13:27:13 -0800318bool AssetManager::addDefaultAssets()
319{
320 const char* root = getenv("ANDROID_ROOT");
321 LOG_ALWAYS_FATAL_IF(root == NULL, "ANDROID_ROOT not set");
322
323 String8 path(root);
324 path.appendPath(kSystemAssets);
325
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800326 return addAssetPath(path, NULL, false /* appAsLib */, true /* isSystemAsset */);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800327}
328
Narayan Kamatha0c62602014-01-24 13:51:51 +0000329int32_t AssetManager::nextAssetPath(const int32_t cookie) const
Adam Lesinski16c4d152014-01-24 13:27:13 -0800330{
331 AutoMutex _l(mLock);
Narayan Kamatha0c62602014-01-24 13:51:51 +0000332 const size_t next = static_cast<size_t>(cookie) + 1;
333 return next > mAssetPaths.size() ? -1 : next;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800334}
335
Narayan Kamatha0c62602014-01-24 13:51:51 +0000336String8 AssetManager::getAssetPath(const int32_t cookie) const
Adam Lesinski16c4d152014-01-24 13:27:13 -0800337{
338 AutoMutex _l(mLock);
Narayan Kamatha0c62602014-01-24 13:51:51 +0000339 const size_t which = static_cast<size_t>(cookie) - 1;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800340 if (which < mAssetPaths.size()) {
341 return mAssetPaths[which].path;
342 }
343 return String8();
344}
345
346/*
347 * Set the current locale. Use NULL to indicate no locale.
348 *
349 * Close and reopen Zip archives as appropriate, and reset cached
350 * information in the locale-specific sections of the tree.
351 */
352void AssetManager::setLocale(const char* locale)
353{
354 AutoMutex _l(mLock);
355 setLocaleLocked(locale);
356}
357
Narayan Kamathe4345db2014-06-26 16:01:28 +0100358
Adam Lesinski16c4d152014-01-24 13:27:13 -0800359void AssetManager::setLocaleLocked(const char* locale)
360{
361 if (mLocale != NULL) {
362 /* previously set, purge cached data */
363 purgeFileNameCacheLocked();
364 //mZipSet.purgeLocale();
365 delete[] mLocale;
366 }
Elliott Hughesc367d482013-10-29 13:12:55 -0700367
Adam Lesinski16c4d152014-01-24 13:27:13 -0800368 mLocale = strdupNew(locale);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800369 updateResourceParamsLocked();
370}
371
372/*
373 * Set the current vendor. Use NULL to indicate no vendor.
374 *
375 * Close and reopen Zip archives as appropriate, and reset cached
376 * information in the vendor-specific sections of the tree.
377 */
378void AssetManager::setVendor(const char* vendor)
379{
380 AutoMutex _l(mLock);
381
382 if (mVendor != NULL) {
383 /* previously set, purge cached data */
384 purgeFileNameCacheLocked();
385 //mZipSet.purgeVendor();
386 delete[] mVendor;
387 }
388 mVendor = strdupNew(vendor);
389}
390
391void AssetManager::setConfiguration(const ResTable_config& config, const char* locale)
392{
393 AutoMutex _l(mLock);
394 *mConfig = config;
395 if (locale) {
396 setLocaleLocked(locale);
397 } else if (config.language[0] != 0) {
Narayan Kamath91447d82014-01-21 15:32:36 +0000398 char spec[RESTABLE_MAX_LOCALE_LEN];
399 config.getBcp47Locale(spec);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800400 setLocaleLocked(spec);
401 } else {
402 updateResourceParamsLocked();
403 }
404}
405
406void AssetManager::getConfiguration(ResTable_config* outConfig) const
407{
408 AutoMutex _l(mLock);
409 *outConfig = *mConfig;
410}
411
412/*
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--;
456 ALOGV("Looking for asset '%s' in '%s'\n",
457 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 */
Adam Lesinskide898ff2014-01-29 18:20:45 -0800473Asset* AssetManager::openNonAsset(const char* fileName, AccessMode mode, int32_t* outCookie)
Adam Lesinski16c4d152014-01-24 13:27:13 -0800474{
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--;
490 ALOGV("Looking for non-asset '%s' in '%s'\n", fileName, mAssetPaths.itemAt(i).path.string());
491 Asset* pAsset = openNonAssetInPathLocked(
492 fileName, mode, mAssetPaths.itemAt(i));
493 if (pAsset != NULL) {
Adam Lesinskide898ff2014-01-29 18:20:45 -0800494 if (outCookie != NULL) *outCookie = static_cast<int32_t>(i + 1);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800495 return pAsset != kExcludedAsset ? pAsset : NULL;
496 }
497 }
498
499 return NULL;
500}
501
Narayan Kamatha0c62602014-01-24 13:51:51 +0000502Asset* AssetManager::openNonAsset(const int32_t cookie, const char* fileName, AccessMode mode)
Adam Lesinski16c4d152014-01-24 13:27:13 -0800503{
Narayan Kamatha0c62602014-01-24 13:51:51 +0000504 const size_t which = static_cast<size_t>(cookie) - 1;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800505
506 AutoMutex _l(mLock);
507
508 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
509
Adam Lesinski16c4d152014-01-24 13:27:13 -0800510 if (mCacheMode != CACHE_OFF && !mCacheValid)
511 loadFileNameCacheLocked();
512
513 if (which < mAssetPaths.size()) {
514 ALOGV("Looking for non-asset '%s' in '%s'\n", fileName,
515 mAssetPaths.itemAt(which).path.string());
516 Asset* pAsset = openNonAssetInPathLocked(
517 fileName, mode, mAssetPaths.itemAt(which));
518 if (pAsset != NULL) {
519 return pAsset != kExcludedAsset ? pAsset : NULL;
520 }
521 }
522
523 return NULL;
524}
525
526/*
527 * Get the type of a file in the asset namespace.
528 *
529 * This currently only works for regular files. All others (including
530 * directories) will return kFileTypeNonexistent.
531 */
532FileType AssetManager::getFileType(const char* fileName)
533{
534 Asset* pAsset = NULL;
535
536 /*
537 * Open the asset. This is less efficient than simply finding the
538 * file, but it's not too bad (we don't uncompress or mmap data until
539 * the first read() call).
540 */
541 pAsset = open(fileName, Asset::ACCESS_STREAMING);
542 delete pAsset;
543
544 if (pAsset == NULL)
545 return kFileTypeNonexistent;
546 else
547 return kFileTypeRegular;
548}
549
Tao Baia6d7e3f2015-09-01 18:49:54 -0700550bool AssetManager::appendPathToResTable(const asset_path& ap, bool appAsLib) const {
Mårten Kongstadcb7b63d2014-11-07 10:57:15 +0100551 // skip those ap's that correspond to system overlays
552 if (ap.isSystemOverlay) {
553 return true;
554 }
555
Martin Kosiba7df36252014-01-16 16:25:56 +0000556 Asset* ass = NULL;
557 ResTable* sharedRes = NULL;
558 bool shared = true;
559 bool onlyEmptyResources = true;
Adam Lesinskib7e1ce02016-04-11 20:03:01 -0700560 ATRACE_NAME(ap.path.string());
Martin Kosiba7df36252014-01-16 16:25:56 +0000561 Asset* idmap = openIdmapLocked(ap);
562 size_t nextEntryIdx = mResources->getTableCount();
563 ALOGV("Looking for resource asset in '%s'\n", ap.path.string());
564 if (ap.type != kFileTypeDirectory) {
565 if (nextEntryIdx == 0) {
566 // The first item is typically the framework resources,
567 // which we want to avoid parsing every time.
568 sharedRes = const_cast<AssetManager*>(this)->
569 mZipSet.getZipResourceTable(ap.path);
570 if (sharedRes != NULL) {
571 // skip ahead the number of system overlay packages preloaded
572 nextEntryIdx = sharedRes->getTableCount();
573 }
574 }
575 if (sharedRes == NULL) {
576 ass = const_cast<AssetManager*>(this)->
577 mZipSet.getZipResourceTableAsset(ap.path);
578 if (ass == NULL) {
579 ALOGV("loading resource table %s\n", ap.path.string());
580 ass = const_cast<AssetManager*>(this)->
581 openNonAssetInPathLocked("resources.arsc",
582 Asset::ACCESS_BUFFER,
583 ap);
584 if (ass != NULL && ass != kExcludedAsset) {
585 ass = const_cast<AssetManager*>(this)->
586 mZipSet.setZipResourceTableAsset(ap.path, ass);
587 }
588 }
589
590 if (nextEntryIdx == 0 && ass != NULL) {
591 // If this is the first resource table in the asset
592 // manager, then we are going to cache it so that we
593 // can quickly copy it out for others.
594 ALOGV("Creating shared resources for %s", ap.path.string());
595 sharedRes = new ResTable();
596 sharedRes->add(ass, idmap, nextEntryIdx + 1, false);
Elliott Hughesba3fe562015-08-12 14:49:53 -0700597#ifdef __ANDROID__
Martin Kosiba7df36252014-01-16 16:25:56 +0000598 const char* data = getenv("ANDROID_DATA");
599 LOG_ALWAYS_FATAL_IF(data == NULL, "ANDROID_DATA not set");
600 String8 overlaysListPath(data);
601 overlaysListPath.appendPath(kResourceCache);
602 overlaysListPath.appendPath("overlays.list");
603 addSystemOverlays(overlaysListPath.string(), ap.path, sharedRes, nextEntryIdx);
604#endif
605 sharedRes = const_cast<AssetManager*>(this)->
606 mZipSet.setZipResourceTable(ap.path, sharedRes);
607 }
608 }
609 } else {
610 ALOGV("loading resource table %s\n", ap.path.string());
611 ass = const_cast<AssetManager*>(this)->
612 openNonAssetInPathLocked("resources.arsc",
613 Asset::ACCESS_BUFFER,
614 ap);
615 shared = false;
616 }
617
618 if ((ass != NULL || sharedRes != NULL) && ass != kExcludedAsset) {
619 ALOGV("Installing resource asset %p in to table %p\n", ass, mResources);
620 if (sharedRes != NULL) {
621 ALOGV("Copying existing resources for %s", ap.path.string());
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800622 mResources->add(sharedRes, ap.isSystemAsset);
Martin Kosiba7df36252014-01-16 16:25:56 +0000623 } else {
624 ALOGV("Parsing resources for %s", ap.path.string());
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800625 mResources->add(ass, idmap, nextEntryIdx + 1, !shared, appAsLib, ap.isSystemAsset);
Martin Kosiba7df36252014-01-16 16:25:56 +0000626 }
627 onlyEmptyResources = false;
628
629 if (!shared) {
630 delete ass;
631 }
632 } else {
633 ALOGV("Installing empty resources in to table %p\n", mResources);
634 mResources->addEmpty(nextEntryIdx + 1);
635 }
636
637 if (idmap != NULL) {
638 delete idmap;
639 }
Martin Kosiba7df36252014-01-16 16:25:56 +0000640 return onlyEmptyResources;
641}
642
Adam Lesinski16c4d152014-01-24 13:27:13 -0800643const ResTable* AssetManager::getResTable(bool required) const
644{
645 ResTable* rt = mResources;
646 if (rt) {
647 return rt;
648 }
649
650 // Iterate through all asset packages, collecting resources from each.
651
652 AutoMutex _l(mLock);
653
654 if (mResources != NULL) {
655 return mResources;
656 }
657
658 if (required) {
659 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
660 }
661
Adam Lesinskide898ff2014-01-29 18:20:45 -0800662 if (mCacheMode != CACHE_OFF && !mCacheValid) {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800663 const_cast<AssetManager*>(this)->loadFileNameCacheLocked();
Adam Lesinskide898ff2014-01-29 18:20:45 -0800664 }
Adam Lesinski16c4d152014-01-24 13:27:13 -0800665
Adam Lesinskide898ff2014-01-29 18:20:45 -0800666 mResources = new ResTable();
667 updateResourceParamsLocked();
668
669 bool onlyEmptyResources = true;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800670 const size_t N = mAssetPaths.size();
671 for (size_t i=0; i<N; i++) {
Martin Kosiba7df36252014-01-16 16:25:56 +0000672 bool empty = appendPathToResTable(mAssetPaths.itemAt(i));
673 onlyEmptyResources = onlyEmptyResources && empty;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800674 }
675
Adam Lesinskide898ff2014-01-29 18:20:45 -0800676 if (required && onlyEmptyResources) {
677 ALOGW("Unable to find resources file resources.arsc");
678 delete mResources;
679 mResources = NULL;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800680 }
Adam Lesinskide898ff2014-01-29 18:20:45 -0800681
682 return mResources;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800683}
684
685void AssetManager::updateResourceParamsLocked() const
686{
Adam Lesinskib7e1ce02016-04-11 20:03:01 -0700687 ATRACE_CALL();
Adam Lesinski16c4d152014-01-24 13:27:13 -0800688 ResTable* res = mResources;
689 if (!res) {
690 return;
691 }
692
Narayan Kamath91447d82014-01-21 15:32:36 +0000693 if (mLocale) {
694 mConfig->setBcp47Locale(mLocale);
695 } else {
696 mConfig->clearLocale();
Adam Lesinski16c4d152014-01-24 13:27:13 -0800697 }
Adam Lesinski16c4d152014-01-24 13:27:13 -0800698
699 res->setParameters(mConfig);
700}
701
702Asset* AssetManager::openIdmapLocked(const struct asset_path& ap) const
703{
704 Asset* ass = NULL;
705 if (ap.idmap.size() != 0) {
706 ass = const_cast<AssetManager*>(this)->
707 openAssetFromFileLocked(ap.idmap, Asset::ACCESS_BUFFER);
708 if (ass) {
709 ALOGV("loading idmap %s\n", ap.idmap.string());
710 } else {
711 ALOGW("failed to load idmap %s\n", ap.idmap.string());
712 }
713 }
714 return ass;
715}
716
Mårten Kongstad48d22322014-01-31 14:43:27 +0100717void AssetManager::addSystemOverlays(const char* pathOverlaysList,
718 const String8& targetPackagePath, ResTable* sharedRes, size_t offset) const
719{
720 FILE* fin = fopen(pathOverlaysList, "r");
721 if (fin == NULL) {
722 return;
723 }
724
725 char buf[1024];
726 while (fgets(buf, sizeof(buf), fin)) {
727 // format of each line:
728 // <path to apk><space><path to idmap><newline>
729 char* space = strchr(buf, ' ');
730 char* newline = strchr(buf, '\n');
731 asset_path oap;
732
733 if (space == NULL || newline == NULL || newline < space) {
734 continue;
735 }
736
737 oap.path = String8(buf, space - buf);
738 oap.type = kFileTypeRegular;
739 oap.idmap = String8(space + 1, newline - space - 1);
Mårten Kongstadcb7b63d2014-11-07 10:57:15 +0100740 oap.isSystemOverlay = true;
Mårten Kongstad48d22322014-01-31 14:43:27 +0100741
742 Asset* oass = const_cast<AssetManager*>(this)->
743 openNonAssetInPathLocked("resources.arsc",
744 Asset::ACCESS_BUFFER,
745 oap);
746
747 if (oass != NULL) {
748 Asset* oidmap = openIdmapLocked(oap);
749 offset++;
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700750 sharedRes->add(oass, oidmap, offset + 1, false);
Mårten Kongstad48d22322014-01-31 14:43:27 +0100751 const_cast<AssetManager*>(this)->mAssetPaths.add(oap);
752 const_cast<AssetManager*>(this)->mZipSet.addOverlay(targetPackagePath, oap);
753 }
754 }
755 fclose(fin);
756}
757
Adam Lesinski16c4d152014-01-24 13:27:13 -0800758const ResTable& AssetManager::getResources(bool required) const
759{
760 const ResTable* rt = getResTable(required);
761 return *rt;
762}
763
764bool AssetManager::isUpToDate()
765{
766 AutoMutex _l(mLock);
767 return mZipSet.isUpToDate();
768}
769
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800770void AssetManager::getLocales(Vector<String8>* locales, bool includeSystemLocales) const
Adam Lesinski16c4d152014-01-24 13:27:13 -0800771{
772 ResTable* res = mResources;
773 if (res != NULL) {
Roozbeh Pournader7e5f96f2016-06-13 18:10:49 -0700774 res->getLocales(locales, includeSystemLocales, true /* mergeEquivalentLangs */);
Narayan Kamathe4345db2014-06-26 16:01:28 +0100775 }
Adam Lesinski16c4d152014-01-24 13:27:13 -0800776}
777
778/*
779 * Open a non-asset file as if it were an asset, searching for it in the
780 * specified app.
781 *
782 * Pass in a NULL values for "appName" if the common app directory should
783 * be used.
784 */
785Asset* AssetManager::openNonAssetInPathLocked(const char* fileName, AccessMode mode,
786 const asset_path& ap)
787{
788 Asset* pAsset = NULL;
789
790 /* look at the filesystem on disk */
791 if (ap.type == kFileTypeDirectory) {
792 String8 path(ap.path);
793 path.appendPath(fileName);
794
795 pAsset = openAssetFromFileLocked(path, mode);
796
797 if (pAsset == NULL) {
798 /* try again, this time with ".gz" */
799 path.append(".gz");
800 pAsset = openAssetFromFileLocked(path, mode);
801 }
802
803 if (pAsset != NULL) {
804 //printf("FOUND NA '%s' on disk\n", fileName);
805 pAsset->setAssetSource(path);
806 }
807
808 /* look inside the zip file */
809 } else {
810 String8 path(fileName);
811
812 /* check the appropriate Zip file */
Narayan Kamath560566d2013-12-03 13:16:03 +0000813 ZipFileRO* pZip = getZipFileLocked(ap);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800814 if (pZip != NULL) {
815 //printf("GOT zip, checking NA '%s'\n", (const char*) path);
Narayan Kamath560566d2013-12-03 13:16:03 +0000816 ZipEntryRO entry = pZip->findEntryByName(path.string());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800817 if (entry != NULL) {
818 //printf("FOUND NA in Zip file for %s\n", appName ? appName : kAppCommon);
819 pAsset = openAssetFromZipLocked(pZip, entry, mode, path);
Narayan Kamath560566d2013-12-03 13:16:03 +0000820 pZip->releaseEntry(entry);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800821 }
822 }
823
824 if (pAsset != NULL) {
825 /* create a "source" name, for debug/display */
826 pAsset->setAssetSource(
827 createZipSourceNameLocked(ZipSet::getPathName(ap.path.string()), String8(""),
828 String8(fileName)));
829 }
830 }
831
832 return pAsset;
833}
834
835/*
836 * Open an asset, searching for it in the directory hierarchy for the
837 * specified app.
838 *
839 * Pass in a NULL values for "appName" if the common app directory should
840 * be used.
841 */
842Asset* AssetManager::openInPathLocked(const char* fileName, AccessMode mode,
843 const asset_path& ap)
844{
845 Asset* pAsset = NULL;
846
847 /*
848 * Try various combinations of locale and vendor.
849 */
850 if (mLocale != NULL && mVendor != NULL)
851 pAsset = openInLocaleVendorLocked(fileName, mode, ap, mLocale, mVendor);
852 if (pAsset == NULL && mVendor != NULL)
853 pAsset = openInLocaleVendorLocked(fileName, mode, ap, NULL, mVendor);
854 if (pAsset == NULL && mLocale != NULL)
855 pAsset = openInLocaleVendorLocked(fileName, mode, ap, mLocale, NULL);
856 if (pAsset == NULL)
857 pAsset = openInLocaleVendorLocked(fileName, mode, ap, NULL, NULL);
858
859 return pAsset;
860}
861
862/*
863 * Open an asset, searching for it in the directory hierarchy for the
864 * specified locale and vendor.
865 *
866 * We also search in "app.jar".
867 *
868 * Pass in NULL values for "appName", "locale", and "vendor" if the
869 * defaults should be used.
870 */
871Asset* AssetManager::openInLocaleVendorLocked(const char* fileName, AccessMode mode,
872 const asset_path& ap, const char* locale, const char* vendor)
873{
874 Asset* pAsset = NULL;
875
876 if (ap.type == kFileTypeDirectory) {
877 if (mCacheMode == CACHE_OFF) {
878 /* look at the filesystem on disk */
879 String8 path(createPathNameLocked(ap, locale, vendor));
880 path.appendPath(fileName);
881
882 String8 excludeName(path);
883 excludeName.append(kExcludeExtension);
884 if (::getFileType(excludeName.string()) != kFileTypeNonexistent) {
885 /* say no more */
886 //printf("+++ excluding '%s'\n", (const char*) excludeName);
887 return kExcludedAsset;
888 }
889
890 pAsset = openAssetFromFileLocked(path, mode);
891
892 if (pAsset == NULL) {
893 /* try again, this time with ".gz" */
894 path.append(".gz");
895 pAsset = openAssetFromFileLocked(path, mode);
896 }
897
898 if (pAsset != NULL)
899 pAsset->setAssetSource(path);
900 } else {
901 /* find in cache */
902 String8 path(createPathNameLocked(ap, locale, vendor));
903 path.appendPath(fileName);
904
905 AssetDir::FileInfo tmpInfo;
906 bool found = false;
907
908 String8 excludeName(path);
909 excludeName.append(kExcludeExtension);
910
911 if (mCache.indexOf(excludeName) != NAME_NOT_FOUND) {
912 /* go no farther */
913 //printf("+++ Excluding '%s'\n", (const char*) excludeName);
914 return kExcludedAsset;
915 }
916
917 /*
918 * File compression extensions (".gz") don't get stored in the
919 * name cache, so we have to try both here.
920 */
921 if (mCache.indexOf(path) != NAME_NOT_FOUND) {
922 found = true;
923 pAsset = openAssetFromFileLocked(path, mode);
924 if (pAsset == NULL) {
925 /* try again, this time with ".gz" */
926 path.append(".gz");
927 pAsset = openAssetFromFileLocked(path, mode);
928 }
929 }
930
931 if (pAsset != NULL)
932 pAsset->setAssetSource(path);
933
934 /*
935 * Don't continue the search into the Zip files. Our cached info
936 * said it was a file on disk; to be consistent with openDir()
937 * we want to return the loose asset. If the cached file gets
938 * removed, we fail.
939 *
940 * The alternative is to update our cache when files get deleted,
941 * or make some sort of "best effort" promise, but for now I'm
942 * taking the hard line.
943 */
944 if (found) {
945 if (pAsset == NULL)
946 ALOGD("Expected file not found: '%s'\n", path.string());
947 return pAsset;
948 }
949 }
950 }
951
952 /*
953 * Either it wasn't found on disk or on the cached view of the disk.
954 * Dig through the currently-opened set of Zip files. If caching
955 * is disabled, the Zip file may get reopened.
956 */
957 if (pAsset == NULL && ap.type == kFileTypeRegular) {
958 String8 path;
959
960 path.appendPath((locale != NULL) ? locale : kDefaultLocale);
961 path.appendPath((vendor != NULL) ? vendor : kDefaultVendor);
962 path.appendPath(fileName);
963
964 /* check the appropriate Zip file */
Narayan Kamath560566d2013-12-03 13:16:03 +0000965 ZipFileRO* pZip = getZipFileLocked(ap);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800966 if (pZip != NULL) {
967 //printf("GOT zip, checking '%s'\n", (const char*) path);
Narayan Kamath560566d2013-12-03 13:16:03 +0000968 ZipEntryRO entry = pZip->findEntryByName(path.string());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800969 if (entry != NULL) {
970 //printf("FOUND in Zip file for %s/%s-%s\n",
971 // appName, locale, vendor);
972 pAsset = openAssetFromZipLocked(pZip, entry, mode, path);
Narayan Kamath560566d2013-12-03 13:16:03 +0000973 pZip->releaseEntry(entry);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800974 }
975 }
976
977 if (pAsset != NULL) {
978 /* create a "source" name, for debug/display */
979 pAsset->setAssetSource(createZipSourceNameLocked(ZipSet::getPathName(ap.path.string()),
980 String8(""), String8(fileName)));
981 }
982 }
983
984 return pAsset;
985}
986
987/*
988 * Create a "source name" for a file from a Zip archive.
989 */
990String8 AssetManager::createZipSourceNameLocked(const String8& zipFileName,
991 const String8& dirName, const String8& fileName)
992{
993 String8 sourceName("zip:");
994 sourceName.append(zipFileName);
995 sourceName.append(":");
996 if (dirName.length() > 0) {
997 sourceName.appendPath(dirName);
998 }
999 sourceName.appendPath(fileName);
1000 return sourceName;
1001}
1002
1003/*
1004 * Create a path to a loose asset (asset-base/app/locale/vendor).
1005 */
1006String8 AssetManager::createPathNameLocked(const asset_path& ap, const char* locale,
1007 const char* vendor)
1008{
1009 String8 path(ap.path);
1010 path.appendPath((locale != NULL) ? locale : kDefaultLocale);
1011 path.appendPath((vendor != NULL) ? vendor : kDefaultVendor);
1012 return path;
1013}
1014
1015/*
1016 * Create a path to a loose asset (asset-base/app/rootDir).
1017 */
1018String8 AssetManager::createPathNameLocked(const asset_path& ap, const char* rootDir)
1019{
1020 String8 path(ap.path);
1021 if (rootDir != NULL) path.appendPath(rootDir);
1022 return path;
1023}
1024
1025/*
1026 * Return a pointer to one of our open Zip archives. Returns NULL if no
1027 * matching Zip file exists.
1028 *
1029 * Right now we have 2 possible Zip files (1 each in app/"common").
1030 *
1031 * If caching is set to CACHE_OFF, to get the expected behavior we
1032 * need to reopen the Zip file on every request. That would be silly
1033 * and expensive, so instead we just check the file modification date.
1034 *
1035 * Pass in NULL values for "appName", "locale", and "vendor" if the
1036 * generics should be used.
1037 */
1038ZipFileRO* AssetManager::getZipFileLocked(const asset_path& ap)
1039{
1040 ALOGV("getZipFileLocked() in %p\n", this);
1041
1042 return mZipSet.getZip(ap.path);
1043}
1044
1045/*
1046 * Try to open an asset from a file on disk.
1047 *
1048 * If the file is compressed with gzip, we seek to the start of the
1049 * deflated data and pass that in (just like we would for a Zip archive).
1050 *
1051 * For uncompressed data, we may already have an mmap()ed version sitting
1052 * around. If so, we want to hand that to the Asset instead.
1053 *
1054 * This returns NULL if the file doesn't exist, couldn't be opened, or
1055 * claims to be a ".gz" but isn't.
1056 */
1057Asset* AssetManager::openAssetFromFileLocked(const String8& pathName,
1058 AccessMode mode)
1059{
1060 Asset* pAsset = NULL;
1061
1062 if (strcasecmp(pathName.getPathExtension().string(), ".gz") == 0) {
1063 //printf("TRYING '%s'\n", (const char*) pathName);
1064 pAsset = Asset::createFromCompressedFile(pathName.string(), mode);
1065 } else {
1066 //printf("TRYING '%s'\n", (const char*) pathName);
1067 pAsset = Asset::createFromFile(pathName.string(), mode);
1068 }
1069
1070 return pAsset;
1071}
1072
1073/*
1074 * Given an entry in a Zip archive, create a new Asset object.
1075 *
1076 * If the entry is uncompressed, we may want to create or share a
1077 * slice of shared memory.
1078 */
1079Asset* AssetManager::openAssetFromZipLocked(const ZipFileRO* pZipFile,
1080 const ZipEntryRO entry, AccessMode mode, const String8& entryName)
1081{
1082 Asset* pAsset = NULL;
1083
1084 // TODO: look for previously-created shared memory slice?
Narayan Kamath407753c2015-06-16 12:02:57 +01001085 uint16_t method;
1086 uint32_t uncompressedLen;
Adam Lesinski16c4d152014-01-24 13:27:13 -08001087
1088 //printf("USING Zip '%s'\n", pEntry->getFileName());
1089
Adam Lesinski16c4d152014-01-24 13:27:13 -08001090 if (!pZipFile->getEntryInfo(entry, &method, &uncompressedLen, NULL, NULL,
1091 NULL, NULL))
1092 {
1093 ALOGW("getEntryInfo failed\n");
1094 return NULL;
1095 }
1096
1097 FileMap* dataMap = pZipFile->createEntryFileMap(entry);
1098 if (dataMap == NULL) {
1099 ALOGW("create map from entry failed\n");
1100 return NULL;
1101 }
1102
1103 if (method == ZipFileRO::kCompressStored) {
1104 pAsset = Asset::createFromUncompressedMap(dataMap, mode);
1105 ALOGV("Opened uncompressed entry %s in zip %s mode %d: %p", entryName.string(),
1106 dataMap->getFileName(), mode, pAsset);
1107 } else {
Narayan Kamath407753c2015-06-16 12:02:57 +01001108 pAsset = Asset::createFromCompressedMap(dataMap,
1109 static_cast<size_t>(uncompressedLen), mode);
Adam Lesinski16c4d152014-01-24 13:27:13 -08001110 ALOGV("Opened compressed entry %s in zip %s mode %d: %p", entryName.string(),
1111 dataMap->getFileName(), mode, pAsset);
1112 }
1113 if (pAsset == NULL) {
1114 /* unexpected */
1115 ALOGW("create from segment failed\n");
1116 }
1117
1118 return pAsset;
1119}
1120
1121
1122
1123/*
1124 * Open a directory in the asset namespace.
1125 *
1126 * An "asset directory" is simply the combination of all files in all
1127 * locations, with ".gz" stripped for loose files. With app, locale, and
1128 * vendor defined, we have 8 directories and 2 Zip archives to scan.
1129 *
1130 * Pass in "" for the root dir.
1131 */
1132AssetDir* AssetManager::openDir(const char* dirName)
1133{
1134 AutoMutex _l(mLock);
1135
1136 AssetDir* pDir = NULL;
1137 SortedVector<AssetDir::FileInfo>* pMergedInfo = NULL;
1138
1139 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
1140 assert(dirName != NULL);
1141
1142 //printf("+++ openDir(%s) in '%s'\n", dirName, (const char*) mAssetBase);
1143
1144 if (mCacheMode != CACHE_OFF && !mCacheValid)
1145 loadFileNameCacheLocked();
1146
1147 pDir = new AssetDir;
1148
1149 /*
1150 * Scan the various directories, merging what we find into a single
1151 * vector. We want to scan them in reverse priority order so that
1152 * the ".EXCLUDE" processing works correctly. Also, if we decide we
1153 * want to remember where the file is coming from, we'll get the right
1154 * version.
1155 *
1156 * We start with Zip archives, then do loose files.
1157 */
1158 pMergedInfo = new SortedVector<AssetDir::FileInfo>;
1159
1160 size_t i = mAssetPaths.size();
1161 while (i > 0) {
1162 i--;
1163 const asset_path& ap = mAssetPaths.itemAt(i);
1164 if (ap.type == kFileTypeRegular) {
1165 ALOGV("Adding directory %s from zip %s", dirName, ap.path.string());
1166 scanAndMergeZipLocked(pMergedInfo, ap, kAssetsRoot, dirName);
1167 } else {
1168 ALOGV("Adding directory %s from dir %s", dirName, ap.path.string());
1169 scanAndMergeDirLocked(pMergedInfo, ap, kAssetsRoot, dirName);
1170 }
1171 }
1172
1173#if 0
1174 printf("FILE LIST:\n");
1175 for (i = 0; i < (size_t) pMergedInfo->size(); i++) {
1176 printf(" %d: (%d) '%s'\n", i,
1177 pMergedInfo->itemAt(i).getFileType(),
1178 (const char*) pMergedInfo->itemAt(i).getFileName());
1179 }
1180#endif
1181
1182 pDir->setFileList(pMergedInfo);
1183 return pDir;
1184}
1185
1186/*
1187 * Open a directory in the non-asset namespace.
1188 *
1189 * An "asset directory" is simply the combination of all files in all
1190 * locations, with ".gz" stripped for loose files. With app, locale, and
1191 * vendor defined, we have 8 directories and 2 Zip archives to scan.
1192 *
1193 * Pass in "" for the root dir.
1194 */
Narayan Kamatha0c62602014-01-24 13:51:51 +00001195AssetDir* AssetManager::openNonAssetDir(const int32_t cookie, const char* dirName)
Adam Lesinski16c4d152014-01-24 13:27:13 -08001196{
1197 AutoMutex _l(mLock);
1198
1199 AssetDir* pDir = NULL;
1200 SortedVector<AssetDir::FileInfo>* pMergedInfo = NULL;
1201
1202 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
1203 assert(dirName != NULL);
1204
1205 //printf("+++ openDir(%s) in '%s'\n", dirName, (const char*) mAssetBase);
1206
1207 if (mCacheMode != CACHE_OFF && !mCacheValid)
1208 loadFileNameCacheLocked();
1209
1210 pDir = new AssetDir;
1211
1212 pMergedInfo = new SortedVector<AssetDir::FileInfo>;
1213
Narayan Kamatha0c62602014-01-24 13:51:51 +00001214 const size_t which = static_cast<size_t>(cookie) - 1;
Adam Lesinski16c4d152014-01-24 13:27:13 -08001215
1216 if (which < mAssetPaths.size()) {
1217 const asset_path& ap = mAssetPaths.itemAt(which);
1218 if (ap.type == kFileTypeRegular) {
1219 ALOGV("Adding directory %s from zip %s", dirName, ap.path.string());
1220 scanAndMergeZipLocked(pMergedInfo, ap, NULL, dirName);
1221 } else {
1222 ALOGV("Adding directory %s from dir %s", dirName, ap.path.string());
1223 scanAndMergeDirLocked(pMergedInfo, ap, NULL, dirName);
1224 }
1225 }
1226
1227#if 0
1228 printf("FILE LIST:\n");
1229 for (i = 0; i < (size_t) pMergedInfo->size(); i++) {
1230 printf(" %d: (%d) '%s'\n", i,
1231 pMergedInfo->itemAt(i).getFileType(),
1232 (const char*) pMergedInfo->itemAt(i).getFileName());
1233 }
1234#endif
1235
1236 pDir->setFileList(pMergedInfo);
1237 return pDir;
1238}
1239
1240/*
1241 * Scan the contents of the specified directory and merge them into the
1242 * "pMergedInfo" vector, removing previous entries if we find "exclude"
1243 * directives.
1244 *
1245 * Returns "false" if we found nothing to contribute.
1246 */
1247bool AssetManager::scanAndMergeDirLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
1248 const asset_path& ap, const char* rootDir, const char* dirName)
1249{
1250 SortedVector<AssetDir::FileInfo>* pContents;
1251 String8 path;
1252
1253 assert(pMergedInfo != NULL);
1254
1255 //printf("scanAndMergeDir: %s %s %s %s\n", appName, locale, vendor,dirName);
1256
1257 if (mCacheValid) {
1258 int i, start, count;
1259
1260 pContents = new SortedVector<AssetDir::FileInfo>;
1261
1262 /*
1263 * Get the basic partial path and find it in the cache. That's
1264 * the start point for the search.
1265 */
1266 path = createPathNameLocked(ap, rootDir);
1267 if (dirName[0] != '\0')
1268 path.appendPath(dirName);
1269
1270 start = mCache.indexOf(path);
1271 if (start == NAME_NOT_FOUND) {
1272 //printf("+++ not found in cache: dir '%s'\n", (const char*) path);
1273 delete pContents;
1274 return false;
1275 }
1276
1277 /*
1278 * The match string looks like "common/default/default/foo/bar/".
1279 * The '/' on the end ensures that we don't match on the directory
1280 * itself or on ".../foo/barfy/".
1281 */
1282 path.append("/");
1283
1284 count = mCache.size();
1285
1286 /*
1287 * Pick out the stuff in the current dir by examining the pathname.
1288 * It needs to match the partial pathname prefix, and not have a '/'
1289 * (fssep) anywhere after the prefix.
1290 */
1291 for (i = start+1; i < count; i++) {
1292 if (mCache[i].getFileName().length() > path.length() &&
1293 strncmp(mCache[i].getFileName().string(), path.string(), path.length()) == 0)
1294 {
1295 const char* name = mCache[i].getFileName().string();
1296 // XXX THIS IS BROKEN! Looks like we need to store the full
1297 // path prefix separately from the file path.
1298 if (strchr(name + path.length(), '/') == NULL) {
1299 /* grab it, reducing path to just the filename component */
1300 AssetDir::FileInfo tmp = mCache[i];
1301 tmp.setFileName(tmp.getFileName().getPathLeaf());
1302 pContents->add(tmp);
1303 }
1304 } else {
1305 /* no longer in the dir or its subdirs */
1306 break;
1307 }
1308
1309 }
1310 } else {
1311 path = createPathNameLocked(ap, rootDir);
1312 if (dirName[0] != '\0')
1313 path.appendPath(dirName);
1314 pContents = scanDirLocked(path);
1315 if (pContents == NULL)
1316 return false;
1317 }
1318
1319 // if we wanted to do an incremental cache fill, we would do it here
1320
1321 /*
1322 * Process "exclude" directives. If we find a filename that ends with
1323 * ".EXCLUDE", we look for a matching entry in the "merged" set, and
1324 * remove it if we find it. We also delete the "exclude" entry.
1325 */
1326 int i, count, exclExtLen;
1327
1328 count = pContents->size();
1329 exclExtLen = strlen(kExcludeExtension);
1330 for (i = 0; i < count; i++) {
1331 const char* name;
1332 int nameLen;
1333
1334 name = pContents->itemAt(i).getFileName().string();
1335 nameLen = strlen(name);
1336 if (nameLen > exclExtLen &&
1337 strcmp(name + (nameLen - exclExtLen), kExcludeExtension) == 0)
1338 {
1339 String8 match(name, nameLen - exclExtLen);
1340 int matchIdx;
1341
1342 matchIdx = AssetDir::FileInfo::findEntry(pMergedInfo, match);
1343 if (matchIdx > 0) {
1344 ALOGV("Excluding '%s' [%s]\n",
1345 pMergedInfo->itemAt(matchIdx).getFileName().string(),
1346 pMergedInfo->itemAt(matchIdx).getSourceName().string());
1347 pMergedInfo->removeAt(matchIdx);
1348 } else {
1349 //printf("+++ no match on '%s'\n", (const char*) match);
1350 }
1351
1352 ALOGD("HEY: size=%d removing %d\n", (int)pContents->size(), i);
1353 pContents->removeAt(i);
1354 i--; // adjust "for" loop
1355 count--; // and loop limit
1356 }
1357 }
1358
1359 mergeInfoLocked(pMergedInfo, pContents);
1360
1361 delete pContents;
1362
1363 return true;
1364}
1365
1366/*
1367 * Scan the contents of the specified directory, and stuff what we find
1368 * into a newly-allocated vector.
1369 *
1370 * Files ending in ".gz" will have their extensions removed.
1371 *
1372 * We should probably think about skipping files with "illegal" names,
1373 * e.g. illegal characters (/\:) or excessive length.
1374 *
1375 * Returns NULL if the specified directory doesn't exist.
1376 */
1377SortedVector<AssetDir::FileInfo>* AssetManager::scanDirLocked(const String8& path)
1378{
1379 SortedVector<AssetDir::FileInfo>* pContents = NULL;
1380 DIR* dir;
1381 struct dirent* entry;
1382 FileType fileType;
1383
1384 ALOGV("Scanning dir '%s'\n", path.string());
1385
1386 dir = opendir(path.string());
1387 if (dir == NULL)
1388 return NULL;
1389
1390 pContents = new SortedVector<AssetDir::FileInfo>;
1391
1392 while (1) {
1393 entry = readdir(dir);
1394 if (entry == NULL)
1395 break;
1396
1397 if (strcmp(entry->d_name, ".") == 0 ||
1398 strcmp(entry->d_name, "..") == 0)
1399 continue;
1400
1401#ifdef _DIRENT_HAVE_D_TYPE
1402 if (entry->d_type == DT_REG)
1403 fileType = kFileTypeRegular;
1404 else if (entry->d_type == DT_DIR)
1405 fileType = kFileTypeDirectory;
1406 else
1407 fileType = kFileTypeUnknown;
1408#else
1409 // stat the file
1410 fileType = ::getFileType(path.appendPathCopy(entry->d_name).string());
1411#endif
1412
1413 if (fileType != kFileTypeRegular && fileType != kFileTypeDirectory)
1414 continue;
1415
1416 AssetDir::FileInfo info;
1417 info.set(String8(entry->d_name), fileType);
1418 if (strcasecmp(info.getFileName().getPathExtension().string(), ".gz") == 0)
1419 info.setFileName(info.getFileName().getBasePath());
1420 info.setSourceName(path.appendPathCopy(info.getFileName()));
1421 pContents->add(info);
1422 }
1423
1424 closedir(dir);
1425 return pContents;
1426}
1427
1428/*
1429 * Scan the contents out of the specified Zip archive, and merge what we
1430 * find into "pMergedInfo". If the Zip archive in question doesn't exist,
1431 * we return immediately.
1432 *
1433 * Returns "false" if we found nothing to contribute.
1434 */
1435bool AssetManager::scanAndMergeZipLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
1436 const asset_path& ap, const char* rootDir, const char* baseDirName)
1437{
1438 ZipFileRO* pZip;
1439 Vector<String8> dirs;
1440 AssetDir::FileInfo info;
1441 SortedVector<AssetDir::FileInfo> contents;
1442 String8 sourceName, zipName, dirName;
1443
1444 pZip = mZipSet.getZip(ap.path);
1445 if (pZip == NULL) {
1446 ALOGW("Failure opening zip %s\n", ap.path.string());
1447 return false;
1448 }
1449
1450 zipName = ZipSet::getPathName(ap.path.string());
1451
1452 /* convert "sounds" to "rootDir/sounds" */
1453 if (rootDir != NULL) dirName = rootDir;
1454 dirName.appendPath(baseDirName);
1455
1456 /*
1457 * Scan through the list of files, looking for a match. The files in
1458 * the Zip table of contents are not in sorted order, so we have to
1459 * process the entire list. We're looking for a string that begins
1460 * with the characters in "dirName", is followed by a '/', and has no
1461 * subsequent '/' in the stuff that follows.
1462 *
1463 * What makes this especially fun is that directories are not stored
1464 * explicitly in Zip archives, so we have to infer them from context.
1465 * When we see "sounds/foo.wav" we have to leave a note to ourselves
1466 * to insert a directory called "sounds" into the list. We store
1467 * these in temporary vector so that we only return each one once.
1468 *
1469 * Name comparisons are case-sensitive to match UNIX filesystem
1470 * semantics.
1471 */
1472 int dirNameLen = dirName.length();
Narayan Kamath560566d2013-12-03 13:16:03 +00001473 void *iterationCookie;
Yusuke Sato05f648e2015-08-03 16:21:10 -07001474 if (!pZip->startIteration(&iterationCookie, dirName.string(), NULL)) {
Narayan Kamath560566d2013-12-03 13:16:03 +00001475 ALOGW("ZipFileRO::startIteration returned false");
1476 return false;
1477 }
1478
1479 ZipEntryRO entry;
1480 while ((entry = pZip->nextEntry(iterationCookie)) != NULL) {
Adam Lesinski16c4d152014-01-24 13:27:13 -08001481 char nameBuf[256];
1482
Adam Lesinski16c4d152014-01-24 13:27:13 -08001483 if (pZip->getEntryFileName(entry, nameBuf, sizeof(nameBuf)) != 0) {
1484 // TODO: fix this if we expect to have long names
1485 ALOGE("ARGH: name too long?\n");
1486 continue;
1487 }
1488 //printf("Comparing %s in %s?\n", nameBuf, dirName.string());
Yusuke Sato05f648e2015-08-03 16:21:10 -07001489 if (dirNameLen == 0 || nameBuf[dirNameLen] == '/')
Adam Lesinski16c4d152014-01-24 13:27:13 -08001490 {
1491 const char* cp;
1492 const char* nextSlash;
1493
1494 cp = nameBuf + dirNameLen;
1495 if (dirNameLen != 0)
1496 cp++; // advance past the '/'
1497
1498 nextSlash = strchr(cp, '/');
1499//xxx this may break if there are bare directory entries
1500 if (nextSlash == NULL) {
1501 /* this is a file in the requested directory */
1502
1503 info.set(String8(nameBuf).getPathLeaf(), kFileTypeRegular);
1504
1505 info.setSourceName(
1506 createZipSourceNameLocked(zipName, dirName, info.getFileName()));
1507
1508 contents.add(info);
1509 //printf("FOUND: file '%s'\n", info.getFileName().string());
1510 } else {
1511 /* this is a subdir; add it if we don't already have it*/
1512 String8 subdirName(cp, nextSlash - cp);
1513 size_t j;
1514 size_t N = dirs.size();
1515
1516 for (j = 0; j < N; j++) {
1517 if (subdirName == dirs[j]) {
1518 break;
1519 }
1520 }
1521 if (j == N) {
1522 dirs.add(subdirName);
1523 }
1524
1525 //printf("FOUND: dir '%s'\n", subdirName.string());
1526 }
1527 }
1528 }
1529
Narayan Kamath560566d2013-12-03 13:16:03 +00001530 pZip->endIteration(iterationCookie);
1531
Adam Lesinski16c4d152014-01-24 13:27:13 -08001532 /*
1533 * Add the set of unique directories.
1534 */
1535 for (int i = 0; i < (int) dirs.size(); i++) {
1536 info.set(dirs[i], kFileTypeDirectory);
1537 info.setSourceName(
1538 createZipSourceNameLocked(zipName, dirName, info.getFileName()));
1539 contents.add(info);
1540 }
1541
1542 mergeInfoLocked(pMergedInfo, &contents);
1543
1544 return true;
1545}
1546
1547
1548/*
1549 * Merge two vectors of FileInfo.
1550 *
1551 * The merged contents will be stuffed into *pMergedInfo.
1552 *
1553 * If an entry for a file exists in both "pMergedInfo" and "pContents",
1554 * we use the newer "pContents" entry.
1555 */
1556void AssetManager::mergeInfoLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
1557 const SortedVector<AssetDir::FileInfo>* pContents)
1558{
1559 /*
1560 * Merge what we found in this directory with what we found in
1561 * other places.
1562 *
1563 * Two basic approaches:
1564 * (1) Create a new array that holds the unique values of the two
1565 * arrays.
1566 * (2) Take the elements from pContents and shove them into pMergedInfo.
1567 *
1568 * Because these are vectors of complex objects, moving elements around
1569 * inside the vector requires constructing new objects and allocating
1570 * storage for members. With approach #1, we're always adding to the
1571 * end, whereas with #2 we could be inserting multiple elements at the
1572 * front of the vector. Approach #1 requires a full copy of the
1573 * contents of pMergedInfo, but approach #2 requires the same copy for
1574 * every insertion at the front of pMergedInfo.
1575 *
1576 * (We should probably use a SortedVector interface that allows us to
1577 * just stuff items in, trusting us to maintain the sort order.)
1578 */
1579 SortedVector<AssetDir::FileInfo>* pNewSorted;
1580 int mergeMax, contMax;
1581 int mergeIdx, contIdx;
1582
1583 pNewSorted = new SortedVector<AssetDir::FileInfo>;
1584 mergeMax = pMergedInfo->size();
1585 contMax = pContents->size();
1586 mergeIdx = contIdx = 0;
1587
1588 while (mergeIdx < mergeMax || contIdx < contMax) {
1589 if (mergeIdx == mergeMax) {
1590 /* hit end of "merge" list, copy rest of "contents" */
1591 pNewSorted->add(pContents->itemAt(contIdx));
1592 contIdx++;
1593 } else if (contIdx == contMax) {
1594 /* hit end of "cont" list, copy rest of "merge" */
1595 pNewSorted->add(pMergedInfo->itemAt(mergeIdx));
1596 mergeIdx++;
1597 } else if (pMergedInfo->itemAt(mergeIdx) == pContents->itemAt(contIdx))
1598 {
1599 /* items are identical, add newer and advance both indices */
1600 pNewSorted->add(pContents->itemAt(contIdx));
1601 mergeIdx++;
1602 contIdx++;
1603 } else if (pMergedInfo->itemAt(mergeIdx) < pContents->itemAt(contIdx))
1604 {
1605 /* "merge" is lower, add that one */
1606 pNewSorted->add(pMergedInfo->itemAt(mergeIdx));
1607 mergeIdx++;
1608 } else {
1609 /* "cont" is lower, add that one */
1610 assert(pContents->itemAt(contIdx) < pMergedInfo->itemAt(mergeIdx));
1611 pNewSorted->add(pContents->itemAt(contIdx));
1612 contIdx++;
1613 }
1614 }
1615
1616 /*
1617 * Overwrite the "merged" list with the new stuff.
1618 */
1619 *pMergedInfo = *pNewSorted;
1620 delete pNewSorted;
1621
1622#if 0 // for Vector, rather than SortedVector
1623 int i, j;
1624 for (i = pContents->size() -1; i >= 0; i--) {
1625 bool add = true;
1626
1627 for (j = pMergedInfo->size() -1; j >= 0; j--) {
1628 /* case-sensitive comparisons, to behave like UNIX fs */
1629 if (strcmp(pContents->itemAt(i).mFileName,
1630 pMergedInfo->itemAt(j).mFileName) == 0)
1631 {
1632 /* match, don't add this entry */
1633 add = false;
1634 break;
1635 }
1636 }
1637
1638 if (add)
1639 pMergedInfo->add(pContents->itemAt(i));
1640 }
1641#endif
1642}
1643
1644
1645/*
1646 * Load all files into the file name cache. We want to do this across
1647 * all combinations of { appname, locale, vendor }, performing a recursive
1648 * directory traversal.
1649 *
1650 * This is not the most efficient data structure. Also, gathering the
1651 * information as we needed it (file-by-file or directory-by-directory)
1652 * would be faster. However, on the actual device, 99% of the files will
1653 * live in Zip archives, so this list will be very small. The trouble
1654 * is that we have to check the "loose" files first, so it's important
1655 * that we don't beat the filesystem silly looking for files that aren't
1656 * there.
1657 *
1658 * Note on thread safety: this is the only function that causes updates
1659 * to mCache, and anybody who tries to use it will call here if !mCacheValid,
1660 * so we need to employ a mutex here.
1661 */
1662void AssetManager::loadFileNameCacheLocked(void)
1663{
1664 assert(!mCacheValid);
1665 assert(mCache.size() == 0);
1666
1667#ifdef DO_TIMINGS // need to link against -lrt for this now
1668 DurationTimer timer;
1669 timer.start();
1670#endif
1671
1672 fncScanLocked(&mCache, "");
1673
1674#ifdef DO_TIMINGS
1675 timer.stop();
1676 ALOGD("Cache scan took %.3fms\n",
1677 timer.durationUsecs() / 1000.0);
1678#endif
1679
1680#if 0
1681 int i;
1682 printf("CACHED FILE LIST (%d entries):\n", mCache.size());
1683 for (i = 0; i < (int) mCache.size(); i++) {
1684 printf(" %d: (%d) '%s'\n", i,
1685 mCache.itemAt(i).getFileType(),
1686 (const char*) mCache.itemAt(i).getFileName());
1687 }
1688#endif
1689
1690 mCacheValid = true;
1691}
1692
1693/*
1694 * Scan up to 8 versions of the specified directory.
1695 */
1696void AssetManager::fncScanLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
1697 const char* dirName)
1698{
1699 size_t i = mAssetPaths.size();
1700 while (i > 0) {
1701 i--;
1702 const asset_path& ap = mAssetPaths.itemAt(i);
1703 fncScanAndMergeDirLocked(pMergedInfo, ap, NULL, NULL, dirName);
1704 if (mLocale != NULL)
1705 fncScanAndMergeDirLocked(pMergedInfo, ap, mLocale, NULL, dirName);
1706 if (mVendor != NULL)
1707 fncScanAndMergeDirLocked(pMergedInfo, ap, NULL, mVendor, dirName);
1708 if (mLocale != NULL && mVendor != NULL)
1709 fncScanAndMergeDirLocked(pMergedInfo, ap, mLocale, mVendor, dirName);
1710 }
1711}
1712
1713/*
1714 * Recursively scan this directory and all subdirs.
1715 *
1716 * This is similar to scanAndMergeDir, but we don't remove the .EXCLUDE
1717 * files, and we prepend the extended partial path to the filenames.
1718 */
1719bool AssetManager::fncScanAndMergeDirLocked(
1720 SortedVector<AssetDir::FileInfo>* pMergedInfo,
1721 const asset_path& ap, const char* locale, const char* vendor,
1722 const char* dirName)
1723{
1724 SortedVector<AssetDir::FileInfo>* pContents;
1725 String8 partialPath;
1726 String8 fullPath;
1727
1728 // XXX This is broken -- the filename cache needs to hold the base
1729 // asset path separately from its filename.
1730
1731 partialPath = createPathNameLocked(ap, locale, vendor);
1732 if (dirName[0] != '\0') {
1733 partialPath.appendPath(dirName);
1734 }
1735
1736 fullPath = partialPath;
1737 pContents = scanDirLocked(fullPath);
1738 if (pContents == NULL) {
1739 return false; // directory did not exist
1740 }
1741
1742 /*
1743 * Scan all subdirectories of the current dir, merging what we find
1744 * into "pMergedInfo".
1745 */
1746 for (int i = 0; i < (int) pContents->size(); i++) {
1747 if (pContents->itemAt(i).getFileType() == kFileTypeDirectory) {
1748 String8 subdir(dirName);
1749 subdir.appendPath(pContents->itemAt(i).getFileName());
1750
1751 fncScanAndMergeDirLocked(pMergedInfo, ap, locale, vendor, subdir.string());
1752 }
1753 }
1754
1755 /*
1756 * To be consistent, we want entries for the root directory. If
1757 * we're the root, add one now.
1758 */
1759 if (dirName[0] == '\0') {
1760 AssetDir::FileInfo tmpInfo;
1761
1762 tmpInfo.set(String8(""), kFileTypeDirectory);
1763 tmpInfo.setSourceName(createPathNameLocked(ap, locale, vendor));
1764 pContents->add(tmpInfo);
1765 }
1766
1767 /*
1768 * We want to prepend the extended partial path to every entry in
1769 * "pContents". It's the same value for each entry, so this will
1770 * not change the sorting order of the vector contents.
1771 */
1772 for (int i = 0; i < (int) pContents->size(); i++) {
1773 const AssetDir::FileInfo& info = pContents->itemAt(i);
1774 pContents->editItemAt(i).setFileName(partialPath.appendPathCopy(info.getFileName()));
1775 }
1776
1777 mergeInfoLocked(pMergedInfo, pContents);
sean_lu7c57d232014-06-16 15:11:29 +08001778 delete pContents;
Adam Lesinski16c4d152014-01-24 13:27:13 -08001779 return true;
1780}
1781
1782/*
1783 * Trash the cache.
1784 */
1785void AssetManager::purgeFileNameCacheLocked(void)
1786{
1787 mCacheValid = false;
1788 mCache.clear();
1789}
1790
1791/*
1792 * ===========================================================================
1793 * AssetManager::SharedZip
1794 * ===========================================================================
1795 */
1796
1797
1798Mutex AssetManager::SharedZip::gLock;
1799DefaultKeyedVector<String8, wp<AssetManager::SharedZip> > AssetManager::SharedZip::gOpen;
1800
1801AssetManager::SharedZip::SharedZip(const String8& path, time_t modWhen)
1802 : mPath(path), mZipFile(NULL), mModWhen(modWhen),
1803 mResourceTableAsset(NULL), mResourceTable(NULL)
1804{
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001805 if (kIsDebug) {
1806 ALOGI("Creating SharedZip %p %s\n", this, (const char*)mPath);
1807 }
Adam Lesinski16c4d152014-01-24 13:27:13 -08001808 ALOGV("+++ opening zip '%s'\n", mPath.string());
Narayan Kamath560566d2013-12-03 13:16:03 +00001809 mZipFile = ZipFileRO::open(mPath.string());
1810 if (mZipFile == NULL) {
Adam Lesinski16c4d152014-01-24 13:27:13 -08001811 ALOGD("failed to open Zip archive '%s'\n", mPath.string());
Adam Lesinski16c4d152014-01-24 13:27:13 -08001812 }
1813}
1814
Mårten Kongstad48d22322014-01-31 14:43:27 +01001815sp<AssetManager::SharedZip> AssetManager::SharedZip::get(const String8& path,
1816 bool createIfNotPresent)
Adam Lesinski16c4d152014-01-24 13:27:13 -08001817{
1818 AutoMutex _l(gLock);
1819 time_t modWhen = getFileModDate(path);
1820 sp<SharedZip> zip = gOpen.valueFor(path).promote();
1821 if (zip != NULL && zip->mModWhen == modWhen) {
1822 return zip;
1823 }
Mårten Kongstad48d22322014-01-31 14:43:27 +01001824 if (zip == NULL && !createIfNotPresent) {
1825 return NULL;
1826 }
Adam Lesinski16c4d152014-01-24 13:27:13 -08001827 zip = new SharedZip(path, modWhen);
1828 gOpen.add(path, zip);
1829 return zip;
1830
1831}
1832
1833ZipFileRO* AssetManager::SharedZip::getZip()
1834{
1835 return mZipFile;
1836}
1837
1838Asset* AssetManager::SharedZip::getResourceTableAsset()
1839{
songjinshi49921f22016-09-08 15:24:30 +08001840 AutoMutex _l(gLock);
Adam Lesinski16c4d152014-01-24 13:27:13 -08001841 ALOGV("Getting from SharedZip %p resource asset %p\n", this, mResourceTableAsset);
1842 return mResourceTableAsset;
1843}
1844
1845Asset* AssetManager::SharedZip::setResourceTableAsset(Asset* asset)
1846{
1847 {
1848 AutoMutex _l(gLock);
1849 if (mResourceTableAsset == NULL) {
Adam Lesinski16c4d152014-01-24 13:27:13 -08001850 // This is not thread safe the first time it is called, so
1851 // do it here with the global lock held.
1852 asset->getBuffer(true);
songjinshi49921f22016-09-08 15:24:30 +08001853 mResourceTableAsset = asset;
Adam Lesinski16c4d152014-01-24 13:27:13 -08001854 return asset;
1855 }
1856 }
1857 delete asset;
1858 return mResourceTableAsset;
1859}
1860
1861ResTable* AssetManager::SharedZip::getResourceTable()
1862{
1863 ALOGV("Getting from SharedZip %p resource table %p\n", this, mResourceTable);
1864 return mResourceTable;
1865}
1866
1867ResTable* AssetManager::SharedZip::setResourceTable(ResTable* res)
1868{
1869 {
1870 AutoMutex _l(gLock);
1871 if (mResourceTable == NULL) {
1872 mResourceTable = res;
1873 return res;
1874 }
1875 }
1876 delete res;
1877 return mResourceTable;
1878}
1879
1880bool AssetManager::SharedZip::isUpToDate()
1881{
1882 time_t modWhen = getFileModDate(mPath.string());
1883 return mModWhen == modWhen;
1884}
1885
Mårten Kongstad48d22322014-01-31 14:43:27 +01001886void AssetManager::SharedZip::addOverlay(const asset_path& ap)
1887{
1888 mOverlays.add(ap);
1889}
1890
1891bool AssetManager::SharedZip::getOverlay(size_t idx, asset_path* out) const
1892{
1893 if (idx >= mOverlays.size()) {
1894 return false;
1895 }
1896 *out = mOverlays[idx];
1897 return true;
1898}
1899
Adam Lesinski16c4d152014-01-24 13:27:13 -08001900AssetManager::SharedZip::~SharedZip()
1901{
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001902 if (kIsDebug) {
1903 ALOGI("Destroying SharedZip %p %s\n", this, (const char*)mPath);
1904 }
Adam Lesinski16c4d152014-01-24 13:27:13 -08001905 if (mResourceTable != NULL) {
1906 delete mResourceTable;
1907 }
1908 if (mResourceTableAsset != NULL) {
1909 delete mResourceTableAsset;
1910 }
1911 if (mZipFile != NULL) {
1912 delete mZipFile;
1913 ALOGV("Closed '%s'\n", mPath.string());
1914 }
1915}
1916
1917/*
1918 * ===========================================================================
1919 * AssetManager::ZipSet
1920 * ===========================================================================
1921 */
1922
1923/*
1924 * Constructor.
1925 */
1926AssetManager::ZipSet::ZipSet(void)
1927{
1928}
1929
1930/*
1931 * Destructor. Close any open archives.
1932 */
1933AssetManager::ZipSet::~ZipSet(void)
1934{
1935 size_t N = mZipFile.size();
1936 for (size_t i = 0; i < N; i++)
1937 closeZip(i);
1938}
1939
1940/*
1941 * Close a Zip file and reset the entry.
1942 */
1943void AssetManager::ZipSet::closeZip(int idx)
1944{
1945 mZipFile.editItemAt(idx) = NULL;
1946}
1947
1948
1949/*
1950 * Retrieve the appropriate Zip file from the set.
1951 */
1952ZipFileRO* AssetManager::ZipSet::getZip(const String8& path)
1953{
1954 int idx = getIndex(path);
1955 sp<SharedZip> zip = mZipFile[idx];
1956 if (zip == NULL) {
1957 zip = SharedZip::get(path);
1958 mZipFile.editItemAt(idx) = zip;
1959 }
1960 return zip->getZip();
1961}
1962
1963Asset* AssetManager::ZipSet::getZipResourceTableAsset(const String8& path)
1964{
1965 int idx = getIndex(path);
1966 sp<SharedZip> zip = mZipFile[idx];
1967 if (zip == NULL) {
1968 zip = SharedZip::get(path);
1969 mZipFile.editItemAt(idx) = zip;
1970 }
1971 return zip->getResourceTableAsset();
1972}
1973
1974Asset* AssetManager::ZipSet::setZipResourceTableAsset(const String8& path,
1975 Asset* asset)
1976{
1977 int idx = getIndex(path);
1978 sp<SharedZip> zip = mZipFile[idx];
1979 // doesn't make sense to call before previously accessing.
1980 return zip->setResourceTableAsset(asset);
1981}
1982
1983ResTable* AssetManager::ZipSet::getZipResourceTable(const String8& path)
1984{
1985 int idx = getIndex(path);
1986 sp<SharedZip> zip = mZipFile[idx];
1987 if (zip == NULL) {
1988 zip = SharedZip::get(path);
1989 mZipFile.editItemAt(idx) = zip;
1990 }
1991 return zip->getResourceTable();
1992}
1993
1994ResTable* AssetManager::ZipSet::setZipResourceTable(const String8& path,
1995 ResTable* res)
1996{
1997 int idx = getIndex(path);
1998 sp<SharedZip> zip = mZipFile[idx];
1999 // doesn't make sense to call before previously accessing.
2000 return zip->setResourceTable(res);
2001}
2002
2003/*
2004 * Generate the partial pathname for the specified archive. The caller
2005 * gets to prepend the asset root directory.
2006 *
2007 * Returns something like "common/en-US-noogle.jar".
2008 */
2009/*static*/ String8 AssetManager::ZipSet::getPathName(const char* zipPath)
2010{
2011 return String8(zipPath);
2012}
2013
2014bool AssetManager::ZipSet::isUpToDate()
2015{
2016 const size_t N = mZipFile.size();
2017 for (size_t i=0; i<N; i++) {
2018 if (mZipFile[i] != NULL && !mZipFile[i]->isUpToDate()) {
2019 return false;
2020 }
2021 }
2022 return true;
2023}
2024
Mårten Kongstad48d22322014-01-31 14:43:27 +01002025void AssetManager::ZipSet::addOverlay(const String8& path, const asset_path& overlay)
2026{
2027 int idx = getIndex(path);
2028 sp<SharedZip> zip = mZipFile[idx];
2029 zip->addOverlay(overlay);
2030}
2031
2032bool AssetManager::ZipSet::getOverlay(const String8& path, size_t idx, asset_path* out) const
2033{
2034 sp<SharedZip> zip = SharedZip::get(path, false);
2035 if (zip == NULL) {
2036 return false;
2037 }
2038 return zip->getOverlay(idx, out);
2039}
2040
Adam Lesinski16c4d152014-01-24 13:27:13 -08002041/*
2042 * Compute the zip file's index.
2043 *
2044 * "appName", "locale", and "vendor" should be set to NULL to indicate the
2045 * default directory.
2046 */
2047int AssetManager::ZipSet::getIndex(const String8& zip) const
2048{
2049 const size_t N = mZipPath.size();
2050 for (size_t i=0; i<N; i++) {
2051 if (mZipPath[i] == zip) {
2052 return i;
2053 }
2054 }
2055
2056 mZipPath.add(zip);
2057 mZipFile.add(NULL);
2058
2059 return mZipPath.size()-1;
2060}