blob: 0485625e81e83e080d2bc506951272e380b6d475 [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";
Mårten Kongstad48d22322014-01-31 14:43:27 +010077const char* AssetManager::TARGET_PACKAGE_NAME = "android";
78const char* AssetManager::TARGET_APK_PATH = "/system/framework/framework-res.apk";
79const char* AssetManager::IDMAP_DIR = "/data/resource-cache";
Mårten Kongstad65a05fd2014-01-31 14:01:52 +010080
Adam Lesinski16c4d152014-01-24 13:27:13 -080081namespace {
Adam Lesinski16c4d152014-01-24 13:27:13 -080082
Adam Lesinskia77685f2016-10-03 16:26:28 -070083String8 idmapPathForPackagePath(const String8& pkgPath) {
84 const char* root = getenv("ANDROID_DATA");
85 LOG_ALWAYS_FATAL_IF(root == NULL, "ANDROID_DATA not set");
86 String8 path(root);
87 path.appendPath(kResourceCache);
Adam Lesinski16c4d152014-01-24 13:27:13 -080088
Adam Lesinskia77685f2016-10-03 16:26:28 -070089 char buf[256]; // 256 chars should be enough for anyone...
90 strncpy(buf, pkgPath.string(), 255);
91 buf[255] = '\0';
92 char* filename = buf;
93 while (*filename && *filename == '/') {
94 ++filename;
Adam Lesinski16c4d152014-01-24 13:27:13 -080095 }
Adam Lesinskia77685f2016-10-03 16:26:28 -070096 char* p = filename;
97 while (*p) {
98 if (*p == '/') {
99 *p = '@';
100 }
101 ++p;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800102 }
Adam Lesinskia77685f2016-10-03 16:26:28 -0700103 path.appendPath(filename);
104 path.append("@idmap");
105
106 return path;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800107}
108
109/*
Adam Lesinskia77685f2016-10-03 16:26:28 -0700110 * Like strdup(), but uses C++ "new" operator instead of malloc.
111 */
112static char* strdupNew(const char* str) {
113 char* newStr;
114 int len;
115
116 if (str == NULL)
117 return NULL;
118
119 len = strlen(str);
120 newStr = new char[len+1];
121 memcpy(newStr, str, len+1);
122
123 return newStr;
124}
125
126} // namespace
127
128/*
Adam Lesinski16c4d152014-01-24 13:27:13 -0800129 * ===========================================================================
130 * AssetManager
131 * ===========================================================================
132 */
133
Adam Lesinskia77685f2016-10-03 16:26:28 -0700134int32_t AssetManager::getGlobalCount() {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800135 return gCount;
136}
137
Adam Lesinskia77685f2016-10-03 16:26:28 -0700138AssetManager::AssetManager() :
139 mLocale(NULL), mResources(NULL), mConfig(new ResTable_config) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700140 int count = android_atomic_inc(&gCount) + 1;
141 if (kIsDebug) {
142 ALOGI("Creating AssetManager %p #%d\n", this, count);
143 }
Adam Lesinski16c4d152014-01-24 13:27:13 -0800144 memset(mConfig, 0, sizeof(ResTable_config));
145}
146
Adam Lesinskia77685f2016-10-03 16:26:28 -0700147AssetManager::~AssetManager() {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800148 int count = android_atomic_dec(&gCount);
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700149 if (kIsDebug) {
150 ALOGI("Destroying AssetManager in %p #%d\n", this, count);
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000151 } else {
152 ALOGV("Destroying AssetManager in %p #%d\n", this, count);
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700153 }
Adam Lesinski16c4d152014-01-24 13:27:13 -0800154
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700155 // Manually close any fd paths for which we have not yet opened their zip (which
156 // will take ownership of the fd and close it when done).
157 for (size_t i=0; i<mAssetPaths.size(); i++) {
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000158 ALOGV("Cleaning path #%d: fd=%d, zip=%p", (int)i, mAssetPaths[i].rawFd,
159 mAssetPaths[i].zip.get());
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700160 if (mAssetPaths[i].rawFd >= 0 && mAssetPaths[i].zip == NULL) {
161 close(mAssetPaths[i].rawFd);
162 }
163 }
164
Adam Lesinski16c4d152014-01-24 13:27:13 -0800165 delete mConfig;
166 delete mResources;
167
168 // don't have a String class yet, so make sure we clean up
169 delete[] mLocale;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800170}
171
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800172bool AssetManager::addAssetPath(
Adam Lesinskia77685f2016-10-03 16:26:28 -0700173 const String8& path, int32_t* cookie, bool appAsLib, bool isSystemAsset) {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800174 AutoMutex _l(mLock);
175
176 asset_path ap;
177
178 String8 realPath(path);
179 if (kAppZipName) {
180 realPath.appendPath(kAppZipName);
181 }
182 ap.type = ::getFileType(realPath.string());
183 if (ap.type == kFileTypeRegular) {
184 ap.path = realPath;
185 } else {
186 ap.path = path;
187 ap.type = ::getFileType(path.string());
188 if (ap.type != kFileTypeDirectory && ap.type != kFileTypeRegular) {
189 ALOGW("Asset path %s is neither a directory nor file (type=%d).",
190 path.string(), (int)ap.type);
191 return false;
192 }
193 }
194
195 // Skip if we have it already.
196 for (size_t i=0; i<mAssetPaths.size(); i++) {
197 if (mAssetPaths[i].path == ap.path) {
198 if (cookie) {
Narayan Kamatha0c62602014-01-24 13:51:51 +0000199 *cookie = static_cast<int32_t>(i+1);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800200 }
201 return true;
202 }
203 }
204
205 ALOGV("In %p Asset %s path: %s", this,
206 ap.type == kFileTypeDirectory ? "dir" : "zip", ap.path.string());
207
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800208 ap.isSystemAsset = isSystemAsset;
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000209 ssize_t apPos = mAssetPaths.add(ap);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800210
211 // new paths are always added at the end
212 if (cookie) {
Narayan Kamatha0c62602014-01-24 13:51:51 +0000213 *cookie = static_cast<int32_t>(mAssetPaths.size());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800214 }
215
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +0900216#ifdef __ANDROID__
217 // Load overlays, if any
218 asset_path oap;
219 for (size_t idx = 0; mZipSet.getOverlay(ap.path, idx, &oap); idx++) {
220 oap.isSystemAsset = isSystemAsset;
221 mAssetPaths.add(oap);
222 }
223#endif
224
Martin Kosiba7df36252014-01-16 16:25:56 +0000225 if (mResources != NULL) {
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000226 appendPathToResTable(mAssetPaths.editItemAt(apPos), appAsLib);
Martin Kosiba7df36252014-01-16 16:25:56 +0000227 }
228
Adam Lesinski16c4d152014-01-24 13:27:13 -0800229 return true;
230}
231
Mårten Kongstad48d22322014-01-31 14:43:27 +0100232bool AssetManager::addOverlayPath(const String8& packagePath, int32_t* cookie)
233{
234 const String8 idmapPath = idmapPathForPackagePath(packagePath);
235
236 AutoMutex _l(mLock);
237
238 for (size_t i = 0; i < mAssetPaths.size(); ++i) {
239 if (mAssetPaths[i].idmap == idmapPath) {
240 *cookie = static_cast<int32_t>(i + 1);
241 return true;
242 }
243 }
244
245 Asset* idmap = NULL;
246 if ((idmap = openAssetFromFileLocked(idmapPath, Asset::ACCESS_BUFFER)) == NULL) {
247 ALOGW("failed to open idmap file %s\n", idmapPath.string());
248 return false;
249 }
250
251 String8 targetPath;
252 String8 overlayPath;
253 if (!ResTable::getIdmapInfo(idmap->getBuffer(false), idmap->getLength(),
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700254 NULL, NULL, NULL, &targetPath, &overlayPath)) {
Mårten Kongstad48d22322014-01-31 14:43:27 +0100255 ALOGW("failed to read idmap file %s\n", idmapPath.string());
256 delete idmap;
257 return false;
258 }
259 delete idmap;
260
261 if (overlayPath != packagePath) {
262 ALOGW("idmap file %s inconcistent: expected path %s does not match actual path %s\n",
263 idmapPath.string(), packagePath.string(), overlayPath.string());
264 return false;
265 }
266 if (access(targetPath.string(), R_OK) != 0) {
267 ALOGW("failed to access file %s: %s\n", targetPath.string(), strerror(errno));
268 return false;
269 }
270 if (access(idmapPath.string(), R_OK) != 0) {
271 ALOGW("failed to access file %s: %s\n", idmapPath.string(), strerror(errno));
272 return false;
273 }
274 if (access(overlayPath.string(), R_OK) != 0) {
275 ALOGW("failed to access file %s: %s\n", overlayPath.string(), strerror(errno));
276 return false;
277 }
278
279 asset_path oap;
280 oap.path = overlayPath;
281 oap.type = ::getFileType(overlayPath.string());
282 oap.idmap = idmapPath;
283#if 0
284 ALOGD("Overlay added: targetPath=%s overlayPath=%s idmapPath=%s\n",
285 targetPath.string(), overlayPath.string(), idmapPath.string());
286#endif
287 mAssetPaths.add(oap);
288 *cookie = static_cast<int32_t>(mAssetPaths.size());
289
Mårten Kongstad30113132014-11-07 10:52:17 +0100290 if (mResources != NULL) {
291 appendPathToResTable(oap);
292 }
293
Mårten Kongstad48d22322014-01-31 14:43:27 +0100294 return true;
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700295}
296
297bool AssetManager::addAssetFd(
298 int fd, const String8& debugPathName, int32_t* cookie, bool appAsLib,
299 bool assume_ownership) {
300 AutoMutex _l(mLock);
301
302 asset_path ap;
303
304 ap.path = debugPathName;
305 ap.rawFd = fd;
306 ap.type = kFileTypeRegular;
307 ap.assumeOwnership = assume_ownership;
308
309 ALOGV("In %p Asset fd %d name: %s", this, fd, ap.path.string());
310
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000311 ssize_t apPos = mAssetPaths.add(ap);
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700312
313 // new paths are always added at the end
314 if (cookie) {
315 *cookie = static_cast<int32_t>(mAssetPaths.size());
316 }
317
318 if (mResources != NULL) {
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000319 appendPathToResTable(mAssetPaths.editItemAt(apPos), appAsLib);
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700320 }
321
322 return true;
323}
Mårten Kongstad48d22322014-01-31 14:43:27 +0100324
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100325bool AssetManager::createIdmap(const char* targetApkPath, const char* overlayApkPath,
Dianne Hackborn32bb5fa2014-02-11 13:56:21 -0800326 uint32_t targetCrc, uint32_t overlayCrc, uint32_t** outData, size_t* outSize)
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100327{
328 AutoMutex _l(mLock);
329 const String8 paths[2] = { String8(targetApkPath), String8(overlayApkPath) };
Mårten Kongstad6bb13da2016-06-02 09:34:36 +0200330 Asset* assets[2] = {NULL, NULL};
331 bool ret = false;
332 {
333 ResTable tables[2];
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100334
Mårten Kongstad6bb13da2016-06-02 09:34:36 +0200335 for (int i = 0; i < 2; ++i) {
336 asset_path ap;
337 ap.type = kFileTypeRegular;
338 ap.path = paths[i];
339 assets[i] = openNonAssetInPathLocked("resources.arsc",
340 Asset::ACCESS_BUFFER, ap);
341 if (assets[i] == NULL) {
342 ALOGW("failed to find resources.arsc in %s\n", ap.path.string());
343 goto exit;
344 }
345 if (tables[i].add(assets[i]) != NO_ERROR) {
346 ALOGW("failed to add %s to resource table", paths[i].string());
347 goto exit;
348 }
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100349 }
Mårten Kongstad6bb13da2016-06-02 09:34:36 +0200350 ret = tables[0].createIdmap(tables[1], targetCrc, overlayCrc,
351 targetApkPath, overlayApkPath, (void**)outData, outSize) == NO_ERROR;
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100352 }
353
Mårten Kongstad6bb13da2016-06-02 09:34:36 +0200354exit:
355 delete assets[0];
356 delete assets[1];
357 return ret;
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100358}
359
Adam Lesinski16c4d152014-01-24 13:27:13 -0800360bool AssetManager::addDefaultAssets()
361{
362 const char* root = getenv("ANDROID_ROOT");
363 LOG_ALWAYS_FATAL_IF(root == NULL, "ANDROID_ROOT not set");
364
365 String8 path(root);
366 path.appendPath(kSystemAssets);
367
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800368 return addAssetPath(path, NULL, false /* appAsLib */, true /* isSystemAsset */);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800369}
370
Narayan Kamatha0c62602014-01-24 13:51:51 +0000371int32_t AssetManager::nextAssetPath(const int32_t cookie) const
Adam Lesinski16c4d152014-01-24 13:27:13 -0800372{
373 AutoMutex _l(mLock);
Narayan Kamatha0c62602014-01-24 13:51:51 +0000374 const size_t next = static_cast<size_t>(cookie) + 1;
375 return next > mAssetPaths.size() ? -1 : next;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800376}
377
Narayan Kamatha0c62602014-01-24 13:51:51 +0000378String8 AssetManager::getAssetPath(const int32_t cookie) const
Adam Lesinski16c4d152014-01-24 13:27:13 -0800379{
380 AutoMutex _l(mLock);
Narayan Kamatha0c62602014-01-24 13:51:51 +0000381 const size_t which = static_cast<size_t>(cookie) - 1;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800382 if (which < mAssetPaths.size()) {
383 return mAssetPaths[which].path;
384 }
385 return String8();
386}
387
Adam Lesinski16c4d152014-01-24 13:27:13 -0800388void AssetManager::setLocaleLocked(const char* locale)
389{
390 if (mLocale != NULL) {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800391 delete[] mLocale;
392 }
Elliott Hughesc367d482013-10-29 13:12:55 -0700393
Adam Lesinski16c4d152014-01-24 13:27:13 -0800394 mLocale = strdupNew(locale);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800395 updateResourceParamsLocked();
396}
397
Adam Lesinski16c4d152014-01-24 13:27:13 -0800398void AssetManager::setConfiguration(const ResTable_config& config, const char* locale)
399{
400 AutoMutex _l(mLock);
401 *mConfig = config;
402 if (locale) {
403 setLocaleLocked(locale);
404 } else if (config.language[0] != 0) {
Narayan Kamath91447d82014-01-21 15:32:36 +0000405 char spec[RESTABLE_MAX_LOCALE_LEN];
406 config.getBcp47Locale(spec);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800407 setLocaleLocked(spec);
408 } else {
409 updateResourceParamsLocked();
410 }
411}
412
413void AssetManager::getConfiguration(ResTable_config* outConfig) const
414{
415 AutoMutex _l(mLock);
416 *outConfig = *mConfig;
417}
418
419/*
420 * Open an asset.
421 *
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700422 * The data could be in any asset path. Each asset path could be:
423 * - A directory on disk.
424 * - A Zip archive, uncompressed or compressed.
Adam Lesinski16c4d152014-01-24 13:27:13 -0800425 *
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700426 * If the file is in a directory, it could have a .gz suffix, meaning it is compressed.
Adam Lesinski16c4d152014-01-24 13:27:13 -0800427 *
428 * We should probably reject requests for "illegal" filenames, e.g. those
429 * with illegal characters or "../" backward relative paths.
430 */
431Asset* AssetManager::open(const char* fileName, AccessMode mode)
432{
433 AutoMutex _l(mLock);
434
435 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
436
Adam Lesinski16c4d152014-01-24 13:27:13 -0800437 String8 assetName(kAssetsRoot);
438 assetName.appendPath(fileName);
439
440 /*
441 * For each top-level asset path, search for the asset.
442 */
443
444 size_t i = mAssetPaths.size();
445 while (i > 0) {
446 i--;
447 ALOGV("Looking for asset '%s' in '%s'\n",
448 assetName.string(), mAssetPaths.itemAt(i).path.string());
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000449 Asset* pAsset = openNonAssetInPathLocked(assetName.string(), mode,
450 mAssetPaths.editItemAt(i));
Adam Lesinski16c4d152014-01-24 13:27:13 -0800451 if (pAsset != NULL) {
452 return pAsset != kExcludedAsset ? pAsset : NULL;
453 }
454 }
455
456 return NULL;
457}
458
459/*
460 * Open a non-asset file as if it were an asset.
461 *
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700462 * The "fileName" is the partial path starting from the application name.
Adam Lesinski16c4d152014-01-24 13:27:13 -0800463 */
Adam Lesinskide898ff2014-01-29 18:20:45 -0800464Asset* AssetManager::openNonAsset(const char* fileName, AccessMode mode, int32_t* outCookie)
Adam Lesinski16c4d152014-01-24 13:27:13 -0800465{
466 AutoMutex _l(mLock);
467
468 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
469
Adam Lesinski16c4d152014-01-24 13:27:13 -0800470 /*
471 * For each top-level asset path, search for the asset.
472 */
473
474 size_t i = mAssetPaths.size();
475 while (i > 0) {
476 i--;
477 ALOGV("Looking for non-asset '%s' in '%s'\n", fileName, mAssetPaths.itemAt(i).path.string());
478 Asset* pAsset = openNonAssetInPathLocked(
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000479 fileName, mode, mAssetPaths.editItemAt(i));
Adam Lesinski16c4d152014-01-24 13:27:13 -0800480 if (pAsset != NULL) {
Adam Lesinskide898ff2014-01-29 18:20:45 -0800481 if (outCookie != NULL) *outCookie = static_cast<int32_t>(i + 1);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800482 return pAsset != kExcludedAsset ? pAsset : NULL;
483 }
484 }
485
486 return NULL;
487}
488
Narayan Kamatha0c62602014-01-24 13:51:51 +0000489Asset* AssetManager::openNonAsset(const int32_t cookie, const char* fileName, AccessMode mode)
Adam Lesinski16c4d152014-01-24 13:27:13 -0800490{
Narayan Kamatha0c62602014-01-24 13:51:51 +0000491 const size_t which = static_cast<size_t>(cookie) - 1;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800492
493 AutoMutex _l(mLock);
494
495 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
496
Adam Lesinski16c4d152014-01-24 13:27:13 -0800497 if (which < mAssetPaths.size()) {
498 ALOGV("Looking for non-asset '%s' in '%s'\n", fileName,
499 mAssetPaths.itemAt(which).path.string());
500 Asset* pAsset = openNonAssetInPathLocked(
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000501 fileName, mode, mAssetPaths.editItemAt(which));
Adam Lesinski16c4d152014-01-24 13:27:13 -0800502 if (pAsset != NULL) {
503 return pAsset != kExcludedAsset ? pAsset : NULL;
504 }
505 }
506
507 return NULL;
508}
509
510/*
511 * Get the type of a file in the asset namespace.
512 *
513 * This currently only works for regular files. All others (including
514 * directories) will return kFileTypeNonexistent.
515 */
516FileType AssetManager::getFileType(const char* fileName)
517{
518 Asset* pAsset = NULL;
519
520 /*
521 * Open the asset. This is less efficient than simply finding the
522 * file, but it's not too bad (we don't uncompress or mmap data until
523 * the first read() call).
524 */
525 pAsset = open(fileName, Asset::ACCESS_STREAMING);
526 delete pAsset;
527
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700528 if (pAsset == NULL) {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800529 return kFileTypeNonexistent;
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700530 } else {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800531 return kFileTypeRegular;
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700532 }
Adam Lesinski16c4d152014-01-24 13:27:13 -0800533}
534
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000535bool AssetManager::appendPathToResTable(asset_path& ap, bool appAsLib) const {
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +0900536 // skip those ap's that correspond to system overlays
537 if (ap.isSystemOverlay) {
538 return true;
539 }
540
Martin Kosiba7df36252014-01-16 16:25:56 +0000541 Asset* ass = NULL;
542 ResTable* sharedRes = NULL;
543 bool shared = true;
544 bool onlyEmptyResources = true;
Adam Lesinskib7e1ce02016-04-11 20:03:01 -0700545 ATRACE_NAME(ap.path.string());
Martin Kosiba7df36252014-01-16 16:25:56 +0000546 Asset* idmap = openIdmapLocked(ap);
547 size_t nextEntryIdx = mResources->getTableCount();
548 ALOGV("Looking for resource asset in '%s'\n", ap.path.string());
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700549 if (ap.type != kFileTypeDirectory && ap.rawFd < 0) {
Martin Kosiba7df36252014-01-16 16:25:56 +0000550 if (nextEntryIdx == 0) {
551 // The first item is typically the framework resources,
552 // which we want to avoid parsing every time.
553 sharedRes = const_cast<AssetManager*>(this)->
554 mZipSet.getZipResourceTable(ap.path);
555 if (sharedRes != NULL) {
556 // skip ahead the number of system overlay packages preloaded
557 nextEntryIdx = sharedRes->getTableCount();
558 }
559 }
560 if (sharedRes == NULL) {
561 ass = const_cast<AssetManager*>(this)->
562 mZipSet.getZipResourceTableAsset(ap.path);
563 if (ass == NULL) {
564 ALOGV("loading resource table %s\n", ap.path.string());
565 ass = const_cast<AssetManager*>(this)->
566 openNonAssetInPathLocked("resources.arsc",
567 Asset::ACCESS_BUFFER,
568 ap);
569 if (ass != NULL && ass != kExcludedAsset) {
570 ass = const_cast<AssetManager*>(this)->
571 mZipSet.setZipResourceTableAsset(ap.path, ass);
572 }
573 }
574
575 if (nextEntryIdx == 0 && ass != NULL) {
576 // If this is the first resource table in the asset
577 // manager, then we are going to cache it so that we
578 // can quickly copy it out for others.
579 ALOGV("Creating shared resources for %s", ap.path.string());
580 sharedRes = new ResTable();
581 sharedRes->add(ass, idmap, nextEntryIdx + 1, false);
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +0900582#ifdef __ANDROID__
583 const char* data = getenv("ANDROID_DATA");
584 LOG_ALWAYS_FATAL_IF(data == NULL, "ANDROID_DATA not set");
585 String8 overlaysListPath(data);
586 overlaysListPath.appendPath(kResourceCache);
587 overlaysListPath.appendPath("overlays.list");
588 addSystemOverlays(overlaysListPath.string(), ap.path, sharedRes, nextEntryIdx);
589#endif
Martin Kosiba7df36252014-01-16 16:25:56 +0000590 sharedRes = const_cast<AssetManager*>(this)->
591 mZipSet.setZipResourceTable(ap.path, sharedRes);
592 }
593 }
594 } else {
595 ALOGV("loading resource table %s\n", ap.path.string());
596 ass = const_cast<AssetManager*>(this)->
597 openNonAssetInPathLocked("resources.arsc",
598 Asset::ACCESS_BUFFER,
599 ap);
600 shared = false;
601 }
602
603 if ((ass != NULL || sharedRes != NULL) && ass != kExcludedAsset) {
604 ALOGV("Installing resource asset %p in to table %p\n", ass, mResources);
605 if (sharedRes != NULL) {
606 ALOGV("Copying existing resources for %s", ap.path.string());
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800607 mResources->add(sharedRes, ap.isSystemAsset);
Martin Kosiba7df36252014-01-16 16:25:56 +0000608 } else {
609 ALOGV("Parsing resources for %s", ap.path.string());
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800610 mResources->add(ass, idmap, nextEntryIdx + 1, !shared, appAsLib, ap.isSystemAsset);
Martin Kosiba7df36252014-01-16 16:25:56 +0000611 }
612 onlyEmptyResources = false;
613
614 if (!shared) {
615 delete ass;
616 }
617 } else {
618 ALOGV("Installing empty resources in to table %p\n", mResources);
619 mResources->addEmpty(nextEntryIdx + 1);
620 }
621
622 if (idmap != NULL) {
623 delete idmap;
624 }
Martin Kosiba7df36252014-01-16 16:25:56 +0000625 return onlyEmptyResources;
626}
627
Adam Lesinski16c4d152014-01-24 13:27:13 -0800628const ResTable* AssetManager::getResTable(bool required) const
629{
630 ResTable* rt = mResources;
631 if (rt) {
632 return rt;
633 }
634
635 // Iterate through all asset packages, collecting resources from each.
636
637 AutoMutex _l(mLock);
638
639 if (mResources != NULL) {
640 return mResources;
641 }
642
643 if (required) {
644 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
645 }
646
Adam Lesinskide898ff2014-01-29 18:20:45 -0800647 mResources = new ResTable();
648 updateResourceParamsLocked();
649
650 bool onlyEmptyResources = true;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800651 const size_t N = mAssetPaths.size();
652 for (size_t i=0; i<N; i++) {
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000653 bool empty = appendPathToResTable(
654 const_cast<AssetManager*>(this)->mAssetPaths.editItemAt(i));
Martin Kosiba7df36252014-01-16 16:25:56 +0000655 onlyEmptyResources = onlyEmptyResources && empty;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800656 }
657
Adam Lesinskide898ff2014-01-29 18:20:45 -0800658 if (required && onlyEmptyResources) {
659 ALOGW("Unable to find resources file resources.arsc");
660 delete mResources;
661 mResources = NULL;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800662 }
Adam Lesinskide898ff2014-01-29 18:20:45 -0800663
664 return mResources;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800665}
666
667void AssetManager::updateResourceParamsLocked() const
668{
Adam Lesinskib7e1ce02016-04-11 20:03:01 -0700669 ATRACE_CALL();
Adam Lesinski16c4d152014-01-24 13:27:13 -0800670 ResTable* res = mResources;
671 if (!res) {
672 return;
673 }
674
Narayan Kamath91447d82014-01-21 15:32:36 +0000675 if (mLocale) {
676 mConfig->setBcp47Locale(mLocale);
677 } else {
678 mConfig->clearLocale();
Adam Lesinski16c4d152014-01-24 13:27:13 -0800679 }
Adam Lesinski16c4d152014-01-24 13:27:13 -0800680
681 res->setParameters(mConfig);
682}
683
684Asset* AssetManager::openIdmapLocked(const struct asset_path& ap) const
685{
686 Asset* ass = NULL;
687 if (ap.idmap.size() != 0) {
688 ass = const_cast<AssetManager*>(this)->
689 openAssetFromFileLocked(ap.idmap, Asset::ACCESS_BUFFER);
690 if (ass) {
691 ALOGV("loading idmap %s\n", ap.idmap.string());
692 } else {
693 ALOGW("failed to load idmap %s\n", ap.idmap.string());
694 }
695 }
696 return ass;
697}
698
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +0900699void AssetManager::addSystemOverlays(const char* pathOverlaysList,
700 const String8& targetPackagePath, ResTable* sharedRes, size_t offset) const
701{
702 FILE* fin = fopen(pathOverlaysList, "r");
703 if (fin == NULL) {
704 return;
705 }
706
707#ifndef _WIN32
708 if (TEMP_FAILURE_RETRY(flock(fileno(fin), LOCK_SH)) != 0) {
709 fclose(fin);
710 return;
711 }
712#endif
713 char buf[1024];
714 while (fgets(buf, sizeof(buf), fin)) {
715 // format of each line:
716 // <path to apk><space><path to idmap><newline>
717 char* space = strchr(buf, ' ');
718 char* newline = strchr(buf, '\n');
719 asset_path oap;
720
721 if (space == NULL || newline == NULL || newline < space) {
722 continue;
723 }
724
725 oap.path = String8(buf, space - buf);
726 oap.type = kFileTypeRegular;
727 oap.idmap = String8(space + 1, newline - space - 1);
728 oap.isSystemOverlay = true;
729
730 Asset* oass = const_cast<AssetManager*>(this)->
731 openNonAssetInPathLocked("resources.arsc",
732 Asset::ACCESS_BUFFER,
733 oap);
734
735 if (oass != NULL) {
736 Asset* oidmap = openIdmapLocked(oap);
737 offset++;
738 sharedRes->add(oass, oidmap, offset + 1, false);
739 const_cast<AssetManager*>(this)->mAssetPaths.add(oap);
740 const_cast<AssetManager*>(this)->mZipSet.addOverlay(targetPackagePath, oap);
741 delete oidmap;
742 }
743 }
744
745#ifndef _WIN32
746 TEMP_FAILURE_RETRY(flock(fileno(fin), LOCK_UN));
747#endif
748 fclose(fin);
749}
750
Adam Lesinski16c4d152014-01-24 13:27:13 -0800751const ResTable& AssetManager::getResources(bool required) const
752{
753 const ResTable* rt = getResTable(required);
754 return *rt;
755}
756
757bool AssetManager::isUpToDate()
758{
759 AutoMutex _l(mLock);
760 return mZipSet.isUpToDate();
761}
762
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800763void AssetManager::getLocales(Vector<String8>* locales, bool includeSystemLocales) const
Adam Lesinski16c4d152014-01-24 13:27:13 -0800764{
765 ResTable* res = mResources;
766 if (res != NULL) {
Roozbeh Pournader7e5f96f2016-06-13 18:10:49 -0700767 res->getLocales(locales, includeSystemLocales, true /* mergeEquivalentLangs */);
Narayan Kamathe4345db2014-06-26 16:01:28 +0100768 }
Adam Lesinski16c4d152014-01-24 13:27:13 -0800769}
770
771/*
772 * Open a non-asset file as if it were an asset, searching for it in the
773 * specified app.
774 *
775 * Pass in a NULL values for "appName" if the common app directory should
776 * be used.
777 */
778Asset* AssetManager::openNonAssetInPathLocked(const char* fileName, AccessMode mode,
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000779 asset_path& ap)
Adam Lesinski16c4d152014-01-24 13:27:13 -0800780{
781 Asset* pAsset = NULL;
782
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700783 ALOGV("openNonAssetInPath: name=%s type=%d fd=%d", fileName, ap.type, ap.rawFd);
784
Adam Lesinski16c4d152014-01-24 13:27:13 -0800785 /* look at the filesystem on disk */
786 if (ap.type == kFileTypeDirectory) {
787 String8 path(ap.path);
788 path.appendPath(fileName);
789
790 pAsset = openAssetFromFileLocked(path, mode);
791
792 if (pAsset == NULL) {
793 /* try again, this time with ".gz" */
794 path.append(".gz");
795 pAsset = openAssetFromFileLocked(path, mode);
796 }
797
798 if (pAsset != NULL) {
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700799 ALOGV("FOUND NA '%s' on disk", fileName);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800800 pAsset->setAssetSource(path);
801 }
802
803 /* look inside the zip file */
804 } else {
805 String8 path(fileName);
806
807 /* check the appropriate Zip file */
Narayan Kamath560566d2013-12-03 13:16:03 +0000808 ZipFileRO* pZip = getZipFileLocked(ap);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800809 if (pZip != NULL) {
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700810 ALOGV("GOT zip, checking NA '%s'", (const char*) path);
Narayan Kamath560566d2013-12-03 13:16:03 +0000811 ZipEntryRO entry = pZip->findEntryByName(path.string());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800812 if (entry != NULL) {
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700813 ALOGV("FOUND NA in Zip file for %s", (const char*) path);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800814 pAsset = openAssetFromZipLocked(pZip, entry, mode, path);
Narayan Kamath560566d2013-12-03 13:16:03 +0000815 pZip->releaseEntry(entry);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800816 }
817 }
818
819 if (pAsset != NULL) {
820 /* create a "source" name, for debug/display */
821 pAsset->setAssetSource(
822 createZipSourceNameLocked(ZipSet::getPathName(ap.path.string()), String8(""),
823 String8(fileName)));
824 }
825 }
826
827 return pAsset;
828}
829
830/*
Adam Lesinski16c4d152014-01-24 13:27:13 -0800831 * Create a "source name" for a file from a Zip archive.
832 */
833String8 AssetManager::createZipSourceNameLocked(const String8& zipFileName,
834 const String8& dirName, const String8& fileName)
835{
836 String8 sourceName("zip:");
837 sourceName.append(zipFileName);
838 sourceName.append(":");
839 if (dirName.length() > 0) {
840 sourceName.appendPath(dirName);
841 }
842 sourceName.appendPath(fileName);
843 return sourceName;
844}
845
846/*
Adam Lesinski16c4d152014-01-24 13:27:13 -0800847 * Create a path to a loose asset (asset-base/app/rootDir).
848 */
849String8 AssetManager::createPathNameLocked(const asset_path& ap, const char* rootDir)
850{
851 String8 path(ap.path);
852 if (rootDir != NULL) path.appendPath(rootDir);
853 return path;
854}
855
856/*
857 * Return a pointer to one of our open Zip archives. Returns NULL if no
858 * matching Zip file exists.
Adam Lesinski16c4d152014-01-24 13:27:13 -0800859 */
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000860ZipFileRO* AssetManager::getZipFileLocked(asset_path& ap)
Adam Lesinski16c4d152014-01-24 13:27:13 -0800861{
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000862 ALOGV("getZipFileLocked() in %p: ap=%p zip=%p", this, &ap, ap.zip.get());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800863
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700864 if (ap.zip != NULL) {
865 return ap.zip->getZip();
866 }
867
868 if (ap.rawFd < 0) {
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000869 ALOGV("getZipFileLocked: Creating new zip from path %s", ap.path.string());
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700870 ap.zip = mZipSet.getSharedZip(ap.path);
871 } else {
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000872 ALOGV("getZipFileLocked: Creating new zip from fd %d", ap.rawFd);
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700873 ap.zip = SharedZip::create(ap.rawFd, ap.path);
874
875 }
876 return ap.zip != NULL ? ap.zip->getZip() : NULL;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800877}
878
879/*
880 * Try to open an asset from a file on disk.
881 *
882 * If the file is compressed with gzip, we seek to the start of the
883 * deflated data and pass that in (just like we would for a Zip archive).
884 *
885 * For uncompressed data, we may already have an mmap()ed version sitting
886 * around. If so, we want to hand that to the Asset instead.
887 *
888 * This returns NULL if the file doesn't exist, couldn't be opened, or
889 * claims to be a ".gz" but isn't.
890 */
891Asset* AssetManager::openAssetFromFileLocked(const String8& pathName,
892 AccessMode mode)
893{
894 Asset* pAsset = NULL;
895
896 if (strcasecmp(pathName.getPathExtension().string(), ".gz") == 0) {
897 //printf("TRYING '%s'\n", (const char*) pathName);
898 pAsset = Asset::createFromCompressedFile(pathName.string(), mode);
899 } else {
900 //printf("TRYING '%s'\n", (const char*) pathName);
901 pAsset = Asset::createFromFile(pathName.string(), mode);
902 }
903
904 return pAsset;
905}
906
907/*
908 * Given an entry in a Zip archive, create a new Asset object.
909 *
910 * If the entry is uncompressed, we may want to create or share a
911 * slice of shared memory.
912 */
913Asset* AssetManager::openAssetFromZipLocked(const ZipFileRO* pZipFile,
914 const ZipEntryRO entry, AccessMode mode, const String8& entryName)
915{
916 Asset* pAsset = NULL;
917
918 // TODO: look for previously-created shared memory slice?
Narayan Kamath407753c2015-06-16 12:02:57 +0100919 uint16_t method;
920 uint32_t uncompressedLen;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800921
922 //printf("USING Zip '%s'\n", pEntry->getFileName());
923
Adam Lesinski16c4d152014-01-24 13:27:13 -0800924 if (!pZipFile->getEntryInfo(entry, &method, &uncompressedLen, NULL, NULL,
925 NULL, NULL))
926 {
927 ALOGW("getEntryInfo failed\n");
928 return NULL;
929 }
930
931 FileMap* dataMap = pZipFile->createEntryFileMap(entry);
932 if (dataMap == NULL) {
933 ALOGW("create map from entry failed\n");
934 return NULL;
935 }
936
937 if (method == ZipFileRO::kCompressStored) {
938 pAsset = Asset::createFromUncompressedMap(dataMap, mode);
939 ALOGV("Opened uncompressed entry %s in zip %s mode %d: %p", entryName.string(),
940 dataMap->getFileName(), mode, pAsset);
941 } else {
Narayan Kamath407753c2015-06-16 12:02:57 +0100942 pAsset = Asset::createFromCompressedMap(dataMap,
943 static_cast<size_t>(uncompressedLen), mode);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800944 ALOGV("Opened compressed entry %s in zip %s mode %d: %p", entryName.string(),
945 dataMap->getFileName(), mode, pAsset);
946 }
947 if (pAsset == NULL) {
948 /* unexpected */
949 ALOGW("create from segment failed\n");
950 }
951
952 return pAsset;
953}
954
Adam Lesinski16c4d152014-01-24 13:27:13 -0800955/*
956 * Open a directory in the asset namespace.
957 *
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700958 * An "asset directory" is simply the combination of all asset paths' "assets/" directories.
Adam Lesinski16c4d152014-01-24 13:27:13 -0800959 *
960 * Pass in "" for the root dir.
961 */
962AssetDir* AssetManager::openDir(const char* dirName)
963{
964 AutoMutex _l(mLock);
965
966 AssetDir* pDir = NULL;
967 SortedVector<AssetDir::FileInfo>* pMergedInfo = NULL;
968
969 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
970 assert(dirName != NULL);
971
972 //printf("+++ openDir(%s) in '%s'\n", dirName, (const char*) mAssetBase);
973
Adam Lesinski16c4d152014-01-24 13:27:13 -0800974 pDir = new AssetDir;
975
976 /*
977 * Scan the various directories, merging what we find into a single
978 * vector. We want to scan them in reverse priority order so that
979 * the ".EXCLUDE" processing works correctly. Also, if we decide we
980 * want to remember where the file is coming from, we'll get the right
981 * version.
982 *
983 * We start with Zip archives, then do loose files.
984 */
985 pMergedInfo = new SortedVector<AssetDir::FileInfo>;
986
987 size_t i = mAssetPaths.size();
988 while (i > 0) {
989 i--;
990 const asset_path& ap = mAssetPaths.itemAt(i);
991 if (ap.type == kFileTypeRegular) {
992 ALOGV("Adding directory %s from zip %s", dirName, ap.path.string());
993 scanAndMergeZipLocked(pMergedInfo, ap, kAssetsRoot, dirName);
994 } else {
995 ALOGV("Adding directory %s from dir %s", dirName, ap.path.string());
996 scanAndMergeDirLocked(pMergedInfo, ap, kAssetsRoot, dirName);
997 }
998 }
999
1000#if 0
1001 printf("FILE LIST:\n");
1002 for (i = 0; i < (size_t) pMergedInfo->size(); i++) {
1003 printf(" %d: (%d) '%s'\n", i,
1004 pMergedInfo->itemAt(i).getFileType(),
1005 (const char*) pMergedInfo->itemAt(i).getFileName());
1006 }
1007#endif
1008
1009 pDir->setFileList(pMergedInfo);
1010 return pDir;
1011}
1012
1013/*
1014 * Open a directory in the non-asset namespace.
1015 *
Adam Lesinskife90eaf2016-10-04 13:31:31 -07001016 * An "asset directory" is simply the combination of all asset paths' "assets/" directories.
Adam Lesinski16c4d152014-01-24 13:27:13 -08001017 *
1018 * Pass in "" for the root dir.
1019 */
Narayan Kamatha0c62602014-01-24 13:51:51 +00001020AssetDir* AssetManager::openNonAssetDir(const int32_t cookie, const char* dirName)
Adam Lesinski16c4d152014-01-24 13:27:13 -08001021{
1022 AutoMutex _l(mLock);
1023
1024 AssetDir* pDir = NULL;
1025 SortedVector<AssetDir::FileInfo>* pMergedInfo = NULL;
1026
1027 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
1028 assert(dirName != NULL);
1029
1030 //printf("+++ openDir(%s) in '%s'\n", dirName, (const char*) mAssetBase);
1031
Adam Lesinski16c4d152014-01-24 13:27:13 -08001032 pDir = new AssetDir;
1033
1034 pMergedInfo = new SortedVector<AssetDir::FileInfo>;
1035
Narayan Kamatha0c62602014-01-24 13:51:51 +00001036 const size_t which = static_cast<size_t>(cookie) - 1;
Adam Lesinski16c4d152014-01-24 13:27:13 -08001037
1038 if (which < mAssetPaths.size()) {
1039 const asset_path& ap = mAssetPaths.itemAt(which);
1040 if (ap.type == kFileTypeRegular) {
1041 ALOGV("Adding directory %s from zip %s", dirName, ap.path.string());
1042 scanAndMergeZipLocked(pMergedInfo, ap, NULL, dirName);
1043 } else {
1044 ALOGV("Adding directory %s from dir %s", dirName, ap.path.string());
1045 scanAndMergeDirLocked(pMergedInfo, ap, NULL, dirName);
1046 }
1047 }
1048
1049#if 0
1050 printf("FILE LIST:\n");
1051 for (i = 0; i < (size_t) pMergedInfo->size(); i++) {
1052 printf(" %d: (%d) '%s'\n", i,
1053 pMergedInfo->itemAt(i).getFileType(),
1054 (const char*) pMergedInfo->itemAt(i).getFileName());
1055 }
1056#endif
1057
1058 pDir->setFileList(pMergedInfo);
1059 return pDir;
1060}
1061
1062/*
1063 * Scan the contents of the specified directory and merge them into the
1064 * "pMergedInfo" vector, removing previous entries if we find "exclude"
1065 * directives.
1066 *
1067 * Returns "false" if we found nothing to contribute.
1068 */
1069bool AssetManager::scanAndMergeDirLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
1070 const asset_path& ap, const char* rootDir, const char* dirName)
1071{
Adam Lesinski16c4d152014-01-24 13:27:13 -08001072 assert(pMergedInfo != NULL);
1073
Adam Lesinskia77685f2016-10-03 16:26:28 -07001074 //printf("scanAndMergeDir: %s %s %s\n", ap.path.string(), rootDir, dirName);
Adam Lesinski16c4d152014-01-24 13:27:13 -08001075
Adam Lesinskia77685f2016-10-03 16:26:28 -07001076 String8 path = createPathNameLocked(ap, rootDir);
1077 if (dirName[0] != '\0')
1078 path.appendPath(dirName);
Adam Lesinski16c4d152014-01-24 13:27:13 -08001079
Adam Lesinskia77685f2016-10-03 16:26:28 -07001080 SortedVector<AssetDir::FileInfo>* pContents = scanDirLocked(path);
1081 if (pContents == NULL)
1082 return false;
Adam Lesinski16c4d152014-01-24 13:27:13 -08001083
1084 // if we wanted to do an incremental cache fill, we would do it here
1085
1086 /*
1087 * Process "exclude" directives. If we find a filename that ends with
1088 * ".EXCLUDE", we look for a matching entry in the "merged" set, and
1089 * remove it if we find it. We also delete the "exclude" entry.
1090 */
1091 int i, count, exclExtLen;
1092
1093 count = pContents->size();
1094 exclExtLen = strlen(kExcludeExtension);
1095 for (i = 0; i < count; i++) {
1096 const char* name;
1097 int nameLen;
1098
1099 name = pContents->itemAt(i).getFileName().string();
1100 nameLen = strlen(name);
1101 if (nameLen > exclExtLen &&
1102 strcmp(name + (nameLen - exclExtLen), kExcludeExtension) == 0)
1103 {
1104 String8 match(name, nameLen - exclExtLen);
1105 int matchIdx;
1106
1107 matchIdx = AssetDir::FileInfo::findEntry(pMergedInfo, match);
1108 if (matchIdx > 0) {
1109 ALOGV("Excluding '%s' [%s]\n",
1110 pMergedInfo->itemAt(matchIdx).getFileName().string(),
1111 pMergedInfo->itemAt(matchIdx).getSourceName().string());
1112 pMergedInfo->removeAt(matchIdx);
1113 } else {
1114 //printf("+++ no match on '%s'\n", (const char*) match);
1115 }
1116
1117 ALOGD("HEY: size=%d removing %d\n", (int)pContents->size(), i);
1118 pContents->removeAt(i);
1119 i--; // adjust "for" loop
1120 count--; // and loop limit
1121 }
1122 }
1123
1124 mergeInfoLocked(pMergedInfo, pContents);
1125
1126 delete pContents;
1127
1128 return true;
1129}
1130
1131/*
1132 * Scan the contents of the specified directory, and stuff what we find
1133 * into a newly-allocated vector.
1134 *
1135 * Files ending in ".gz" will have their extensions removed.
1136 *
1137 * We should probably think about skipping files with "illegal" names,
1138 * e.g. illegal characters (/\:) or excessive length.
1139 *
1140 * Returns NULL if the specified directory doesn't exist.
1141 */
1142SortedVector<AssetDir::FileInfo>* AssetManager::scanDirLocked(const String8& path)
1143{
1144 SortedVector<AssetDir::FileInfo>* pContents = NULL;
1145 DIR* dir;
1146 struct dirent* entry;
1147 FileType fileType;
1148
1149 ALOGV("Scanning dir '%s'\n", path.string());
1150
1151 dir = opendir(path.string());
1152 if (dir == NULL)
1153 return NULL;
1154
1155 pContents = new SortedVector<AssetDir::FileInfo>;
1156
1157 while (1) {
1158 entry = readdir(dir);
1159 if (entry == NULL)
1160 break;
1161
1162 if (strcmp(entry->d_name, ".") == 0 ||
1163 strcmp(entry->d_name, "..") == 0)
1164 continue;
1165
1166#ifdef _DIRENT_HAVE_D_TYPE
1167 if (entry->d_type == DT_REG)
1168 fileType = kFileTypeRegular;
1169 else if (entry->d_type == DT_DIR)
1170 fileType = kFileTypeDirectory;
1171 else
1172 fileType = kFileTypeUnknown;
1173#else
1174 // stat the file
1175 fileType = ::getFileType(path.appendPathCopy(entry->d_name).string());
1176#endif
1177
1178 if (fileType != kFileTypeRegular && fileType != kFileTypeDirectory)
1179 continue;
1180
1181 AssetDir::FileInfo info;
1182 info.set(String8(entry->d_name), fileType);
1183 if (strcasecmp(info.getFileName().getPathExtension().string(), ".gz") == 0)
1184 info.setFileName(info.getFileName().getBasePath());
1185 info.setSourceName(path.appendPathCopy(info.getFileName()));
1186 pContents->add(info);
1187 }
1188
1189 closedir(dir);
1190 return pContents;
1191}
1192
1193/*
1194 * Scan the contents out of the specified Zip archive, and merge what we
1195 * find into "pMergedInfo". If the Zip archive in question doesn't exist,
1196 * we return immediately.
1197 *
1198 * Returns "false" if we found nothing to contribute.
1199 */
1200bool AssetManager::scanAndMergeZipLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
1201 const asset_path& ap, const char* rootDir, const char* baseDirName)
1202{
1203 ZipFileRO* pZip;
1204 Vector<String8> dirs;
1205 AssetDir::FileInfo info;
1206 SortedVector<AssetDir::FileInfo> contents;
1207 String8 sourceName, zipName, dirName;
1208
1209 pZip = mZipSet.getZip(ap.path);
1210 if (pZip == NULL) {
1211 ALOGW("Failure opening zip %s\n", ap.path.string());
1212 return false;
1213 }
1214
1215 zipName = ZipSet::getPathName(ap.path.string());
1216
1217 /* convert "sounds" to "rootDir/sounds" */
1218 if (rootDir != NULL) dirName = rootDir;
1219 dirName.appendPath(baseDirName);
1220
1221 /*
1222 * Scan through the list of files, looking for a match. The files in
1223 * the Zip table of contents are not in sorted order, so we have to
1224 * process the entire list. We're looking for a string that begins
1225 * with the characters in "dirName", is followed by a '/', and has no
1226 * subsequent '/' in the stuff that follows.
1227 *
1228 * What makes this especially fun is that directories are not stored
1229 * explicitly in Zip archives, so we have to infer them from context.
1230 * When we see "sounds/foo.wav" we have to leave a note to ourselves
1231 * to insert a directory called "sounds" into the list. We store
1232 * these in temporary vector so that we only return each one once.
1233 *
1234 * Name comparisons are case-sensitive to match UNIX filesystem
1235 * semantics.
1236 */
1237 int dirNameLen = dirName.length();
Narayan Kamath560566d2013-12-03 13:16:03 +00001238 void *iterationCookie;
Yusuke Sato05f648e2015-08-03 16:21:10 -07001239 if (!pZip->startIteration(&iterationCookie, dirName.string(), NULL)) {
Narayan Kamath560566d2013-12-03 13:16:03 +00001240 ALOGW("ZipFileRO::startIteration returned false");
1241 return false;
1242 }
1243
1244 ZipEntryRO entry;
1245 while ((entry = pZip->nextEntry(iterationCookie)) != NULL) {
Adam Lesinski16c4d152014-01-24 13:27:13 -08001246 char nameBuf[256];
1247
Adam Lesinski16c4d152014-01-24 13:27:13 -08001248 if (pZip->getEntryFileName(entry, nameBuf, sizeof(nameBuf)) != 0) {
1249 // TODO: fix this if we expect to have long names
1250 ALOGE("ARGH: name too long?\n");
1251 continue;
1252 }
1253 //printf("Comparing %s in %s?\n", nameBuf, dirName.string());
Yusuke Sato05f648e2015-08-03 16:21:10 -07001254 if (dirNameLen == 0 || nameBuf[dirNameLen] == '/')
Adam Lesinski16c4d152014-01-24 13:27:13 -08001255 {
1256 const char* cp;
1257 const char* nextSlash;
1258
1259 cp = nameBuf + dirNameLen;
1260 if (dirNameLen != 0)
1261 cp++; // advance past the '/'
1262
1263 nextSlash = strchr(cp, '/');
1264//xxx this may break if there are bare directory entries
1265 if (nextSlash == NULL) {
1266 /* this is a file in the requested directory */
1267
1268 info.set(String8(nameBuf).getPathLeaf(), kFileTypeRegular);
1269
1270 info.setSourceName(
1271 createZipSourceNameLocked(zipName, dirName, info.getFileName()));
1272
1273 contents.add(info);
1274 //printf("FOUND: file '%s'\n", info.getFileName().string());
1275 } else {
1276 /* this is a subdir; add it if we don't already have it*/
1277 String8 subdirName(cp, nextSlash - cp);
1278 size_t j;
1279 size_t N = dirs.size();
1280
1281 for (j = 0; j < N; j++) {
1282 if (subdirName == dirs[j]) {
1283 break;
1284 }
1285 }
1286 if (j == N) {
1287 dirs.add(subdirName);
1288 }
1289
1290 //printf("FOUND: dir '%s'\n", subdirName.string());
1291 }
1292 }
1293 }
1294
Narayan Kamath560566d2013-12-03 13:16:03 +00001295 pZip->endIteration(iterationCookie);
1296
Adam Lesinski16c4d152014-01-24 13:27:13 -08001297 /*
1298 * Add the set of unique directories.
1299 */
1300 for (int i = 0; i < (int) dirs.size(); i++) {
1301 info.set(dirs[i], kFileTypeDirectory);
1302 info.setSourceName(
1303 createZipSourceNameLocked(zipName, dirName, info.getFileName()));
1304 contents.add(info);
1305 }
1306
1307 mergeInfoLocked(pMergedInfo, &contents);
1308
1309 return true;
1310}
1311
1312
1313/*
1314 * Merge two vectors of FileInfo.
1315 *
1316 * The merged contents will be stuffed into *pMergedInfo.
1317 *
1318 * If an entry for a file exists in both "pMergedInfo" and "pContents",
1319 * we use the newer "pContents" entry.
1320 */
1321void AssetManager::mergeInfoLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
1322 const SortedVector<AssetDir::FileInfo>* pContents)
1323{
1324 /*
1325 * Merge what we found in this directory with what we found in
1326 * other places.
1327 *
1328 * Two basic approaches:
1329 * (1) Create a new array that holds the unique values of the two
1330 * arrays.
1331 * (2) Take the elements from pContents and shove them into pMergedInfo.
1332 *
1333 * Because these are vectors of complex objects, moving elements around
1334 * inside the vector requires constructing new objects and allocating
1335 * storage for members. With approach #1, we're always adding to the
1336 * end, whereas with #2 we could be inserting multiple elements at the
1337 * front of the vector. Approach #1 requires a full copy of the
1338 * contents of pMergedInfo, but approach #2 requires the same copy for
1339 * every insertion at the front of pMergedInfo.
1340 *
1341 * (We should probably use a SortedVector interface that allows us to
1342 * just stuff items in, trusting us to maintain the sort order.)
1343 */
1344 SortedVector<AssetDir::FileInfo>* pNewSorted;
1345 int mergeMax, contMax;
1346 int mergeIdx, contIdx;
1347
1348 pNewSorted = new SortedVector<AssetDir::FileInfo>;
1349 mergeMax = pMergedInfo->size();
1350 contMax = pContents->size();
1351 mergeIdx = contIdx = 0;
1352
1353 while (mergeIdx < mergeMax || contIdx < contMax) {
1354 if (mergeIdx == mergeMax) {
1355 /* hit end of "merge" list, copy rest of "contents" */
1356 pNewSorted->add(pContents->itemAt(contIdx));
1357 contIdx++;
1358 } else if (contIdx == contMax) {
1359 /* hit end of "cont" list, copy rest of "merge" */
1360 pNewSorted->add(pMergedInfo->itemAt(mergeIdx));
1361 mergeIdx++;
1362 } else if (pMergedInfo->itemAt(mergeIdx) == pContents->itemAt(contIdx))
1363 {
1364 /* items are identical, add newer and advance both indices */
1365 pNewSorted->add(pContents->itemAt(contIdx));
1366 mergeIdx++;
1367 contIdx++;
1368 } else if (pMergedInfo->itemAt(mergeIdx) < pContents->itemAt(contIdx))
1369 {
1370 /* "merge" is lower, add that one */
1371 pNewSorted->add(pMergedInfo->itemAt(mergeIdx));
1372 mergeIdx++;
1373 } else {
1374 /* "cont" is lower, add that one */
1375 assert(pContents->itemAt(contIdx) < pMergedInfo->itemAt(mergeIdx));
1376 pNewSorted->add(pContents->itemAt(contIdx));
1377 contIdx++;
1378 }
1379 }
1380
1381 /*
1382 * Overwrite the "merged" list with the new stuff.
1383 */
1384 *pMergedInfo = *pNewSorted;
1385 delete pNewSorted;
1386
1387#if 0 // for Vector, rather than SortedVector
1388 int i, j;
1389 for (i = pContents->size() -1; i >= 0; i--) {
1390 bool add = true;
1391
1392 for (j = pMergedInfo->size() -1; j >= 0; j--) {
1393 /* case-sensitive comparisons, to behave like UNIX fs */
1394 if (strcmp(pContents->itemAt(i).mFileName,
1395 pMergedInfo->itemAt(j).mFileName) == 0)
1396 {
1397 /* match, don't add this entry */
1398 add = false;
1399 break;
1400 }
1401 }
1402
1403 if (add)
1404 pMergedInfo->add(pContents->itemAt(i));
1405 }
1406#endif
1407}
1408
Adam Lesinski16c4d152014-01-24 13:27:13 -08001409/*
1410 * ===========================================================================
1411 * AssetManager::SharedZip
1412 * ===========================================================================
1413 */
1414
1415
1416Mutex AssetManager::SharedZip::gLock;
1417DefaultKeyedVector<String8, wp<AssetManager::SharedZip> > AssetManager::SharedZip::gOpen;
1418
1419AssetManager::SharedZip::SharedZip(const String8& path, time_t modWhen)
1420 : mPath(path), mZipFile(NULL), mModWhen(modWhen),
1421 mResourceTableAsset(NULL), mResourceTable(NULL)
1422{
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001423 if (kIsDebug) {
1424 ALOGI("Creating SharedZip %p %s\n", this, (const char*)mPath);
1425 }
Adam Lesinski16c4d152014-01-24 13:27:13 -08001426 ALOGV("+++ opening zip '%s'\n", mPath.string());
Narayan Kamath560566d2013-12-03 13:16:03 +00001427 mZipFile = ZipFileRO::open(mPath.string());
1428 if (mZipFile == NULL) {
Adam Lesinski16c4d152014-01-24 13:27:13 -08001429 ALOGD("failed to open Zip archive '%s'\n", mPath.string());
Adam Lesinski16c4d152014-01-24 13:27:13 -08001430 }
1431}
1432
Dianne Hackbornca3872c2017-10-30 14:19:32 -07001433AssetManager::SharedZip::SharedZip(int fd, const String8& path)
1434 : mPath(path), mZipFile(NULL), mModWhen(0),
1435 mResourceTableAsset(NULL), mResourceTable(NULL)
1436{
1437 if (kIsDebug) {
1438 ALOGI("Creating SharedZip %p fd=%d %s\n", this, fd, (const char*)mPath);
1439 }
1440 ALOGV("+++ opening zip fd=%d '%s'\n", fd, mPath.string());
1441 mZipFile = ZipFileRO::openFd(fd, mPath.string());
1442 if (mZipFile == NULL) {
1443 ::close(fd);
1444 ALOGD("failed to open Zip archive fd=%d '%s'\n", fd, mPath.string());
1445 }
1446}
1447
Mårten Kongstad48d22322014-01-31 14:43:27 +01001448sp<AssetManager::SharedZip> AssetManager::SharedZip::get(const String8& path,
1449 bool createIfNotPresent)
Adam Lesinski16c4d152014-01-24 13:27:13 -08001450{
1451 AutoMutex _l(gLock);
1452 time_t modWhen = getFileModDate(path);
1453 sp<SharedZip> zip = gOpen.valueFor(path).promote();
1454 if (zip != NULL && zip->mModWhen == modWhen) {
1455 return zip;
1456 }
Mårten Kongstad48d22322014-01-31 14:43:27 +01001457 if (zip == NULL && !createIfNotPresent) {
1458 return NULL;
1459 }
Adam Lesinski16c4d152014-01-24 13:27:13 -08001460 zip = new SharedZip(path, modWhen);
1461 gOpen.add(path, zip);
1462 return zip;
Dianne Hackbornca3872c2017-10-30 14:19:32 -07001463}
Adam Lesinski16c4d152014-01-24 13:27:13 -08001464
Dianne Hackbornca3872c2017-10-30 14:19:32 -07001465sp<AssetManager::SharedZip> AssetManager::SharedZip::create(int fd, const String8& path)
1466{
1467 return new SharedZip(fd, path);
Adam Lesinski16c4d152014-01-24 13:27:13 -08001468}
1469
1470ZipFileRO* AssetManager::SharedZip::getZip()
1471{
1472 return mZipFile;
1473}
1474
1475Asset* AssetManager::SharedZip::getResourceTableAsset()
1476{
songjinshi49921f22016-09-08 15:24:30 +08001477 AutoMutex _l(gLock);
Adam Lesinski16c4d152014-01-24 13:27:13 -08001478 ALOGV("Getting from SharedZip %p resource asset %p\n", this, mResourceTableAsset);
1479 return mResourceTableAsset;
1480}
1481
1482Asset* AssetManager::SharedZip::setResourceTableAsset(Asset* asset)
1483{
1484 {
1485 AutoMutex _l(gLock);
1486 if (mResourceTableAsset == NULL) {
Adam Lesinski16c4d152014-01-24 13:27:13 -08001487 // This is not thread safe the first time it is called, so
1488 // do it here with the global lock held.
1489 asset->getBuffer(true);
songjinshi49921f22016-09-08 15:24:30 +08001490 mResourceTableAsset = asset;
Adam Lesinski16c4d152014-01-24 13:27:13 -08001491 return asset;
1492 }
1493 }
1494 delete asset;
1495 return mResourceTableAsset;
1496}
1497
1498ResTable* AssetManager::SharedZip::getResourceTable()
1499{
1500 ALOGV("Getting from SharedZip %p resource table %p\n", this, mResourceTable);
1501 return mResourceTable;
1502}
1503
1504ResTable* AssetManager::SharedZip::setResourceTable(ResTable* res)
1505{
1506 {
1507 AutoMutex _l(gLock);
1508 if (mResourceTable == NULL) {
1509 mResourceTable = res;
1510 return res;
1511 }
1512 }
1513 delete res;
1514 return mResourceTable;
1515}
1516
1517bool AssetManager::SharedZip::isUpToDate()
1518{
1519 time_t modWhen = getFileModDate(mPath.string());
1520 return mModWhen == modWhen;
1521}
1522
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +09001523void AssetManager::SharedZip::addOverlay(const asset_path& ap)
1524{
1525 mOverlays.add(ap);
1526}
1527
1528bool AssetManager::SharedZip::getOverlay(size_t idx, asset_path* out) const
1529{
1530 if (idx >= mOverlays.size()) {
1531 return false;
1532 }
1533 *out = mOverlays[idx];
1534 return true;
1535}
1536
Adam Lesinski16c4d152014-01-24 13:27:13 -08001537AssetManager::SharedZip::~SharedZip()
1538{
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001539 if (kIsDebug) {
1540 ALOGI("Destroying SharedZip %p %s\n", this, (const char*)mPath);
1541 }
Adam Lesinski16c4d152014-01-24 13:27:13 -08001542 if (mResourceTable != NULL) {
1543 delete mResourceTable;
1544 }
1545 if (mResourceTableAsset != NULL) {
1546 delete mResourceTableAsset;
1547 }
1548 if (mZipFile != NULL) {
1549 delete mZipFile;
1550 ALOGV("Closed '%s'\n", mPath.string());
1551 }
1552}
1553
1554/*
1555 * ===========================================================================
1556 * AssetManager::ZipSet
1557 * ===========================================================================
1558 */
1559
1560/*
Adam Lesinski16c4d152014-01-24 13:27:13 -08001561 * Destructor. Close any open archives.
1562 */
1563AssetManager::ZipSet::~ZipSet(void)
1564{
1565 size_t N = mZipFile.size();
1566 for (size_t i = 0; i < N; i++)
1567 closeZip(i);
1568}
1569
1570/*
1571 * Close a Zip file and reset the entry.
1572 */
1573void AssetManager::ZipSet::closeZip(int idx)
1574{
1575 mZipFile.editItemAt(idx) = NULL;
1576}
1577
Adam Lesinski16c4d152014-01-24 13:27:13 -08001578/*
1579 * Retrieve the appropriate Zip file from the set.
1580 */
1581ZipFileRO* AssetManager::ZipSet::getZip(const String8& path)
1582{
Dianne Hackbornca3872c2017-10-30 14:19:32 -07001583 return getSharedZip(path)->getZip();
1584}
1585
1586const sp<AssetManager::SharedZip> AssetManager::ZipSet::getSharedZip(const String8& path)
1587{
Adam Lesinski16c4d152014-01-24 13:27:13 -08001588 int idx = getIndex(path);
1589 sp<SharedZip> zip = mZipFile[idx];
1590 if (zip == NULL) {
1591 zip = SharedZip::get(path);
1592 mZipFile.editItemAt(idx) = zip;
1593 }
Dianne Hackbornca3872c2017-10-30 14:19:32 -07001594 return zip;
Adam Lesinski16c4d152014-01-24 13:27:13 -08001595}
1596
1597Asset* AssetManager::ZipSet::getZipResourceTableAsset(const String8& path)
1598{
1599 int idx = getIndex(path);
1600 sp<SharedZip> zip = mZipFile[idx];
1601 if (zip == NULL) {
1602 zip = SharedZip::get(path);
1603 mZipFile.editItemAt(idx) = zip;
1604 }
1605 return zip->getResourceTableAsset();
1606}
1607
1608Asset* AssetManager::ZipSet::setZipResourceTableAsset(const String8& path,
1609 Asset* asset)
1610{
1611 int idx = getIndex(path);
1612 sp<SharedZip> zip = mZipFile[idx];
1613 // doesn't make sense to call before previously accessing.
1614 return zip->setResourceTableAsset(asset);
1615}
1616
1617ResTable* AssetManager::ZipSet::getZipResourceTable(const String8& path)
1618{
1619 int idx = getIndex(path);
1620 sp<SharedZip> zip = mZipFile[idx];
1621 if (zip == NULL) {
1622 zip = SharedZip::get(path);
1623 mZipFile.editItemAt(idx) = zip;
1624 }
1625 return zip->getResourceTable();
1626}
1627
1628ResTable* AssetManager::ZipSet::setZipResourceTable(const String8& path,
1629 ResTable* res)
1630{
1631 int idx = getIndex(path);
1632 sp<SharedZip> zip = mZipFile[idx];
1633 // doesn't make sense to call before previously accessing.
1634 return zip->setResourceTable(res);
1635}
1636
1637/*
1638 * Generate the partial pathname for the specified archive. The caller
1639 * gets to prepend the asset root directory.
1640 *
1641 * Returns something like "common/en-US-noogle.jar".
1642 */
1643/*static*/ String8 AssetManager::ZipSet::getPathName(const char* zipPath)
1644{
1645 return String8(zipPath);
1646}
1647
1648bool AssetManager::ZipSet::isUpToDate()
1649{
1650 const size_t N = mZipFile.size();
1651 for (size_t i=0; i<N; i++) {
1652 if (mZipFile[i] != NULL && !mZipFile[i]->isUpToDate()) {
1653 return false;
1654 }
1655 }
1656 return true;
1657}
1658
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +09001659void AssetManager::ZipSet::addOverlay(const String8& path, const asset_path& overlay)
1660{
1661 int idx = getIndex(path);
1662 sp<SharedZip> zip = mZipFile[idx];
1663 zip->addOverlay(overlay);
1664}
1665
1666bool AssetManager::ZipSet::getOverlay(const String8& path, size_t idx, asset_path* out) const
1667{
1668 sp<SharedZip> zip = SharedZip::get(path, false);
1669 if (zip == NULL) {
1670 return false;
1671 }
1672 return zip->getOverlay(idx, out);
1673}
1674
Adam Lesinski16c4d152014-01-24 13:27:13 -08001675/*
1676 * Compute the zip file's index.
1677 *
1678 * "appName", "locale", and "vendor" should be set to NULL to indicate the
1679 * default directory.
1680 */
1681int AssetManager::ZipSet::getIndex(const String8& zip) const
1682{
1683 const size_t N = mZipPath.size();
1684 for (size_t i=0; i<N; i++) {
1685 if (mZipPath[i] == zip) {
1686 return i;
1687 }
1688 }
1689
1690 mZipPath.add(zip);
1691 mZipFile.add(NULL);
1692
1693 return mZipPath.size()-1;
1694}