blob: 1cb0d25d8c0802e7c8c5370972ee75307d5f91af [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";
Jakub Adamek54dcaab2016-10-19 11:46:13 +010078const char* AssetManager::OVERLAY_THEME_DIR_PROPERTY = "ro.boot.vendor.overlay.theme";
Mårten Kongstad48d22322014-01-31 14:43:27 +010079const char* AssetManager::TARGET_PACKAGE_NAME = "android";
80const char* AssetManager::TARGET_APK_PATH = "/system/framework/framework-res.apk";
81const char* AssetManager::IDMAP_DIR = "/data/resource-cache";
Mårten Kongstad65a05fd2014-01-31 14:01:52 +010082
Adam Lesinski16c4d152014-01-24 13:27:13 -080083namespace {
Adam Lesinski16c4d152014-01-24 13:27:13 -080084
Adam Lesinskia77685f2016-10-03 16:26:28 -070085String8 idmapPathForPackagePath(const String8& pkgPath) {
86 const char* root = getenv("ANDROID_DATA");
87 LOG_ALWAYS_FATAL_IF(root == NULL, "ANDROID_DATA not set");
88 String8 path(root);
89 path.appendPath(kResourceCache);
Adam Lesinski16c4d152014-01-24 13:27:13 -080090
Adam Lesinskia77685f2016-10-03 16:26:28 -070091 char buf[256]; // 256 chars should be enough for anyone...
92 strncpy(buf, pkgPath.string(), 255);
93 buf[255] = '\0';
94 char* filename = buf;
95 while (*filename && *filename == '/') {
96 ++filename;
Adam Lesinski16c4d152014-01-24 13:27:13 -080097 }
Adam Lesinskia77685f2016-10-03 16:26:28 -070098 char* p = filename;
99 while (*p) {
100 if (*p == '/') {
101 *p = '@';
102 }
103 ++p;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800104 }
Adam Lesinskia77685f2016-10-03 16:26:28 -0700105 path.appendPath(filename);
106 path.append("@idmap");
107
108 return path;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800109}
110
111/*
Adam Lesinskia77685f2016-10-03 16:26:28 -0700112 * Like strdup(), but uses C++ "new" operator instead of malloc.
113 */
114static char* strdupNew(const char* str) {
115 char* newStr;
116 int len;
117
118 if (str == NULL)
119 return NULL;
120
121 len = strlen(str);
122 newStr = new char[len+1];
123 memcpy(newStr, str, len+1);
124
125 return newStr;
126}
127
128} // namespace
129
130/*
Adam Lesinski16c4d152014-01-24 13:27:13 -0800131 * ===========================================================================
132 * AssetManager
133 * ===========================================================================
134 */
135
Adam Lesinskia77685f2016-10-03 16:26:28 -0700136int32_t AssetManager::getGlobalCount() {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800137 return gCount;
138}
139
Adam Lesinskia77685f2016-10-03 16:26:28 -0700140AssetManager::AssetManager() :
141 mLocale(NULL), mResources(NULL), mConfig(new ResTable_config) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700142 int count = android_atomic_inc(&gCount) + 1;
143 if (kIsDebug) {
144 ALOGI("Creating AssetManager %p #%d\n", this, count);
145 }
Adam Lesinski16c4d152014-01-24 13:27:13 -0800146 memset(mConfig, 0, sizeof(ResTable_config));
147}
148
Adam Lesinskia77685f2016-10-03 16:26:28 -0700149AssetManager::~AssetManager() {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800150 int count = android_atomic_dec(&gCount);
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700151 if (kIsDebug) {
152 ALOGI("Destroying AssetManager in %p #%d\n", this, count);
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000153 } else {
154 ALOGV("Destroying AssetManager in %p #%d\n", this, count);
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700155 }
Adam Lesinski16c4d152014-01-24 13:27:13 -0800156
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700157 // Manually close any fd paths for which we have not yet opened their zip (which
158 // will take ownership of the fd and close it when done).
159 for (size_t i=0; i<mAssetPaths.size(); i++) {
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000160 ALOGV("Cleaning path #%d: fd=%d, zip=%p", (int)i, mAssetPaths[i].rawFd,
161 mAssetPaths[i].zip.get());
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700162 if (mAssetPaths[i].rawFd >= 0 && mAssetPaths[i].zip == NULL) {
163 close(mAssetPaths[i].rawFd);
164 }
165 }
166
Adam Lesinski16c4d152014-01-24 13:27:13 -0800167 delete mConfig;
168 delete mResources;
169
170 // don't have a String class yet, so make sure we clean up
171 delete[] mLocale;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800172}
173
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800174bool AssetManager::addAssetPath(
Adam Lesinskia77685f2016-10-03 16:26:28 -0700175 const String8& path, int32_t* cookie, bool appAsLib, bool isSystemAsset) {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800176 AutoMutex _l(mLock);
177
178 asset_path ap;
179
180 String8 realPath(path);
181 if (kAppZipName) {
182 realPath.appendPath(kAppZipName);
183 }
184 ap.type = ::getFileType(realPath.string());
185 if (ap.type == kFileTypeRegular) {
186 ap.path = realPath;
187 } else {
188 ap.path = path;
189 ap.type = ::getFileType(path.string());
190 if (ap.type != kFileTypeDirectory && ap.type != kFileTypeRegular) {
191 ALOGW("Asset path %s is neither a directory nor file (type=%d).",
192 path.string(), (int)ap.type);
193 return false;
194 }
195 }
196
197 // Skip if we have it already.
198 for (size_t i=0; i<mAssetPaths.size(); i++) {
199 if (mAssetPaths[i].path == ap.path) {
200 if (cookie) {
Narayan Kamatha0c62602014-01-24 13:51:51 +0000201 *cookie = static_cast<int32_t>(i+1);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800202 }
203 return true;
204 }
205 }
206
207 ALOGV("In %p Asset %s path: %s", this,
208 ap.type == kFileTypeDirectory ? "dir" : "zip", ap.path.string());
209
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800210 ap.isSystemAsset = isSystemAsset;
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000211 ssize_t apPos = mAssetPaths.add(ap);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800212
213 // new paths are always added at the end
214 if (cookie) {
Narayan Kamatha0c62602014-01-24 13:51:51 +0000215 *cookie = static_cast<int32_t>(mAssetPaths.size());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800216 }
217
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +0900218#ifdef __ANDROID__
219 // Load overlays, if any
220 asset_path oap;
221 for (size_t idx = 0; mZipSet.getOverlay(ap.path, idx, &oap); idx++) {
222 oap.isSystemAsset = isSystemAsset;
223 mAssetPaths.add(oap);
224 }
225#endif
226
Martin Kosiba7df36252014-01-16 16:25:56 +0000227 if (mResources != NULL) {
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000228 appendPathToResTable(mAssetPaths.editItemAt(apPos), appAsLib);
Martin Kosiba7df36252014-01-16 16:25:56 +0000229 }
230
Adam Lesinski16c4d152014-01-24 13:27:13 -0800231 return true;
232}
233
Mårten Kongstad48d22322014-01-31 14:43:27 +0100234bool AssetManager::addOverlayPath(const String8& packagePath, int32_t* cookie)
235{
236 const String8 idmapPath = idmapPathForPackagePath(packagePath);
237
238 AutoMutex _l(mLock);
239
240 for (size_t i = 0; i < mAssetPaths.size(); ++i) {
241 if (mAssetPaths[i].idmap == idmapPath) {
242 *cookie = static_cast<int32_t>(i + 1);
243 return true;
244 }
245 }
246
247 Asset* idmap = NULL;
248 if ((idmap = openAssetFromFileLocked(idmapPath, Asset::ACCESS_BUFFER)) == NULL) {
249 ALOGW("failed to open idmap file %s\n", idmapPath.string());
250 return false;
251 }
252
253 String8 targetPath;
254 String8 overlayPath;
255 if (!ResTable::getIdmapInfo(idmap->getBuffer(false), idmap->getLength(),
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700256 NULL, NULL, NULL, &targetPath, &overlayPath)) {
Mårten Kongstad48d22322014-01-31 14:43:27 +0100257 ALOGW("failed to read idmap file %s\n", idmapPath.string());
258 delete idmap;
259 return false;
260 }
261 delete idmap;
262
263 if (overlayPath != packagePath) {
264 ALOGW("idmap file %s inconcistent: expected path %s does not match actual path %s\n",
265 idmapPath.string(), packagePath.string(), overlayPath.string());
266 return false;
267 }
268 if (access(targetPath.string(), R_OK) != 0) {
269 ALOGW("failed to access file %s: %s\n", targetPath.string(), strerror(errno));
270 return false;
271 }
272 if (access(idmapPath.string(), R_OK) != 0) {
273 ALOGW("failed to access file %s: %s\n", idmapPath.string(), strerror(errno));
274 return false;
275 }
276 if (access(overlayPath.string(), R_OK) != 0) {
277 ALOGW("failed to access file %s: %s\n", overlayPath.string(), strerror(errno));
278 return false;
279 }
280
281 asset_path oap;
282 oap.path = overlayPath;
283 oap.type = ::getFileType(overlayPath.string());
284 oap.idmap = idmapPath;
285#if 0
286 ALOGD("Overlay added: targetPath=%s overlayPath=%s idmapPath=%s\n",
287 targetPath.string(), overlayPath.string(), idmapPath.string());
288#endif
289 mAssetPaths.add(oap);
290 *cookie = static_cast<int32_t>(mAssetPaths.size());
291
Mårten Kongstad30113132014-11-07 10:52:17 +0100292 if (mResources != NULL) {
293 appendPathToResTable(oap);
294 }
295
Mårten Kongstad48d22322014-01-31 14:43:27 +0100296 return true;
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700297}
298
299bool AssetManager::addAssetFd(
300 int fd, const String8& debugPathName, int32_t* cookie, bool appAsLib,
301 bool assume_ownership) {
302 AutoMutex _l(mLock);
303
304 asset_path ap;
305
306 ap.path = debugPathName;
307 ap.rawFd = fd;
308 ap.type = kFileTypeRegular;
309 ap.assumeOwnership = assume_ownership;
310
311 ALOGV("In %p Asset fd %d name: %s", this, fd, ap.path.string());
312
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000313 ssize_t apPos = mAssetPaths.add(ap);
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700314
315 // new paths are always added at the end
316 if (cookie) {
317 *cookie = static_cast<int32_t>(mAssetPaths.size());
318 }
319
320 if (mResources != NULL) {
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000321 appendPathToResTable(mAssetPaths.editItemAt(apPos), appAsLib);
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700322 }
323
324 return true;
325}
Mårten Kongstad48d22322014-01-31 14:43:27 +0100326
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100327bool AssetManager::createIdmap(const char* targetApkPath, const char* overlayApkPath,
Dianne Hackborn32bb5fa2014-02-11 13:56:21 -0800328 uint32_t targetCrc, uint32_t overlayCrc, uint32_t** outData, size_t* outSize)
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100329{
330 AutoMutex _l(mLock);
331 const String8 paths[2] = { String8(targetApkPath), String8(overlayApkPath) };
Mårten Kongstad6bb13da2016-06-02 09:34:36 +0200332 Asset* assets[2] = {NULL, NULL};
333 bool ret = false;
334 {
335 ResTable tables[2];
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100336
Mårten Kongstad6bb13da2016-06-02 09:34:36 +0200337 for (int i = 0; i < 2; ++i) {
338 asset_path ap;
339 ap.type = kFileTypeRegular;
340 ap.path = paths[i];
341 assets[i] = openNonAssetInPathLocked("resources.arsc",
342 Asset::ACCESS_BUFFER, ap);
343 if (assets[i] == NULL) {
344 ALOGW("failed to find resources.arsc in %s\n", ap.path.string());
345 goto exit;
346 }
347 if (tables[i].add(assets[i]) != NO_ERROR) {
348 ALOGW("failed to add %s to resource table", paths[i].string());
349 goto exit;
350 }
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100351 }
Mårten Kongstad6bb13da2016-06-02 09:34:36 +0200352 ret = tables[0].createIdmap(tables[1], targetCrc, overlayCrc,
353 targetApkPath, overlayApkPath, (void**)outData, outSize) == NO_ERROR;
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100354 }
355
Mårten Kongstad6bb13da2016-06-02 09:34:36 +0200356exit:
357 delete assets[0];
358 delete assets[1];
359 return ret;
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100360}
361
Adam Lesinski16c4d152014-01-24 13:27:13 -0800362bool AssetManager::addDefaultAssets()
363{
364 const char* root = getenv("ANDROID_ROOT");
365 LOG_ALWAYS_FATAL_IF(root == NULL, "ANDROID_ROOT not set");
366
367 String8 path(root);
368 path.appendPath(kSystemAssets);
369
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800370 return addAssetPath(path, NULL, false /* appAsLib */, true /* isSystemAsset */);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800371}
372
Narayan Kamatha0c62602014-01-24 13:51:51 +0000373int32_t AssetManager::nextAssetPath(const int32_t cookie) const
Adam Lesinski16c4d152014-01-24 13:27:13 -0800374{
375 AutoMutex _l(mLock);
Narayan Kamatha0c62602014-01-24 13:51:51 +0000376 const size_t next = static_cast<size_t>(cookie) + 1;
377 return next > mAssetPaths.size() ? -1 : next;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800378}
379
Narayan Kamatha0c62602014-01-24 13:51:51 +0000380String8 AssetManager::getAssetPath(const int32_t cookie) const
Adam Lesinski16c4d152014-01-24 13:27:13 -0800381{
382 AutoMutex _l(mLock);
Narayan Kamatha0c62602014-01-24 13:51:51 +0000383 const size_t which = static_cast<size_t>(cookie) - 1;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800384 if (which < mAssetPaths.size()) {
385 return mAssetPaths[which].path;
386 }
387 return String8();
388}
389
Adam Lesinski16c4d152014-01-24 13:27:13 -0800390void AssetManager::setLocaleLocked(const char* locale)
391{
392 if (mLocale != NULL) {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800393 delete[] mLocale;
394 }
Elliott Hughesc367d482013-10-29 13:12:55 -0700395
Adam Lesinski16c4d152014-01-24 13:27:13 -0800396 mLocale = strdupNew(locale);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800397 updateResourceParamsLocked();
398}
399
Adam Lesinski16c4d152014-01-24 13:27:13 -0800400void AssetManager::setConfiguration(const ResTable_config& config, const char* locale)
401{
402 AutoMutex _l(mLock);
403 *mConfig = config;
404 if (locale) {
405 setLocaleLocked(locale);
406 } else if (config.language[0] != 0) {
Narayan Kamath91447d82014-01-21 15:32:36 +0000407 char spec[RESTABLE_MAX_LOCALE_LEN];
408 config.getBcp47Locale(spec);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800409 setLocaleLocked(spec);
410 } else {
411 updateResourceParamsLocked();
412 }
413}
414
415void AssetManager::getConfiguration(ResTable_config* outConfig) const
416{
417 AutoMutex _l(mLock);
418 *outConfig = *mConfig;
419}
420
421/*
422 * Open an asset.
423 *
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700424 * The data could be in any asset path. Each asset path could be:
425 * - A directory on disk.
426 * - A Zip archive, uncompressed or compressed.
Adam Lesinski16c4d152014-01-24 13:27:13 -0800427 *
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700428 * If the file is in a directory, it could have a .gz suffix, meaning it is compressed.
Adam Lesinski16c4d152014-01-24 13:27:13 -0800429 *
430 * We should probably reject requests for "illegal" filenames, e.g. those
431 * with illegal characters or "../" backward relative paths.
432 */
433Asset* AssetManager::open(const char* fileName, AccessMode mode)
434{
435 AutoMutex _l(mLock);
436
437 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
438
Adam Lesinski16c4d152014-01-24 13:27:13 -0800439 String8 assetName(kAssetsRoot);
440 assetName.appendPath(fileName);
441
442 /*
443 * For each top-level asset path, search for the asset.
444 */
445
446 size_t i = mAssetPaths.size();
447 while (i > 0) {
448 i--;
449 ALOGV("Looking for asset '%s' in '%s'\n",
450 assetName.string(), mAssetPaths.itemAt(i).path.string());
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000451 Asset* pAsset = openNonAssetInPathLocked(assetName.string(), mode,
452 mAssetPaths.editItemAt(i));
Adam Lesinski16c4d152014-01-24 13:27:13 -0800453 if (pAsset != NULL) {
454 return pAsset != kExcludedAsset ? pAsset : NULL;
455 }
456 }
457
458 return NULL;
459}
460
461/*
462 * Open a non-asset file as if it were an asset.
463 *
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700464 * The "fileName" is the partial path starting from the application name.
Adam Lesinski16c4d152014-01-24 13:27:13 -0800465 */
Adam Lesinskide898ff2014-01-29 18:20:45 -0800466Asset* AssetManager::openNonAsset(const char* fileName, AccessMode mode, int32_t* outCookie)
Adam Lesinski16c4d152014-01-24 13:27:13 -0800467{
468 AutoMutex _l(mLock);
469
470 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
471
Adam Lesinski16c4d152014-01-24 13:27:13 -0800472 /*
473 * For each top-level asset path, search for the asset.
474 */
475
476 size_t i = mAssetPaths.size();
477 while (i > 0) {
478 i--;
479 ALOGV("Looking for non-asset '%s' in '%s'\n", fileName, mAssetPaths.itemAt(i).path.string());
480 Asset* pAsset = openNonAssetInPathLocked(
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000481 fileName, mode, mAssetPaths.editItemAt(i));
Adam Lesinski16c4d152014-01-24 13:27:13 -0800482 if (pAsset != NULL) {
Adam Lesinskide898ff2014-01-29 18:20:45 -0800483 if (outCookie != NULL) *outCookie = static_cast<int32_t>(i + 1);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800484 return pAsset != kExcludedAsset ? pAsset : NULL;
485 }
486 }
487
488 return NULL;
489}
490
Narayan Kamatha0c62602014-01-24 13:51:51 +0000491Asset* AssetManager::openNonAsset(const int32_t cookie, const char* fileName, AccessMode mode)
Adam Lesinski16c4d152014-01-24 13:27:13 -0800492{
Narayan Kamatha0c62602014-01-24 13:51:51 +0000493 const size_t which = static_cast<size_t>(cookie) - 1;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800494
495 AutoMutex _l(mLock);
496
497 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
498
Adam Lesinski16c4d152014-01-24 13:27:13 -0800499 if (which < mAssetPaths.size()) {
500 ALOGV("Looking for non-asset '%s' in '%s'\n", fileName,
501 mAssetPaths.itemAt(which).path.string());
502 Asset* pAsset = openNonAssetInPathLocked(
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000503 fileName, mode, mAssetPaths.editItemAt(which));
Adam Lesinski16c4d152014-01-24 13:27:13 -0800504 if (pAsset != NULL) {
505 return pAsset != kExcludedAsset ? pAsset : NULL;
506 }
507 }
508
509 return NULL;
510}
511
512/*
513 * Get the type of a file in the asset namespace.
514 *
515 * This currently only works for regular files. All others (including
516 * directories) will return kFileTypeNonexistent.
517 */
518FileType AssetManager::getFileType(const char* fileName)
519{
520 Asset* pAsset = NULL;
521
522 /*
523 * Open the asset. This is less efficient than simply finding the
524 * file, but it's not too bad (we don't uncompress or mmap data until
525 * the first read() call).
526 */
527 pAsset = open(fileName, Asset::ACCESS_STREAMING);
528 delete pAsset;
529
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700530 if (pAsset == NULL) {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800531 return kFileTypeNonexistent;
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700532 } else {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800533 return kFileTypeRegular;
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700534 }
Adam Lesinski16c4d152014-01-24 13:27:13 -0800535}
536
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000537bool AssetManager::appendPathToResTable(asset_path& ap, bool appAsLib) const {
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +0900538 // skip those ap's that correspond to system overlays
539 if (ap.isSystemOverlay) {
540 return true;
541 }
542
Martin Kosiba7df36252014-01-16 16:25:56 +0000543 Asset* ass = NULL;
544 ResTable* sharedRes = NULL;
545 bool shared = true;
546 bool onlyEmptyResources = true;
Adam Lesinskib7e1ce02016-04-11 20:03:01 -0700547 ATRACE_NAME(ap.path.string());
Martin Kosiba7df36252014-01-16 16:25:56 +0000548 Asset* idmap = openIdmapLocked(ap);
549 size_t nextEntryIdx = mResources->getTableCount();
550 ALOGV("Looking for resource asset in '%s'\n", ap.path.string());
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700551 if (ap.type != kFileTypeDirectory && ap.rawFd < 0) {
Martin Kosiba7df36252014-01-16 16:25:56 +0000552 if (nextEntryIdx == 0) {
553 // The first item is typically the framework resources,
554 // which we want to avoid parsing every time.
555 sharedRes = const_cast<AssetManager*>(this)->
556 mZipSet.getZipResourceTable(ap.path);
557 if (sharedRes != NULL) {
558 // skip ahead the number of system overlay packages preloaded
559 nextEntryIdx = sharedRes->getTableCount();
560 }
561 }
562 if (sharedRes == NULL) {
563 ass = const_cast<AssetManager*>(this)->
564 mZipSet.getZipResourceTableAsset(ap.path);
565 if (ass == NULL) {
566 ALOGV("loading resource table %s\n", ap.path.string());
567 ass = const_cast<AssetManager*>(this)->
568 openNonAssetInPathLocked("resources.arsc",
569 Asset::ACCESS_BUFFER,
570 ap);
571 if (ass != NULL && ass != kExcludedAsset) {
572 ass = const_cast<AssetManager*>(this)->
573 mZipSet.setZipResourceTableAsset(ap.path, ass);
574 }
575 }
576
577 if (nextEntryIdx == 0 && ass != NULL) {
578 // If this is the first resource table in the asset
579 // manager, then we are going to cache it so that we
580 // can quickly copy it out for others.
581 ALOGV("Creating shared resources for %s", ap.path.string());
582 sharedRes = new ResTable();
583 sharedRes->add(ass, idmap, nextEntryIdx + 1, false);
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +0900584#ifdef __ANDROID__
585 const char* data = getenv("ANDROID_DATA");
586 LOG_ALWAYS_FATAL_IF(data == NULL, "ANDROID_DATA not set");
587 String8 overlaysListPath(data);
588 overlaysListPath.appendPath(kResourceCache);
589 overlaysListPath.appendPath("overlays.list");
590 addSystemOverlays(overlaysListPath.string(), ap.path, sharedRes, nextEntryIdx);
591#endif
Martin Kosiba7df36252014-01-16 16:25:56 +0000592 sharedRes = const_cast<AssetManager*>(this)->
593 mZipSet.setZipResourceTable(ap.path, sharedRes);
594 }
595 }
596 } else {
597 ALOGV("loading resource table %s\n", ap.path.string());
598 ass = const_cast<AssetManager*>(this)->
599 openNonAssetInPathLocked("resources.arsc",
600 Asset::ACCESS_BUFFER,
601 ap);
602 shared = false;
603 }
604
605 if ((ass != NULL || sharedRes != NULL) && ass != kExcludedAsset) {
606 ALOGV("Installing resource asset %p in to table %p\n", ass, mResources);
607 if (sharedRes != NULL) {
608 ALOGV("Copying existing resources for %s", ap.path.string());
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800609 mResources->add(sharedRes, ap.isSystemAsset);
Martin Kosiba7df36252014-01-16 16:25:56 +0000610 } else {
611 ALOGV("Parsing resources for %s", ap.path.string());
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800612 mResources->add(ass, idmap, nextEntryIdx + 1, !shared, appAsLib, ap.isSystemAsset);
Martin Kosiba7df36252014-01-16 16:25:56 +0000613 }
614 onlyEmptyResources = false;
615
616 if (!shared) {
617 delete ass;
618 }
619 } else {
620 ALOGV("Installing empty resources in to table %p\n", mResources);
621 mResources->addEmpty(nextEntryIdx + 1);
622 }
623
624 if (idmap != NULL) {
625 delete idmap;
626 }
Martin Kosiba7df36252014-01-16 16:25:56 +0000627 return onlyEmptyResources;
628}
629
Adam Lesinski16c4d152014-01-24 13:27:13 -0800630const ResTable* AssetManager::getResTable(bool required) const
631{
632 ResTable* rt = mResources;
633 if (rt) {
634 return rt;
635 }
636
637 // Iterate through all asset packages, collecting resources from each.
638
639 AutoMutex _l(mLock);
640
641 if (mResources != NULL) {
642 return mResources;
643 }
644
645 if (required) {
646 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
647 }
648
Adam Lesinskide898ff2014-01-29 18:20:45 -0800649 mResources = new ResTable();
650 updateResourceParamsLocked();
651
652 bool onlyEmptyResources = true;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800653 const size_t N = mAssetPaths.size();
654 for (size_t i=0; i<N; i++) {
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000655 bool empty = appendPathToResTable(
656 const_cast<AssetManager*>(this)->mAssetPaths.editItemAt(i));
Martin Kosiba7df36252014-01-16 16:25:56 +0000657 onlyEmptyResources = onlyEmptyResources && empty;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800658 }
659
Adam Lesinskide898ff2014-01-29 18:20:45 -0800660 if (required && onlyEmptyResources) {
661 ALOGW("Unable to find resources file resources.arsc");
662 delete mResources;
663 mResources = NULL;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800664 }
Adam Lesinskide898ff2014-01-29 18:20:45 -0800665
666 return mResources;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800667}
668
669void AssetManager::updateResourceParamsLocked() const
670{
Adam Lesinskib7e1ce02016-04-11 20:03:01 -0700671 ATRACE_CALL();
Adam Lesinski16c4d152014-01-24 13:27:13 -0800672 ResTable* res = mResources;
673 if (!res) {
674 return;
675 }
676
Narayan Kamath91447d82014-01-21 15:32:36 +0000677 if (mLocale) {
678 mConfig->setBcp47Locale(mLocale);
679 } else {
680 mConfig->clearLocale();
Adam Lesinski16c4d152014-01-24 13:27:13 -0800681 }
Adam Lesinski16c4d152014-01-24 13:27:13 -0800682
683 res->setParameters(mConfig);
684}
685
686Asset* AssetManager::openIdmapLocked(const struct asset_path& ap) const
687{
688 Asset* ass = NULL;
689 if (ap.idmap.size() != 0) {
690 ass = const_cast<AssetManager*>(this)->
691 openAssetFromFileLocked(ap.idmap, Asset::ACCESS_BUFFER);
692 if (ass) {
693 ALOGV("loading idmap %s\n", ap.idmap.string());
694 } else {
695 ALOGW("failed to load idmap %s\n", ap.idmap.string());
696 }
697 }
698 return ass;
699}
700
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +0900701void AssetManager::addSystemOverlays(const char* pathOverlaysList,
702 const String8& targetPackagePath, ResTable* sharedRes, size_t offset) const
703{
704 FILE* fin = fopen(pathOverlaysList, "r");
705 if (fin == NULL) {
706 return;
707 }
708
709#ifndef _WIN32
710 if (TEMP_FAILURE_RETRY(flock(fileno(fin), LOCK_SH)) != 0) {
711 fclose(fin);
712 return;
713 }
714#endif
715 char buf[1024];
716 while (fgets(buf, sizeof(buf), fin)) {
717 // format of each line:
718 // <path to apk><space><path to idmap><newline>
719 char* space = strchr(buf, ' ');
720 char* newline = strchr(buf, '\n');
721 asset_path oap;
722
723 if (space == NULL || newline == NULL || newline < space) {
724 continue;
725 }
726
727 oap.path = String8(buf, space - buf);
728 oap.type = kFileTypeRegular;
729 oap.idmap = String8(space + 1, newline - space - 1);
730 oap.isSystemOverlay = true;
731
732 Asset* oass = const_cast<AssetManager*>(this)->
733 openNonAssetInPathLocked("resources.arsc",
734 Asset::ACCESS_BUFFER,
735 oap);
736
737 if (oass != NULL) {
738 Asset* oidmap = openIdmapLocked(oap);
739 offset++;
740 sharedRes->add(oass, oidmap, offset + 1, false);
741 const_cast<AssetManager*>(this)->mAssetPaths.add(oap);
742 const_cast<AssetManager*>(this)->mZipSet.addOverlay(targetPackagePath, oap);
743 delete oidmap;
744 }
745 }
746
747#ifndef _WIN32
748 TEMP_FAILURE_RETRY(flock(fileno(fin), LOCK_UN));
749#endif
750 fclose(fin);
751}
752
Adam Lesinski16c4d152014-01-24 13:27:13 -0800753const ResTable& AssetManager::getResources(bool required) const
754{
755 const ResTable* rt = getResTable(required);
756 return *rt;
757}
758
759bool AssetManager::isUpToDate()
760{
761 AutoMutex _l(mLock);
762 return mZipSet.isUpToDate();
763}
764
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800765void AssetManager::getLocales(Vector<String8>* locales, bool includeSystemLocales) const
Adam Lesinski16c4d152014-01-24 13:27:13 -0800766{
767 ResTable* res = mResources;
768 if (res != NULL) {
Roozbeh Pournader7e5f96f2016-06-13 18:10:49 -0700769 res->getLocales(locales, includeSystemLocales, true /* mergeEquivalentLangs */);
Narayan Kamathe4345db2014-06-26 16:01:28 +0100770 }
Adam Lesinski16c4d152014-01-24 13:27:13 -0800771}
772
773/*
774 * Open a non-asset file as if it were an asset, searching for it in the
775 * specified app.
776 *
777 * Pass in a NULL values for "appName" if the common app directory should
778 * be used.
779 */
780Asset* AssetManager::openNonAssetInPathLocked(const char* fileName, AccessMode mode,
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000781 asset_path& ap)
Adam Lesinski16c4d152014-01-24 13:27:13 -0800782{
783 Asset* pAsset = NULL;
784
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700785 ALOGV("openNonAssetInPath: name=%s type=%d fd=%d", fileName, ap.type, ap.rawFd);
786
Adam Lesinski16c4d152014-01-24 13:27:13 -0800787 /* look at the filesystem on disk */
788 if (ap.type == kFileTypeDirectory) {
789 String8 path(ap.path);
790 path.appendPath(fileName);
791
792 pAsset = openAssetFromFileLocked(path, mode);
793
794 if (pAsset == NULL) {
795 /* try again, this time with ".gz" */
796 path.append(".gz");
797 pAsset = openAssetFromFileLocked(path, mode);
798 }
799
800 if (pAsset != NULL) {
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700801 ALOGV("FOUND NA '%s' on disk", fileName);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800802 pAsset->setAssetSource(path);
803 }
804
805 /* look inside the zip file */
806 } else {
807 String8 path(fileName);
808
809 /* check the appropriate Zip file */
Narayan Kamath560566d2013-12-03 13:16:03 +0000810 ZipFileRO* pZip = getZipFileLocked(ap);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800811 if (pZip != NULL) {
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700812 ALOGV("GOT zip, checking NA '%s'", (const char*) path);
Narayan Kamath560566d2013-12-03 13:16:03 +0000813 ZipEntryRO entry = pZip->findEntryByName(path.string());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800814 if (entry != NULL) {
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700815 ALOGV("FOUND NA in Zip file for %s", (const char*) path);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800816 pAsset = openAssetFromZipLocked(pZip, entry, mode, path);
Narayan Kamath560566d2013-12-03 13:16:03 +0000817 pZip->releaseEntry(entry);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800818 }
819 }
820
821 if (pAsset != NULL) {
822 /* create a "source" name, for debug/display */
823 pAsset->setAssetSource(
824 createZipSourceNameLocked(ZipSet::getPathName(ap.path.string()), String8(""),
825 String8(fileName)));
826 }
827 }
828
829 return pAsset;
830}
831
832/*
Adam Lesinski16c4d152014-01-24 13:27:13 -0800833 * Create a "source name" for a file from a Zip archive.
834 */
835String8 AssetManager::createZipSourceNameLocked(const String8& zipFileName,
836 const String8& dirName, const String8& fileName)
837{
838 String8 sourceName("zip:");
839 sourceName.append(zipFileName);
840 sourceName.append(":");
841 if (dirName.length() > 0) {
842 sourceName.appendPath(dirName);
843 }
844 sourceName.appendPath(fileName);
845 return sourceName;
846}
847
848/*
Adam Lesinski16c4d152014-01-24 13:27:13 -0800849 * Create a path to a loose asset (asset-base/app/rootDir).
850 */
851String8 AssetManager::createPathNameLocked(const asset_path& ap, const char* rootDir)
852{
853 String8 path(ap.path);
854 if (rootDir != NULL) path.appendPath(rootDir);
855 return path;
856}
857
858/*
859 * Return a pointer to one of our open Zip archives. Returns NULL if no
860 * matching Zip file exists.
Adam Lesinski16c4d152014-01-24 13:27:13 -0800861 */
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000862ZipFileRO* AssetManager::getZipFileLocked(asset_path& ap)
Adam Lesinski16c4d152014-01-24 13:27:13 -0800863{
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000864 ALOGV("getZipFileLocked() in %p: ap=%p zip=%p", this, &ap, ap.zip.get());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800865
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700866 if (ap.zip != NULL) {
867 return ap.zip->getZip();
868 }
869
870 if (ap.rawFd < 0) {
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000871 ALOGV("getZipFileLocked: Creating new zip from path %s", ap.path.string());
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700872 ap.zip = mZipSet.getSharedZip(ap.path);
873 } else {
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000874 ALOGV("getZipFileLocked: Creating new zip from fd %d", ap.rawFd);
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700875 ap.zip = SharedZip::create(ap.rawFd, ap.path);
876
877 }
878 return ap.zip != NULL ? ap.zip->getZip() : NULL;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800879}
880
881/*
882 * Try to open an asset from a file on disk.
883 *
884 * If the file is compressed with gzip, we seek to the start of the
885 * deflated data and pass that in (just like we would for a Zip archive).
886 *
887 * For uncompressed data, we may already have an mmap()ed version sitting
888 * around. If so, we want to hand that to the Asset instead.
889 *
890 * This returns NULL if the file doesn't exist, couldn't be opened, or
891 * claims to be a ".gz" but isn't.
892 */
893Asset* AssetManager::openAssetFromFileLocked(const String8& pathName,
894 AccessMode mode)
895{
896 Asset* pAsset = NULL;
897
898 if (strcasecmp(pathName.getPathExtension().string(), ".gz") == 0) {
899 //printf("TRYING '%s'\n", (const char*) pathName);
900 pAsset = Asset::createFromCompressedFile(pathName.string(), mode);
901 } else {
902 //printf("TRYING '%s'\n", (const char*) pathName);
903 pAsset = Asset::createFromFile(pathName.string(), mode);
904 }
905
906 return pAsset;
907}
908
909/*
910 * Given an entry in a Zip archive, create a new Asset object.
911 *
912 * If the entry is uncompressed, we may want to create or share a
913 * slice of shared memory.
914 */
915Asset* AssetManager::openAssetFromZipLocked(const ZipFileRO* pZipFile,
916 const ZipEntryRO entry, AccessMode mode, const String8& entryName)
917{
918 Asset* pAsset = NULL;
919
920 // TODO: look for previously-created shared memory slice?
Narayan Kamath407753c2015-06-16 12:02:57 +0100921 uint16_t method;
922 uint32_t uncompressedLen;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800923
924 //printf("USING Zip '%s'\n", pEntry->getFileName());
925
Adam Lesinski16c4d152014-01-24 13:27:13 -0800926 if (!pZipFile->getEntryInfo(entry, &method, &uncompressedLen, NULL, NULL,
927 NULL, NULL))
928 {
929 ALOGW("getEntryInfo failed\n");
930 return NULL;
931 }
932
933 FileMap* dataMap = pZipFile->createEntryFileMap(entry);
934 if (dataMap == NULL) {
935 ALOGW("create map from entry failed\n");
936 return NULL;
937 }
938
939 if (method == ZipFileRO::kCompressStored) {
940 pAsset = Asset::createFromUncompressedMap(dataMap, mode);
941 ALOGV("Opened uncompressed entry %s in zip %s mode %d: %p", entryName.string(),
942 dataMap->getFileName(), mode, pAsset);
943 } else {
Narayan Kamath407753c2015-06-16 12:02:57 +0100944 pAsset = Asset::createFromCompressedMap(dataMap,
945 static_cast<size_t>(uncompressedLen), mode);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800946 ALOGV("Opened compressed entry %s in zip %s mode %d: %p", entryName.string(),
947 dataMap->getFileName(), mode, pAsset);
948 }
949 if (pAsset == NULL) {
950 /* unexpected */
951 ALOGW("create from segment failed\n");
952 }
953
954 return pAsset;
955}
956
Adam Lesinski16c4d152014-01-24 13:27:13 -0800957/*
958 * Open a directory in the asset namespace.
959 *
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700960 * An "asset directory" is simply the combination of all asset paths' "assets/" directories.
Adam Lesinski16c4d152014-01-24 13:27:13 -0800961 *
962 * Pass in "" for the root dir.
963 */
964AssetDir* AssetManager::openDir(const char* dirName)
965{
966 AutoMutex _l(mLock);
967
968 AssetDir* pDir = NULL;
969 SortedVector<AssetDir::FileInfo>* pMergedInfo = NULL;
970
971 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
972 assert(dirName != NULL);
973
974 //printf("+++ openDir(%s) in '%s'\n", dirName, (const char*) mAssetBase);
975
Adam Lesinski16c4d152014-01-24 13:27:13 -0800976 pDir = new AssetDir;
977
978 /*
979 * Scan the various directories, merging what we find into a single
980 * vector. We want to scan them in reverse priority order so that
981 * the ".EXCLUDE" processing works correctly. Also, if we decide we
982 * want to remember where the file is coming from, we'll get the right
983 * version.
984 *
985 * We start with Zip archives, then do loose files.
986 */
987 pMergedInfo = new SortedVector<AssetDir::FileInfo>;
988
989 size_t i = mAssetPaths.size();
990 while (i > 0) {
991 i--;
992 const asset_path& ap = mAssetPaths.itemAt(i);
993 if (ap.type == kFileTypeRegular) {
994 ALOGV("Adding directory %s from zip %s", dirName, ap.path.string());
995 scanAndMergeZipLocked(pMergedInfo, ap, kAssetsRoot, dirName);
996 } else {
997 ALOGV("Adding directory %s from dir %s", dirName, ap.path.string());
998 scanAndMergeDirLocked(pMergedInfo, ap, kAssetsRoot, dirName);
999 }
1000 }
1001
1002#if 0
1003 printf("FILE LIST:\n");
1004 for (i = 0; i < (size_t) pMergedInfo->size(); i++) {
1005 printf(" %d: (%d) '%s'\n", i,
1006 pMergedInfo->itemAt(i).getFileType(),
1007 (const char*) pMergedInfo->itemAt(i).getFileName());
1008 }
1009#endif
1010
1011 pDir->setFileList(pMergedInfo);
1012 return pDir;
1013}
1014
1015/*
1016 * Open a directory in the non-asset namespace.
1017 *
Adam Lesinskife90eaf2016-10-04 13:31:31 -07001018 * An "asset directory" is simply the combination of all asset paths' "assets/" directories.
Adam Lesinski16c4d152014-01-24 13:27:13 -08001019 *
1020 * Pass in "" for the root dir.
1021 */
Narayan Kamatha0c62602014-01-24 13:51:51 +00001022AssetDir* AssetManager::openNonAssetDir(const int32_t cookie, const char* dirName)
Adam Lesinski16c4d152014-01-24 13:27:13 -08001023{
1024 AutoMutex _l(mLock);
1025
1026 AssetDir* pDir = NULL;
1027 SortedVector<AssetDir::FileInfo>* pMergedInfo = NULL;
1028
1029 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
1030 assert(dirName != NULL);
1031
1032 //printf("+++ openDir(%s) in '%s'\n", dirName, (const char*) mAssetBase);
1033
Adam Lesinski16c4d152014-01-24 13:27:13 -08001034 pDir = new AssetDir;
1035
1036 pMergedInfo = new SortedVector<AssetDir::FileInfo>;
1037
Narayan Kamatha0c62602014-01-24 13:51:51 +00001038 const size_t which = static_cast<size_t>(cookie) - 1;
Adam Lesinski16c4d152014-01-24 13:27:13 -08001039
1040 if (which < mAssetPaths.size()) {
1041 const asset_path& ap = mAssetPaths.itemAt(which);
1042 if (ap.type == kFileTypeRegular) {
1043 ALOGV("Adding directory %s from zip %s", dirName, ap.path.string());
1044 scanAndMergeZipLocked(pMergedInfo, ap, NULL, dirName);
1045 } else {
1046 ALOGV("Adding directory %s from dir %s", dirName, ap.path.string());
1047 scanAndMergeDirLocked(pMergedInfo, ap, NULL, dirName);
1048 }
1049 }
1050
1051#if 0
1052 printf("FILE LIST:\n");
1053 for (i = 0; i < (size_t) pMergedInfo->size(); i++) {
1054 printf(" %d: (%d) '%s'\n", i,
1055 pMergedInfo->itemAt(i).getFileType(),
1056 (const char*) pMergedInfo->itemAt(i).getFileName());
1057 }
1058#endif
1059
1060 pDir->setFileList(pMergedInfo);
1061 return pDir;
1062}
1063
1064/*
1065 * Scan the contents of the specified directory and merge them into the
1066 * "pMergedInfo" vector, removing previous entries if we find "exclude"
1067 * directives.
1068 *
1069 * Returns "false" if we found nothing to contribute.
1070 */
1071bool AssetManager::scanAndMergeDirLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
1072 const asset_path& ap, const char* rootDir, const char* dirName)
1073{
Adam Lesinski16c4d152014-01-24 13:27:13 -08001074 assert(pMergedInfo != NULL);
1075
Adam Lesinskia77685f2016-10-03 16:26:28 -07001076 //printf("scanAndMergeDir: %s %s %s\n", ap.path.string(), rootDir, dirName);
Adam Lesinski16c4d152014-01-24 13:27:13 -08001077
Adam Lesinskia77685f2016-10-03 16:26:28 -07001078 String8 path = createPathNameLocked(ap, rootDir);
1079 if (dirName[0] != '\0')
1080 path.appendPath(dirName);
Adam Lesinski16c4d152014-01-24 13:27:13 -08001081
Adam Lesinskia77685f2016-10-03 16:26:28 -07001082 SortedVector<AssetDir::FileInfo>* pContents = scanDirLocked(path);
1083 if (pContents == NULL)
1084 return false;
Adam Lesinski16c4d152014-01-24 13:27:13 -08001085
1086 // if we wanted to do an incremental cache fill, we would do it here
1087
1088 /*
1089 * Process "exclude" directives. If we find a filename that ends with
1090 * ".EXCLUDE", we look for a matching entry in the "merged" set, and
1091 * remove it if we find it. We also delete the "exclude" entry.
1092 */
1093 int i, count, exclExtLen;
1094
1095 count = pContents->size();
1096 exclExtLen = strlen(kExcludeExtension);
1097 for (i = 0; i < count; i++) {
1098 const char* name;
1099 int nameLen;
1100
1101 name = pContents->itemAt(i).getFileName().string();
1102 nameLen = strlen(name);
1103 if (nameLen > exclExtLen &&
1104 strcmp(name + (nameLen - exclExtLen), kExcludeExtension) == 0)
1105 {
1106 String8 match(name, nameLen - exclExtLen);
1107 int matchIdx;
1108
1109 matchIdx = AssetDir::FileInfo::findEntry(pMergedInfo, match);
1110 if (matchIdx > 0) {
1111 ALOGV("Excluding '%s' [%s]\n",
1112 pMergedInfo->itemAt(matchIdx).getFileName().string(),
1113 pMergedInfo->itemAt(matchIdx).getSourceName().string());
1114 pMergedInfo->removeAt(matchIdx);
1115 } else {
1116 //printf("+++ no match on '%s'\n", (const char*) match);
1117 }
1118
1119 ALOGD("HEY: size=%d removing %d\n", (int)pContents->size(), i);
1120 pContents->removeAt(i);
1121 i--; // adjust "for" loop
1122 count--; // and loop limit
1123 }
1124 }
1125
1126 mergeInfoLocked(pMergedInfo, pContents);
1127
1128 delete pContents;
1129
1130 return true;
1131}
1132
1133/*
1134 * Scan the contents of the specified directory, and stuff what we find
1135 * into a newly-allocated vector.
1136 *
1137 * Files ending in ".gz" will have their extensions removed.
1138 *
1139 * We should probably think about skipping files with "illegal" names,
1140 * e.g. illegal characters (/\:) or excessive length.
1141 *
1142 * Returns NULL if the specified directory doesn't exist.
1143 */
1144SortedVector<AssetDir::FileInfo>* AssetManager::scanDirLocked(const String8& path)
1145{
1146 SortedVector<AssetDir::FileInfo>* pContents = NULL;
1147 DIR* dir;
1148 struct dirent* entry;
1149 FileType fileType;
1150
1151 ALOGV("Scanning dir '%s'\n", path.string());
1152
1153 dir = opendir(path.string());
1154 if (dir == NULL)
1155 return NULL;
1156
1157 pContents = new SortedVector<AssetDir::FileInfo>;
1158
1159 while (1) {
1160 entry = readdir(dir);
1161 if (entry == NULL)
1162 break;
1163
1164 if (strcmp(entry->d_name, ".") == 0 ||
1165 strcmp(entry->d_name, "..") == 0)
1166 continue;
1167
1168#ifdef _DIRENT_HAVE_D_TYPE
1169 if (entry->d_type == DT_REG)
1170 fileType = kFileTypeRegular;
1171 else if (entry->d_type == DT_DIR)
1172 fileType = kFileTypeDirectory;
1173 else
1174 fileType = kFileTypeUnknown;
1175#else
1176 // stat the file
1177 fileType = ::getFileType(path.appendPathCopy(entry->d_name).string());
1178#endif
1179
1180 if (fileType != kFileTypeRegular && fileType != kFileTypeDirectory)
1181 continue;
1182
1183 AssetDir::FileInfo info;
1184 info.set(String8(entry->d_name), fileType);
1185 if (strcasecmp(info.getFileName().getPathExtension().string(), ".gz") == 0)
1186 info.setFileName(info.getFileName().getBasePath());
1187 info.setSourceName(path.appendPathCopy(info.getFileName()));
1188 pContents->add(info);
1189 }
1190
1191 closedir(dir);
1192 return pContents;
1193}
1194
1195/*
1196 * Scan the contents out of the specified Zip archive, and merge what we
1197 * find into "pMergedInfo". If the Zip archive in question doesn't exist,
1198 * we return immediately.
1199 *
1200 * Returns "false" if we found nothing to contribute.
1201 */
1202bool AssetManager::scanAndMergeZipLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
1203 const asset_path& ap, const char* rootDir, const char* baseDirName)
1204{
1205 ZipFileRO* pZip;
1206 Vector<String8> dirs;
1207 AssetDir::FileInfo info;
1208 SortedVector<AssetDir::FileInfo> contents;
1209 String8 sourceName, zipName, dirName;
1210
1211 pZip = mZipSet.getZip(ap.path);
1212 if (pZip == NULL) {
1213 ALOGW("Failure opening zip %s\n", ap.path.string());
1214 return false;
1215 }
1216
1217 zipName = ZipSet::getPathName(ap.path.string());
1218
1219 /* convert "sounds" to "rootDir/sounds" */
1220 if (rootDir != NULL) dirName = rootDir;
1221 dirName.appendPath(baseDirName);
1222
1223 /*
1224 * Scan through the list of files, looking for a match. The files in
1225 * the Zip table of contents are not in sorted order, so we have to
1226 * process the entire list. We're looking for a string that begins
1227 * with the characters in "dirName", is followed by a '/', and has no
1228 * subsequent '/' in the stuff that follows.
1229 *
1230 * What makes this especially fun is that directories are not stored
1231 * explicitly in Zip archives, so we have to infer them from context.
1232 * When we see "sounds/foo.wav" we have to leave a note to ourselves
1233 * to insert a directory called "sounds" into the list. We store
1234 * these in temporary vector so that we only return each one once.
1235 *
1236 * Name comparisons are case-sensitive to match UNIX filesystem
1237 * semantics.
1238 */
1239 int dirNameLen = dirName.length();
Narayan Kamath560566d2013-12-03 13:16:03 +00001240 void *iterationCookie;
Yusuke Sato05f648e2015-08-03 16:21:10 -07001241 if (!pZip->startIteration(&iterationCookie, dirName.string(), NULL)) {
Narayan Kamath560566d2013-12-03 13:16:03 +00001242 ALOGW("ZipFileRO::startIteration returned false");
1243 return false;
1244 }
1245
1246 ZipEntryRO entry;
1247 while ((entry = pZip->nextEntry(iterationCookie)) != NULL) {
Adam Lesinski16c4d152014-01-24 13:27:13 -08001248 char nameBuf[256];
1249
Adam Lesinski16c4d152014-01-24 13:27:13 -08001250 if (pZip->getEntryFileName(entry, nameBuf, sizeof(nameBuf)) != 0) {
1251 // TODO: fix this if we expect to have long names
1252 ALOGE("ARGH: name too long?\n");
1253 continue;
1254 }
1255 //printf("Comparing %s in %s?\n", nameBuf, dirName.string());
Yusuke Sato05f648e2015-08-03 16:21:10 -07001256 if (dirNameLen == 0 || nameBuf[dirNameLen] == '/')
Adam Lesinski16c4d152014-01-24 13:27:13 -08001257 {
1258 const char* cp;
1259 const char* nextSlash;
1260
1261 cp = nameBuf + dirNameLen;
1262 if (dirNameLen != 0)
1263 cp++; // advance past the '/'
1264
1265 nextSlash = strchr(cp, '/');
1266//xxx this may break if there are bare directory entries
1267 if (nextSlash == NULL) {
1268 /* this is a file in the requested directory */
1269
1270 info.set(String8(nameBuf).getPathLeaf(), kFileTypeRegular);
1271
1272 info.setSourceName(
1273 createZipSourceNameLocked(zipName, dirName, info.getFileName()));
1274
1275 contents.add(info);
1276 //printf("FOUND: file '%s'\n", info.getFileName().string());
1277 } else {
1278 /* this is a subdir; add it if we don't already have it*/
1279 String8 subdirName(cp, nextSlash - cp);
1280 size_t j;
1281 size_t N = dirs.size();
1282
1283 for (j = 0; j < N; j++) {
1284 if (subdirName == dirs[j]) {
1285 break;
1286 }
1287 }
1288 if (j == N) {
1289 dirs.add(subdirName);
1290 }
1291
1292 //printf("FOUND: dir '%s'\n", subdirName.string());
1293 }
1294 }
1295 }
1296
Narayan Kamath560566d2013-12-03 13:16:03 +00001297 pZip->endIteration(iterationCookie);
1298
Adam Lesinski16c4d152014-01-24 13:27:13 -08001299 /*
1300 * Add the set of unique directories.
1301 */
1302 for (int i = 0; i < (int) dirs.size(); i++) {
1303 info.set(dirs[i], kFileTypeDirectory);
1304 info.setSourceName(
1305 createZipSourceNameLocked(zipName, dirName, info.getFileName()));
1306 contents.add(info);
1307 }
1308
1309 mergeInfoLocked(pMergedInfo, &contents);
1310
1311 return true;
1312}
1313
1314
1315/*
1316 * Merge two vectors of FileInfo.
1317 *
1318 * The merged contents will be stuffed into *pMergedInfo.
1319 *
1320 * If an entry for a file exists in both "pMergedInfo" and "pContents",
1321 * we use the newer "pContents" entry.
1322 */
1323void AssetManager::mergeInfoLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
1324 const SortedVector<AssetDir::FileInfo>* pContents)
1325{
1326 /*
1327 * Merge what we found in this directory with what we found in
1328 * other places.
1329 *
1330 * Two basic approaches:
1331 * (1) Create a new array that holds the unique values of the two
1332 * arrays.
1333 * (2) Take the elements from pContents and shove them into pMergedInfo.
1334 *
1335 * Because these are vectors of complex objects, moving elements around
1336 * inside the vector requires constructing new objects and allocating
1337 * storage for members. With approach #1, we're always adding to the
1338 * end, whereas with #2 we could be inserting multiple elements at the
1339 * front of the vector. Approach #1 requires a full copy of the
1340 * contents of pMergedInfo, but approach #2 requires the same copy for
1341 * every insertion at the front of pMergedInfo.
1342 *
1343 * (We should probably use a SortedVector interface that allows us to
1344 * just stuff items in, trusting us to maintain the sort order.)
1345 */
1346 SortedVector<AssetDir::FileInfo>* pNewSorted;
1347 int mergeMax, contMax;
1348 int mergeIdx, contIdx;
1349
1350 pNewSorted = new SortedVector<AssetDir::FileInfo>;
1351 mergeMax = pMergedInfo->size();
1352 contMax = pContents->size();
1353 mergeIdx = contIdx = 0;
1354
1355 while (mergeIdx < mergeMax || contIdx < contMax) {
1356 if (mergeIdx == mergeMax) {
1357 /* hit end of "merge" list, copy rest of "contents" */
1358 pNewSorted->add(pContents->itemAt(contIdx));
1359 contIdx++;
1360 } else if (contIdx == contMax) {
1361 /* hit end of "cont" list, copy rest of "merge" */
1362 pNewSorted->add(pMergedInfo->itemAt(mergeIdx));
1363 mergeIdx++;
1364 } else if (pMergedInfo->itemAt(mergeIdx) == pContents->itemAt(contIdx))
1365 {
1366 /* items are identical, add newer and advance both indices */
1367 pNewSorted->add(pContents->itemAt(contIdx));
1368 mergeIdx++;
1369 contIdx++;
1370 } else if (pMergedInfo->itemAt(mergeIdx) < pContents->itemAt(contIdx))
1371 {
1372 /* "merge" is lower, add that one */
1373 pNewSorted->add(pMergedInfo->itemAt(mergeIdx));
1374 mergeIdx++;
1375 } else {
1376 /* "cont" is lower, add that one */
1377 assert(pContents->itemAt(contIdx) < pMergedInfo->itemAt(mergeIdx));
1378 pNewSorted->add(pContents->itemAt(contIdx));
1379 contIdx++;
1380 }
1381 }
1382
1383 /*
1384 * Overwrite the "merged" list with the new stuff.
1385 */
1386 *pMergedInfo = *pNewSorted;
1387 delete pNewSorted;
1388
1389#if 0 // for Vector, rather than SortedVector
1390 int i, j;
1391 for (i = pContents->size() -1; i >= 0; i--) {
1392 bool add = true;
1393
1394 for (j = pMergedInfo->size() -1; j >= 0; j--) {
1395 /* case-sensitive comparisons, to behave like UNIX fs */
1396 if (strcmp(pContents->itemAt(i).mFileName,
1397 pMergedInfo->itemAt(j).mFileName) == 0)
1398 {
1399 /* match, don't add this entry */
1400 add = false;
1401 break;
1402 }
1403 }
1404
1405 if (add)
1406 pMergedInfo->add(pContents->itemAt(i));
1407 }
1408#endif
1409}
1410
Adam Lesinski16c4d152014-01-24 13:27:13 -08001411/*
1412 * ===========================================================================
1413 * AssetManager::SharedZip
1414 * ===========================================================================
1415 */
1416
1417
1418Mutex AssetManager::SharedZip::gLock;
1419DefaultKeyedVector<String8, wp<AssetManager::SharedZip> > AssetManager::SharedZip::gOpen;
1420
1421AssetManager::SharedZip::SharedZip(const String8& path, time_t modWhen)
1422 : mPath(path), mZipFile(NULL), mModWhen(modWhen),
1423 mResourceTableAsset(NULL), mResourceTable(NULL)
1424{
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001425 if (kIsDebug) {
1426 ALOGI("Creating SharedZip %p %s\n", this, (const char*)mPath);
1427 }
Adam Lesinski16c4d152014-01-24 13:27:13 -08001428 ALOGV("+++ opening zip '%s'\n", mPath.string());
Narayan Kamath560566d2013-12-03 13:16:03 +00001429 mZipFile = ZipFileRO::open(mPath.string());
1430 if (mZipFile == NULL) {
Adam Lesinski16c4d152014-01-24 13:27:13 -08001431 ALOGD("failed to open Zip archive '%s'\n", mPath.string());
Adam Lesinski16c4d152014-01-24 13:27:13 -08001432 }
1433}
1434
Dianne Hackbornca3872c2017-10-30 14:19:32 -07001435AssetManager::SharedZip::SharedZip(int fd, const String8& path)
1436 : mPath(path), mZipFile(NULL), mModWhen(0),
1437 mResourceTableAsset(NULL), mResourceTable(NULL)
1438{
1439 if (kIsDebug) {
1440 ALOGI("Creating SharedZip %p fd=%d %s\n", this, fd, (const char*)mPath);
1441 }
1442 ALOGV("+++ opening zip fd=%d '%s'\n", fd, mPath.string());
1443 mZipFile = ZipFileRO::openFd(fd, mPath.string());
1444 if (mZipFile == NULL) {
1445 ::close(fd);
1446 ALOGD("failed to open Zip archive fd=%d '%s'\n", fd, mPath.string());
1447 }
1448}
1449
Mårten Kongstad48d22322014-01-31 14:43:27 +01001450sp<AssetManager::SharedZip> AssetManager::SharedZip::get(const String8& path,
1451 bool createIfNotPresent)
Adam Lesinski16c4d152014-01-24 13:27:13 -08001452{
1453 AutoMutex _l(gLock);
1454 time_t modWhen = getFileModDate(path);
1455 sp<SharedZip> zip = gOpen.valueFor(path).promote();
1456 if (zip != NULL && zip->mModWhen == modWhen) {
1457 return zip;
1458 }
Mårten Kongstad48d22322014-01-31 14:43:27 +01001459 if (zip == NULL && !createIfNotPresent) {
1460 return NULL;
1461 }
Adam Lesinski16c4d152014-01-24 13:27:13 -08001462 zip = new SharedZip(path, modWhen);
1463 gOpen.add(path, zip);
1464 return zip;
Dianne Hackbornca3872c2017-10-30 14:19:32 -07001465}
Adam Lesinski16c4d152014-01-24 13:27:13 -08001466
Dianne Hackbornca3872c2017-10-30 14:19:32 -07001467sp<AssetManager::SharedZip> AssetManager::SharedZip::create(int fd, const String8& path)
1468{
1469 return new SharedZip(fd, path);
Adam Lesinski16c4d152014-01-24 13:27:13 -08001470}
1471
1472ZipFileRO* AssetManager::SharedZip::getZip()
1473{
1474 return mZipFile;
1475}
1476
1477Asset* AssetManager::SharedZip::getResourceTableAsset()
1478{
songjinshi49921f22016-09-08 15:24:30 +08001479 AutoMutex _l(gLock);
Adam Lesinski16c4d152014-01-24 13:27:13 -08001480 ALOGV("Getting from SharedZip %p resource asset %p\n", this, mResourceTableAsset);
1481 return mResourceTableAsset;
1482}
1483
1484Asset* AssetManager::SharedZip::setResourceTableAsset(Asset* asset)
1485{
1486 {
1487 AutoMutex _l(gLock);
1488 if (mResourceTableAsset == NULL) {
Adam Lesinski16c4d152014-01-24 13:27:13 -08001489 // This is not thread safe the first time it is called, so
1490 // do it here with the global lock held.
1491 asset->getBuffer(true);
songjinshi49921f22016-09-08 15:24:30 +08001492 mResourceTableAsset = asset;
Adam Lesinski16c4d152014-01-24 13:27:13 -08001493 return asset;
1494 }
1495 }
1496 delete asset;
1497 return mResourceTableAsset;
1498}
1499
1500ResTable* AssetManager::SharedZip::getResourceTable()
1501{
1502 ALOGV("Getting from SharedZip %p resource table %p\n", this, mResourceTable);
1503 return mResourceTable;
1504}
1505
1506ResTable* AssetManager::SharedZip::setResourceTable(ResTable* res)
1507{
1508 {
1509 AutoMutex _l(gLock);
1510 if (mResourceTable == NULL) {
1511 mResourceTable = res;
1512 return res;
1513 }
1514 }
1515 delete res;
1516 return mResourceTable;
1517}
1518
1519bool AssetManager::SharedZip::isUpToDate()
1520{
1521 time_t modWhen = getFileModDate(mPath.string());
1522 return mModWhen == modWhen;
1523}
1524
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +09001525void AssetManager::SharedZip::addOverlay(const asset_path& ap)
1526{
1527 mOverlays.add(ap);
1528}
1529
1530bool AssetManager::SharedZip::getOverlay(size_t idx, asset_path* out) const
1531{
1532 if (idx >= mOverlays.size()) {
1533 return false;
1534 }
1535 *out = mOverlays[idx];
1536 return true;
1537}
1538
Adam Lesinski16c4d152014-01-24 13:27:13 -08001539AssetManager::SharedZip::~SharedZip()
1540{
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001541 if (kIsDebug) {
1542 ALOGI("Destroying SharedZip %p %s\n", this, (const char*)mPath);
1543 }
Adam Lesinski16c4d152014-01-24 13:27:13 -08001544 if (mResourceTable != NULL) {
1545 delete mResourceTable;
1546 }
1547 if (mResourceTableAsset != NULL) {
1548 delete mResourceTableAsset;
1549 }
1550 if (mZipFile != NULL) {
1551 delete mZipFile;
1552 ALOGV("Closed '%s'\n", mPath.string());
1553 }
1554}
1555
1556/*
1557 * ===========================================================================
1558 * AssetManager::ZipSet
1559 * ===========================================================================
1560 */
1561
1562/*
Adam Lesinski16c4d152014-01-24 13:27:13 -08001563 * Destructor. Close any open archives.
1564 */
1565AssetManager::ZipSet::~ZipSet(void)
1566{
1567 size_t N = mZipFile.size();
1568 for (size_t i = 0; i < N; i++)
1569 closeZip(i);
1570}
1571
1572/*
1573 * Close a Zip file and reset the entry.
1574 */
1575void AssetManager::ZipSet::closeZip(int idx)
1576{
1577 mZipFile.editItemAt(idx) = NULL;
1578}
1579
Adam Lesinski16c4d152014-01-24 13:27:13 -08001580/*
1581 * Retrieve the appropriate Zip file from the set.
1582 */
1583ZipFileRO* AssetManager::ZipSet::getZip(const String8& path)
1584{
Dianne Hackbornca3872c2017-10-30 14:19:32 -07001585 return getSharedZip(path)->getZip();
1586}
1587
1588const sp<AssetManager::SharedZip> AssetManager::ZipSet::getSharedZip(const String8& path)
1589{
Adam Lesinski16c4d152014-01-24 13:27:13 -08001590 int idx = getIndex(path);
1591 sp<SharedZip> zip = mZipFile[idx];
1592 if (zip == NULL) {
1593 zip = SharedZip::get(path);
1594 mZipFile.editItemAt(idx) = zip;
1595 }
Dianne Hackbornca3872c2017-10-30 14:19:32 -07001596 return zip;
Adam Lesinski16c4d152014-01-24 13:27:13 -08001597}
1598
1599Asset* AssetManager::ZipSet::getZipResourceTableAsset(const String8& path)
1600{
1601 int idx = getIndex(path);
1602 sp<SharedZip> zip = mZipFile[idx];
1603 if (zip == NULL) {
1604 zip = SharedZip::get(path);
1605 mZipFile.editItemAt(idx) = zip;
1606 }
1607 return zip->getResourceTableAsset();
1608}
1609
1610Asset* AssetManager::ZipSet::setZipResourceTableAsset(const String8& path,
1611 Asset* asset)
1612{
1613 int idx = getIndex(path);
1614 sp<SharedZip> zip = mZipFile[idx];
1615 // doesn't make sense to call before previously accessing.
1616 return zip->setResourceTableAsset(asset);
1617}
1618
1619ResTable* AssetManager::ZipSet::getZipResourceTable(const String8& path)
1620{
1621 int idx = getIndex(path);
1622 sp<SharedZip> zip = mZipFile[idx];
1623 if (zip == NULL) {
1624 zip = SharedZip::get(path);
1625 mZipFile.editItemAt(idx) = zip;
1626 }
1627 return zip->getResourceTable();
1628}
1629
1630ResTable* AssetManager::ZipSet::setZipResourceTable(const String8& path,
1631 ResTable* res)
1632{
1633 int idx = getIndex(path);
1634 sp<SharedZip> zip = mZipFile[idx];
1635 // doesn't make sense to call before previously accessing.
1636 return zip->setResourceTable(res);
1637}
1638
1639/*
1640 * Generate the partial pathname for the specified archive. The caller
1641 * gets to prepend the asset root directory.
1642 *
1643 * Returns something like "common/en-US-noogle.jar".
1644 */
1645/*static*/ String8 AssetManager::ZipSet::getPathName(const char* zipPath)
1646{
1647 return String8(zipPath);
1648}
1649
1650bool AssetManager::ZipSet::isUpToDate()
1651{
1652 const size_t N = mZipFile.size();
1653 for (size_t i=0; i<N; i++) {
1654 if (mZipFile[i] != NULL && !mZipFile[i]->isUpToDate()) {
1655 return false;
1656 }
1657 }
1658 return true;
1659}
1660
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +09001661void AssetManager::ZipSet::addOverlay(const String8& path, const asset_path& overlay)
1662{
1663 int idx = getIndex(path);
1664 sp<SharedZip> zip = mZipFile[idx];
1665 zip->addOverlay(overlay);
1666}
1667
1668bool AssetManager::ZipSet::getOverlay(const String8& path, size_t idx, asset_path* out) const
1669{
1670 sp<SharedZip> zip = SharedZip::get(path, false);
1671 if (zip == NULL) {
1672 return false;
1673 }
1674 return zip->getOverlay(idx, out);
1675}
1676
Adam Lesinski16c4d152014-01-24 13:27:13 -08001677/*
1678 * Compute the zip file's index.
1679 *
1680 * "appName", "locale", and "vendor" should be set to NULL to indicate the
1681 * default directory.
1682 */
1683int AssetManager::ZipSet::getIndex(const String8& zip) const
1684{
1685 const size_t N = mZipPath.size();
1686 for (size_t i=0; i<N; i++) {
1687 if (mZipPath[i] == zip) {
1688 return i;
1689 }
1690 }
1691
1692 mZipPath.add(zip);
1693 mZipFile.add(NULL);
1694
1695 return mZipPath.size()-1;
1696}