blob: acacd7654cf1baf3696966d0e2cef19bf1ed2ec9 [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>
Martin Wallgren0fbb6082015-08-11 15:10:31 +020038#ifndef _WIN32
39#include <sys/file.h>
40#endif
Adam Lesinski16c4d152014-01-24 13:27:13 -080041
42#include <assert.h>
43#include <dirent.h>
44#include <errno.h>
Mårten Kongstad48d22322014-01-31 14:43:27 +010045#include <string.h> // strerror
Adam Lesinski16c4d152014-01-24 13:27:13 -080046#include <strings.h>
Adam Lesinski16c4d152014-01-24 13:27:13 -080047
48#ifndef TEMP_FAILURE_RETRY
49/* Used to retry syscalls that can return EINTR. */
50#define TEMP_FAILURE_RETRY(exp) ({ \
51 typeof (exp) _rc; \
52 do { \
53 _rc = (exp); \
54 } while (_rc == -1 && errno == EINTR); \
55 _rc; })
56#endif
57
Adam Lesinski16c4d152014-01-24 13:27:13 -080058using namespace android;
59
Andreas Gampe2204f0b2014-10-21 23:04:54 -070060static const bool kIsDebug = false;
61
Adam Lesinski16c4d152014-01-24 13:27:13 -080062static const char* kAssetsRoot = "assets";
63static const char* kAppZipName = NULL; //"classes.jar";
64static const char* kSystemAssets = "framework/framework-res.apk";
Mårten Kongstad48d22322014-01-31 14:43:27 +010065static const char* kResourceCache = "resource-cache";
Adam Lesinski16c4d152014-01-24 13:27:13 -080066
67static const char* kExcludeExtension = ".EXCLUDE";
68
69static Asset* const kExcludedAsset = (Asset*) 0xd000000d;
70
71static volatile int32_t gCount = 0;
72
Mårten Kongstad65a05fd2014-01-31 14:01:52 +010073const char* AssetManager::RESOURCES_FILENAME = "resources.arsc";
Mårten Kongstad48d22322014-01-31 14:43:27 +010074const char* AssetManager::IDMAP_BIN = "/system/bin/idmap";
75const char* AssetManager::OVERLAY_DIR = "/vendor/overlay";
Jakub Adamek54dcaab2016-10-19 11:46:13 +010076const char* AssetManager::OVERLAY_THEME_DIR_PROPERTY = "ro.boot.vendor.overlay.theme";
Jason Monk2dc804be2016-11-07 11:17:02 -050077const char* AssetManager::OVERLAY_THEME_DIR_PERSIST_PROPERTY = "persist.vendor.overlay.theme";
Mårten Kongstad48d22322014-01-31 14:43:27 +010078const char* AssetManager::TARGET_PACKAGE_NAME = "android";
79const char* AssetManager::TARGET_APK_PATH = "/system/framework/framework-res.apk";
80const char* AssetManager::IDMAP_DIR = "/data/resource-cache";
Mårten Kongstad65a05fd2014-01-31 14:01:52 +010081
Adam Lesinski16c4d152014-01-24 13:27:13 -080082namespace {
Adam Lesinski16c4d152014-01-24 13:27:13 -080083
Adam Lesinskia77685f2016-10-03 16:26:28 -070084String8 idmapPathForPackagePath(const String8& pkgPath) {
85 const char* root = getenv("ANDROID_DATA");
86 LOG_ALWAYS_FATAL_IF(root == NULL, "ANDROID_DATA not set");
87 String8 path(root);
88 path.appendPath(kResourceCache);
Adam Lesinski16c4d152014-01-24 13:27:13 -080089
Adam Lesinskia77685f2016-10-03 16:26:28 -070090 char buf[256]; // 256 chars should be enough for anyone...
91 strncpy(buf, pkgPath.string(), 255);
92 buf[255] = '\0';
93 char* filename = buf;
94 while (*filename && *filename == '/') {
95 ++filename;
Adam Lesinski16c4d152014-01-24 13:27:13 -080096 }
Adam Lesinskia77685f2016-10-03 16:26:28 -070097 char* p = filename;
98 while (*p) {
99 if (*p == '/') {
100 *p = '@';
101 }
102 ++p;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800103 }
Adam Lesinskia77685f2016-10-03 16:26:28 -0700104 path.appendPath(filename);
105 path.append("@idmap");
106
107 return path;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800108}
109
110/*
Adam Lesinskia77685f2016-10-03 16:26:28 -0700111 * Like strdup(), but uses C++ "new" operator instead of malloc.
112 */
113static char* strdupNew(const char* str) {
114 char* newStr;
115 int len;
116
117 if (str == NULL)
118 return NULL;
119
120 len = strlen(str);
121 newStr = new char[len+1];
122 memcpy(newStr, str, len+1);
123
124 return newStr;
125}
126
127} // namespace
128
129/*
Adam Lesinski16c4d152014-01-24 13:27:13 -0800130 * ===========================================================================
131 * AssetManager
132 * ===========================================================================
133 */
134
Adam Lesinskia77685f2016-10-03 16:26:28 -0700135int32_t AssetManager::getGlobalCount() {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800136 return gCount;
137}
138
Adam Lesinskia77685f2016-10-03 16:26:28 -0700139AssetManager::AssetManager() :
140 mLocale(NULL), mResources(NULL), mConfig(new ResTable_config) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700141 int count = android_atomic_inc(&gCount) + 1;
142 if (kIsDebug) {
143 ALOGI("Creating AssetManager %p #%d\n", this, count);
144 }
Adam Lesinski16c4d152014-01-24 13:27:13 -0800145 memset(mConfig, 0, sizeof(ResTable_config));
146}
147
Adam Lesinskia77685f2016-10-03 16:26:28 -0700148AssetManager::~AssetManager() {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800149 int count = android_atomic_dec(&gCount);
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700150 if (kIsDebug) {
151 ALOGI("Destroying AssetManager in %p #%d\n", this, count);
152 }
Adam Lesinski16c4d152014-01-24 13:27:13 -0800153
154 delete mConfig;
155 delete mResources;
156
157 // don't have a String class yet, so make sure we clean up
158 delete[] mLocale;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800159}
160
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800161bool AssetManager::addAssetPath(
Adam Lesinskia77685f2016-10-03 16:26:28 -0700162 const String8& path, int32_t* cookie, bool appAsLib, bool isSystemAsset) {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800163 AutoMutex _l(mLock);
164
165 asset_path ap;
166
167 String8 realPath(path);
168 if (kAppZipName) {
169 realPath.appendPath(kAppZipName);
170 }
171 ap.type = ::getFileType(realPath.string());
172 if (ap.type == kFileTypeRegular) {
173 ap.path = realPath;
174 } else {
175 ap.path = path;
176 ap.type = ::getFileType(path.string());
177 if (ap.type != kFileTypeDirectory && ap.type != kFileTypeRegular) {
178 ALOGW("Asset path %s is neither a directory nor file (type=%d).",
179 path.string(), (int)ap.type);
180 return false;
181 }
182 }
183
184 // Skip if we have it already.
185 for (size_t i=0; i<mAssetPaths.size(); i++) {
186 if (mAssetPaths[i].path == ap.path) {
187 if (cookie) {
Narayan Kamatha0c62602014-01-24 13:51:51 +0000188 *cookie = static_cast<int32_t>(i+1);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800189 }
190 return true;
191 }
192 }
193
194 ALOGV("In %p Asset %s path: %s", this,
195 ap.type == kFileTypeDirectory ? "dir" : "zip", ap.path.string());
196
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800197 ap.isSystemAsset = isSystemAsset;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800198 mAssetPaths.add(ap);
199
200 // new paths are always added at the end
201 if (cookie) {
Narayan Kamatha0c62602014-01-24 13:51:51 +0000202 *cookie = static_cast<int32_t>(mAssetPaths.size());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800203 }
204
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +0900205#ifdef __ANDROID__
206 // Load overlays, if any
207 asset_path oap;
208 for (size_t idx = 0; mZipSet.getOverlay(ap.path, idx, &oap); idx++) {
209 oap.isSystemAsset = isSystemAsset;
210 mAssetPaths.add(oap);
211 }
212#endif
213
Martin Kosiba7df36252014-01-16 16:25:56 +0000214 if (mResources != NULL) {
Tao Baia6d7e3f2015-09-01 18:49:54 -0700215 appendPathToResTable(ap, appAsLib);
Martin Kosiba7df36252014-01-16 16:25:56 +0000216 }
217
Adam Lesinski16c4d152014-01-24 13:27:13 -0800218 return true;
219}
220
Mårten Kongstad48d22322014-01-31 14:43:27 +0100221bool AssetManager::addOverlayPath(const String8& packagePath, int32_t* cookie)
222{
223 const String8 idmapPath = idmapPathForPackagePath(packagePath);
224
225 AutoMutex _l(mLock);
226
227 for (size_t i = 0; i < mAssetPaths.size(); ++i) {
228 if (mAssetPaths[i].idmap == idmapPath) {
229 *cookie = static_cast<int32_t>(i + 1);
230 return true;
231 }
232 }
233
234 Asset* idmap = NULL;
235 if ((idmap = openAssetFromFileLocked(idmapPath, Asset::ACCESS_BUFFER)) == NULL) {
236 ALOGW("failed to open idmap file %s\n", idmapPath.string());
237 return false;
238 }
239
240 String8 targetPath;
241 String8 overlayPath;
242 if (!ResTable::getIdmapInfo(idmap->getBuffer(false), idmap->getLength(),
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700243 NULL, NULL, NULL, &targetPath, &overlayPath)) {
Mårten Kongstad48d22322014-01-31 14:43:27 +0100244 ALOGW("failed to read idmap file %s\n", idmapPath.string());
245 delete idmap;
246 return false;
247 }
248 delete idmap;
249
250 if (overlayPath != packagePath) {
251 ALOGW("idmap file %s inconcistent: expected path %s does not match actual path %s\n",
252 idmapPath.string(), packagePath.string(), overlayPath.string());
253 return false;
254 }
255 if (access(targetPath.string(), R_OK) != 0) {
256 ALOGW("failed to access file %s: %s\n", targetPath.string(), strerror(errno));
257 return false;
258 }
259 if (access(idmapPath.string(), R_OK) != 0) {
260 ALOGW("failed to access file %s: %s\n", idmapPath.string(), strerror(errno));
261 return false;
262 }
263 if (access(overlayPath.string(), R_OK) != 0) {
264 ALOGW("failed to access file %s: %s\n", overlayPath.string(), strerror(errno));
265 return false;
266 }
267
268 asset_path oap;
269 oap.path = overlayPath;
270 oap.type = ::getFileType(overlayPath.string());
271 oap.idmap = idmapPath;
272#if 0
273 ALOGD("Overlay added: targetPath=%s overlayPath=%s idmapPath=%s\n",
274 targetPath.string(), overlayPath.string(), idmapPath.string());
275#endif
276 mAssetPaths.add(oap);
277 *cookie = static_cast<int32_t>(mAssetPaths.size());
278
Mårten Kongstad30113132014-11-07 10:52:17 +0100279 if (mResources != NULL) {
280 appendPathToResTable(oap);
281 }
282
Mårten Kongstad48d22322014-01-31 14:43:27 +0100283 return true;
284 }
285
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100286bool AssetManager::createIdmap(const char* targetApkPath, const char* overlayApkPath,
Dianne Hackborn32bb5fa2014-02-11 13:56:21 -0800287 uint32_t targetCrc, uint32_t overlayCrc, uint32_t** outData, size_t* outSize)
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100288{
289 AutoMutex _l(mLock);
290 const String8 paths[2] = { String8(targetApkPath), String8(overlayApkPath) };
Mårten Kongstad6bb13da2016-06-02 09:34:36 +0200291 Asset* assets[2] = {NULL, NULL};
292 bool ret = false;
293 {
294 ResTable tables[2];
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100295
Mårten Kongstad6bb13da2016-06-02 09:34:36 +0200296 for (int i = 0; i < 2; ++i) {
297 asset_path ap;
298 ap.type = kFileTypeRegular;
299 ap.path = paths[i];
300 assets[i] = openNonAssetInPathLocked("resources.arsc",
301 Asset::ACCESS_BUFFER, ap);
302 if (assets[i] == NULL) {
303 ALOGW("failed to find resources.arsc in %s\n", ap.path.string());
304 goto exit;
305 }
306 if (tables[i].add(assets[i]) != NO_ERROR) {
307 ALOGW("failed to add %s to resource table", paths[i].string());
308 goto exit;
309 }
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100310 }
Mårten Kongstad6bb13da2016-06-02 09:34:36 +0200311 ret = tables[0].createIdmap(tables[1], targetCrc, overlayCrc,
312 targetApkPath, overlayApkPath, (void**)outData, outSize) == NO_ERROR;
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100313 }
314
Mårten Kongstad6bb13da2016-06-02 09:34:36 +0200315exit:
316 delete assets[0];
317 delete assets[1];
318 return ret;
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100319}
320
Adam Lesinski16c4d152014-01-24 13:27:13 -0800321bool AssetManager::addDefaultAssets()
322{
323 const char* root = getenv("ANDROID_ROOT");
324 LOG_ALWAYS_FATAL_IF(root == NULL, "ANDROID_ROOT not set");
325
326 String8 path(root);
327 path.appendPath(kSystemAssets);
328
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800329 return addAssetPath(path, NULL, false /* appAsLib */, true /* isSystemAsset */);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800330}
331
Narayan Kamatha0c62602014-01-24 13:51:51 +0000332int32_t AssetManager::nextAssetPath(const int32_t cookie) const
Adam Lesinski16c4d152014-01-24 13:27:13 -0800333{
334 AutoMutex _l(mLock);
Narayan Kamatha0c62602014-01-24 13:51:51 +0000335 const size_t next = static_cast<size_t>(cookie) + 1;
336 return next > mAssetPaths.size() ? -1 : next;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800337}
338
Narayan Kamatha0c62602014-01-24 13:51:51 +0000339String8 AssetManager::getAssetPath(const int32_t cookie) const
Adam Lesinski16c4d152014-01-24 13:27:13 -0800340{
341 AutoMutex _l(mLock);
Narayan Kamatha0c62602014-01-24 13:51:51 +0000342 const size_t which = static_cast<size_t>(cookie) - 1;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800343 if (which < mAssetPaths.size()) {
344 return mAssetPaths[which].path;
345 }
346 return String8();
347}
348
Adam Lesinski16c4d152014-01-24 13:27:13 -0800349void AssetManager::setLocaleLocked(const char* locale)
350{
351 if (mLocale != NULL) {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800352 delete[] mLocale;
353 }
Elliott Hughesc367d482013-10-29 13:12:55 -0700354
Adam Lesinski16c4d152014-01-24 13:27:13 -0800355 mLocale = strdupNew(locale);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800356 updateResourceParamsLocked();
357}
358
Adam Lesinski16c4d152014-01-24 13:27:13 -0800359void AssetManager::setConfiguration(const ResTable_config& config, const char* locale)
360{
361 AutoMutex _l(mLock);
362 *mConfig = config;
363 if (locale) {
364 setLocaleLocked(locale);
365 } else if (config.language[0] != 0) {
Narayan Kamath91447d82014-01-21 15:32:36 +0000366 char spec[RESTABLE_MAX_LOCALE_LEN];
367 config.getBcp47Locale(spec);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800368 setLocaleLocked(spec);
369 } else {
370 updateResourceParamsLocked();
371 }
372}
373
374void AssetManager::getConfiguration(ResTable_config* outConfig) const
375{
376 AutoMutex _l(mLock);
377 *outConfig = *mConfig;
378}
379
380/*
381 * Open an asset.
382 *
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700383 * The data could be in any asset path. Each asset path could be:
384 * - A directory on disk.
385 * - A Zip archive, uncompressed or compressed.
Adam Lesinski16c4d152014-01-24 13:27:13 -0800386 *
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700387 * If the file is in a directory, it could have a .gz suffix, meaning it is compressed.
Adam Lesinski16c4d152014-01-24 13:27:13 -0800388 *
389 * We should probably reject requests for "illegal" filenames, e.g. those
390 * with illegal characters or "../" backward relative paths.
391 */
392Asset* AssetManager::open(const char* fileName, AccessMode mode)
393{
394 AutoMutex _l(mLock);
395
396 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
397
Adam Lesinski16c4d152014-01-24 13:27:13 -0800398 String8 assetName(kAssetsRoot);
399 assetName.appendPath(fileName);
400
401 /*
402 * For each top-level asset path, search for the asset.
403 */
404
405 size_t i = mAssetPaths.size();
406 while (i > 0) {
407 i--;
408 ALOGV("Looking for asset '%s' in '%s'\n",
409 assetName.string(), mAssetPaths.itemAt(i).path.string());
410 Asset* pAsset = openNonAssetInPathLocked(assetName.string(), mode, mAssetPaths.itemAt(i));
411 if (pAsset != NULL) {
412 return pAsset != kExcludedAsset ? pAsset : NULL;
413 }
414 }
415
416 return NULL;
417}
418
419/*
420 * Open a non-asset file as if it were an asset.
421 *
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700422 * The "fileName" is the partial path starting from the application name.
Adam Lesinski16c4d152014-01-24 13:27:13 -0800423 */
Adam Lesinskide898ff2014-01-29 18:20:45 -0800424Asset* AssetManager::openNonAsset(const char* fileName, AccessMode mode, int32_t* outCookie)
Adam Lesinski16c4d152014-01-24 13:27:13 -0800425{
426 AutoMutex _l(mLock);
427
428 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
429
Adam Lesinski16c4d152014-01-24 13:27:13 -0800430 /*
431 * For each top-level asset path, search for the asset.
432 */
433
434 size_t i = mAssetPaths.size();
435 while (i > 0) {
436 i--;
437 ALOGV("Looking for non-asset '%s' in '%s'\n", fileName, mAssetPaths.itemAt(i).path.string());
438 Asset* pAsset = openNonAssetInPathLocked(
439 fileName, mode, mAssetPaths.itemAt(i));
440 if (pAsset != NULL) {
Adam Lesinskide898ff2014-01-29 18:20:45 -0800441 if (outCookie != NULL) *outCookie = static_cast<int32_t>(i + 1);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800442 return pAsset != kExcludedAsset ? pAsset : NULL;
443 }
444 }
445
446 return NULL;
447}
448
Narayan Kamatha0c62602014-01-24 13:51:51 +0000449Asset* AssetManager::openNonAsset(const int32_t cookie, const char* fileName, AccessMode mode)
Adam Lesinski16c4d152014-01-24 13:27:13 -0800450{
Narayan Kamatha0c62602014-01-24 13:51:51 +0000451 const size_t which = static_cast<size_t>(cookie) - 1;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800452
453 AutoMutex _l(mLock);
454
455 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
456
Adam Lesinski16c4d152014-01-24 13:27:13 -0800457 if (which < mAssetPaths.size()) {
458 ALOGV("Looking for non-asset '%s' in '%s'\n", fileName,
459 mAssetPaths.itemAt(which).path.string());
460 Asset* pAsset = openNonAssetInPathLocked(
461 fileName, mode, mAssetPaths.itemAt(which));
462 if (pAsset != NULL) {
463 return pAsset != kExcludedAsset ? pAsset : NULL;
464 }
465 }
466
467 return NULL;
468}
469
470/*
471 * Get the type of a file in the asset namespace.
472 *
473 * This currently only works for regular files. All others (including
474 * directories) will return kFileTypeNonexistent.
475 */
476FileType AssetManager::getFileType(const char* fileName)
477{
478 Asset* pAsset = NULL;
479
480 /*
481 * Open the asset. This is less efficient than simply finding the
482 * file, but it's not too bad (we don't uncompress or mmap data until
483 * the first read() call).
484 */
485 pAsset = open(fileName, Asset::ACCESS_STREAMING);
486 delete pAsset;
487
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700488 if (pAsset == NULL) {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800489 return kFileTypeNonexistent;
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700490 } else {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800491 return kFileTypeRegular;
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700492 }
Adam Lesinski16c4d152014-01-24 13:27:13 -0800493}
494
Tao Baia6d7e3f2015-09-01 18:49:54 -0700495bool AssetManager::appendPathToResTable(const asset_path& ap, bool appAsLib) const {
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +0900496 // skip those ap's that correspond to system overlays
497 if (ap.isSystemOverlay) {
498 return true;
499 }
500
Martin Kosiba7df36252014-01-16 16:25:56 +0000501 Asset* ass = NULL;
502 ResTable* sharedRes = NULL;
503 bool shared = true;
504 bool onlyEmptyResources = true;
Adam Lesinskib7e1ce02016-04-11 20:03:01 -0700505 ATRACE_NAME(ap.path.string());
Martin Kosiba7df36252014-01-16 16:25:56 +0000506 Asset* idmap = openIdmapLocked(ap);
507 size_t nextEntryIdx = mResources->getTableCount();
508 ALOGV("Looking for resource asset in '%s'\n", ap.path.string());
509 if (ap.type != kFileTypeDirectory) {
510 if (nextEntryIdx == 0) {
511 // The first item is typically the framework resources,
512 // which we want to avoid parsing every time.
513 sharedRes = const_cast<AssetManager*>(this)->
514 mZipSet.getZipResourceTable(ap.path);
515 if (sharedRes != NULL) {
516 // skip ahead the number of system overlay packages preloaded
517 nextEntryIdx = sharedRes->getTableCount();
518 }
519 }
520 if (sharedRes == NULL) {
521 ass = const_cast<AssetManager*>(this)->
522 mZipSet.getZipResourceTableAsset(ap.path);
523 if (ass == NULL) {
524 ALOGV("loading resource table %s\n", ap.path.string());
525 ass = const_cast<AssetManager*>(this)->
526 openNonAssetInPathLocked("resources.arsc",
527 Asset::ACCESS_BUFFER,
528 ap);
529 if (ass != NULL && ass != kExcludedAsset) {
530 ass = const_cast<AssetManager*>(this)->
531 mZipSet.setZipResourceTableAsset(ap.path, ass);
532 }
533 }
534
535 if (nextEntryIdx == 0 && ass != NULL) {
536 // If this is the first resource table in the asset
537 // manager, then we are going to cache it so that we
538 // can quickly copy it out for others.
539 ALOGV("Creating shared resources for %s", ap.path.string());
540 sharedRes = new ResTable();
541 sharedRes->add(ass, idmap, nextEntryIdx + 1, false);
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +0900542#ifdef __ANDROID__
543 const char* data = getenv("ANDROID_DATA");
544 LOG_ALWAYS_FATAL_IF(data == NULL, "ANDROID_DATA not set");
545 String8 overlaysListPath(data);
546 overlaysListPath.appendPath(kResourceCache);
547 overlaysListPath.appendPath("overlays.list");
548 addSystemOverlays(overlaysListPath.string(), ap.path, sharedRes, nextEntryIdx);
549#endif
Martin Kosiba7df36252014-01-16 16:25:56 +0000550 sharedRes = const_cast<AssetManager*>(this)->
551 mZipSet.setZipResourceTable(ap.path, sharedRes);
552 }
553 }
554 } else {
555 ALOGV("loading resource table %s\n", ap.path.string());
556 ass = const_cast<AssetManager*>(this)->
557 openNonAssetInPathLocked("resources.arsc",
558 Asset::ACCESS_BUFFER,
559 ap);
560 shared = false;
561 }
562
563 if ((ass != NULL || sharedRes != NULL) && ass != kExcludedAsset) {
564 ALOGV("Installing resource asset %p in to table %p\n", ass, mResources);
565 if (sharedRes != NULL) {
566 ALOGV("Copying existing resources for %s", ap.path.string());
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800567 mResources->add(sharedRes, ap.isSystemAsset);
Martin Kosiba7df36252014-01-16 16:25:56 +0000568 } else {
569 ALOGV("Parsing resources for %s", ap.path.string());
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800570 mResources->add(ass, idmap, nextEntryIdx + 1, !shared, appAsLib, ap.isSystemAsset);
Martin Kosiba7df36252014-01-16 16:25:56 +0000571 }
572 onlyEmptyResources = false;
573
574 if (!shared) {
575 delete ass;
576 }
577 } else {
578 ALOGV("Installing empty resources in to table %p\n", mResources);
579 mResources->addEmpty(nextEntryIdx + 1);
580 }
581
582 if (idmap != NULL) {
583 delete idmap;
584 }
Martin Kosiba7df36252014-01-16 16:25:56 +0000585 return onlyEmptyResources;
586}
587
Adam Lesinski16c4d152014-01-24 13:27:13 -0800588const ResTable* AssetManager::getResTable(bool required) const
589{
590 ResTable* rt = mResources;
591 if (rt) {
592 return rt;
593 }
594
595 // Iterate through all asset packages, collecting resources from each.
596
597 AutoMutex _l(mLock);
598
599 if (mResources != NULL) {
600 return mResources;
601 }
602
603 if (required) {
604 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
605 }
606
Adam Lesinskide898ff2014-01-29 18:20:45 -0800607 mResources = new ResTable();
608 updateResourceParamsLocked();
609
610 bool onlyEmptyResources = true;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800611 const size_t N = mAssetPaths.size();
612 for (size_t i=0; i<N; i++) {
Martin Kosiba7df36252014-01-16 16:25:56 +0000613 bool empty = appendPathToResTable(mAssetPaths.itemAt(i));
614 onlyEmptyResources = onlyEmptyResources && empty;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800615 }
616
Adam Lesinskide898ff2014-01-29 18:20:45 -0800617 if (required && onlyEmptyResources) {
618 ALOGW("Unable to find resources file resources.arsc");
619 delete mResources;
620 mResources = NULL;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800621 }
Adam Lesinskide898ff2014-01-29 18:20:45 -0800622
623 return mResources;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800624}
625
626void AssetManager::updateResourceParamsLocked() const
627{
Adam Lesinskib7e1ce02016-04-11 20:03:01 -0700628 ATRACE_CALL();
Adam Lesinski16c4d152014-01-24 13:27:13 -0800629 ResTable* res = mResources;
630 if (!res) {
631 return;
632 }
633
Narayan Kamath91447d82014-01-21 15:32:36 +0000634 if (mLocale) {
635 mConfig->setBcp47Locale(mLocale);
636 } else {
637 mConfig->clearLocale();
Adam Lesinski16c4d152014-01-24 13:27:13 -0800638 }
Adam Lesinski16c4d152014-01-24 13:27:13 -0800639
640 res->setParameters(mConfig);
641}
642
643Asset* AssetManager::openIdmapLocked(const struct asset_path& ap) const
644{
645 Asset* ass = NULL;
646 if (ap.idmap.size() != 0) {
647 ass = const_cast<AssetManager*>(this)->
648 openAssetFromFileLocked(ap.idmap, Asset::ACCESS_BUFFER);
649 if (ass) {
650 ALOGV("loading idmap %s\n", ap.idmap.string());
651 } else {
652 ALOGW("failed to load idmap %s\n", ap.idmap.string());
653 }
654 }
655 return ass;
656}
657
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +0900658void AssetManager::addSystemOverlays(const char* pathOverlaysList,
659 const String8& targetPackagePath, ResTable* sharedRes, size_t offset) const
660{
661 FILE* fin = fopen(pathOverlaysList, "r");
662 if (fin == NULL) {
663 return;
664 }
665
666#ifndef _WIN32
667 if (TEMP_FAILURE_RETRY(flock(fileno(fin), LOCK_SH)) != 0) {
668 fclose(fin);
669 return;
670 }
671#endif
672 char buf[1024];
673 while (fgets(buf, sizeof(buf), fin)) {
674 // format of each line:
675 // <path to apk><space><path to idmap><newline>
676 char* space = strchr(buf, ' ');
677 char* newline = strchr(buf, '\n');
678 asset_path oap;
679
680 if (space == NULL || newline == NULL || newline < space) {
681 continue;
682 }
683
684 oap.path = String8(buf, space - buf);
685 oap.type = kFileTypeRegular;
686 oap.idmap = String8(space + 1, newline - space - 1);
687 oap.isSystemOverlay = true;
688
689 Asset* oass = const_cast<AssetManager*>(this)->
690 openNonAssetInPathLocked("resources.arsc",
691 Asset::ACCESS_BUFFER,
692 oap);
693
694 if (oass != NULL) {
695 Asset* oidmap = openIdmapLocked(oap);
696 offset++;
697 sharedRes->add(oass, oidmap, offset + 1, false);
698 const_cast<AssetManager*>(this)->mAssetPaths.add(oap);
699 const_cast<AssetManager*>(this)->mZipSet.addOverlay(targetPackagePath, oap);
700 delete oidmap;
701 }
702 }
703
704#ifndef _WIN32
705 TEMP_FAILURE_RETRY(flock(fileno(fin), LOCK_UN));
706#endif
707 fclose(fin);
708}
709
Adam Lesinski16c4d152014-01-24 13:27:13 -0800710const ResTable& AssetManager::getResources(bool required) const
711{
712 const ResTable* rt = getResTable(required);
713 return *rt;
714}
715
716bool AssetManager::isUpToDate()
717{
718 AutoMutex _l(mLock);
719 return mZipSet.isUpToDate();
720}
721
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800722void AssetManager::getLocales(Vector<String8>* locales, bool includeSystemLocales) const
Adam Lesinski16c4d152014-01-24 13:27:13 -0800723{
724 ResTable* res = mResources;
725 if (res != NULL) {
Roozbeh Pournader7e5f96f2016-06-13 18:10:49 -0700726 res->getLocales(locales, includeSystemLocales, true /* mergeEquivalentLangs */);
Narayan Kamathe4345db2014-06-26 16:01:28 +0100727 }
Adam Lesinski16c4d152014-01-24 13:27:13 -0800728}
729
730/*
731 * Open a non-asset file as if it were an asset, searching for it in the
732 * specified app.
733 *
734 * Pass in a NULL values for "appName" if the common app directory should
735 * be used.
736 */
737Asset* AssetManager::openNonAssetInPathLocked(const char* fileName, AccessMode mode,
738 const asset_path& ap)
739{
740 Asset* pAsset = NULL;
741
742 /* look at the filesystem on disk */
743 if (ap.type == kFileTypeDirectory) {
744 String8 path(ap.path);
745 path.appendPath(fileName);
746
747 pAsset = openAssetFromFileLocked(path, mode);
748
749 if (pAsset == NULL) {
750 /* try again, this time with ".gz" */
751 path.append(".gz");
752 pAsset = openAssetFromFileLocked(path, mode);
753 }
754
755 if (pAsset != NULL) {
756 //printf("FOUND NA '%s' on disk\n", fileName);
757 pAsset->setAssetSource(path);
758 }
759
760 /* look inside the zip file */
761 } else {
762 String8 path(fileName);
763
764 /* check the appropriate Zip file */
Narayan Kamath560566d2013-12-03 13:16:03 +0000765 ZipFileRO* pZip = getZipFileLocked(ap);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800766 if (pZip != NULL) {
767 //printf("GOT zip, checking NA '%s'\n", (const char*) path);
Narayan Kamath560566d2013-12-03 13:16:03 +0000768 ZipEntryRO entry = pZip->findEntryByName(path.string());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800769 if (entry != NULL) {
770 //printf("FOUND NA in Zip file for %s\n", appName ? appName : kAppCommon);
771 pAsset = openAssetFromZipLocked(pZip, entry, mode, path);
Narayan Kamath560566d2013-12-03 13:16:03 +0000772 pZip->releaseEntry(entry);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800773 }
774 }
775
776 if (pAsset != NULL) {
777 /* create a "source" name, for debug/display */
778 pAsset->setAssetSource(
779 createZipSourceNameLocked(ZipSet::getPathName(ap.path.string()), String8(""),
780 String8(fileName)));
781 }
782 }
783
784 return pAsset;
785}
786
787/*
Adam Lesinski16c4d152014-01-24 13:27:13 -0800788 * Create a "source name" for a file from a Zip archive.
789 */
790String8 AssetManager::createZipSourceNameLocked(const String8& zipFileName,
791 const String8& dirName, const String8& fileName)
792{
793 String8 sourceName("zip:");
794 sourceName.append(zipFileName);
795 sourceName.append(":");
796 if (dirName.length() > 0) {
797 sourceName.appendPath(dirName);
798 }
799 sourceName.appendPath(fileName);
800 return sourceName;
801}
802
803/*
Adam Lesinski16c4d152014-01-24 13:27:13 -0800804 * Create a path to a loose asset (asset-base/app/rootDir).
805 */
806String8 AssetManager::createPathNameLocked(const asset_path& ap, const char* rootDir)
807{
808 String8 path(ap.path);
809 if (rootDir != NULL) path.appendPath(rootDir);
810 return path;
811}
812
813/*
814 * Return a pointer to one of our open Zip archives. Returns NULL if no
815 * matching Zip file exists.
Adam Lesinski16c4d152014-01-24 13:27:13 -0800816 */
817ZipFileRO* AssetManager::getZipFileLocked(const asset_path& ap)
818{
819 ALOGV("getZipFileLocked() in %p\n", this);
820
821 return mZipSet.getZip(ap.path);
822}
823
824/*
825 * Try to open an asset from a file on disk.
826 *
827 * If the file is compressed with gzip, we seek to the start of the
828 * deflated data and pass that in (just like we would for a Zip archive).
829 *
830 * For uncompressed data, we may already have an mmap()ed version sitting
831 * around. If so, we want to hand that to the Asset instead.
832 *
833 * This returns NULL if the file doesn't exist, couldn't be opened, or
834 * claims to be a ".gz" but isn't.
835 */
836Asset* AssetManager::openAssetFromFileLocked(const String8& pathName,
837 AccessMode mode)
838{
839 Asset* pAsset = NULL;
840
841 if (strcasecmp(pathName.getPathExtension().string(), ".gz") == 0) {
842 //printf("TRYING '%s'\n", (const char*) pathName);
843 pAsset = Asset::createFromCompressedFile(pathName.string(), mode);
844 } else {
845 //printf("TRYING '%s'\n", (const char*) pathName);
846 pAsset = Asset::createFromFile(pathName.string(), mode);
847 }
848
849 return pAsset;
850}
851
852/*
853 * Given an entry in a Zip archive, create a new Asset object.
854 *
855 * If the entry is uncompressed, we may want to create or share a
856 * slice of shared memory.
857 */
858Asset* AssetManager::openAssetFromZipLocked(const ZipFileRO* pZipFile,
859 const ZipEntryRO entry, AccessMode mode, const String8& entryName)
860{
861 Asset* pAsset = NULL;
862
863 // TODO: look for previously-created shared memory slice?
Narayan Kamath407753c2015-06-16 12:02:57 +0100864 uint16_t method;
865 uint32_t uncompressedLen;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800866
867 //printf("USING Zip '%s'\n", pEntry->getFileName());
868
Adam Lesinski16c4d152014-01-24 13:27:13 -0800869 if (!pZipFile->getEntryInfo(entry, &method, &uncompressedLen, NULL, NULL,
870 NULL, NULL))
871 {
872 ALOGW("getEntryInfo failed\n");
873 return NULL;
874 }
875
876 FileMap* dataMap = pZipFile->createEntryFileMap(entry);
877 if (dataMap == NULL) {
878 ALOGW("create map from entry failed\n");
879 return NULL;
880 }
881
882 if (method == ZipFileRO::kCompressStored) {
883 pAsset = Asset::createFromUncompressedMap(dataMap, mode);
884 ALOGV("Opened uncompressed entry %s in zip %s mode %d: %p", entryName.string(),
885 dataMap->getFileName(), mode, pAsset);
886 } else {
Narayan Kamath407753c2015-06-16 12:02:57 +0100887 pAsset = Asset::createFromCompressedMap(dataMap,
888 static_cast<size_t>(uncompressedLen), mode);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800889 ALOGV("Opened compressed entry %s in zip %s mode %d: %p", entryName.string(),
890 dataMap->getFileName(), mode, pAsset);
891 }
892 if (pAsset == NULL) {
893 /* unexpected */
894 ALOGW("create from segment failed\n");
895 }
896
897 return pAsset;
898}
899
Adam Lesinski16c4d152014-01-24 13:27:13 -0800900/*
901 * Open a directory in the asset namespace.
902 *
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700903 * An "asset directory" is simply the combination of all asset paths' "assets/" directories.
Adam Lesinski16c4d152014-01-24 13:27:13 -0800904 *
905 * Pass in "" for the root dir.
906 */
907AssetDir* AssetManager::openDir(const char* dirName)
908{
909 AutoMutex _l(mLock);
910
911 AssetDir* pDir = NULL;
912 SortedVector<AssetDir::FileInfo>* pMergedInfo = NULL;
913
914 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
915 assert(dirName != NULL);
916
917 //printf("+++ openDir(%s) in '%s'\n", dirName, (const char*) mAssetBase);
918
Adam Lesinski16c4d152014-01-24 13:27:13 -0800919 pDir = new AssetDir;
920
921 /*
922 * Scan the various directories, merging what we find into a single
923 * vector. We want to scan them in reverse priority order so that
924 * the ".EXCLUDE" processing works correctly. Also, if we decide we
925 * want to remember where the file is coming from, we'll get the right
926 * version.
927 *
928 * We start with Zip archives, then do loose files.
929 */
930 pMergedInfo = new SortedVector<AssetDir::FileInfo>;
931
932 size_t i = mAssetPaths.size();
933 while (i > 0) {
934 i--;
935 const asset_path& ap = mAssetPaths.itemAt(i);
936 if (ap.type == kFileTypeRegular) {
937 ALOGV("Adding directory %s from zip %s", dirName, ap.path.string());
938 scanAndMergeZipLocked(pMergedInfo, ap, kAssetsRoot, dirName);
939 } else {
940 ALOGV("Adding directory %s from dir %s", dirName, ap.path.string());
941 scanAndMergeDirLocked(pMergedInfo, ap, kAssetsRoot, dirName);
942 }
943 }
944
945#if 0
946 printf("FILE LIST:\n");
947 for (i = 0; i < (size_t) pMergedInfo->size(); i++) {
948 printf(" %d: (%d) '%s'\n", i,
949 pMergedInfo->itemAt(i).getFileType(),
950 (const char*) pMergedInfo->itemAt(i).getFileName());
951 }
952#endif
953
954 pDir->setFileList(pMergedInfo);
955 return pDir;
956}
957
958/*
959 * Open a directory in the non-asset namespace.
960 *
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700961 * An "asset directory" is simply the combination of all asset paths' "assets/" directories.
Adam Lesinski16c4d152014-01-24 13:27:13 -0800962 *
963 * Pass in "" for the root dir.
964 */
Narayan Kamatha0c62602014-01-24 13:51:51 +0000965AssetDir* AssetManager::openNonAssetDir(const int32_t cookie, const char* dirName)
Adam Lesinski16c4d152014-01-24 13:27:13 -0800966{
967 AutoMutex _l(mLock);
968
969 AssetDir* pDir = NULL;
970 SortedVector<AssetDir::FileInfo>* pMergedInfo = NULL;
971
972 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
973 assert(dirName != NULL);
974
975 //printf("+++ openDir(%s) in '%s'\n", dirName, (const char*) mAssetBase);
976
Adam Lesinski16c4d152014-01-24 13:27:13 -0800977 pDir = new AssetDir;
978
979 pMergedInfo = new SortedVector<AssetDir::FileInfo>;
980
Narayan Kamatha0c62602014-01-24 13:51:51 +0000981 const size_t which = static_cast<size_t>(cookie) - 1;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800982
983 if (which < mAssetPaths.size()) {
984 const asset_path& ap = mAssetPaths.itemAt(which);
985 if (ap.type == kFileTypeRegular) {
986 ALOGV("Adding directory %s from zip %s", dirName, ap.path.string());
987 scanAndMergeZipLocked(pMergedInfo, ap, NULL, dirName);
988 } else {
989 ALOGV("Adding directory %s from dir %s", dirName, ap.path.string());
990 scanAndMergeDirLocked(pMergedInfo, ap, NULL, dirName);
991 }
992 }
993
994#if 0
995 printf("FILE LIST:\n");
996 for (i = 0; i < (size_t) pMergedInfo->size(); i++) {
997 printf(" %d: (%d) '%s'\n", i,
998 pMergedInfo->itemAt(i).getFileType(),
999 (const char*) pMergedInfo->itemAt(i).getFileName());
1000 }
1001#endif
1002
1003 pDir->setFileList(pMergedInfo);
1004 return pDir;
1005}
1006
1007/*
1008 * Scan the contents of the specified directory and merge them into the
1009 * "pMergedInfo" vector, removing previous entries if we find "exclude"
1010 * directives.
1011 *
1012 * Returns "false" if we found nothing to contribute.
1013 */
1014bool AssetManager::scanAndMergeDirLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
1015 const asset_path& ap, const char* rootDir, const char* dirName)
1016{
Adam Lesinski16c4d152014-01-24 13:27:13 -08001017 assert(pMergedInfo != NULL);
1018
Adam Lesinskia77685f2016-10-03 16:26:28 -07001019 //printf("scanAndMergeDir: %s %s %s\n", ap.path.string(), rootDir, dirName);
Adam Lesinski16c4d152014-01-24 13:27:13 -08001020
Adam Lesinskia77685f2016-10-03 16:26:28 -07001021 String8 path = createPathNameLocked(ap, rootDir);
1022 if (dirName[0] != '\0')
1023 path.appendPath(dirName);
Adam Lesinski16c4d152014-01-24 13:27:13 -08001024
Adam Lesinskia77685f2016-10-03 16:26:28 -07001025 SortedVector<AssetDir::FileInfo>* pContents = scanDirLocked(path);
1026 if (pContents == NULL)
1027 return false;
Adam Lesinski16c4d152014-01-24 13:27:13 -08001028
1029 // if we wanted to do an incremental cache fill, we would do it here
1030
1031 /*
1032 * Process "exclude" directives. If we find a filename that ends with
1033 * ".EXCLUDE", we look for a matching entry in the "merged" set, and
1034 * remove it if we find it. We also delete the "exclude" entry.
1035 */
1036 int i, count, exclExtLen;
1037
1038 count = pContents->size();
1039 exclExtLen = strlen(kExcludeExtension);
1040 for (i = 0; i < count; i++) {
1041 const char* name;
1042 int nameLen;
1043
1044 name = pContents->itemAt(i).getFileName().string();
1045 nameLen = strlen(name);
1046 if (nameLen > exclExtLen &&
1047 strcmp(name + (nameLen - exclExtLen), kExcludeExtension) == 0)
1048 {
1049 String8 match(name, nameLen - exclExtLen);
1050 int matchIdx;
1051
1052 matchIdx = AssetDir::FileInfo::findEntry(pMergedInfo, match);
1053 if (matchIdx > 0) {
1054 ALOGV("Excluding '%s' [%s]\n",
1055 pMergedInfo->itemAt(matchIdx).getFileName().string(),
1056 pMergedInfo->itemAt(matchIdx).getSourceName().string());
1057 pMergedInfo->removeAt(matchIdx);
1058 } else {
1059 //printf("+++ no match on '%s'\n", (const char*) match);
1060 }
1061
1062 ALOGD("HEY: size=%d removing %d\n", (int)pContents->size(), i);
1063 pContents->removeAt(i);
1064 i--; // adjust "for" loop
1065 count--; // and loop limit
1066 }
1067 }
1068
1069 mergeInfoLocked(pMergedInfo, pContents);
1070
1071 delete pContents;
1072
1073 return true;
1074}
1075
1076/*
1077 * Scan the contents of the specified directory, and stuff what we find
1078 * into a newly-allocated vector.
1079 *
1080 * Files ending in ".gz" will have their extensions removed.
1081 *
1082 * We should probably think about skipping files with "illegal" names,
1083 * e.g. illegal characters (/\:) or excessive length.
1084 *
1085 * Returns NULL if the specified directory doesn't exist.
1086 */
1087SortedVector<AssetDir::FileInfo>* AssetManager::scanDirLocked(const String8& path)
1088{
1089 SortedVector<AssetDir::FileInfo>* pContents = NULL;
1090 DIR* dir;
1091 struct dirent* entry;
1092 FileType fileType;
1093
1094 ALOGV("Scanning dir '%s'\n", path.string());
1095
1096 dir = opendir(path.string());
1097 if (dir == NULL)
1098 return NULL;
1099
1100 pContents = new SortedVector<AssetDir::FileInfo>;
1101
1102 while (1) {
1103 entry = readdir(dir);
1104 if (entry == NULL)
1105 break;
1106
1107 if (strcmp(entry->d_name, ".") == 0 ||
1108 strcmp(entry->d_name, "..") == 0)
1109 continue;
1110
1111#ifdef _DIRENT_HAVE_D_TYPE
1112 if (entry->d_type == DT_REG)
1113 fileType = kFileTypeRegular;
1114 else if (entry->d_type == DT_DIR)
1115 fileType = kFileTypeDirectory;
1116 else
1117 fileType = kFileTypeUnknown;
1118#else
1119 // stat the file
1120 fileType = ::getFileType(path.appendPathCopy(entry->d_name).string());
1121#endif
1122
1123 if (fileType != kFileTypeRegular && fileType != kFileTypeDirectory)
1124 continue;
1125
1126 AssetDir::FileInfo info;
1127 info.set(String8(entry->d_name), fileType);
1128 if (strcasecmp(info.getFileName().getPathExtension().string(), ".gz") == 0)
1129 info.setFileName(info.getFileName().getBasePath());
1130 info.setSourceName(path.appendPathCopy(info.getFileName()));
1131 pContents->add(info);
1132 }
1133
1134 closedir(dir);
1135 return pContents;
1136}
1137
1138/*
1139 * Scan the contents out of the specified Zip archive, and merge what we
1140 * find into "pMergedInfo". If the Zip archive in question doesn't exist,
1141 * we return immediately.
1142 *
1143 * Returns "false" if we found nothing to contribute.
1144 */
1145bool AssetManager::scanAndMergeZipLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
1146 const asset_path& ap, const char* rootDir, const char* baseDirName)
1147{
1148 ZipFileRO* pZip;
1149 Vector<String8> dirs;
1150 AssetDir::FileInfo info;
1151 SortedVector<AssetDir::FileInfo> contents;
1152 String8 sourceName, zipName, dirName;
1153
1154 pZip = mZipSet.getZip(ap.path);
1155 if (pZip == NULL) {
1156 ALOGW("Failure opening zip %s\n", ap.path.string());
1157 return false;
1158 }
1159
1160 zipName = ZipSet::getPathName(ap.path.string());
1161
1162 /* convert "sounds" to "rootDir/sounds" */
1163 if (rootDir != NULL) dirName = rootDir;
1164 dirName.appendPath(baseDirName);
1165
1166 /*
1167 * Scan through the list of files, looking for a match. The files in
1168 * the Zip table of contents are not in sorted order, so we have to
1169 * process the entire list. We're looking for a string that begins
1170 * with the characters in "dirName", is followed by a '/', and has no
1171 * subsequent '/' in the stuff that follows.
1172 *
1173 * What makes this especially fun is that directories are not stored
1174 * explicitly in Zip archives, so we have to infer them from context.
1175 * When we see "sounds/foo.wav" we have to leave a note to ourselves
1176 * to insert a directory called "sounds" into the list. We store
1177 * these in temporary vector so that we only return each one once.
1178 *
1179 * Name comparisons are case-sensitive to match UNIX filesystem
1180 * semantics.
1181 */
1182 int dirNameLen = dirName.length();
Narayan Kamath560566d2013-12-03 13:16:03 +00001183 void *iterationCookie;
Yusuke Sato05f648e2015-08-03 16:21:10 -07001184 if (!pZip->startIteration(&iterationCookie, dirName.string(), NULL)) {
Narayan Kamath560566d2013-12-03 13:16:03 +00001185 ALOGW("ZipFileRO::startIteration returned false");
1186 return false;
1187 }
1188
1189 ZipEntryRO entry;
1190 while ((entry = pZip->nextEntry(iterationCookie)) != NULL) {
Adam Lesinski16c4d152014-01-24 13:27:13 -08001191 char nameBuf[256];
1192
Adam Lesinski16c4d152014-01-24 13:27:13 -08001193 if (pZip->getEntryFileName(entry, nameBuf, sizeof(nameBuf)) != 0) {
1194 // TODO: fix this if we expect to have long names
1195 ALOGE("ARGH: name too long?\n");
1196 continue;
1197 }
1198 //printf("Comparing %s in %s?\n", nameBuf, dirName.string());
Yusuke Sato05f648e2015-08-03 16:21:10 -07001199 if (dirNameLen == 0 || nameBuf[dirNameLen] == '/')
Adam Lesinski16c4d152014-01-24 13:27:13 -08001200 {
1201 const char* cp;
1202 const char* nextSlash;
1203
1204 cp = nameBuf + dirNameLen;
1205 if (dirNameLen != 0)
1206 cp++; // advance past the '/'
1207
1208 nextSlash = strchr(cp, '/');
1209//xxx this may break if there are bare directory entries
1210 if (nextSlash == NULL) {
1211 /* this is a file in the requested directory */
1212
1213 info.set(String8(nameBuf).getPathLeaf(), kFileTypeRegular);
1214
1215 info.setSourceName(
1216 createZipSourceNameLocked(zipName, dirName, info.getFileName()));
1217
1218 contents.add(info);
1219 //printf("FOUND: file '%s'\n", info.getFileName().string());
1220 } else {
1221 /* this is a subdir; add it if we don't already have it*/
1222 String8 subdirName(cp, nextSlash - cp);
1223 size_t j;
1224 size_t N = dirs.size();
1225
1226 for (j = 0; j < N; j++) {
1227 if (subdirName == dirs[j]) {
1228 break;
1229 }
1230 }
1231 if (j == N) {
1232 dirs.add(subdirName);
1233 }
1234
1235 //printf("FOUND: dir '%s'\n", subdirName.string());
1236 }
1237 }
1238 }
1239
Narayan Kamath560566d2013-12-03 13:16:03 +00001240 pZip->endIteration(iterationCookie);
1241
Adam Lesinski16c4d152014-01-24 13:27:13 -08001242 /*
1243 * Add the set of unique directories.
1244 */
1245 for (int i = 0; i < (int) dirs.size(); i++) {
1246 info.set(dirs[i], kFileTypeDirectory);
1247 info.setSourceName(
1248 createZipSourceNameLocked(zipName, dirName, info.getFileName()));
1249 contents.add(info);
1250 }
1251
1252 mergeInfoLocked(pMergedInfo, &contents);
1253
1254 return true;
1255}
1256
1257
1258/*
1259 * Merge two vectors of FileInfo.
1260 *
1261 * The merged contents will be stuffed into *pMergedInfo.
1262 *
1263 * If an entry for a file exists in both "pMergedInfo" and "pContents",
1264 * we use the newer "pContents" entry.
1265 */
1266void AssetManager::mergeInfoLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
1267 const SortedVector<AssetDir::FileInfo>* pContents)
1268{
1269 /*
1270 * Merge what we found in this directory with what we found in
1271 * other places.
1272 *
1273 * Two basic approaches:
1274 * (1) Create a new array that holds the unique values of the two
1275 * arrays.
1276 * (2) Take the elements from pContents and shove them into pMergedInfo.
1277 *
1278 * Because these are vectors of complex objects, moving elements around
1279 * inside the vector requires constructing new objects and allocating
1280 * storage for members. With approach #1, we're always adding to the
1281 * end, whereas with #2 we could be inserting multiple elements at the
1282 * front of the vector. Approach #1 requires a full copy of the
1283 * contents of pMergedInfo, but approach #2 requires the same copy for
1284 * every insertion at the front of pMergedInfo.
1285 *
1286 * (We should probably use a SortedVector interface that allows us to
1287 * just stuff items in, trusting us to maintain the sort order.)
1288 */
1289 SortedVector<AssetDir::FileInfo>* pNewSorted;
1290 int mergeMax, contMax;
1291 int mergeIdx, contIdx;
1292
1293 pNewSorted = new SortedVector<AssetDir::FileInfo>;
1294 mergeMax = pMergedInfo->size();
1295 contMax = pContents->size();
1296 mergeIdx = contIdx = 0;
1297
1298 while (mergeIdx < mergeMax || contIdx < contMax) {
1299 if (mergeIdx == mergeMax) {
1300 /* hit end of "merge" list, copy rest of "contents" */
1301 pNewSorted->add(pContents->itemAt(contIdx));
1302 contIdx++;
1303 } else if (contIdx == contMax) {
1304 /* hit end of "cont" list, copy rest of "merge" */
1305 pNewSorted->add(pMergedInfo->itemAt(mergeIdx));
1306 mergeIdx++;
1307 } else if (pMergedInfo->itemAt(mergeIdx) == pContents->itemAt(contIdx))
1308 {
1309 /* items are identical, add newer and advance both indices */
1310 pNewSorted->add(pContents->itemAt(contIdx));
1311 mergeIdx++;
1312 contIdx++;
1313 } else if (pMergedInfo->itemAt(mergeIdx) < pContents->itemAt(contIdx))
1314 {
1315 /* "merge" is lower, add that one */
1316 pNewSorted->add(pMergedInfo->itemAt(mergeIdx));
1317 mergeIdx++;
1318 } else {
1319 /* "cont" is lower, add that one */
1320 assert(pContents->itemAt(contIdx) < pMergedInfo->itemAt(mergeIdx));
1321 pNewSorted->add(pContents->itemAt(contIdx));
1322 contIdx++;
1323 }
1324 }
1325
1326 /*
1327 * Overwrite the "merged" list with the new stuff.
1328 */
1329 *pMergedInfo = *pNewSorted;
1330 delete pNewSorted;
1331
1332#if 0 // for Vector, rather than SortedVector
1333 int i, j;
1334 for (i = pContents->size() -1; i >= 0; i--) {
1335 bool add = true;
1336
1337 for (j = pMergedInfo->size() -1; j >= 0; j--) {
1338 /* case-sensitive comparisons, to behave like UNIX fs */
1339 if (strcmp(pContents->itemAt(i).mFileName,
1340 pMergedInfo->itemAt(j).mFileName) == 0)
1341 {
1342 /* match, don't add this entry */
1343 add = false;
1344 break;
1345 }
1346 }
1347
1348 if (add)
1349 pMergedInfo->add(pContents->itemAt(i));
1350 }
1351#endif
1352}
1353
Adam Lesinski16c4d152014-01-24 13:27:13 -08001354/*
1355 * ===========================================================================
1356 * AssetManager::SharedZip
1357 * ===========================================================================
1358 */
1359
1360
1361Mutex AssetManager::SharedZip::gLock;
1362DefaultKeyedVector<String8, wp<AssetManager::SharedZip> > AssetManager::SharedZip::gOpen;
1363
1364AssetManager::SharedZip::SharedZip(const String8& path, time_t modWhen)
1365 : mPath(path), mZipFile(NULL), mModWhen(modWhen),
1366 mResourceTableAsset(NULL), mResourceTable(NULL)
1367{
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001368 if (kIsDebug) {
1369 ALOGI("Creating SharedZip %p %s\n", this, (const char*)mPath);
1370 }
Adam Lesinski16c4d152014-01-24 13:27:13 -08001371 ALOGV("+++ opening zip '%s'\n", mPath.string());
Narayan Kamath560566d2013-12-03 13:16:03 +00001372 mZipFile = ZipFileRO::open(mPath.string());
1373 if (mZipFile == NULL) {
Adam Lesinski16c4d152014-01-24 13:27:13 -08001374 ALOGD("failed to open Zip archive '%s'\n", mPath.string());
Adam Lesinski16c4d152014-01-24 13:27:13 -08001375 }
1376}
1377
Mårten Kongstad48d22322014-01-31 14:43:27 +01001378sp<AssetManager::SharedZip> AssetManager::SharedZip::get(const String8& path,
1379 bool createIfNotPresent)
Adam Lesinski16c4d152014-01-24 13:27:13 -08001380{
1381 AutoMutex _l(gLock);
1382 time_t modWhen = getFileModDate(path);
1383 sp<SharedZip> zip = gOpen.valueFor(path).promote();
1384 if (zip != NULL && zip->mModWhen == modWhen) {
1385 return zip;
1386 }
Mårten Kongstad48d22322014-01-31 14:43:27 +01001387 if (zip == NULL && !createIfNotPresent) {
1388 return NULL;
1389 }
Adam Lesinski16c4d152014-01-24 13:27:13 -08001390 zip = new SharedZip(path, modWhen);
1391 gOpen.add(path, zip);
1392 return zip;
1393
1394}
1395
1396ZipFileRO* AssetManager::SharedZip::getZip()
1397{
1398 return mZipFile;
1399}
1400
1401Asset* AssetManager::SharedZip::getResourceTableAsset()
1402{
songjinshi49921f22016-09-08 15:24:30 +08001403 AutoMutex _l(gLock);
Adam Lesinski16c4d152014-01-24 13:27:13 -08001404 ALOGV("Getting from SharedZip %p resource asset %p\n", this, mResourceTableAsset);
1405 return mResourceTableAsset;
1406}
1407
1408Asset* AssetManager::SharedZip::setResourceTableAsset(Asset* asset)
1409{
1410 {
1411 AutoMutex _l(gLock);
1412 if (mResourceTableAsset == NULL) {
Adam Lesinski16c4d152014-01-24 13:27:13 -08001413 // This is not thread safe the first time it is called, so
1414 // do it here with the global lock held.
1415 asset->getBuffer(true);
songjinshi49921f22016-09-08 15:24:30 +08001416 mResourceTableAsset = asset;
Adam Lesinski16c4d152014-01-24 13:27:13 -08001417 return asset;
1418 }
1419 }
1420 delete asset;
1421 return mResourceTableAsset;
1422}
1423
1424ResTable* AssetManager::SharedZip::getResourceTable()
1425{
1426 ALOGV("Getting from SharedZip %p resource table %p\n", this, mResourceTable);
1427 return mResourceTable;
1428}
1429
1430ResTable* AssetManager::SharedZip::setResourceTable(ResTable* res)
1431{
1432 {
1433 AutoMutex _l(gLock);
1434 if (mResourceTable == NULL) {
1435 mResourceTable = res;
1436 return res;
1437 }
1438 }
1439 delete res;
1440 return mResourceTable;
1441}
1442
1443bool AssetManager::SharedZip::isUpToDate()
1444{
1445 time_t modWhen = getFileModDate(mPath.string());
1446 return mModWhen == modWhen;
1447}
1448
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +09001449void AssetManager::SharedZip::addOverlay(const asset_path& ap)
1450{
1451 mOverlays.add(ap);
1452}
1453
1454bool AssetManager::SharedZip::getOverlay(size_t idx, asset_path* out) const
1455{
1456 if (idx >= mOverlays.size()) {
1457 return false;
1458 }
1459 *out = mOverlays[idx];
1460 return true;
1461}
1462
Adam Lesinski16c4d152014-01-24 13:27:13 -08001463AssetManager::SharedZip::~SharedZip()
1464{
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001465 if (kIsDebug) {
1466 ALOGI("Destroying SharedZip %p %s\n", this, (const char*)mPath);
1467 }
Adam Lesinski16c4d152014-01-24 13:27:13 -08001468 if (mResourceTable != NULL) {
1469 delete mResourceTable;
1470 }
1471 if (mResourceTableAsset != NULL) {
1472 delete mResourceTableAsset;
1473 }
1474 if (mZipFile != NULL) {
1475 delete mZipFile;
1476 ALOGV("Closed '%s'\n", mPath.string());
1477 }
1478}
1479
1480/*
1481 * ===========================================================================
1482 * AssetManager::ZipSet
1483 * ===========================================================================
1484 */
1485
1486/*
Adam Lesinski16c4d152014-01-24 13:27:13 -08001487 * Destructor. Close any open archives.
1488 */
1489AssetManager::ZipSet::~ZipSet(void)
1490{
1491 size_t N = mZipFile.size();
1492 for (size_t i = 0; i < N; i++)
1493 closeZip(i);
1494}
1495
1496/*
1497 * Close a Zip file and reset the entry.
1498 */
1499void AssetManager::ZipSet::closeZip(int idx)
1500{
1501 mZipFile.editItemAt(idx) = NULL;
1502}
1503
1504
1505/*
1506 * Retrieve the appropriate Zip file from the set.
1507 */
1508ZipFileRO* AssetManager::ZipSet::getZip(const String8& path)
1509{
1510 int idx = getIndex(path);
1511 sp<SharedZip> zip = mZipFile[idx];
1512 if (zip == NULL) {
1513 zip = SharedZip::get(path);
1514 mZipFile.editItemAt(idx) = zip;
1515 }
1516 return zip->getZip();
1517}
1518
1519Asset* AssetManager::ZipSet::getZipResourceTableAsset(const String8& path)
1520{
1521 int idx = getIndex(path);
1522 sp<SharedZip> zip = mZipFile[idx];
1523 if (zip == NULL) {
1524 zip = SharedZip::get(path);
1525 mZipFile.editItemAt(idx) = zip;
1526 }
1527 return zip->getResourceTableAsset();
1528}
1529
1530Asset* AssetManager::ZipSet::setZipResourceTableAsset(const String8& path,
1531 Asset* asset)
1532{
1533 int idx = getIndex(path);
1534 sp<SharedZip> zip = mZipFile[idx];
1535 // doesn't make sense to call before previously accessing.
1536 return zip->setResourceTableAsset(asset);
1537}
1538
1539ResTable* AssetManager::ZipSet::getZipResourceTable(const String8& path)
1540{
1541 int idx = getIndex(path);
1542 sp<SharedZip> zip = mZipFile[idx];
1543 if (zip == NULL) {
1544 zip = SharedZip::get(path);
1545 mZipFile.editItemAt(idx) = zip;
1546 }
1547 return zip->getResourceTable();
1548}
1549
1550ResTable* AssetManager::ZipSet::setZipResourceTable(const String8& path,
1551 ResTable* res)
1552{
1553 int idx = getIndex(path);
1554 sp<SharedZip> zip = mZipFile[idx];
1555 // doesn't make sense to call before previously accessing.
1556 return zip->setResourceTable(res);
1557}
1558
1559/*
1560 * Generate the partial pathname for the specified archive. The caller
1561 * gets to prepend the asset root directory.
1562 *
1563 * Returns something like "common/en-US-noogle.jar".
1564 */
1565/*static*/ String8 AssetManager::ZipSet::getPathName(const char* zipPath)
1566{
1567 return String8(zipPath);
1568}
1569
1570bool AssetManager::ZipSet::isUpToDate()
1571{
1572 const size_t N = mZipFile.size();
1573 for (size_t i=0; i<N; i++) {
1574 if (mZipFile[i] != NULL && !mZipFile[i]->isUpToDate()) {
1575 return false;
1576 }
1577 }
1578 return true;
1579}
1580
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +09001581void AssetManager::ZipSet::addOverlay(const String8& path, const asset_path& overlay)
1582{
1583 int idx = getIndex(path);
1584 sp<SharedZip> zip = mZipFile[idx];
1585 zip->addOverlay(overlay);
1586}
1587
1588bool AssetManager::ZipSet::getOverlay(const String8& path, size_t idx, asset_path* out) const
1589{
1590 sp<SharedZip> zip = SharedZip::get(path, false);
1591 if (zip == NULL) {
1592 return false;
1593 }
1594 return zip->getOverlay(idx, out);
1595}
1596
Adam Lesinski16c4d152014-01-24 13:27:13 -08001597/*
1598 * Compute the zip file's index.
1599 *
1600 * "appName", "locale", and "vendor" should be set to NULL to indicate the
1601 * default directory.
1602 */
1603int AssetManager::ZipSet::getIndex(const String8& zip) const
1604{
1605 const size_t N = mZipPath.size();
1606 for (size_t i=0; i<N; i++) {
1607 if (mZipPath[i] == zip) {
1608 return i;
1609 }
1610 }
1611
1612 mZipPath.add(zip);
1613 mZipFile.add(NULL);
1614
1615 return mZipPath.size()-1;
1616}