blob: 21609d30e92c29035669547590947697b43b8daf [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>
Steven Morelandfb7952f2018-02-23 14:58:50 -080031#include <cutils/atomic.h>
Adam Lesinski16c4d152014-01-24 13:27:13 -080032#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";
Mårten Kongstad06a1ac82018-09-20 13:09:47 +020075const char* AssetManager::VENDOR_OVERLAY_DIR = "/vendor/overlay";
Jaekyun Seok1713d9e2018-01-12 21:47:26 +090076const char* AssetManager::PRODUCT_OVERLAY_DIR = "/product/overlay";
Dario Freni4ce46792018-06-01 14:02:08 +010077const char* AssetManager::PRODUCT_SERVICES_OVERLAY_DIR = "/product_services/overlay";
Mårten Kongstad48c24cf2019-02-25 10:54:09 +010078const char* AssetManager::ODM_OVERLAY_DIR = "/odm/overlay";
Jakub Adamek54dcaab2016-10-19 11:46:13 +010079const char* AssetManager::OVERLAY_THEME_DIR_PROPERTY = "ro.boot.vendor.overlay.theme";
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
Adam Lesinskia77685f2016-10-03 16:26:28 -070086String8 idmapPathForPackagePath(const String8& pkgPath) {
87 const char* root = getenv("ANDROID_DATA");
88 LOG_ALWAYS_FATAL_IF(root == NULL, "ANDROID_DATA not set");
89 String8 path(root);
90 path.appendPath(kResourceCache);
Adam Lesinski16c4d152014-01-24 13:27:13 -080091
Adam Lesinskia77685f2016-10-03 16:26:28 -070092 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;
Adam Lesinski16c4d152014-01-24 13:27:13 -080098 }
Adam Lesinskia77685f2016-10-03 16:26:28 -070099 char* p = filename;
100 while (*p) {
101 if (*p == '/') {
102 *p = '@';
103 }
104 ++p;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800105 }
Adam Lesinskia77685f2016-10-03 16:26:28 -0700106 path.appendPath(filename);
107 path.append("@idmap");
108
109 return path;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800110}
111
112/*
Adam Lesinskia77685f2016-10-03 16:26:28 -0700113 * Like strdup(), but uses C++ "new" operator instead of malloc.
114 */
115static char* strdupNew(const char* str) {
116 char* newStr;
117 int len;
118
119 if (str == NULL)
120 return NULL;
121
122 len = strlen(str);
123 newStr = new char[len+1];
124 memcpy(newStr, str, len+1);
125
126 return newStr;
127}
128
129} // namespace
130
131/*
Adam Lesinski16c4d152014-01-24 13:27:13 -0800132 * ===========================================================================
133 * AssetManager
134 * ===========================================================================
135 */
136
Adam Lesinskia77685f2016-10-03 16:26:28 -0700137int32_t AssetManager::getGlobalCount() {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800138 return gCount;
139}
140
Adam Lesinskia77685f2016-10-03 16:26:28 -0700141AssetManager::AssetManager() :
142 mLocale(NULL), mResources(NULL), mConfig(new ResTable_config) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700143 int count = android_atomic_inc(&gCount) + 1;
144 if (kIsDebug) {
145 ALOGI("Creating AssetManager %p #%d\n", this, count);
146 }
Adam Lesinski16c4d152014-01-24 13:27:13 -0800147 memset(mConfig, 0, sizeof(ResTable_config));
148}
149
Adam Lesinskia77685f2016-10-03 16:26:28 -0700150AssetManager::~AssetManager() {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800151 int count = android_atomic_dec(&gCount);
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700152 if (kIsDebug) {
153 ALOGI("Destroying AssetManager in %p #%d\n", this, count);
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000154 } else {
155 ALOGV("Destroying AssetManager in %p #%d\n", this, count);
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700156 }
Adam Lesinski16c4d152014-01-24 13:27:13 -0800157
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700158 // Manually close any fd paths for which we have not yet opened their zip (which
159 // will take ownership of the fd and close it when done).
160 for (size_t i=0; i<mAssetPaths.size(); i++) {
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000161 ALOGV("Cleaning path #%d: fd=%d, zip=%p", (int)i, mAssetPaths[i].rawFd,
162 mAssetPaths[i].zip.get());
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700163 if (mAssetPaths[i].rawFd >= 0 && mAssetPaths[i].zip == NULL) {
164 close(mAssetPaths[i].rawFd);
165 }
166 }
167
Adam Lesinski16c4d152014-01-24 13:27:13 -0800168 delete mConfig;
169 delete mResources;
170
171 // don't have a String class yet, so make sure we clean up
172 delete[] mLocale;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800173}
174
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800175bool AssetManager::addAssetPath(
Adam Lesinskia77685f2016-10-03 16:26:28 -0700176 const String8& path, int32_t* cookie, bool appAsLib, bool isSystemAsset) {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800177 AutoMutex _l(mLock);
178
179 asset_path ap;
180
181 String8 realPath(path);
182 if (kAppZipName) {
183 realPath.appendPath(kAppZipName);
184 }
185 ap.type = ::getFileType(realPath.string());
186 if (ap.type == kFileTypeRegular) {
187 ap.path = realPath;
188 } else {
189 ap.path = path;
190 ap.type = ::getFileType(path.string());
191 if (ap.type != kFileTypeDirectory && ap.type != kFileTypeRegular) {
192 ALOGW("Asset path %s is neither a directory nor file (type=%d).",
193 path.string(), (int)ap.type);
194 return false;
195 }
196 }
197
198 // Skip if we have it already.
199 for (size_t i=0; i<mAssetPaths.size(); i++) {
200 if (mAssetPaths[i].path == ap.path) {
201 if (cookie) {
Narayan Kamatha0c62602014-01-24 13:51:51 +0000202 *cookie = static_cast<int32_t>(i+1);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800203 }
204 return true;
205 }
206 }
207
208 ALOGV("In %p Asset %s path: %s", this,
209 ap.type == kFileTypeDirectory ? "dir" : "zip", ap.path.string());
210
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800211 ap.isSystemAsset = isSystemAsset;
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000212 ssize_t apPos = mAssetPaths.add(ap);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800213
214 // new paths are always added at the end
215 if (cookie) {
Narayan Kamatha0c62602014-01-24 13:51:51 +0000216 *cookie = static_cast<int32_t>(mAssetPaths.size());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800217 }
218
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +0900219#ifdef __ANDROID__
220 // Load overlays, if any
221 asset_path oap;
222 for (size_t idx = 0; mZipSet.getOverlay(ap.path, idx, &oap); idx++) {
223 oap.isSystemAsset = isSystemAsset;
224 mAssetPaths.add(oap);
225 }
226#endif
227
Martin Kosiba7df36252014-01-16 16:25:56 +0000228 if (mResources != NULL) {
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000229 appendPathToResTable(mAssetPaths.editItemAt(apPos), appAsLib);
Martin Kosiba7df36252014-01-16 16:25:56 +0000230 }
231
Adam Lesinski16c4d152014-01-24 13:27:13 -0800232 return true;
233}
234
Mårten Kongstad48d22322014-01-31 14:43:27 +0100235bool AssetManager::addOverlayPath(const String8& packagePath, int32_t* cookie)
236{
237 const String8 idmapPath = idmapPathForPackagePath(packagePath);
238
239 AutoMutex _l(mLock);
240
241 for (size_t i = 0; i < mAssetPaths.size(); ++i) {
242 if (mAssetPaths[i].idmap == idmapPath) {
243 *cookie = static_cast<int32_t>(i + 1);
244 return true;
245 }
246 }
247
248 Asset* idmap = NULL;
249 if ((idmap = openAssetFromFileLocked(idmapPath, Asset::ACCESS_BUFFER)) == NULL) {
250 ALOGW("failed to open idmap file %s\n", idmapPath.string());
251 return false;
252 }
253
254 String8 targetPath;
255 String8 overlayPath;
256 if (!ResTable::getIdmapInfo(idmap->getBuffer(false), idmap->getLength(),
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700257 NULL, NULL, NULL, &targetPath, &overlayPath)) {
Mårten Kongstad48d22322014-01-31 14:43:27 +0100258 ALOGW("failed to read idmap file %s\n", idmapPath.string());
259 delete idmap;
260 return false;
261 }
262 delete idmap;
263
264 if (overlayPath != packagePath) {
265 ALOGW("idmap file %s inconcistent: expected path %s does not match actual path %s\n",
266 idmapPath.string(), packagePath.string(), overlayPath.string());
267 return false;
268 }
269 if (access(targetPath.string(), R_OK) != 0) {
270 ALOGW("failed to access file %s: %s\n", targetPath.string(), strerror(errno));
271 return false;
272 }
273 if (access(idmapPath.string(), R_OK) != 0) {
274 ALOGW("failed to access file %s: %s\n", idmapPath.string(), strerror(errno));
275 return false;
276 }
277 if (access(overlayPath.string(), R_OK) != 0) {
278 ALOGW("failed to access file %s: %s\n", overlayPath.string(), strerror(errno));
279 return false;
280 }
281
282 asset_path oap;
283 oap.path = overlayPath;
284 oap.type = ::getFileType(overlayPath.string());
285 oap.idmap = idmapPath;
286#if 0
287 ALOGD("Overlay added: targetPath=%s overlayPath=%s idmapPath=%s\n",
288 targetPath.string(), overlayPath.string(), idmapPath.string());
289#endif
290 mAssetPaths.add(oap);
291 *cookie = static_cast<int32_t>(mAssetPaths.size());
292
Mårten Kongstad30113132014-11-07 10:52:17 +0100293 if (mResources != NULL) {
294 appendPathToResTable(oap);
295 }
296
Mårten Kongstad48d22322014-01-31 14:43:27 +0100297 return true;
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700298}
299
300bool AssetManager::addAssetFd(
301 int fd, const String8& debugPathName, int32_t* cookie, bool appAsLib,
302 bool assume_ownership) {
303 AutoMutex _l(mLock);
304
305 asset_path ap;
306
307 ap.path = debugPathName;
308 ap.rawFd = fd;
309 ap.type = kFileTypeRegular;
310 ap.assumeOwnership = assume_ownership;
311
312 ALOGV("In %p Asset fd %d name: %s", this, fd, ap.path.string());
313
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000314 ssize_t apPos = mAssetPaths.add(ap);
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700315
316 // new paths are always added at the end
317 if (cookie) {
318 *cookie = static_cast<int32_t>(mAssetPaths.size());
319 }
320
321 if (mResources != NULL) {
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000322 appendPathToResTable(mAssetPaths.editItemAt(apPos), appAsLib);
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700323 }
324
325 return true;
326}
Mårten Kongstad48d22322014-01-31 14:43:27 +0100327
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100328bool AssetManager::createIdmap(const char* targetApkPath, const char* overlayApkPath,
Dianne Hackborn32bb5fa2014-02-11 13:56:21 -0800329 uint32_t targetCrc, uint32_t overlayCrc, uint32_t** outData, size_t* outSize)
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100330{
331 AutoMutex _l(mLock);
332 const String8 paths[2] = { String8(targetApkPath), String8(overlayApkPath) };
Mårten Kongstad6bb13da2016-06-02 09:34:36 +0200333 Asset* assets[2] = {NULL, NULL};
334 bool ret = false;
335 {
336 ResTable tables[2];
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100337
Mårten Kongstad6bb13da2016-06-02 09:34:36 +0200338 for (int i = 0; i < 2; ++i) {
339 asset_path ap;
340 ap.type = kFileTypeRegular;
341 ap.path = paths[i];
342 assets[i] = openNonAssetInPathLocked("resources.arsc",
343 Asset::ACCESS_BUFFER, ap);
344 if (assets[i] == NULL) {
345 ALOGW("failed to find resources.arsc in %s\n", ap.path.string());
346 goto exit;
347 }
348 if (tables[i].add(assets[i]) != NO_ERROR) {
349 ALOGW("failed to add %s to resource table", paths[i].string());
350 goto exit;
351 }
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100352 }
Mårten Kongstad67d5c932018-05-25 15:58:17 +0200353 ret = tables[1].createIdmap(tables[0], targetCrc, overlayCrc,
Mårten Kongstad6bb13da2016-06-02 09:34:36 +0200354 targetApkPath, overlayApkPath, (void**)outData, outSize) == NO_ERROR;
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100355 }
356
Mårten Kongstad6bb13da2016-06-02 09:34:36 +0200357exit:
358 delete assets[0];
359 delete assets[1];
360 return ret;
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100361}
362
Adam Lesinski16c4d152014-01-24 13:27:13 -0800363bool AssetManager::addDefaultAssets()
364{
365 const char* root = getenv("ANDROID_ROOT");
366 LOG_ALWAYS_FATAL_IF(root == NULL, "ANDROID_ROOT not set");
367
368 String8 path(root);
369 path.appendPath(kSystemAssets);
370
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800371 return addAssetPath(path, NULL, false /* appAsLib */, true /* isSystemAsset */);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800372}
373
Narayan Kamatha0c62602014-01-24 13:51:51 +0000374int32_t AssetManager::nextAssetPath(const int32_t cookie) const
Adam Lesinski16c4d152014-01-24 13:27:13 -0800375{
376 AutoMutex _l(mLock);
Narayan Kamatha0c62602014-01-24 13:51:51 +0000377 const size_t next = static_cast<size_t>(cookie) + 1;
378 return next > mAssetPaths.size() ? -1 : next;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800379}
380
Narayan Kamatha0c62602014-01-24 13:51:51 +0000381String8 AssetManager::getAssetPath(const int32_t cookie) const
Adam Lesinski16c4d152014-01-24 13:27:13 -0800382{
383 AutoMutex _l(mLock);
Narayan Kamatha0c62602014-01-24 13:51:51 +0000384 const size_t which = static_cast<size_t>(cookie) - 1;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800385 if (which < mAssetPaths.size()) {
386 return mAssetPaths[which].path;
387 }
388 return String8();
389}
390
Adam Lesinski16c4d152014-01-24 13:27:13 -0800391void AssetManager::setLocaleLocked(const char* locale)
392{
393 if (mLocale != NULL) {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800394 delete[] mLocale;
395 }
Elliott Hughesc367d482013-10-29 13:12:55 -0700396
Adam Lesinski16c4d152014-01-24 13:27:13 -0800397 mLocale = strdupNew(locale);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800398 updateResourceParamsLocked();
399}
400
Adam Lesinski16c4d152014-01-24 13:27:13 -0800401void AssetManager::setConfiguration(const ResTable_config& config, const char* locale)
402{
403 AutoMutex _l(mLock);
404 *mConfig = config;
405 if (locale) {
406 setLocaleLocked(locale);
407 } else if (config.language[0] != 0) {
Narayan Kamath91447d82014-01-21 15:32:36 +0000408 char spec[RESTABLE_MAX_LOCALE_LEN];
409 config.getBcp47Locale(spec);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800410 setLocaleLocked(spec);
411 } else {
412 updateResourceParamsLocked();
413 }
414}
415
416void AssetManager::getConfiguration(ResTable_config* outConfig) const
417{
418 AutoMutex _l(mLock);
419 *outConfig = *mConfig;
420}
421
422/*
423 * Open an asset.
424 *
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700425 * The data could be in any asset path. Each asset path could be:
426 * - A directory on disk.
427 * - A Zip archive, uncompressed or compressed.
Adam Lesinski16c4d152014-01-24 13:27:13 -0800428 *
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700429 * If the file is in a directory, it could have a .gz suffix, meaning it is compressed.
Adam Lesinski16c4d152014-01-24 13:27:13 -0800430 *
431 * We should probably reject requests for "illegal" filenames, e.g. those
432 * with illegal characters or "../" backward relative paths.
433 */
434Asset* AssetManager::open(const char* fileName, AccessMode mode)
435{
436 AutoMutex _l(mLock);
437
438 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
439
Adam Lesinski16c4d152014-01-24 13:27:13 -0800440 String8 assetName(kAssetsRoot);
441 assetName.appendPath(fileName);
442
443 /*
444 * For each top-level asset path, search for the asset.
445 */
446
447 size_t i = mAssetPaths.size();
448 while (i > 0) {
449 i--;
450 ALOGV("Looking for asset '%s' in '%s'\n",
451 assetName.string(), mAssetPaths.itemAt(i).path.string());
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000452 Asset* pAsset = openNonAssetInPathLocked(assetName.string(), mode,
453 mAssetPaths.editItemAt(i));
Adam Lesinski16c4d152014-01-24 13:27:13 -0800454 if (pAsset != NULL) {
455 return pAsset != kExcludedAsset ? pAsset : NULL;
456 }
457 }
458
459 return NULL;
460}
461
462/*
463 * Open a non-asset file as if it were an asset.
464 *
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700465 * The "fileName" is the partial path starting from the application name.
Adam Lesinski16c4d152014-01-24 13:27:13 -0800466 */
Adam Lesinskide898ff2014-01-29 18:20:45 -0800467Asset* AssetManager::openNonAsset(const char* fileName, AccessMode mode, int32_t* outCookie)
Adam Lesinski16c4d152014-01-24 13:27:13 -0800468{
469 AutoMutex _l(mLock);
470
471 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
472
Adam Lesinski16c4d152014-01-24 13:27:13 -0800473 /*
474 * For each top-level asset path, search for the asset.
475 */
476
477 size_t i = mAssetPaths.size();
478 while (i > 0) {
479 i--;
480 ALOGV("Looking for non-asset '%s' in '%s'\n", fileName, mAssetPaths.itemAt(i).path.string());
481 Asset* pAsset = openNonAssetInPathLocked(
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000482 fileName, mode, mAssetPaths.editItemAt(i));
Adam Lesinski16c4d152014-01-24 13:27:13 -0800483 if (pAsset != NULL) {
Adam Lesinskide898ff2014-01-29 18:20:45 -0800484 if (outCookie != NULL) *outCookie = static_cast<int32_t>(i + 1);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800485 return pAsset != kExcludedAsset ? pAsset : NULL;
486 }
487 }
488
489 return NULL;
490}
491
Narayan Kamatha0c62602014-01-24 13:51:51 +0000492Asset* AssetManager::openNonAsset(const int32_t cookie, const char* fileName, AccessMode mode)
Adam Lesinski16c4d152014-01-24 13:27:13 -0800493{
Narayan Kamatha0c62602014-01-24 13:51:51 +0000494 const size_t which = static_cast<size_t>(cookie) - 1;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800495
496 AutoMutex _l(mLock);
497
498 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
499
Adam Lesinski16c4d152014-01-24 13:27:13 -0800500 if (which < mAssetPaths.size()) {
501 ALOGV("Looking for non-asset '%s' in '%s'\n", fileName,
502 mAssetPaths.itemAt(which).path.string());
503 Asset* pAsset = openNonAssetInPathLocked(
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000504 fileName, mode, mAssetPaths.editItemAt(which));
Adam Lesinski16c4d152014-01-24 13:27:13 -0800505 if (pAsset != NULL) {
506 return pAsset != kExcludedAsset ? pAsset : NULL;
507 }
508 }
509
510 return NULL;
511}
512
513/*
514 * Get the type of a file in the asset namespace.
515 *
516 * This currently only works for regular files. All others (including
517 * directories) will return kFileTypeNonexistent.
518 */
519FileType AssetManager::getFileType(const char* fileName)
520{
521 Asset* pAsset = NULL;
522
523 /*
524 * Open the asset. This is less efficient than simply finding the
525 * file, but it's not too bad (we don't uncompress or mmap data until
526 * the first read() call).
527 */
528 pAsset = open(fileName, Asset::ACCESS_STREAMING);
529 delete pAsset;
530
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700531 if (pAsset == NULL) {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800532 return kFileTypeNonexistent;
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700533 } else {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800534 return kFileTypeRegular;
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700535 }
Adam Lesinski16c4d152014-01-24 13:27:13 -0800536}
537
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000538bool AssetManager::appendPathToResTable(asset_path& ap, bool appAsLib) const {
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +0900539 // skip those ap's that correspond to system overlays
540 if (ap.isSystemOverlay) {
541 return true;
542 }
543
Martin Kosiba7df36252014-01-16 16:25:56 +0000544 Asset* ass = NULL;
545 ResTable* sharedRes = NULL;
546 bool shared = true;
547 bool onlyEmptyResources = true;
Adam Lesinskib7e1ce02016-04-11 20:03:01 -0700548 ATRACE_NAME(ap.path.string());
Martin Kosiba7df36252014-01-16 16:25:56 +0000549 Asset* idmap = openIdmapLocked(ap);
550 size_t nextEntryIdx = mResources->getTableCount();
551 ALOGV("Looking for resource asset in '%s'\n", ap.path.string());
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700552 if (ap.type != kFileTypeDirectory && ap.rawFd < 0) {
Martin Kosiba7df36252014-01-16 16:25:56 +0000553 if (nextEntryIdx == 0) {
554 // The first item is typically the framework resources,
555 // which we want to avoid parsing every time.
556 sharedRes = const_cast<AssetManager*>(this)->
557 mZipSet.getZipResourceTable(ap.path);
558 if (sharedRes != NULL) {
559 // skip ahead the number of system overlay packages preloaded
560 nextEntryIdx = sharedRes->getTableCount();
561 }
562 }
563 if (sharedRes == NULL) {
564 ass = const_cast<AssetManager*>(this)->
565 mZipSet.getZipResourceTableAsset(ap.path);
566 if (ass == NULL) {
567 ALOGV("loading resource table %s\n", ap.path.string());
568 ass = const_cast<AssetManager*>(this)->
569 openNonAssetInPathLocked("resources.arsc",
570 Asset::ACCESS_BUFFER,
571 ap);
572 if (ass != NULL && ass != kExcludedAsset) {
573 ass = const_cast<AssetManager*>(this)->
574 mZipSet.setZipResourceTableAsset(ap.path, ass);
575 }
576 }
577
578 if (nextEntryIdx == 0 && ass != NULL) {
579 // If this is the first resource table in the asset
580 // manager, then we are going to cache it so that we
581 // can quickly copy it out for others.
582 ALOGV("Creating shared resources for %s", ap.path.string());
583 sharedRes = new ResTable();
584 sharedRes->add(ass, idmap, nextEntryIdx + 1, false);
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +0900585#ifdef __ANDROID__
586 const char* data = getenv("ANDROID_DATA");
587 LOG_ALWAYS_FATAL_IF(data == NULL, "ANDROID_DATA not set");
588 String8 overlaysListPath(data);
589 overlaysListPath.appendPath(kResourceCache);
590 overlaysListPath.appendPath("overlays.list");
591 addSystemOverlays(overlaysListPath.string(), ap.path, sharedRes, nextEntryIdx);
592#endif
Martin Kosiba7df36252014-01-16 16:25:56 +0000593 sharedRes = const_cast<AssetManager*>(this)->
594 mZipSet.setZipResourceTable(ap.path, sharedRes);
595 }
596 }
597 } else {
598 ALOGV("loading resource table %s\n", ap.path.string());
599 ass = const_cast<AssetManager*>(this)->
600 openNonAssetInPathLocked("resources.arsc",
601 Asset::ACCESS_BUFFER,
602 ap);
603 shared = false;
604 }
605
606 if ((ass != NULL || sharedRes != NULL) && ass != kExcludedAsset) {
607 ALOGV("Installing resource asset %p in to table %p\n", ass, mResources);
608 if (sharedRes != NULL) {
609 ALOGV("Copying existing resources for %s", ap.path.string());
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800610 mResources->add(sharedRes, ap.isSystemAsset);
Martin Kosiba7df36252014-01-16 16:25:56 +0000611 } else {
612 ALOGV("Parsing resources for %s", ap.path.string());
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800613 mResources->add(ass, idmap, nextEntryIdx + 1, !shared, appAsLib, ap.isSystemAsset);
Martin Kosiba7df36252014-01-16 16:25:56 +0000614 }
615 onlyEmptyResources = false;
616
617 if (!shared) {
618 delete ass;
619 }
620 } else {
621 ALOGV("Installing empty resources in to table %p\n", mResources);
622 mResources->addEmpty(nextEntryIdx + 1);
623 }
624
625 if (idmap != NULL) {
626 delete idmap;
627 }
Martin Kosiba7df36252014-01-16 16:25:56 +0000628 return onlyEmptyResources;
629}
630
Adam Lesinski16c4d152014-01-24 13:27:13 -0800631const ResTable* AssetManager::getResTable(bool required) const
632{
633 ResTable* rt = mResources;
634 if (rt) {
635 return rt;
636 }
637
638 // Iterate through all asset packages, collecting resources from each.
639
640 AutoMutex _l(mLock);
641
642 if (mResources != NULL) {
643 return mResources;
644 }
645
646 if (required) {
647 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
648 }
649
Adam Lesinskide898ff2014-01-29 18:20:45 -0800650 mResources = new ResTable();
651 updateResourceParamsLocked();
652
653 bool onlyEmptyResources = true;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800654 const size_t N = mAssetPaths.size();
655 for (size_t i=0; i<N; i++) {
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000656 bool empty = appendPathToResTable(
657 const_cast<AssetManager*>(this)->mAssetPaths.editItemAt(i));
Martin Kosiba7df36252014-01-16 16:25:56 +0000658 onlyEmptyResources = onlyEmptyResources && empty;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800659 }
660
Adam Lesinskide898ff2014-01-29 18:20:45 -0800661 if (required && onlyEmptyResources) {
662 ALOGW("Unable to find resources file resources.arsc");
663 delete mResources;
664 mResources = NULL;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800665 }
Adam Lesinskide898ff2014-01-29 18:20:45 -0800666
667 return mResources;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800668}
669
670void AssetManager::updateResourceParamsLocked() const
671{
Adam Lesinskib7e1ce02016-04-11 20:03:01 -0700672 ATRACE_CALL();
Adam Lesinski16c4d152014-01-24 13:27:13 -0800673 ResTable* res = mResources;
674 if (!res) {
675 return;
676 }
677
Narayan Kamath91447d82014-01-21 15:32:36 +0000678 if (mLocale) {
679 mConfig->setBcp47Locale(mLocale);
680 } else {
681 mConfig->clearLocale();
Adam Lesinski16c4d152014-01-24 13:27:13 -0800682 }
Adam Lesinski16c4d152014-01-24 13:27:13 -0800683
684 res->setParameters(mConfig);
685}
686
687Asset* AssetManager::openIdmapLocked(const struct asset_path& ap) const
688{
689 Asset* ass = NULL;
690 if (ap.idmap.size() != 0) {
691 ass = const_cast<AssetManager*>(this)->
692 openAssetFromFileLocked(ap.idmap, Asset::ACCESS_BUFFER);
693 if (ass) {
694 ALOGV("loading idmap %s\n", ap.idmap.string());
695 } else {
696 ALOGW("failed to load idmap %s\n", ap.idmap.string());
697 }
698 }
699 return ass;
700}
701
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +0900702void AssetManager::addSystemOverlays(const char* pathOverlaysList,
703 const String8& targetPackagePath, ResTable* sharedRes, size_t offset) const
704{
705 FILE* fin = fopen(pathOverlaysList, "r");
706 if (fin == NULL) {
707 return;
708 }
709
710#ifndef _WIN32
711 if (TEMP_FAILURE_RETRY(flock(fileno(fin), LOCK_SH)) != 0) {
712 fclose(fin);
713 return;
714 }
715#endif
716 char buf[1024];
717 while (fgets(buf, sizeof(buf), fin)) {
718 // format of each line:
719 // <path to apk><space><path to idmap><newline>
720 char* space = strchr(buf, ' ');
721 char* newline = strchr(buf, '\n');
722 asset_path oap;
723
724 if (space == NULL || newline == NULL || newline < space) {
725 continue;
726 }
727
728 oap.path = String8(buf, space - buf);
729 oap.type = kFileTypeRegular;
730 oap.idmap = String8(space + 1, newline - space - 1);
731 oap.isSystemOverlay = true;
732
733 Asset* oass = const_cast<AssetManager*>(this)->
734 openNonAssetInPathLocked("resources.arsc",
735 Asset::ACCESS_BUFFER,
736 oap);
737
738 if (oass != NULL) {
739 Asset* oidmap = openIdmapLocked(oap);
740 offset++;
741 sharedRes->add(oass, oidmap, offset + 1, false);
742 const_cast<AssetManager*>(this)->mAssetPaths.add(oap);
743 const_cast<AssetManager*>(this)->mZipSet.addOverlay(targetPackagePath, oap);
744 delete oidmap;
745 }
746 }
747
748#ifndef _WIN32
749 TEMP_FAILURE_RETRY(flock(fileno(fin), LOCK_UN));
750#endif
751 fclose(fin);
752}
753
Adam Lesinski16c4d152014-01-24 13:27:13 -0800754const ResTable& AssetManager::getResources(bool required) const
755{
756 const ResTable* rt = getResTable(required);
757 return *rt;
758}
759
760bool AssetManager::isUpToDate()
761{
762 AutoMutex _l(mLock);
763 return mZipSet.isUpToDate();
764}
765
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800766void AssetManager::getLocales(Vector<String8>* locales, bool includeSystemLocales) const
Adam Lesinski16c4d152014-01-24 13:27:13 -0800767{
768 ResTable* res = mResources;
769 if (res != NULL) {
Roozbeh Pournader7e5f96f2016-06-13 18:10:49 -0700770 res->getLocales(locales, includeSystemLocales, true /* mergeEquivalentLangs */);
Narayan Kamathe4345db2014-06-26 16:01:28 +0100771 }
Adam Lesinski16c4d152014-01-24 13:27:13 -0800772}
773
774/*
775 * Open a non-asset file as if it were an asset, searching for it in the
776 * specified app.
777 *
778 * Pass in a NULL values for "appName" if the common app directory should
779 * be used.
780 */
781Asset* AssetManager::openNonAssetInPathLocked(const char* fileName, AccessMode mode,
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000782 asset_path& ap)
Adam Lesinski16c4d152014-01-24 13:27:13 -0800783{
784 Asset* pAsset = NULL;
785
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700786 ALOGV("openNonAssetInPath: name=%s type=%d fd=%d", fileName, ap.type, ap.rawFd);
787
Adam Lesinski16c4d152014-01-24 13:27:13 -0800788 /* look at the filesystem on disk */
789 if (ap.type == kFileTypeDirectory) {
790 String8 path(ap.path);
791 path.appendPath(fileName);
792
793 pAsset = openAssetFromFileLocked(path, mode);
794
795 if (pAsset == NULL) {
796 /* try again, this time with ".gz" */
797 path.append(".gz");
798 pAsset = openAssetFromFileLocked(path, mode);
799 }
800
801 if (pAsset != NULL) {
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700802 ALOGV("FOUND NA '%s' on disk", fileName);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800803 pAsset->setAssetSource(path);
804 }
805
806 /* look inside the zip file */
807 } else {
808 String8 path(fileName);
809
810 /* check the appropriate Zip file */
Narayan Kamath560566d2013-12-03 13:16:03 +0000811 ZipFileRO* pZip = getZipFileLocked(ap);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800812 if (pZip != NULL) {
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700813 ALOGV("GOT zip, checking NA '%s'", (const char*) path);
Narayan Kamath560566d2013-12-03 13:16:03 +0000814 ZipEntryRO entry = pZip->findEntryByName(path.string());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800815 if (entry != NULL) {
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700816 ALOGV("FOUND NA in Zip file for %s", (const char*) path);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800817 pAsset = openAssetFromZipLocked(pZip, entry, mode, path);
Narayan Kamath560566d2013-12-03 13:16:03 +0000818 pZip->releaseEntry(entry);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800819 }
820 }
821
822 if (pAsset != NULL) {
823 /* create a "source" name, for debug/display */
824 pAsset->setAssetSource(
825 createZipSourceNameLocked(ZipSet::getPathName(ap.path.string()), String8(""),
826 String8(fileName)));
827 }
828 }
829
830 return pAsset;
831}
832
833/*
Adam Lesinski16c4d152014-01-24 13:27:13 -0800834 * Create a "source name" for a file from a Zip archive.
835 */
836String8 AssetManager::createZipSourceNameLocked(const String8& zipFileName,
837 const String8& dirName, const String8& fileName)
838{
839 String8 sourceName("zip:");
840 sourceName.append(zipFileName);
841 sourceName.append(":");
842 if (dirName.length() > 0) {
843 sourceName.appendPath(dirName);
844 }
845 sourceName.appendPath(fileName);
846 return sourceName;
847}
848
849/*
Adam Lesinski16c4d152014-01-24 13:27:13 -0800850 * Create a path to a loose asset (asset-base/app/rootDir).
851 */
852String8 AssetManager::createPathNameLocked(const asset_path& ap, const char* rootDir)
853{
854 String8 path(ap.path);
855 if (rootDir != NULL) path.appendPath(rootDir);
856 return path;
857}
858
859/*
860 * Return a pointer to one of our open Zip archives. Returns NULL if no
861 * matching Zip file exists.
Adam Lesinski16c4d152014-01-24 13:27:13 -0800862 */
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000863ZipFileRO* AssetManager::getZipFileLocked(asset_path& ap)
Adam Lesinski16c4d152014-01-24 13:27:13 -0800864{
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000865 ALOGV("getZipFileLocked() in %p: ap=%p zip=%p", this, &ap, ap.zip.get());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800866
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700867 if (ap.zip != NULL) {
868 return ap.zip->getZip();
869 }
870
871 if (ap.rawFd < 0) {
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000872 ALOGV("getZipFileLocked: Creating new zip from path %s", ap.path.string());
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700873 ap.zip = mZipSet.getSharedZip(ap.path);
874 } else {
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000875 ALOGV("getZipFileLocked: Creating new zip from fd %d", ap.rawFd);
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700876 ap.zip = SharedZip::create(ap.rawFd, ap.path);
877
878 }
879 return ap.zip != NULL ? ap.zip->getZip() : NULL;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800880}
881
882/*
883 * Try to open an asset from a file on disk.
884 *
885 * If the file is compressed with gzip, we seek to the start of the
886 * deflated data and pass that in (just like we would for a Zip archive).
887 *
888 * For uncompressed data, we may already have an mmap()ed version sitting
889 * around. If so, we want to hand that to the Asset instead.
890 *
891 * This returns NULL if the file doesn't exist, couldn't be opened, or
892 * claims to be a ".gz" but isn't.
893 */
894Asset* AssetManager::openAssetFromFileLocked(const String8& pathName,
895 AccessMode mode)
896{
897 Asset* pAsset = NULL;
898
899 if (strcasecmp(pathName.getPathExtension().string(), ".gz") == 0) {
900 //printf("TRYING '%s'\n", (const char*) pathName);
901 pAsset = Asset::createFromCompressedFile(pathName.string(), mode);
902 } else {
903 //printf("TRYING '%s'\n", (const char*) pathName);
904 pAsset = Asset::createFromFile(pathName.string(), mode);
905 }
906
907 return pAsset;
908}
909
910/*
911 * Given an entry in a Zip archive, create a new Asset object.
912 *
913 * If the entry is uncompressed, we may want to create or share a
914 * slice of shared memory.
915 */
916Asset* AssetManager::openAssetFromZipLocked(const ZipFileRO* pZipFile,
917 const ZipEntryRO entry, AccessMode mode, const String8& entryName)
918{
919 Asset* pAsset = NULL;
920
921 // TODO: look for previously-created shared memory slice?
Narayan Kamath407753c2015-06-16 12:02:57 +0100922 uint16_t method;
923 uint32_t uncompressedLen;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800924
925 //printf("USING Zip '%s'\n", pEntry->getFileName());
926
Adam Lesinski16c4d152014-01-24 13:27:13 -0800927 if (!pZipFile->getEntryInfo(entry, &method, &uncompressedLen, NULL, NULL,
928 NULL, NULL))
929 {
930 ALOGW("getEntryInfo failed\n");
931 return NULL;
932 }
933
934 FileMap* dataMap = pZipFile->createEntryFileMap(entry);
935 if (dataMap == NULL) {
936 ALOGW("create map from entry failed\n");
937 return NULL;
938 }
939
940 if (method == ZipFileRO::kCompressStored) {
941 pAsset = Asset::createFromUncompressedMap(dataMap, mode);
942 ALOGV("Opened uncompressed entry %s in zip %s mode %d: %p", entryName.string(),
943 dataMap->getFileName(), mode, pAsset);
944 } else {
Narayan Kamath407753c2015-06-16 12:02:57 +0100945 pAsset = Asset::createFromCompressedMap(dataMap,
946 static_cast<size_t>(uncompressedLen), mode);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800947 ALOGV("Opened compressed entry %s in zip %s mode %d: %p", entryName.string(),
948 dataMap->getFileName(), mode, pAsset);
949 }
950 if (pAsset == NULL) {
951 /* unexpected */
952 ALOGW("create from segment failed\n");
953 }
954
955 return pAsset;
956}
957
Adam Lesinski16c4d152014-01-24 13:27:13 -0800958/*
959 * Open a directory in the 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 */
965AssetDir* AssetManager::openDir(const char* dirName)
966{
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 /*
980 * Scan the various directories, merging what we find into a single
981 * vector. We want to scan them in reverse priority order so that
982 * the ".EXCLUDE" processing works correctly. Also, if we decide we
983 * want to remember where the file is coming from, we'll get the right
984 * version.
985 *
986 * We start with Zip archives, then do loose files.
987 */
988 pMergedInfo = new SortedVector<AssetDir::FileInfo>;
989
990 size_t i = mAssetPaths.size();
991 while (i > 0) {
992 i--;
993 const asset_path& ap = mAssetPaths.itemAt(i);
994 if (ap.type == kFileTypeRegular) {
995 ALOGV("Adding directory %s from zip %s", dirName, ap.path.string());
996 scanAndMergeZipLocked(pMergedInfo, ap, kAssetsRoot, dirName);
997 } else {
998 ALOGV("Adding directory %s from dir %s", dirName, ap.path.string());
999 scanAndMergeDirLocked(pMergedInfo, ap, kAssetsRoot, dirName);
1000 }
1001 }
1002
1003#if 0
1004 printf("FILE LIST:\n");
1005 for (i = 0; i < (size_t) pMergedInfo->size(); i++) {
1006 printf(" %d: (%d) '%s'\n", i,
1007 pMergedInfo->itemAt(i).getFileType(),
1008 (const char*) pMergedInfo->itemAt(i).getFileName());
1009 }
1010#endif
1011
1012 pDir->setFileList(pMergedInfo);
1013 return pDir;
1014}
1015
1016/*
1017 * Open a directory in the non-asset namespace.
1018 *
Adam Lesinskife90eaf2016-10-04 13:31:31 -07001019 * An "asset directory" is simply the combination of all asset paths' "assets/" directories.
Adam Lesinski16c4d152014-01-24 13:27:13 -08001020 *
1021 * Pass in "" for the root dir.
1022 */
Narayan Kamatha0c62602014-01-24 13:51:51 +00001023AssetDir* AssetManager::openNonAssetDir(const int32_t cookie, const char* dirName)
Adam Lesinski16c4d152014-01-24 13:27:13 -08001024{
1025 AutoMutex _l(mLock);
1026
1027 AssetDir* pDir = NULL;
1028 SortedVector<AssetDir::FileInfo>* pMergedInfo = NULL;
1029
1030 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
1031 assert(dirName != NULL);
1032
1033 //printf("+++ openDir(%s) in '%s'\n", dirName, (const char*) mAssetBase);
1034
Adam Lesinski16c4d152014-01-24 13:27:13 -08001035 pDir = new AssetDir;
1036
1037 pMergedInfo = new SortedVector<AssetDir::FileInfo>;
1038
Narayan Kamatha0c62602014-01-24 13:51:51 +00001039 const size_t which = static_cast<size_t>(cookie) - 1;
Adam Lesinski16c4d152014-01-24 13:27:13 -08001040
1041 if (which < mAssetPaths.size()) {
1042 const asset_path& ap = mAssetPaths.itemAt(which);
1043 if (ap.type == kFileTypeRegular) {
1044 ALOGV("Adding directory %s from zip %s", dirName, ap.path.string());
1045 scanAndMergeZipLocked(pMergedInfo, ap, NULL, dirName);
1046 } else {
1047 ALOGV("Adding directory %s from dir %s", dirName, ap.path.string());
1048 scanAndMergeDirLocked(pMergedInfo, ap, NULL, dirName);
1049 }
1050 }
1051
1052#if 0
1053 printf("FILE LIST:\n");
1054 for (i = 0; i < (size_t) pMergedInfo->size(); i++) {
1055 printf(" %d: (%d) '%s'\n", i,
1056 pMergedInfo->itemAt(i).getFileType(),
1057 (const char*) pMergedInfo->itemAt(i).getFileName());
1058 }
1059#endif
1060
1061 pDir->setFileList(pMergedInfo);
1062 return pDir;
1063}
1064
1065/*
1066 * Scan the contents of the specified directory and merge them into the
1067 * "pMergedInfo" vector, removing previous entries if we find "exclude"
1068 * directives.
1069 *
1070 * Returns "false" if we found nothing to contribute.
1071 */
1072bool AssetManager::scanAndMergeDirLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
1073 const asset_path& ap, const char* rootDir, const char* dirName)
1074{
Adam Lesinski16c4d152014-01-24 13:27:13 -08001075 assert(pMergedInfo != NULL);
1076
Adam Lesinskia77685f2016-10-03 16:26:28 -07001077 //printf("scanAndMergeDir: %s %s %s\n", ap.path.string(), rootDir, dirName);
Adam Lesinski16c4d152014-01-24 13:27:13 -08001078
Adam Lesinskia77685f2016-10-03 16:26:28 -07001079 String8 path = createPathNameLocked(ap, rootDir);
1080 if (dirName[0] != '\0')
1081 path.appendPath(dirName);
Adam Lesinski16c4d152014-01-24 13:27:13 -08001082
Adam Lesinskia77685f2016-10-03 16:26:28 -07001083 SortedVector<AssetDir::FileInfo>* pContents = scanDirLocked(path);
1084 if (pContents == NULL)
1085 return false;
Adam Lesinski16c4d152014-01-24 13:27:13 -08001086
1087 // if we wanted to do an incremental cache fill, we would do it here
1088
1089 /*
1090 * Process "exclude" directives. If we find a filename that ends with
1091 * ".EXCLUDE", we look for a matching entry in the "merged" set, and
1092 * remove it if we find it. We also delete the "exclude" entry.
1093 */
1094 int i, count, exclExtLen;
1095
1096 count = pContents->size();
1097 exclExtLen = strlen(kExcludeExtension);
1098 for (i = 0; i < count; i++) {
1099 const char* name;
1100 int nameLen;
1101
1102 name = pContents->itemAt(i).getFileName().string();
1103 nameLen = strlen(name);
1104 if (nameLen > exclExtLen &&
1105 strcmp(name + (nameLen - exclExtLen), kExcludeExtension) == 0)
1106 {
1107 String8 match(name, nameLen - exclExtLen);
1108 int matchIdx;
1109
1110 matchIdx = AssetDir::FileInfo::findEntry(pMergedInfo, match);
1111 if (matchIdx > 0) {
1112 ALOGV("Excluding '%s' [%s]\n",
1113 pMergedInfo->itemAt(matchIdx).getFileName().string(),
1114 pMergedInfo->itemAt(matchIdx).getSourceName().string());
1115 pMergedInfo->removeAt(matchIdx);
1116 } else {
1117 //printf("+++ no match on '%s'\n", (const char*) match);
1118 }
1119
1120 ALOGD("HEY: size=%d removing %d\n", (int)pContents->size(), i);
1121 pContents->removeAt(i);
1122 i--; // adjust "for" loop
1123 count--; // and loop limit
1124 }
1125 }
1126
1127 mergeInfoLocked(pMergedInfo, pContents);
1128
1129 delete pContents;
1130
1131 return true;
1132}
1133
1134/*
1135 * Scan the contents of the specified directory, and stuff what we find
1136 * into a newly-allocated vector.
1137 *
1138 * Files ending in ".gz" will have their extensions removed.
1139 *
1140 * We should probably think about skipping files with "illegal" names,
1141 * e.g. illegal characters (/\:) or excessive length.
1142 *
1143 * Returns NULL if the specified directory doesn't exist.
1144 */
1145SortedVector<AssetDir::FileInfo>* AssetManager::scanDirLocked(const String8& path)
1146{
1147 SortedVector<AssetDir::FileInfo>* pContents = NULL;
1148 DIR* dir;
1149 struct dirent* entry;
1150 FileType fileType;
1151
1152 ALOGV("Scanning dir '%s'\n", path.string());
1153
1154 dir = opendir(path.string());
1155 if (dir == NULL)
1156 return NULL;
1157
1158 pContents = new SortedVector<AssetDir::FileInfo>;
1159
1160 while (1) {
1161 entry = readdir(dir);
1162 if (entry == NULL)
1163 break;
1164
1165 if (strcmp(entry->d_name, ".") == 0 ||
1166 strcmp(entry->d_name, "..") == 0)
1167 continue;
1168
1169#ifdef _DIRENT_HAVE_D_TYPE
1170 if (entry->d_type == DT_REG)
1171 fileType = kFileTypeRegular;
1172 else if (entry->d_type == DT_DIR)
1173 fileType = kFileTypeDirectory;
1174 else
1175 fileType = kFileTypeUnknown;
1176#else
1177 // stat the file
1178 fileType = ::getFileType(path.appendPathCopy(entry->d_name).string());
1179#endif
1180
1181 if (fileType != kFileTypeRegular && fileType != kFileTypeDirectory)
1182 continue;
1183
1184 AssetDir::FileInfo info;
1185 info.set(String8(entry->d_name), fileType);
1186 if (strcasecmp(info.getFileName().getPathExtension().string(), ".gz") == 0)
1187 info.setFileName(info.getFileName().getBasePath());
1188 info.setSourceName(path.appendPathCopy(info.getFileName()));
1189 pContents->add(info);
1190 }
1191
1192 closedir(dir);
1193 return pContents;
1194}
1195
1196/*
1197 * Scan the contents out of the specified Zip archive, and merge what we
1198 * find into "pMergedInfo". If the Zip archive in question doesn't exist,
1199 * we return immediately.
1200 *
1201 * Returns "false" if we found nothing to contribute.
1202 */
1203bool AssetManager::scanAndMergeZipLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
1204 const asset_path& ap, const char* rootDir, const char* baseDirName)
1205{
1206 ZipFileRO* pZip;
1207 Vector<String8> dirs;
1208 AssetDir::FileInfo info;
1209 SortedVector<AssetDir::FileInfo> contents;
1210 String8 sourceName, zipName, dirName;
1211
1212 pZip = mZipSet.getZip(ap.path);
1213 if (pZip == NULL) {
1214 ALOGW("Failure opening zip %s\n", ap.path.string());
1215 return false;
1216 }
1217
1218 zipName = ZipSet::getPathName(ap.path.string());
1219
1220 /* convert "sounds" to "rootDir/sounds" */
1221 if (rootDir != NULL) dirName = rootDir;
1222 dirName.appendPath(baseDirName);
1223
1224 /*
1225 * Scan through the list of files, looking for a match. The files in
1226 * the Zip table of contents are not in sorted order, so we have to
1227 * process the entire list. We're looking for a string that begins
1228 * with the characters in "dirName", is followed by a '/', and has no
1229 * subsequent '/' in the stuff that follows.
1230 *
1231 * What makes this especially fun is that directories are not stored
1232 * explicitly in Zip archives, so we have to infer them from context.
1233 * When we see "sounds/foo.wav" we have to leave a note to ourselves
1234 * to insert a directory called "sounds" into the list. We store
1235 * these in temporary vector so that we only return each one once.
1236 *
1237 * Name comparisons are case-sensitive to match UNIX filesystem
1238 * semantics.
1239 */
1240 int dirNameLen = dirName.length();
Narayan Kamath560566d2013-12-03 13:16:03 +00001241 void *iterationCookie;
Yusuke Sato05f648e2015-08-03 16:21:10 -07001242 if (!pZip->startIteration(&iterationCookie, dirName.string(), NULL)) {
Narayan Kamath560566d2013-12-03 13:16:03 +00001243 ALOGW("ZipFileRO::startIteration returned false");
1244 return false;
1245 }
1246
1247 ZipEntryRO entry;
1248 while ((entry = pZip->nextEntry(iterationCookie)) != NULL) {
Adam Lesinski16c4d152014-01-24 13:27:13 -08001249 char nameBuf[256];
1250
Adam Lesinski16c4d152014-01-24 13:27:13 -08001251 if (pZip->getEntryFileName(entry, nameBuf, sizeof(nameBuf)) != 0) {
1252 // TODO: fix this if we expect to have long names
1253 ALOGE("ARGH: name too long?\n");
1254 continue;
1255 }
1256 //printf("Comparing %s in %s?\n", nameBuf, dirName.string());
Yusuke Sato05f648e2015-08-03 16:21:10 -07001257 if (dirNameLen == 0 || nameBuf[dirNameLen] == '/')
Adam Lesinski16c4d152014-01-24 13:27:13 -08001258 {
1259 const char* cp;
1260 const char* nextSlash;
1261
1262 cp = nameBuf + dirNameLen;
1263 if (dirNameLen != 0)
1264 cp++; // advance past the '/'
1265
1266 nextSlash = strchr(cp, '/');
1267//xxx this may break if there are bare directory entries
1268 if (nextSlash == NULL) {
1269 /* this is a file in the requested directory */
1270
1271 info.set(String8(nameBuf).getPathLeaf(), kFileTypeRegular);
1272
1273 info.setSourceName(
1274 createZipSourceNameLocked(zipName, dirName, info.getFileName()));
1275
1276 contents.add(info);
1277 //printf("FOUND: file '%s'\n", info.getFileName().string());
1278 } else {
1279 /* this is a subdir; add it if we don't already have it*/
1280 String8 subdirName(cp, nextSlash - cp);
1281 size_t j;
1282 size_t N = dirs.size();
1283
1284 for (j = 0; j < N; j++) {
1285 if (subdirName == dirs[j]) {
1286 break;
1287 }
1288 }
1289 if (j == N) {
1290 dirs.add(subdirName);
1291 }
1292
1293 //printf("FOUND: dir '%s'\n", subdirName.string());
1294 }
1295 }
1296 }
1297
Narayan Kamath560566d2013-12-03 13:16:03 +00001298 pZip->endIteration(iterationCookie);
1299
Adam Lesinski16c4d152014-01-24 13:27:13 -08001300 /*
1301 * Add the set of unique directories.
1302 */
1303 for (int i = 0; i < (int) dirs.size(); i++) {
1304 info.set(dirs[i], kFileTypeDirectory);
1305 info.setSourceName(
1306 createZipSourceNameLocked(zipName, dirName, info.getFileName()));
1307 contents.add(info);
1308 }
1309
1310 mergeInfoLocked(pMergedInfo, &contents);
1311
1312 return true;
1313}
1314
1315
1316/*
1317 * Merge two vectors of FileInfo.
1318 *
1319 * The merged contents will be stuffed into *pMergedInfo.
1320 *
1321 * If an entry for a file exists in both "pMergedInfo" and "pContents",
1322 * we use the newer "pContents" entry.
1323 */
1324void AssetManager::mergeInfoLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
1325 const SortedVector<AssetDir::FileInfo>* pContents)
1326{
1327 /*
1328 * Merge what we found in this directory with what we found in
1329 * other places.
1330 *
1331 * Two basic approaches:
1332 * (1) Create a new array that holds the unique values of the two
1333 * arrays.
1334 * (2) Take the elements from pContents and shove them into pMergedInfo.
1335 *
1336 * Because these are vectors of complex objects, moving elements around
1337 * inside the vector requires constructing new objects and allocating
1338 * storage for members. With approach #1, we're always adding to the
1339 * end, whereas with #2 we could be inserting multiple elements at the
1340 * front of the vector. Approach #1 requires a full copy of the
1341 * contents of pMergedInfo, but approach #2 requires the same copy for
1342 * every insertion at the front of pMergedInfo.
1343 *
1344 * (We should probably use a SortedVector interface that allows us to
1345 * just stuff items in, trusting us to maintain the sort order.)
1346 */
1347 SortedVector<AssetDir::FileInfo>* pNewSorted;
1348 int mergeMax, contMax;
1349 int mergeIdx, contIdx;
1350
1351 pNewSorted = new SortedVector<AssetDir::FileInfo>;
1352 mergeMax = pMergedInfo->size();
1353 contMax = pContents->size();
1354 mergeIdx = contIdx = 0;
1355
1356 while (mergeIdx < mergeMax || contIdx < contMax) {
1357 if (mergeIdx == mergeMax) {
1358 /* hit end of "merge" list, copy rest of "contents" */
1359 pNewSorted->add(pContents->itemAt(contIdx));
1360 contIdx++;
1361 } else if (contIdx == contMax) {
1362 /* hit end of "cont" list, copy rest of "merge" */
1363 pNewSorted->add(pMergedInfo->itemAt(mergeIdx));
1364 mergeIdx++;
1365 } else if (pMergedInfo->itemAt(mergeIdx) == pContents->itemAt(contIdx))
1366 {
1367 /* items are identical, add newer and advance both indices */
1368 pNewSorted->add(pContents->itemAt(contIdx));
1369 mergeIdx++;
1370 contIdx++;
1371 } else if (pMergedInfo->itemAt(mergeIdx) < pContents->itemAt(contIdx))
1372 {
1373 /* "merge" is lower, add that one */
1374 pNewSorted->add(pMergedInfo->itemAt(mergeIdx));
1375 mergeIdx++;
1376 } else {
1377 /* "cont" is lower, add that one */
1378 assert(pContents->itemAt(contIdx) < pMergedInfo->itemAt(mergeIdx));
1379 pNewSorted->add(pContents->itemAt(contIdx));
1380 contIdx++;
1381 }
1382 }
1383
1384 /*
1385 * Overwrite the "merged" list with the new stuff.
1386 */
1387 *pMergedInfo = *pNewSorted;
1388 delete pNewSorted;
1389
1390#if 0 // for Vector, rather than SortedVector
1391 int i, j;
1392 for (i = pContents->size() -1; i >= 0; i--) {
1393 bool add = true;
1394
1395 for (j = pMergedInfo->size() -1; j >= 0; j--) {
1396 /* case-sensitive comparisons, to behave like UNIX fs */
1397 if (strcmp(pContents->itemAt(i).mFileName,
1398 pMergedInfo->itemAt(j).mFileName) == 0)
1399 {
1400 /* match, don't add this entry */
1401 add = false;
1402 break;
1403 }
1404 }
1405
1406 if (add)
1407 pMergedInfo->add(pContents->itemAt(i));
1408 }
1409#endif
1410}
1411
Adam Lesinski16c4d152014-01-24 13:27:13 -08001412/*
1413 * ===========================================================================
1414 * AssetManager::SharedZip
1415 * ===========================================================================
1416 */
1417
1418
1419Mutex AssetManager::SharedZip::gLock;
1420DefaultKeyedVector<String8, wp<AssetManager::SharedZip> > AssetManager::SharedZip::gOpen;
1421
1422AssetManager::SharedZip::SharedZip(const String8& path, time_t modWhen)
1423 : mPath(path), mZipFile(NULL), mModWhen(modWhen),
1424 mResourceTableAsset(NULL), mResourceTable(NULL)
1425{
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001426 if (kIsDebug) {
1427 ALOGI("Creating SharedZip %p %s\n", this, (const char*)mPath);
1428 }
Adam Lesinski16c4d152014-01-24 13:27:13 -08001429 ALOGV("+++ opening zip '%s'\n", mPath.string());
Narayan Kamath560566d2013-12-03 13:16:03 +00001430 mZipFile = ZipFileRO::open(mPath.string());
1431 if (mZipFile == NULL) {
Adam Lesinski16c4d152014-01-24 13:27:13 -08001432 ALOGD("failed to open Zip archive '%s'\n", mPath.string());
Adam Lesinski16c4d152014-01-24 13:27:13 -08001433 }
1434}
1435
Dianne Hackbornca3872c2017-10-30 14:19:32 -07001436AssetManager::SharedZip::SharedZip(int fd, const String8& path)
1437 : mPath(path), mZipFile(NULL), mModWhen(0),
1438 mResourceTableAsset(NULL), mResourceTable(NULL)
1439{
1440 if (kIsDebug) {
1441 ALOGI("Creating SharedZip %p fd=%d %s\n", this, fd, (const char*)mPath);
1442 }
1443 ALOGV("+++ opening zip fd=%d '%s'\n", fd, mPath.string());
1444 mZipFile = ZipFileRO::openFd(fd, mPath.string());
1445 if (mZipFile == NULL) {
1446 ::close(fd);
1447 ALOGD("failed to open Zip archive fd=%d '%s'\n", fd, mPath.string());
1448 }
1449}
1450
Mårten Kongstad48d22322014-01-31 14:43:27 +01001451sp<AssetManager::SharedZip> AssetManager::SharedZip::get(const String8& path,
1452 bool createIfNotPresent)
Adam Lesinski16c4d152014-01-24 13:27:13 -08001453{
1454 AutoMutex _l(gLock);
1455 time_t modWhen = getFileModDate(path);
1456 sp<SharedZip> zip = gOpen.valueFor(path).promote();
1457 if (zip != NULL && zip->mModWhen == modWhen) {
1458 return zip;
1459 }
Mårten Kongstad48d22322014-01-31 14:43:27 +01001460 if (zip == NULL && !createIfNotPresent) {
1461 return NULL;
1462 }
Adam Lesinski16c4d152014-01-24 13:27:13 -08001463 zip = new SharedZip(path, modWhen);
1464 gOpen.add(path, zip);
1465 return zip;
Dianne Hackbornca3872c2017-10-30 14:19:32 -07001466}
Adam Lesinski16c4d152014-01-24 13:27:13 -08001467
Dianne Hackbornca3872c2017-10-30 14:19:32 -07001468sp<AssetManager::SharedZip> AssetManager::SharedZip::create(int fd, const String8& path)
1469{
1470 return new SharedZip(fd, path);
Adam Lesinski16c4d152014-01-24 13:27:13 -08001471}
1472
1473ZipFileRO* AssetManager::SharedZip::getZip()
1474{
1475 return mZipFile;
1476}
1477
1478Asset* AssetManager::SharedZip::getResourceTableAsset()
1479{
songjinshi49921f22016-09-08 15:24:30 +08001480 AutoMutex _l(gLock);
Adam Lesinski16c4d152014-01-24 13:27:13 -08001481 ALOGV("Getting from SharedZip %p resource asset %p\n", this, mResourceTableAsset);
1482 return mResourceTableAsset;
1483}
1484
1485Asset* AssetManager::SharedZip::setResourceTableAsset(Asset* asset)
1486{
1487 {
1488 AutoMutex _l(gLock);
1489 if (mResourceTableAsset == NULL) {
Adam Lesinski16c4d152014-01-24 13:27:13 -08001490 // This is not thread safe the first time it is called, so
1491 // do it here with the global lock held.
1492 asset->getBuffer(true);
songjinshi49921f22016-09-08 15:24:30 +08001493 mResourceTableAsset = asset;
Adam Lesinski16c4d152014-01-24 13:27:13 -08001494 return asset;
1495 }
1496 }
1497 delete asset;
1498 return mResourceTableAsset;
1499}
1500
1501ResTable* AssetManager::SharedZip::getResourceTable()
1502{
1503 ALOGV("Getting from SharedZip %p resource table %p\n", this, mResourceTable);
1504 return mResourceTable;
1505}
1506
1507ResTable* AssetManager::SharedZip::setResourceTable(ResTable* res)
1508{
1509 {
1510 AutoMutex _l(gLock);
1511 if (mResourceTable == NULL) {
1512 mResourceTable = res;
1513 return res;
1514 }
1515 }
1516 delete res;
1517 return mResourceTable;
1518}
1519
1520bool AssetManager::SharedZip::isUpToDate()
1521{
1522 time_t modWhen = getFileModDate(mPath.string());
1523 return mModWhen == modWhen;
1524}
1525
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +09001526void AssetManager::SharedZip::addOverlay(const asset_path& ap)
1527{
1528 mOverlays.add(ap);
1529}
1530
1531bool AssetManager::SharedZip::getOverlay(size_t idx, asset_path* out) const
1532{
1533 if (idx >= mOverlays.size()) {
1534 return false;
1535 }
1536 *out = mOverlays[idx];
1537 return true;
1538}
1539
Adam Lesinski16c4d152014-01-24 13:27:13 -08001540AssetManager::SharedZip::~SharedZip()
1541{
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001542 if (kIsDebug) {
1543 ALOGI("Destroying SharedZip %p %s\n", this, (const char*)mPath);
1544 }
Adam Lesinski16c4d152014-01-24 13:27:13 -08001545 if (mResourceTable != NULL) {
1546 delete mResourceTable;
1547 }
1548 if (mResourceTableAsset != NULL) {
1549 delete mResourceTableAsset;
1550 }
1551 if (mZipFile != NULL) {
1552 delete mZipFile;
1553 ALOGV("Closed '%s'\n", mPath.string());
1554 }
1555}
1556
1557/*
1558 * ===========================================================================
1559 * AssetManager::ZipSet
1560 * ===========================================================================
1561 */
1562
1563/*
Adam Lesinski16c4d152014-01-24 13:27:13 -08001564 * Destructor. Close any open archives.
1565 */
1566AssetManager::ZipSet::~ZipSet(void)
1567{
1568 size_t N = mZipFile.size();
1569 for (size_t i = 0; i < N; i++)
1570 closeZip(i);
1571}
1572
1573/*
1574 * Close a Zip file and reset the entry.
1575 */
1576void AssetManager::ZipSet::closeZip(int idx)
1577{
1578 mZipFile.editItemAt(idx) = NULL;
1579}
1580
Adam Lesinski16c4d152014-01-24 13:27:13 -08001581/*
1582 * Retrieve the appropriate Zip file from the set.
1583 */
1584ZipFileRO* AssetManager::ZipSet::getZip(const String8& path)
1585{
Dianne Hackbornca3872c2017-10-30 14:19:32 -07001586 return getSharedZip(path)->getZip();
1587}
1588
1589const sp<AssetManager::SharedZip> AssetManager::ZipSet::getSharedZip(const String8& path)
1590{
Adam Lesinski16c4d152014-01-24 13:27:13 -08001591 int idx = getIndex(path);
1592 sp<SharedZip> zip = mZipFile[idx];
1593 if (zip == NULL) {
1594 zip = SharedZip::get(path);
1595 mZipFile.editItemAt(idx) = zip;
1596 }
Dianne Hackbornca3872c2017-10-30 14:19:32 -07001597 return zip;
Adam Lesinski16c4d152014-01-24 13:27:13 -08001598}
1599
1600Asset* AssetManager::ZipSet::getZipResourceTableAsset(const String8& path)
1601{
1602 int idx = getIndex(path);
1603 sp<SharedZip> zip = mZipFile[idx];
1604 if (zip == NULL) {
1605 zip = SharedZip::get(path);
1606 mZipFile.editItemAt(idx) = zip;
1607 }
1608 return zip->getResourceTableAsset();
1609}
1610
1611Asset* AssetManager::ZipSet::setZipResourceTableAsset(const String8& path,
1612 Asset* asset)
1613{
1614 int idx = getIndex(path);
1615 sp<SharedZip> zip = mZipFile[idx];
1616 // doesn't make sense to call before previously accessing.
1617 return zip->setResourceTableAsset(asset);
1618}
1619
1620ResTable* AssetManager::ZipSet::getZipResourceTable(const String8& path)
1621{
1622 int idx = getIndex(path);
1623 sp<SharedZip> zip = mZipFile[idx];
1624 if (zip == NULL) {
1625 zip = SharedZip::get(path);
1626 mZipFile.editItemAt(idx) = zip;
1627 }
1628 return zip->getResourceTable();
1629}
1630
1631ResTable* AssetManager::ZipSet::setZipResourceTable(const String8& path,
1632 ResTable* res)
1633{
1634 int idx = getIndex(path);
1635 sp<SharedZip> zip = mZipFile[idx];
1636 // doesn't make sense to call before previously accessing.
1637 return zip->setResourceTable(res);
1638}
1639
1640/*
1641 * Generate the partial pathname for the specified archive. The caller
1642 * gets to prepend the asset root directory.
1643 *
1644 * Returns something like "common/en-US-noogle.jar".
1645 */
1646/*static*/ String8 AssetManager::ZipSet::getPathName(const char* zipPath)
1647{
1648 return String8(zipPath);
1649}
1650
1651bool AssetManager::ZipSet::isUpToDate()
1652{
1653 const size_t N = mZipFile.size();
1654 for (size_t i=0; i<N; i++) {
1655 if (mZipFile[i] != NULL && !mZipFile[i]->isUpToDate()) {
1656 return false;
1657 }
1658 }
1659 return true;
1660}
1661
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +09001662void AssetManager::ZipSet::addOverlay(const String8& path, const asset_path& overlay)
1663{
1664 int idx = getIndex(path);
1665 sp<SharedZip> zip = mZipFile[idx];
1666 zip->addOverlay(overlay);
1667}
1668
1669bool AssetManager::ZipSet::getOverlay(const String8& path, size_t idx, asset_path* out) const
1670{
1671 sp<SharedZip> zip = SharedZip::get(path, false);
1672 if (zip == NULL) {
1673 return false;
1674 }
1675 return zip->getOverlay(idx, out);
1676}
1677
Adam Lesinski16c4d152014-01-24 13:27:13 -08001678/*
1679 * Compute the zip file's index.
1680 *
1681 * "appName", "locale", and "vendor" should be set to NULL to indicate the
1682 * default directory.
1683 */
1684int AssetManager::ZipSet::getIndex(const String8& zip) const
1685{
1686 const size_t N = mZipPath.size();
1687 for (size_t i=0; i<N; i++) {
1688 if (mZipPath[i] == zip) {
1689 return i;
1690 }
1691 }
1692
1693 mZipPath.add(zip);
1694 mZipFile.add(NULL);
1695
1696 return mZipPath.size()-1;
1697}