blob: fc625bbaf72d1eeb9eb5ae0f47a8f2d8f238d304 [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";
75const char* AssetManager::OVERLAY_DIR = "/vendor/overlay";
Jaekyun Seok1713d9e2018-01-12 21:47:26 +090076const char* AssetManager::PRODUCT_OVERLAY_DIR = "/product/overlay";
Jakub Adamek54dcaab2016-10-19 11:46:13 +010077const char* AssetManager::OVERLAY_THEME_DIR_PROPERTY = "ro.boot.vendor.overlay.theme";
Mårten Kongstad48d22322014-01-31 14:43:27 +010078const char* AssetManager::TARGET_PACKAGE_NAME = "android";
79const char* AssetManager::TARGET_APK_PATH = "/system/framework/framework-res.apk";
80const char* AssetManager::IDMAP_DIR = "/data/resource-cache";
Mårten Kongstad65a05fd2014-01-31 14:01:52 +010081
Adam Lesinski16c4d152014-01-24 13:27:13 -080082namespace {
Adam Lesinski16c4d152014-01-24 13:27:13 -080083
Adam Lesinskia77685f2016-10-03 16:26:28 -070084String8 idmapPathForPackagePath(const String8& pkgPath) {
85 const char* root = getenv("ANDROID_DATA");
86 LOG_ALWAYS_FATAL_IF(root == NULL, "ANDROID_DATA not set");
87 String8 path(root);
88 path.appendPath(kResourceCache);
Adam Lesinski16c4d152014-01-24 13:27:13 -080089
Adam Lesinskia77685f2016-10-03 16:26:28 -070090 char buf[256]; // 256 chars should be enough for anyone...
91 strncpy(buf, pkgPath.string(), 255);
92 buf[255] = '\0';
93 char* filename = buf;
94 while (*filename && *filename == '/') {
95 ++filename;
Adam Lesinski16c4d152014-01-24 13:27:13 -080096 }
Adam Lesinskia77685f2016-10-03 16:26:28 -070097 char* p = filename;
98 while (*p) {
99 if (*p == '/') {
100 *p = '@';
101 }
102 ++p;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800103 }
Adam Lesinskia77685f2016-10-03 16:26:28 -0700104 path.appendPath(filename);
105 path.append("@idmap");
106
107 return path;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800108}
109
110/*
Adam Lesinskia77685f2016-10-03 16:26:28 -0700111 * Like strdup(), but uses C++ "new" operator instead of malloc.
112 */
113static char* strdupNew(const char* str) {
114 char* newStr;
115 int len;
116
117 if (str == NULL)
118 return NULL;
119
120 len = strlen(str);
121 newStr = new char[len+1];
122 memcpy(newStr, str, len+1);
123
124 return newStr;
125}
126
127} // namespace
128
129/*
Adam Lesinski16c4d152014-01-24 13:27:13 -0800130 * ===========================================================================
131 * AssetManager
132 * ===========================================================================
133 */
134
Adam Lesinskia77685f2016-10-03 16:26:28 -0700135int32_t AssetManager::getGlobalCount() {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800136 return gCount;
137}
138
Adam Lesinskia77685f2016-10-03 16:26:28 -0700139AssetManager::AssetManager() :
140 mLocale(NULL), mResources(NULL), mConfig(new ResTable_config) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700141 int count = android_atomic_inc(&gCount) + 1;
142 if (kIsDebug) {
143 ALOGI("Creating AssetManager %p #%d\n", this, count);
144 }
Adam Lesinski16c4d152014-01-24 13:27:13 -0800145 memset(mConfig, 0, sizeof(ResTable_config));
146}
147
Adam Lesinskia77685f2016-10-03 16:26:28 -0700148AssetManager::~AssetManager() {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800149 int count = android_atomic_dec(&gCount);
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700150 if (kIsDebug) {
151 ALOGI("Destroying AssetManager in %p #%d\n", this, count);
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000152 } else {
153 ALOGV("Destroying AssetManager in %p #%d\n", this, count);
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700154 }
Adam Lesinski16c4d152014-01-24 13:27:13 -0800155
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700156 // Manually close any fd paths for which we have not yet opened their zip (which
157 // will take ownership of the fd and close it when done).
158 for (size_t i=0; i<mAssetPaths.size(); i++) {
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000159 ALOGV("Cleaning path #%d: fd=%d, zip=%p", (int)i, mAssetPaths[i].rawFd,
160 mAssetPaths[i].zip.get());
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700161 if (mAssetPaths[i].rawFd >= 0 && mAssetPaths[i].zip == NULL) {
162 close(mAssetPaths[i].rawFd);
163 }
164 }
165
Adam Lesinski16c4d152014-01-24 13:27:13 -0800166 delete mConfig;
167 delete mResources;
168
169 // don't have a String class yet, so make sure we clean up
170 delete[] mLocale;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800171}
172
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800173bool AssetManager::addAssetPath(
Adam Lesinskia77685f2016-10-03 16:26:28 -0700174 const String8& path, int32_t* cookie, bool appAsLib, bool isSystemAsset) {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800175 AutoMutex _l(mLock);
176
177 asset_path ap;
178
179 String8 realPath(path);
180 if (kAppZipName) {
181 realPath.appendPath(kAppZipName);
182 }
183 ap.type = ::getFileType(realPath.string());
184 if (ap.type == kFileTypeRegular) {
185 ap.path = realPath;
186 } else {
187 ap.path = path;
188 ap.type = ::getFileType(path.string());
189 if (ap.type != kFileTypeDirectory && ap.type != kFileTypeRegular) {
190 ALOGW("Asset path %s is neither a directory nor file (type=%d).",
191 path.string(), (int)ap.type);
192 return false;
193 }
194 }
195
196 // Skip if we have it already.
197 for (size_t i=0; i<mAssetPaths.size(); i++) {
198 if (mAssetPaths[i].path == ap.path) {
199 if (cookie) {
Narayan Kamatha0c62602014-01-24 13:51:51 +0000200 *cookie = static_cast<int32_t>(i+1);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800201 }
202 return true;
203 }
204 }
205
206 ALOGV("In %p Asset %s path: %s", this,
207 ap.type == kFileTypeDirectory ? "dir" : "zip", ap.path.string());
208
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800209 ap.isSystemAsset = isSystemAsset;
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000210 ssize_t apPos = mAssetPaths.add(ap);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800211
212 // new paths are always added at the end
213 if (cookie) {
Narayan Kamatha0c62602014-01-24 13:51:51 +0000214 *cookie = static_cast<int32_t>(mAssetPaths.size());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800215 }
216
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +0900217#ifdef __ANDROID__
218 // Load overlays, if any
219 asset_path oap;
220 for (size_t idx = 0; mZipSet.getOverlay(ap.path, idx, &oap); idx++) {
221 oap.isSystemAsset = isSystemAsset;
222 mAssetPaths.add(oap);
223 }
224#endif
225
Martin Kosiba7df36252014-01-16 16:25:56 +0000226 if (mResources != NULL) {
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000227 appendPathToResTable(mAssetPaths.editItemAt(apPos), appAsLib);
Martin Kosiba7df36252014-01-16 16:25:56 +0000228 }
229
Adam Lesinski16c4d152014-01-24 13:27:13 -0800230 return true;
231}
232
Mårten Kongstad48d22322014-01-31 14:43:27 +0100233bool AssetManager::addOverlayPath(const String8& packagePath, int32_t* cookie)
234{
235 const String8 idmapPath = idmapPathForPackagePath(packagePath);
236
237 AutoMutex _l(mLock);
238
239 for (size_t i = 0; i < mAssetPaths.size(); ++i) {
240 if (mAssetPaths[i].idmap == idmapPath) {
241 *cookie = static_cast<int32_t>(i + 1);
242 return true;
243 }
244 }
245
246 Asset* idmap = NULL;
247 if ((idmap = openAssetFromFileLocked(idmapPath, Asset::ACCESS_BUFFER)) == NULL) {
248 ALOGW("failed to open idmap file %s\n", idmapPath.string());
249 return false;
250 }
251
252 String8 targetPath;
253 String8 overlayPath;
254 if (!ResTable::getIdmapInfo(idmap->getBuffer(false), idmap->getLength(),
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700255 NULL, NULL, NULL, &targetPath, &overlayPath)) {
Mårten Kongstad48d22322014-01-31 14:43:27 +0100256 ALOGW("failed to read idmap file %s\n", idmapPath.string());
257 delete idmap;
258 return false;
259 }
260 delete idmap;
261
262 if (overlayPath != packagePath) {
263 ALOGW("idmap file %s inconcistent: expected path %s does not match actual path %s\n",
264 idmapPath.string(), packagePath.string(), overlayPath.string());
265 return false;
266 }
267 if (access(targetPath.string(), R_OK) != 0) {
268 ALOGW("failed to access file %s: %s\n", targetPath.string(), strerror(errno));
269 return false;
270 }
271 if (access(idmapPath.string(), R_OK) != 0) {
272 ALOGW("failed to access file %s: %s\n", idmapPath.string(), strerror(errno));
273 return false;
274 }
275 if (access(overlayPath.string(), R_OK) != 0) {
276 ALOGW("failed to access file %s: %s\n", overlayPath.string(), strerror(errno));
277 return false;
278 }
279
280 asset_path oap;
281 oap.path = overlayPath;
282 oap.type = ::getFileType(overlayPath.string());
283 oap.idmap = idmapPath;
284#if 0
285 ALOGD("Overlay added: targetPath=%s overlayPath=%s idmapPath=%s\n",
286 targetPath.string(), overlayPath.string(), idmapPath.string());
287#endif
288 mAssetPaths.add(oap);
289 *cookie = static_cast<int32_t>(mAssetPaths.size());
290
Mårten Kongstad30113132014-11-07 10:52:17 +0100291 if (mResources != NULL) {
292 appendPathToResTable(oap);
293 }
294
Mårten Kongstad48d22322014-01-31 14:43:27 +0100295 return true;
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700296}
297
298bool AssetManager::addAssetFd(
299 int fd, const String8& debugPathName, int32_t* cookie, bool appAsLib,
300 bool assume_ownership) {
301 AutoMutex _l(mLock);
302
303 asset_path ap;
304
305 ap.path = debugPathName;
306 ap.rawFd = fd;
307 ap.type = kFileTypeRegular;
308 ap.assumeOwnership = assume_ownership;
309
310 ALOGV("In %p Asset fd %d name: %s", this, fd, ap.path.string());
311
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000312 ssize_t apPos = mAssetPaths.add(ap);
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700313
314 // new paths are always added at the end
315 if (cookie) {
316 *cookie = static_cast<int32_t>(mAssetPaths.size());
317 }
318
319 if (mResources != NULL) {
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000320 appendPathToResTable(mAssetPaths.editItemAt(apPos), appAsLib);
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700321 }
322
323 return true;
324}
Mårten Kongstad48d22322014-01-31 14:43:27 +0100325
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100326bool AssetManager::createIdmap(const char* targetApkPath, const char* overlayApkPath,
Dianne Hackborn32bb5fa2014-02-11 13:56:21 -0800327 uint32_t targetCrc, uint32_t overlayCrc, uint32_t** outData, size_t* outSize)
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100328{
329 AutoMutex _l(mLock);
330 const String8 paths[2] = { String8(targetApkPath), String8(overlayApkPath) };
Mårten Kongstad6bb13da2016-06-02 09:34:36 +0200331 Asset* assets[2] = {NULL, NULL};
332 bool ret = false;
333 {
334 ResTable tables[2];
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100335
Mårten Kongstad6bb13da2016-06-02 09:34:36 +0200336 for (int i = 0; i < 2; ++i) {
337 asset_path ap;
338 ap.type = kFileTypeRegular;
339 ap.path = paths[i];
340 assets[i] = openNonAssetInPathLocked("resources.arsc",
341 Asset::ACCESS_BUFFER, ap);
342 if (assets[i] == NULL) {
343 ALOGW("failed to find resources.arsc in %s\n", ap.path.string());
344 goto exit;
345 }
346 if (tables[i].add(assets[i]) != NO_ERROR) {
347 ALOGW("failed to add %s to resource table", paths[i].string());
348 goto exit;
349 }
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100350 }
Mårten Kongstad6bb13da2016-06-02 09:34:36 +0200351 ret = tables[0].createIdmap(tables[1], targetCrc, overlayCrc,
352 targetApkPath, overlayApkPath, (void**)outData, outSize) == NO_ERROR;
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100353 }
354
Mårten Kongstad6bb13da2016-06-02 09:34:36 +0200355exit:
356 delete assets[0];
357 delete assets[1];
358 return ret;
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100359}
360
Adam Lesinski16c4d152014-01-24 13:27:13 -0800361bool AssetManager::addDefaultAssets()
362{
363 const char* root = getenv("ANDROID_ROOT");
364 LOG_ALWAYS_FATAL_IF(root == NULL, "ANDROID_ROOT not set");
365
366 String8 path(root);
367 path.appendPath(kSystemAssets);
368
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800369 return addAssetPath(path, NULL, false /* appAsLib */, true /* isSystemAsset */);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800370}
371
Narayan Kamatha0c62602014-01-24 13:51:51 +0000372int32_t AssetManager::nextAssetPath(const int32_t cookie) const
Adam Lesinski16c4d152014-01-24 13:27:13 -0800373{
374 AutoMutex _l(mLock);
Narayan Kamatha0c62602014-01-24 13:51:51 +0000375 const size_t next = static_cast<size_t>(cookie) + 1;
376 return next > mAssetPaths.size() ? -1 : next;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800377}
378
Narayan Kamatha0c62602014-01-24 13:51:51 +0000379String8 AssetManager::getAssetPath(const int32_t cookie) const
Adam Lesinski16c4d152014-01-24 13:27:13 -0800380{
381 AutoMutex _l(mLock);
Narayan Kamatha0c62602014-01-24 13:51:51 +0000382 const size_t which = static_cast<size_t>(cookie) - 1;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800383 if (which < mAssetPaths.size()) {
384 return mAssetPaths[which].path;
385 }
386 return String8();
387}
388
Adam Lesinski16c4d152014-01-24 13:27:13 -0800389void AssetManager::setLocaleLocked(const char* locale)
390{
391 if (mLocale != NULL) {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800392 delete[] mLocale;
393 }
Elliott Hughesc367d482013-10-29 13:12:55 -0700394
Adam Lesinski16c4d152014-01-24 13:27:13 -0800395 mLocale = strdupNew(locale);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800396 updateResourceParamsLocked();
397}
398
Adam Lesinski16c4d152014-01-24 13:27:13 -0800399void AssetManager::setConfiguration(const ResTable_config& config, const char* locale)
400{
401 AutoMutex _l(mLock);
402 *mConfig = config;
403 if (locale) {
404 setLocaleLocked(locale);
405 } else if (config.language[0] != 0) {
Narayan Kamath91447d82014-01-21 15:32:36 +0000406 char spec[RESTABLE_MAX_LOCALE_LEN];
407 config.getBcp47Locale(spec);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800408 setLocaleLocked(spec);
409 } else {
410 updateResourceParamsLocked();
411 }
412}
413
414void AssetManager::getConfiguration(ResTable_config* outConfig) const
415{
416 AutoMutex _l(mLock);
417 *outConfig = *mConfig;
418}
419
420/*
421 * Open an asset.
422 *
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700423 * The data could be in any asset path. Each asset path could be:
424 * - A directory on disk.
425 * - A Zip archive, uncompressed or compressed.
Adam Lesinski16c4d152014-01-24 13:27:13 -0800426 *
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700427 * If the file is in a directory, it could have a .gz suffix, meaning it is compressed.
Adam Lesinski16c4d152014-01-24 13:27:13 -0800428 *
429 * We should probably reject requests for "illegal" filenames, e.g. those
430 * with illegal characters or "../" backward relative paths.
431 */
432Asset* AssetManager::open(const char* fileName, AccessMode mode)
433{
434 AutoMutex _l(mLock);
435
436 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
437
Adam Lesinski16c4d152014-01-24 13:27:13 -0800438 String8 assetName(kAssetsRoot);
439 assetName.appendPath(fileName);
440
441 /*
442 * For each top-level asset path, search for the asset.
443 */
444
445 size_t i = mAssetPaths.size();
446 while (i > 0) {
447 i--;
448 ALOGV("Looking for asset '%s' in '%s'\n",
449 assetName.string(), mAssetPaths.itemAt(i).path.string());
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000450 Asset* pAsset = openNonAssetInPathLocked(assetName.string(), mode,
451 mAssetPaths.editItemAt(i));
Adam Lesinski16c4d152014-01-24 13:27:13 -0800452 if (pAsset != NULL) {
453 return pAsset != kExcludedAsset ? pAsset : NULL;
454 }
455 }
456
457 return NULL;
458}
459
460/*
461 * Open a non-asset file as if it were an asset.
462 *
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700463 * The "fileName" is the partial path starting from the application name.
Adam Lesinski16c4d152014-01-24 13:27:13 -0800464 */
Adam Lesinskide898ff2014-01-29 18:20:45 -0800465Asset* AssetManager::openNonAsset(const char* fileName, AccessMode mode, int32_t* outCookie)
Adam Lesinski16c4d152014-01-24 13:27:13 -0800466{
467 AutoMutex _l(mLock);
468
469 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
470
Adam Lesinski16c4d152014-01-24 13:27:13 -0800471 /*
472 * For each top-level asset path, search for the asset.
473 */
474
475 size_t i = mAssetPaths.size();
476 while (i > 0) {
477 i--;
478 ALOGV("Looking for non-asset '%s' in '%s'\n", fileName, mAssetPaths.itemAt(i).path.string());
479 Asset* pAsset = openNonAssetInPathLocked(
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000480 fileName, mode, mAssetPaths.editItemAt(i));
Adam Lesinski16c4d152014-01-24 13:27:13 -0800481 if (pAsset != NULL) {
Adam Lesinskide898ff2014-01-29 18:20:45 -0800482 if (outCookie != NULL) *outCookie = static_cast<int32_t>(i + 1);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800483 return pAsset != kExcludedAsset ? pAsset : NULL;
484 }
485 }
486
487 return NULL;
488}
489
Narayan Kamatha0c62602014-01-24 13:51:51 +0000490Asset* AssetManager::openNonAsset(const int32_t cookie, const char* fileName, AccessMode mode)
Adam Lesinski16c4d152014-01-24 13:27:13 -0800491{
Narayan Kamatha0c62602014-01-24 13:51:51 +0000492 const size_t which = static_cast<size_t>(cookie) - 1;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800493
494 AutoMutex _l(mLock);
495
496 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
497
Adam Lesinski16c4d152014-01-24 13:27:13 -0800498 if (which < mAssetPaths.size()) {
499 ALOGV("Looking for non-asset '%s' in '%s'\n", fileName,
500 mAssetPaths.itemAt(which).path.string());
501 Asset* pAsset = openNonAssetInPathLocked(
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000502 fileName, mode, mAssetPaths.editItemAt(which));
Adam Lesinski16c4d152014-01-24 13:27:13 -0800503 if (pAsset != NULL) {
504 return pAsset != kExcludedAsset ? pAsset : NULL;
505 }
506 }
507
508 return NULL;
509}
510
511/*
512 * Get the type of a file in the asset namespace.
513 *
514 * This currently only works for regular files. All others (including
515 * directories) will return kFileTypeNonexistent.
516 */
517FileType AssetManager::getFileType(const char* fileName)
518{
519 Asset* pAsset = NULL;
520
521 /*
522 * Open the asset. This is less efficient than simply finding the
523 * file, but it's not too bad (we don't uncompress or mmap data until
524 * the first read() call).
525 */
526 pAsset = open(fileName, Asset::ACCESS_STREAMING);
527 delete pAsset;
528
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700529 if (pAsset == NULL) {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800530 return kFileTypeNonexistent;
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700531 } else {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800532 return kFileTypeRegular;
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700533 }
Adam Lesinski16c4d152014-01-24 13:27:13 -0800534}
535
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000536bool AssetManager::appendPathToResTable(asset_path& ap, bool appAsLib) const {
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +0900537 // skip those ap's that correspond to system overlays
538 if (ap.isSystemOverlay) {
539 return true;
540 }
541
Martin Kosiba7df36252014-01-16 16:25:56 +0000542 Asset* ass = NULL;
543 ResTable* sharedRes = NULL;
544 bool shared = true;
545 bool onlyEmptyResources = true;
Adam Lesinskib7e1ce02016-04-11 20:03:01 -0700546 ATRACE_NAME(ap.path.string());
Martin Kosiba7df36252014-01-16 16:25:56 +0000547 Asset* idmap = openIdmapLocked(ap);
548 size_t nextEntryIdx = mResources->getTableCount();
549 ALOGV("Looking for resource asset in '%s'\n", ap.path.string());
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700550 if (ap.type != kFileTypeDirectory && ap.rawFd < 0) {
Martin Kosiba7df36252014-01-16 16:25:56 +0000551 if (nextEntryIdx == 0) {
552 // The first item is typically the framework resources,
553 // which we want to avoid parsing every time.
554 sharedRes = const_cast<AssetManager*>(this)->
555 mZipSet.getZipResourceTable(ap.path);
556 if (sharedRes != NULL) {
557 // skip ahead the number of system overlay packages preloaded
558 nextEntryIdx = sharedRes->getTableCount();
559 }
560 }
561 if (sharedRes == NULL) {
562 ass = const_cast<AssetManager*>(this)->
563 mZipSet.getZipResourceTableAsset(ap.path);
564 if (ass == NULL) {
565 ALOGV("loading resource table %s\n", ap.path.string());
566 ass = const_cast<AssetManager*>(this)->
567 openNonAssetInPathLocked("resources.arsc",
568 Asset::ACCESS_BUFFER,
569 ap);
570 if (ass != NULL && ass != kExcludedAsset) {
571 ass = const_cast<AssetManager*>(this)->
572 mZipSet.setZipResourceTableAsset(ap.path, ass);
573 }
574 }
575
576 if (nextEntryIdx == 0 && ass != NULL) {
577 // If this is the first resource table in the asset
578 // manager, then we are going to cache it so that we
579 // can quickly copy it out for others.
580 ALOGV("Creating shared resources for %s", ap.path.string());
581 sharedRes = new ResTable();
582 sharedRes->add(ass, idmap, nextEntryIdx + 1, false);
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +0900583#ifdef __ANDROID__
584 const char* data = getenv("ANDROID_DATA");
585 LOG_ALWAYS_FATAL_IF(data == NULL, "ANDROID_DATA not set");
586 String8 overlaysListPath(data);
587 overlaysListPath.appendPath(kResourceCache);
588 overlaysListPath.appendPath("overlays.list");
589 addSystemOverlays(overlaysListPath.string(), ap.path, sharedRes, nextEntryIdx);
590#endif
Martin Kosiba7df36252014-01-16 16:25:56 +0000591 sharedRes = const_cast<AssetManager*>(this)->
592 mZipSet.setZipResourceTable(ap.path, sharedRes);
593 }
594 }
595 } else {
596 ALOGV("loading resource table %s\n", ap.path.string());
597 ass = const_cast<AssetManager*>(this)->
598 openNonAssetInPathLocked("resources.arsc",
599 Asset::ACCESS_BUFFER,
600 ap);
601 shared = false;
602 }
603
604 if ((ass != NULL || sharedRes != NULL) && ass != kExcludedAsset) {
605 ALOGV("Installing resource asset %p in to table %p\n", ass, mResources);
606 if (sharedRes != NULL) {
607 ALOGV("Copying existing resources for %s", ap.path.string());
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800608 mResources->add(sharedRes, ap.isSystemAsset);
Martin Kosiba7df36252014-01-16 16:25:56 +0000609 } else {
610 ALOGV("Parsing resources for %s", ap.path.string());
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800611 mResources->add(ass, idmap, nextEntryIdx + 1, !shared, appAsLib, ap.isSystemAsset);
Martin Kosiba7df36252014-01-16 16:25:56 +0000612 }
613 onlyEmptyResources = false;
614
615 if (!shared) {
616 delete ass;
617 }
618 } else {
619 ALOGV("Installing empty resources in to table %p\n", mResources);
620 mResources->addEmpty(nextEntryIdx + 1);
621 }
622
623 if (idmap != NULL) {
624 delete idmap;
625 }
Martin Kosiba7df36252014-01-16 16:25:56 +0000626 return onlyEmptyResources;
627}
628
Adam Lesinski16c4d152014-01-24 13:27:13 -0800629const ResTable* AssetManager::getResTable(bool required) const
630{
631 ResTable* rt = mResources;
632 if (rt) {
633 return rt;
634 }
635
636 // Iterate through all asset packages, collecting resources from each.
637
638 AutoMutex _l(mLock);
639
640 if (mResources != NULL) {
641 return mResources;
642 }
643
644 if (required) {
645 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
646 }
647
Adam Lesinskide898ff2014-01-29 18:20:45 -0800648 mResources = new ResTable();
649 updateResourceParamsLocked();
650
651 bool onlyEmptyResources = true;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800652 const size_t N = mAssetPaths.size();
653 for (size_t i=0; i<N; i++) {
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000654 bool empty = appendPathToResTable(
655 const_cast<AssetManager*>(this)->mAssetPaths.editItemAt(i));
Martin Kosiba7df36252014-01-16 16:25:56 +0000656 onlyEmptyResources = onlyEmptyResources && empty;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800657 }
658
Adam Lesinskide898ff2014-01-29 18:20:45 -0800659 if (required && onlyEmptyResources) {
660 ALOGW("Unable to find resources file resources.arsc");
661 delete mResources;
662 mResources = NULL;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800663 }
Adam Lesinskide898ff2014-01-29 18:20:45 -0800664
665 return mResources;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800666}
667
668void AssetManager::updateResourceParamsLocked() const
669{
Adam Lesinskib7e1ce02016-04-11 20:03:01 -0700670 ATRACE_CALL();
Adam Lesinski16c4d152014-01-24 13:27:13 -0800671 ResTable* res = mResources;
672 if (!res) {
673 return;
674 }
675
Narayan Kamath91447d82014-01-21 15:32:36 +0000676 if (mLocale) {
677 mConfig->setBcp47Locale(mLocale);
678 } else {
679 mConfig->clearLocale();
Adam Lesinski16c4d152014-01-24 13:27:13 -0800680 }
Adam Lesinski16c4d152014-01-24 13:27:13 -0800681
682 res->setParameters(mConfig);
683}
684
685Asset* AssetManager::openIdmapLocked(const struct asset_path& ap) const
686{
687 Asset* ass = NULL;
688 if (ap.idmap.size() != 0) {
689 ass = const_cast<AssetManager*>(this)->
690 openAssetFromFileLocked(ap.idmap, Asset::ACCESS_BUFFER);
691 if (ass) {
692 ALOGV("loading idmap %s\n", ap.idmap.string());
693 } else {
694 ALOGW("failed to load idmap %s\n", ap.idmap.string());
695 }
696 }
697 return ass;
698}
699
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +0900700void AssetManager::addSystemOverlays(const char* pathOverlaysList,
701 const String8& targetPackagePath, ResTable* sharedRes, size_t offset) const
702{
703 FILE* fin = fopen(pathOverlaysList, "r");
704 if (fin == NULL) {
705 return;
706 }
707
708#ifndef _WIN32
709 if (TEMP_FAILURE_RETRY(flock(fileno(fin), LOCK_SH)) != 0) {
710 fclose(fin);
711 return;
712 }
713#endif
714 char buf[1024];
715 while (fgets(buf, sizeof(buf), fin)) {
716 // format of each line:
717 // <path to apk><space><path to idmap><newline>
718 char* space = strchr(buf, ' ');
719 char* newline = strchr(buf, '\n');
720 asset_path oap;
721
722 if (space == NULL || newline == NULL || newline < space) {
723 continue;
724 }
725
726 oap.path = String8(buf, space - buf);
727 oap.type = kFileTypeRegular;
728 oap.idmap = String8(space + 1, newline - space - 1);
729 oap.isSystemOverlay = true;
730
731 Asset* oass = const_cast<AssetManager*>(this)->
732 openNonAssetInPathLocked("resources.arsc",
733 Asset::ACCESS_BUFFER,
734 oap);
735
736 if (oass != NULL) {
737 Asset* oidmap = openIdmapLocked(oap);
738 offset++;
739 sharedRes->add(oass, oidmap, offset + 1, false);
740 const_cast<AssetManager*>(this)->mAssetPaths.add(oap);
741 const_cast<AssetManager*>(this)->mZipSet.addOverlay(targetPackagePath, oap);
742 delete oidmap;
743 }
744 }
745
746#ifndef _WIN32
747 TEMP_FAILURE_RETRY(flock(fileno(fin), LOCK_UN));
748#endif
749 fclose(fin);
750}
751
Adam Lesinski16c4d152014-01-24 13:27:13 -0800752const ResTable& AssetManager::getResources(bool required) const
753{
754 const ResTable* rt = getResTable(required);
755 return *rt;
756}
757
758bool AssetManager::isUpToDate()
759{
760 AutoMutex _l(mLock);
761 return mZipSet.isUpToDate();
762}
763
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800764void AssetManager::getLocales(Vector<String8>* locales, bool includeSystemLocales) const
Adam Lesinski16c4d152014-01-24 13:27:13 -0800765{
766 ResTable* res = mResources;
767 if (res != NULL) {
Roozbeh Pournader7e5f96f2016-06-13 18:10:49 -0700768 res->getLocales(locales, includeSystemLocales, true /* mergeEquivalentLangs */);
Narayan Kamathe4345db2014-06-26 16:01:28 +0100769 }
Adam Lesinski16c4d152014-01-24 13:27:13 -0800770}
771
772/*
773 * Open a non-asset file as if it were an asset, searching for it in the
774 * specified app.
775 *
776 * Pass in a NULL values for "appName" if the common app directory should
777 * be used.
778 */
779Asset* AssetManager::openNonAssetInPathLocked(const char* fileName, AccessMode mode,
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000780 asset_path& ap)
Adam Lesinski16c4d152014-01-24 13:27:13 -0800781{
782 Asset* pAsset = NULL;
783
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700784 ALOGV("openNonAssetInPath: name=%s type=%d fd=%d", fileName, ap.type, ap.rawFd);
785
Adam Lesinski16c4d152014-01-24 13:27:13 -0800786 /* look at the filesystem on disk */
787 if (ap.type == kFileTypeDirectory) {
788 String8 path(ap.path);
789 path.appendPath(fileName);
790
791 pAsset = openAssetFromFileLocked(path, mode);
792
793 if (pAsset == NULL) {
794 /* try again, this time with ".gz" */
795 path.append(".gz");
796 pAsset = openAssetFromFileLocked(path, mode);
797 }
798
799 if (pAsset != NULL) {
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700800 ALOGV("FOUND NA '%s' on disk", fileName);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800801 pAsset->setAssetSource(path);
802 }
803
804 /* look inside the zip file */
805 } else {
806 String8 path(fileName);
807
808 /* check the appropriate Zip file */
Narayan Kamath560566d2013-12-03 13:16:03 +0000809 ZipFileRO* pZip = getZipFileLocked(ap);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800810 if (pZip != NULL) {
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700811 ALOGV("GOT zip, checking NA '%s'", (const char*) path);
Narayan Kamath560566d2013-12-03 13:16:03 +0000812 ZipEntryRO entry = pZip->findEntryByName(path.string());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800813 if (entry != NULL) {
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700814 ALOGV("FOUND NA in Zip file for %s", (const char*) path);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800815 pAsset = openAssetFromZipLocked(pZip, entry, mode, path);
Narayan Kamath560566d2013-12-03 13:16:03 +0000816 pZip->releaseEntry(entry);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800817 }
818 }
819
820 if (pAsset != NULL) {
821 /* create a "source" name, for debug/display */
822 pAsset->setAssetSource(
823 createZipSourceNameLocked(ZipSet::getPathName(ap.path.string()), String8(""),
824 String8(fileName)));
825 }
826 }
827
828 return pAsset;
829}
830
831/*
Adam Lesinski16c4d152014-01-24 13:27:13 -0800832 * Create a "source name" for a file from a Zip archive.
833 */
834String8 AssetManager::createZipSourceNameLocked(const String8& zipFileName,
835 const String8& dirName, const String8& fileName)
836{
837 String8 sourceName("zip:");
838 sourceName.append(zipFileName);
839 sourceName.append(":");
840 if (dirName.length() > 0) {
841 sourceName.appendPath(dirName);
842 }
843 sourceName.appendPath(fileName);
844 return sourceName;
845}
846
847/*
Adam Lesinski16c4d152014-01-24 13:27:13 -0800848 * Create a path to a loose asset (asset-base/app/rootDir).
849 */
850String8 AssetManager::createPathNameLocked(const asset_path& ap, const char* rootDir)
851{
852 String8 path(ap.path);
853 if (rootDir != NULL) path.appendPath(rootDir);
854 return path;
855}
856
857/*
858 * Return a pointer to one of our open Zip archives. Returns NULL if no
859 * matching Zip file exists.
Adam Lesinski16c4d152014-01-24 13:27:13 -0800860 */
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000861ZipFileRO* AssetManager::getZipFileLocked(asset_path& ap)
Adam Lesinski16c4d152014-01-24 13:27:13 -0800862{
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000863 ALOGV("getZipFileLocked() in %p: ap=%p zip=%p", this, &ap, ap.zip.get());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800864
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700865 if (ap.zip != NULL) {
866 return ap.zip->getZip();
867 }
868
869 if (ap.rawFd < 0) {
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000870 ALOGV("getZipFileLocked: Creating new zip from path %s", ap.path.string());
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700871 ap.zip = mZipSet.getSharedZip(ap.path);
872 } else {
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000873 ALOGV("getZipFileLocked: Creating new zip from fd %d", ap.rawFd);
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700874 ap.zip = SharedZip::create(ap.rawFd, ap.path);
875
876 }
877 return ap.zip != NULL ? ap.zip->getZip() : NULL;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800878}
879
880/*
881 * Try to open an asset from a file on disk.
882 *
883 * If the file is compressed with gzip, we seek to the start of the
884 * deflated data and pass that in (just like we would for a Zip archive).
885 *
886 * For uncompressed data, we may already have an mmap()ed version sitting
887 * around. If so, we want to hand that to the Asset instead.
888 *
889 * This returns NULL if the file doesn't exist, couldn't be opened, or
890 * claims to be a ".gz" but isn't.
891 */
892Asset* AssetManager::openAssetFromFileLocked(const String8& pathName,
893 AccessMode mode)
894{
895 Asset* pAsset = NULL;
896
897 if (strcasecmp(pathName.getPathExtension().string(), ".gz") == 0) {
898 //printf("TRYING '%s'\n", (const char*) pathName);
899 pAsset = Asset::createFromCompressedFile(pathName.string(), mode);
900 } else {
901 //printf("TRYING '%s'\n", (const char*) pathName);
902 pAsset = Asset::createFromFile(pathName.string(), mode);
903 }
904
905 return pAsset;
906}
907
908/*
909 * Given an entry in a Zip archive, create a new Asset object.
910 *
911 * If the entry is uncompressed, we may want to create or share a
912 * slice of shared memory.
913 */
914Asset* AssetManager::openAssetFromZipLocked(const ZipFileRO* pZipFile,
915 const ZipEntryRO entry, AccessMode mode, const String8& entryName)
916{
917 Asset* pAsset = NULL;
918
919 // TODO: look for previously-created shared memory slice?
Narayan Kamath407753c2015-06-16 12:02:57 +0100920 uint16_t method;
921 uint32_t uncompressedLen;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800922
923 //printf("USING Zip '%s'\n", pEntry->getFileName());
924
Adam Lesinski16c4d152014-01-24 13:27:13 -0800925 if (!pZipFile->getEntryInfo(entry, &method, &uncompressedLen, NULL, NULL,
926 NULL, NULL))
927 {
928 ALOGW("getEntryInfo failed\n");
929 return NULL;
930 }
931
932 FileMap* dataMap = pZipFile->createEntryFileMap(entry);
933 if (dataMap == NULL) {
934 ALOGW("create map from entry failed\n");
935 return NULL;
936 }
937
938 if (method == ZipFileRO::kCompressStored) {
939 pAsset = Asset::createFromUncompressedMap(dataMap, mode);
940 ALOGV("Opened uncompressed entry %s in zip %s mode %d: %p", entryName.string(),
941 dataMap->getFileName(), mode, pAsset);
942 } else {
Narayan Kamath407753c2015-06-16 12:02:57 +0100943 pAsset = Asset::createFromCompressedMap(dataMap,
944 static_cast<size_t>(uncompressedLen), mode);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800945 ALOGV("Opened compressed entry %s in zip %s mode %d: %p", entryName.string(),
946 dataMap->getFileName(), mode, pAsset);
947 }
948 if (pAsset == NULL) {
949 /* unexpected */
950 ALOGW("create from segment failed\n");
951 }
952
953 return pAsset;
954}
955
Adam Lesinski16c4d152014-01-24 13:27:13 -0800956/*
957 * Open a directory in the asset namespace.
958 *
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700959 * An "asset directory" is simply the combination of all asset paths' "assets/" directories.
Adam Lesinski16c4d152014-01-24 13:27:13 -0800960 *
961 * Pass in "" for the root dir.
962 */
963AssetDir* AssetManager::openDir(const char* dirName)
964{
965 AutoMutex _l(mLock);
966
967 AssetDir* pDir = NULL;
968 SortedVector<AssetDir::FileInfo>* pMergedInfo = NULL;
969
970 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
971 assert(dirName != NULL);
972
973 //printf("+++ openDir(%s) in '%s'\n", dirName, (const char*) mAssetBase);
974
Adam Lesinski16c4d152014-01-24 13:27:13 -0800975 pDir = new AssetDir;
976
977 /*
978 * Scan the various directories, merging what we find into a single
979 * vector. We want to scan them in reverse priority order so that
980 * the ".EXCLUDE" processing works correctly. Also, if we decide we
981 * want to remember where the file is coming from, we'll get the right
982 * version.
983 *
984 * We start with Zip archives, then do loose files.
985 */
986 pMergedInfo = new SortedVector<AssetDir::FileInfo>;
987
988 size_t i = mAssetPaths.size();
989 while (i > 0) {
990 i--;
991 const asset_path& ap = mAssetPaths.itemAt(i);
992 if (ap.type == kFileTypeRegular) {
993 ALOGV("Adding directory %s from zip %s", dirName, ap.path.string());
994 scanAndMergeZipLocked(pMergedInfo, ap, kAssetsRoot, dirName);
995 } else {
996 ALOGV("Adding directory %s from dir %s", dirName, ap.path.string());
997 scanAndMergeDirLocked(pMergedInfo, ap, kAssetsRoot, dirName);
998 }
999 }
1000
1001#if 0
1002 printf("FILE LIST:\n");
1003 for (i = 0; i < (size_t) pMergedInfo->size(); i++) {
1004 printf(" %d: (%d) '%s'\n", i,
1005 pMergedInfo->itemAt(i).getFileType(),
1006 (const char*) pMergedInfo->itemAt(i).getFileName());
1007 }
1008#endif
1009
1010 pDir->setFileList(pMergedInfo);
1011 return pDir;
1012}
1013
1014/*
1015 * Open a directory in the non-asset namespace.
1016 *
Adam Lesinskife90eaf2016-10-04 13:31:31 -07001017 * An "asset directory" is simply the combination of all asset paths' "assets/" directories.
Adam Lesinski16c4d152014-01-24 13:27:13 -08001018 *
1019 * Pass in "" for the root dir.
1020 */
Narayan Kamatha0c62602014-01-24 13:51:51 +00001021AssetDir* AssetManager::openNonAssetDir(const int32_t cookie, const char* dirName)
Adam Lesinski16c4d152014-01-24 13:27:13 -08001022{
1023 AutoMutex _l(mLock);
1024
1025 AssetDir* pDir = NULL;
1026 SortedVector<AssetDir::FileInfo>* pMergedInfo = NULL;
1027
1028 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
1029 assert(dirName != NULL);
1030
1031 //printf("+++ openDir(%s) in '%s'\n", dirName, (const char*) mAssetBase);
1032
Adam Lesinski16c4d152014-01-24 13:27:13 -08001033 pDir = new AssetDir;
1034
1035 pMergedInfo = new SortedVector<AssetDir::FileInfo>;
1036
Narayan Kamatha0c62602014-01-24 13:51:51 +00001037 const size_t which = static_cast<size_t>(cookie) - 1;
Adam Lesinski16c4d152014-01-24 13:27:13 -08001038
1039 if (which < mAssetPaths.size()) {
1040 const asset_path& ap = mAssetPaths.itemAt(which);
1041 if (ap.type == kFileTypeRegular) {
1042 ALOGV("Adding directory %s from zip %s", dirName, ap.path.string());
1043 scanAndMergeZipLocked(pMergedInfo, ap, NULL, dirName);
1044 } else {
1045 ALOGV("Adding directory %s from dir %s", dirName, ap.path.string());
1046 scanAndMergeDirLocked(pMergedInfo, ap, NULL, dirName);
1047 }
1048 }
1049
1050#if 0
1051 printf("FILE LIST:\n");
1052 for (i = 0; i < (size_t) pMergedInfo->size(); i++) {
1053 printf(" %d: (%d) '%s'\n", i,
1054 pMergedInfo->itemAt(i).getFileType(),
1055 (const char*) pMergedInfo->itemAt(i).getFileName());
1056 }
1057#endif
1058
1059 pDir->setFileList(pMergedInfo);
1060 return pDir;
1061}
1062
1063/*
1064 * Scan the contents of the specified directory and merge them into the
1065 * "pMergedInfo" vector, removing previous entries if we find "exclude"
1066 * directives.
1067 *
1068 * Returns "false" if we found nothing to contribute.
1069 */
1070bool AssetManager::scanAndMergeDirLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
1071 const asset_path& ap, const char* rootDir, const char* dirName)
1072{
Adam Lesinski16c4d152014-01-24 13:27:13 -08001073 assert(pMergedInfo != NULL);
1074
Adam Lesinskia77685f2016-10-03 16:26:28 -07001075 //printf("scanAndMergeDir: %s %s %s\n", ap.path.string(), rootDir, dirName);
Adam Lesinski16c4d152014-01-24 13:27:13 -08001076
Adam Lesinskia77685f2016-10-03 16:26:28 -07001077 String8 path = createPathNameLocked(ap, rootDir);
1078 if (dirName[0] != '\0')
1079 path.appendPath(dirName);
Adam Lesinski16c4d152014-01-24 13:27:13 -08001080
Adam Lesinskia77685f2016-10-03 16:26:28 -07001081 SortedVector<AssetDir::FileInfo>* pContents = scanDirLocked(path);
1082 if (pContents == NULL)
1083 return false;
Adam Lesinski16c4d152014-01-24 13:27:13 -08001084
1085 // if we wanted to do an incremental cache fill, we would do it here
1086
1087 /*
1088 * Process "exclude" directives. If we find a filename that ends with
1089 * ".EXCLUDE", we look for a matching entry in the "merged" set, and
1090 * remove it if we find it. We also delete the "exclude" entry.
1091 */
1092 int i, count, exclExtLen;
1093
1094 count = pContents->size();
1095 exclExtLen = strlen(kExcludeExtension);
1096 for (i = 0; i < count; i++) {
1097 const char* name;
1098 int nameLen;
1099
1100 name = pContents->itemAt(i).getFileName().string();
1101 nameLen = strlen(name);
1102 if (nameLen > exclExtLen &&
1103 strcmp(name + (nameLen - exclExtLen), kExcludeExtension) == 0)
1104 {
1105 String8 match(name, nameLen - exclExtLen);
1106 int matchIdx;
1107
1108 matchIdx = AssetDir::FileInfo::findEntry(pMergedInfo, match);
1109 if (matchIdx > 0) {
1110 ALOGV("Excluding '%s' [%s]\n",
1111 pMergedInfo->itemAt(matchIdx).getFileName().string(),
1112 pMergedInfo->itemAt(matchIdx).getSourceName().string());
1113 pMergedInfo->removeAt(matchIdx);
1114 } else {
1115 //printf("+++ no match on '%s'\n", (const char*) match);
1116 }
1117
1118 ALOGD("HEY: size=%d removing %d\n", (int)pContents->size(), i);
1119 pContents->removeAt(i);
1120 i--; // adjust "for" loop
1121 count--; // and loop limit
1122 }
1123 }
1124
1125 mergeInfoLocked(pMergedInfo, pContents);
1126
1127 delete pContents;
1128
1129 return true;
1130}
1131
1132/*
1133 * Scan the contents of the specified directory, and stuff what we find
1134 * into a newly-allocated vector.
1135 *
1136 * Files ending in ".gz" will have their extensions removed.
1137 *
1138 * We should probably think about skipping files with "illegal" names,
1139 * e.g. illegal characters (/\:) or excessive length.
1140 *
1141 * Returns NULL if the specified directory doesn't exist.
1142 */
1143SortedVector<AssetDir::FileInfo>* AssetManager::scanDirLocked(const String8& path)
1144{
1145 SortedVector<AssetDir::FileInfo>* pContents = NULL;
1146 DIR* dir;
1147 struct dirent* entry;
1148 FileType fileType;
1149
1150 ALOGV("Scanning dir '%s'\n", path.string());
1151
1152 dir = opendir(path.string());
1153 if (dir == NULL)
1154 return NULL;
1155
1156 pContents = new SortedVector<AssetDir::FileInfo>;
1157
1158 while (1) {
1159 entry = readdir(dir);
1160 if (entry == NULL)
1161 break;
1162
1163 if (strcmp(entry->d_name, ".") == 0 ||
1164 strcmp(entry->d_name, "..") == 0)
1165 continue;
1166
1167#ifdef _DIRENT_HAVE_D_TYPE
1168 if (entry->d_type == DT_REG)
1169 fileType = kFileTypeRegular;
1170 else if (entry->d_type == DT_DIR)
1171 fileType = kFileTypeDirectory;
1172 else
1173 fileType = kFileTypeUnknown;
1174#else
1175 // stat the file
1176 fileType = ::getFileType(path.appendPathCopy(entry->d_name).string());
1177#endif
1178
1179 if (fileType != kFileTypeRegular && fileType != kFileTypeDirectory)
1180 continue;
1181
1182 AssetDir::FileInfo info;
1183 info.set(String8(entry->d_name), fileType);
1184 if (strcasecmp(info.getFileName().getPathExtension().string(), ".gz") == 0)
1185 info.setFileName(info.getFileName().getBasePath());
1186 info.setSourceName(path.appendPathCopy(info.getFileName()));
1187 pContents->add(info);
1188 }
1189
1190 closedir(dir);
1191 return pContents;
1192}
1193
1194/*
1195 * Scan the contents out of the specified Zip archive, and merge what we
1196 * find into "pMergedInfo". If the Zip archive in question doesn't exist,
1197 * we return immediately.
1198 *
1199 * Returns "false" if we found nothing to contribute.
1200 */
1201bool AssetManager::scanAndMergeZipLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
1202 const asset_path& ap, const char* rootDir, const char* baseDirName)
1203{
1204 ZipFileRO* pZip;
1205 Vector<String8> dirs;
1206 AssetDir::FileInfo info;
1207 SortedVector<AssetDir::FileInfo> contents;
1208 String8 sourceName, zipName, dirName;
1209
1210 pZip = mZipSet.getZip(ap.path);
1211 if (pZip == NULL) {
1212 ALOGW("Failure opening zip %s\n", ap.path.string());
1213 return false;
1214 }
1215
1216 zipName = ZipSet::getPathName(ap.path.string());
1217
1218 /* convert "sounds" to "rootDir/sounds" */
1219 if (rootDir != NULL) dirName = rootDir;
1220 dirName.appendPath(baseDirName);
1221
1222 /*
1223 * Scan through the list of files, looking for a match. The files in
1224 * the Zip table of contents are not in sorted order, so we have to
1225 * process the entire list. We're looking for a string that begins
1226 * with the characters in "dirName", is followed by a '/', and has no
1227 * subsequent '/' in the stuff that follows.
1228 *
1229 * What makes this especially fun is that directories are not stored
1230 * explicitly in Zip archives, so we have to infer them from context.
1231 * When we see "sounds/foo.wav" we have to leave a note to ourselves
1232 * to insert a directory called "sounds" into the list. We store
1233 * these in temporary vector so that we only return each one once.
1234 *
1235 * Name comparisons are case-sensitive to match UNIX filesystem
1236 * semantics.
1237 */
1238 int dirNameLen = dirName.length();
Narayan Kamath560566d2013-12-03 13:16:03 +00001239 void *iterationCookie;
Yusuke Sato05f648e2015-08-03 16:21:10 -07001240 if (!pZip->startIteration(&iterationCookie, dirName.string(), NULL)) {
Narayan Kamath560566d2013-12-03 13:16:03 +00001241 ALOGW("ZipFileRO::startIteration returned false");
1242 return false;
1243 }
1244
1245 ZipEntryRO entry;
1246 while ((entry = pZip->nextEntry(iterationCookie)) != NULL) {
Adam Lesinski16c4d152014-01-24 13:27:13 -08001247 char nameBuf[256];
1248
Adam Lesinski16c4d152014-01-24 13:27:13 -08001249 if (pZip->getEntryFileName(entry, nameBuf, sizeof(nameBuf)) != 0) {
1250 // TODO: fix this if we expect to have long names
1251 ALOGE("ARGH: name too long?\n");
1252 continue;
1253 }
1254 //printf("Comparing %s in %s?\n", nameBuf, dirName.string());
Yusuke Sato05f648e2015-08-03 16:21:10 -07001255 if (dirNameLen == 0 || nameBuf[dirNameLen] == '/')
Adam Lesinski16c4d152014-01-24 13:27:13 -08001256 {
1257 const char* cp;
1258 const char* nextSlash;
1259
1260 cp = nameBuf + dirNameLen;
1261 if (dirNameLen != 0)
1262 cp++; // advance past the '/'
1263
1264 nextSlash = strchr(cp, '/');
1265//xxx this may break if there are bare directory entries
1266 if (nextSlash == NULL) {
1267 /* this is a file in the requested directory */
1268
1269 info.set(String8(nameBuf).getPathLeaf(), kFileTypeRegular);
1270
1271 info.setSourceName(
1272 createZipSourceNameLocked(zipName, dirName, info.getFileName()));
1273
1274 contents.add(info);
1275 //printf("FOUND: file '%s'\n", info.getFileName().string());
1276 } else {
1277 /* this is a subdir; add it if we don't already have it*/
1278 String8 subdirName(cp, nextSlash - cp);
1279 size_t j;
1280 size_t N = dirs.size();
1281
1282 for (j = 0; j < N; j++) {
1283 if (subdirName == dirs[j]) {
1284 break;
1285 }
1286 }
1287 if (j == N) {
1288 dirs.add(subdirName);
1289 }
1290
1291 //printf("FOUND: dir '%s'\n", subdirName.string());
1292 }
1293 }
1294 }
1295
Narayan Kamath560566d2013-12-03 13:16:03 +00001296 pZip->endIteration(iterationCookie);
1297
Adam Lesinski16c4d152014-01-24 13:27:13 -08001298 /*
1299 * Add the set of unique directories.
1300 */
1301 for (int i = 0; i < (int) dirs.size(); i++) {
1302 info.set(dirs[i], kFileTypeDirectory);
1303 info.setSourceName(
1304 createZipSourceNameLocked(zipName, dirName, info.getFileName()));
1305 contents.add(info);
1306 }
1307
1308 mergeInfoLocked(pMergedInfo, &contents);
1309
1310 return true;
1311}
1312
1313
1314/*
1315 * Merge two vectors of FileInfo.
1316 *
1317 * The merged contents will be stuffed into *pMergedInfo.
1318 *
1319 * If an entry for a file exists in both "pMergedInfo" and "pContents",
1320 * we use the newer "pContents" entry.
1321 */
1322void AssetManager::mergeInfoLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
1323 const SortedVector<AssetDir::FileInfo>* pContents)
1324{
1325 /*
1326 * Merge what we found in this directory with what we found in
1327 * other places.
1328 *
1329 * Two basic approaches:
1330 * (1) Create a new array that holds the unique values of the two
1331 * arrays.
1332 * (2) Take the elements from pContents and shove them into pMergedInfo.
1333 *
1334 * Because these are vectors of complex objects, moving elements around
1335 * inside the vector requires constructing new objects and allocating
1336 * storage for members. With approach #1, we're always adding to the
1337 * end, whereas with #2 we could be inserting multiple elements at the
1338 * front of the vector. Approach #1 requires a full copy of the
1339 * contents of pMergedInfo, but approach #2 requires the same copy for
1340 * every insertion at the front of pMergedInfo.
1341 *
1342 * (We should probably use a SortedVector interface that allows us to
1343 * just stuff items in, trusting us to maintain the sort order.)
1344 */
1345 SortedVector<AssetDir::FileInfo>* pNewSorted;
1346 int mergeMax, contMax;
1347 int mergeIdx, contIdx;
1348
1349 pNewSorted = new SortedVector<AssetDir::FileInfo>;
1350 mergeMax = pMergedInfo->size();
1351 contMax = pContents->size();
1352 mergeIdx = contIdx = 0;
1353
1354 while (mergeIdx < mergeMax || contIdx < contMax) {
1355 if (mergeIdx == mergeMax) {
1356 /* hit end of "merge" list, copy rest of "contents" */
1357 pNewSorted->add(pContents->itemAt(contIdx));
1358 contIdx++;
1359 } else if (contIdx == contMax) {
1360 /* hit end of "cont" list, copy rest of "merge" */
1361 pNewSorted->add(pMergedInfo->itemAt(mergeIdx));
1362 mergeIdx++;
1363 } else if (pMergedInfo->itemAt(mergeIdx) == pContents->itemAt(contIdx))
1364 {
1365 /* items are identical, add newer and advance both indices */
1366 pNewSorted->add(pContents->itemAt(contIdx));
1367 mergeIdx++;
1368 contIdx++;
1369 } else if (pMergedInfo->itemAt(mergeIdx) < pContents->itemAt(contIdx))
1370 {
1371 /* "merge" is lower, add that one */
1372 pNewSorted->add(pMergedInfo->itemAt(mergeIdx));
1373 mergeIdx++;
1374 } else {
1375 /* "cont" is lower, add that one */
1376 assert(pContents->itemAt(contIdx) < pMergedInfo->itemAt(mergeIdx));
1377 pNewSorted->add(pContents->itemAt(contIdx));
1378 contIdx++;
1379 }
1380 }
1381
1382 /*
1383 * Overwrite the "merged" list with the new stuff.
1384 */
1385 *pMergedInfo = *pNewSorted;
1386 delete pNewSorted;
1387
1388#if 0 // for Vector, rather than SortedVector
1389 int i, j;
1390 for (i = pContents->size() -1; i >= 0; i--) {
1391 bool add = true;
1392
1393 for (j = pMergedInfo->size() -1; j >= 0; j--) {
1394 /* case-sensitive comparisons, to behave like UNIX fs */
1395 if (strcmp(pContents->itemAt(i).mFileName,
1396 pMergedInfo->itemAt(j).mFileName) == 0)
1397 {
1398 /* match, don't add this entry */
1399 add = false;
1400 break;
1401 }
1402 }
1403
1404 if (add)
1405 pMergedInfo->add(pContents->itemAt(i));
1406 }
1407#endif
1408}
1409
Adam Lesinski16c4d152014-01-24 13:27:13 -08001410/*
1411 * ===========================================================================
1412 * AssetManager::SharedZip
1413 * ===========================================================================
1414 */
1415
1416
1417Mutex AssetManager::SharedZip::gLock;
1418DefaultKeyedVector<String8, wp<AssetManager::SharedZip> > AssetManager::SharedZip::gOpen;
1419
1420AssetManager::SharedZip::SharedZip(const String8& path, time_t modWhen)
1421 : mPath(path), mZipFile(NULL), mModWhen(modWhen),
1422 mResourceTableAsset(NULL), mResourceTable(NULL)
1423{
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001424 if (kIsDebug) {
1425 ALOGI("Creating SharedZip %p %s\n", this, (const char*)mPath);
1426 }
Adam Lesinski16c4d152014-01-24 13:27:13 -08001427 ALOGV("+++ opening zip '%s'\n", mPath.string());
Narayan Kamath560566d2013-12-03 13:16:03 +00001428 mZipFile = ZipFileRO::open(mPath.string());
1429 if (mZipFile == NULL) {
Adam Lesinski16c4d152014-01-24 13:27:13 -08001430 ALOGD("failed to open Zip archive '%s'\n", mPath.string());
Adam Lesinski16c4d152014-01-24 13:27:13 -08001431 }
1432}
1433
Dianne Hackbornca3872c2017-10-30 14:19:32 -07001434AssetManager::SharedZip::SharedZip(int fd, const String8& path)
1435 : mPath(path), mZipFile(NULL), mModWhen(0),
1436 mResourceTableAsset(NULL), mResourceTable(NULL)
1437{
1438 if (kIsDebug) {
1439 ALOGI("Creating SharedZip %p fd=%d %s\n", this, fd, (const char*)mPath);
1440 }
1441 ALOGV("+++ opening zip fd=%d '%s'\n", fd, mPath.string());
1442 mZipFile = ZipFileRO::openFd(fd, mPath.string());
1443 if (mZipFile == NULL) {
1444 ::close(fd);
1445 ALOGD("failed to open Zip archive fd=%d '%s'\n", fd, mPath.string());
1446 }
1447}
1448
Mårten Kongstad48d22322014-01-31 14:43:27 +01001449sp<AssetManager::SharedZip> AssetManager::SharedZip::get(const String8& path,
1450 bool createIfNotPresent)
Adam Lesinski16c4d152014-01-24 13:27:13 -08001451{
1452 AutoMutex _l(gLock);
1453 time_t modWhen = getFileModDate(path);
1454 sp<SharedZip> zip = gOpen.valueFor(path).promote();
1455 if (zip != NULL && zip->mModWhen == modWhen) {
1456 return zip;
1457 }
Mårten Kongstad48d22322014-01-31 14:43:27 +01001458 if (zip == NULL && !createIfNotPresent) {
1459 return NULL;
1460 }
Adam Lesinski16c4d152014-01-24 13:27:13 -08001461 zip = new SharedZip(path, modWhen);
1462 gOpen.add(path, zip);
1463 return zip;
Dianne Hackbornca3872c2017-10-30 14:19:32 -07001464}
Adam Lesinski16c4d152014-01-24 13:27:13 -08001465
Dianne Hackbornca3872c2017-10-30 14:19:32 -07001466sp<AssetManager::SharedZip> AssetManager::SharedZip::create(int fd, const String8& path)
1467{
1468 return new SharedZip(fd, path);
Adam Lesinski16c4d152014-01-24 13:27:13 -08001469}
1470
1471ZipFileRO* AssetManager::SharedZip::getZip()
1472{
1473 return mZipFile;
1474}
1475
1476Asset* AssetManager::SharedZip::getResourceTableAsset()
1477{
songjinshi49921f22016-09-08 15:24:30 +08001478 AutoMutex _l(gLock);
Adam Lesinski16c4d152014-01-24 13:27:13 -08001479 ALOGV("Getting from SharedZip %p resource asset %p\n", this, mResourceTableAsset);
1480 return mResourceTableAsset;
1481}
1482
1483Asset* AssetManager::SharedZip::setResourceTableAsset(Asset* asset)
1484{
1485 {
1486 AutoMutex _l(gLock);
1487 if (mResourceTableAsset == NULL) {
Adam Lesinski16c4d152014-01-24 13:27:13 -08001488 // This is not thread safe the first time it is called, so
1489 // do it here with the global lock held.
1490 asset->getBuffer(true);
songjinshi49921f22016-09-08 15:24:30 +08001491 mResourceTableAsset = asset;
Adam Lesinski16c4d152014-01-24 13:27:13 -08001492 return asset;
1493 }
1494 }
1495 delete asset;
1496 return mResourceTableAsset;
1497}
1498
1499ResTable* AssetManager::SharedZip::getResourceTable()
1500{
1501 ALOGV("Getting from SharedZip %p resource table %p\n", this, mResourceTable);
1502 return mResourceTable;
1503}
1504
1505ResTable* AssetManager::SharedZip::setResourceTable(ResTable* res)
1506{
1507 {
1508 AutoMutex _l(gLock);
1509 if (mResourceTable == NULL) {
1510 mResourceTable = res;
1511 return res;
1512 }
1513 }
1514 delete res;
1515 return mResourceTable;
1516}
1517
1518bool AssetManager::SharedZip::isUpToDate()
1519{
1520 time_t modWhen = getFileModDate(mPath.string());
1521 return mModWhen == modWhen;
1522}
1523
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +09001524void AssetManager::SharedZip::addOverlay(const asset_path& ap)
1525{
1526 mOverlays.add(ap);
1527}
1528
1529bool AssetManager::SharedZip::getOverlay(size_t idx, asset_path* out) const
1530{
1531 if (idx >= mOverlays.size()) {
1532 return false;
1533 }
1534 *out = mOverlays[idx];
1535 return true;
1536}
1537
Adam Lesinski16c4d152014-01-24 13:27:13 -08001538AssetManager::SharedZip::~SharedZip()
1539{
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001540 if (kIsDebug) {
1541 ALOGI("Destroying SharedZip %p %s\n", this, (const char*)mPath);
1542 }
Adam Lesinski16c4d152014-01-24 13:27:13 -08001543 if (mResourceTable != NULL) {
1544 delete mResourceTable;
1545 }
1546 if (mResourceTableAsset != NULL) {
1547 delete mResourceTableAsset;
1548 }
1549 if (mZipFile != NULL) {
1550 delete mZipFile;
1551 ALOGV("Closed '%s'\n", mPath.string());
1552 }
1553}
1554
1555/*
1556 * ===========================================================================
1557 * AssetManager::ZipSet
1558 * ===========================================================================
1559 */
1560
1561/*
Adam Lesinski16c4d152014-01-24 13:27:13 -08001562 * Destructor. Close any open archives.
1563 */
1564AssetManager::ZipSet::~ZipSet(void)
1565{
1566 size_t N = mZipFile.size();
1567 for (size_t i = 0; i < N; i++)
1568 closeZip(i);
1569}
1570
1571/*
1572 * Close a Zip file and reset the entry.
1573 */
1574void AssetManager::ZipSet::closeZip(int idx)
1575{
1576 mZipFile.editItemAt(idx) = NULL;
1577}
1578
Adam Lesinski16c4d152014-01-24 13:27:13 -08001579/*
1580 * Retrieve the appropriate Zip file from the set.
1581 */
1582ZipFileRO* AssetManager::ZipSet::getZip(const String8& path)
1583{
Dianne Hackbornca3872c2017-10-30 14:19:32 -07001584 return getSharedZip(path)->getZip();
1585}
1586
1587const sp<AssetManager::SharedZip> AssetManager::ZipSet::getSharedZip(const String8& path)
1588{
Adam Lesinski16c4d152014-01-24 13:27:13 -08001589 int idx = getIndex(path);
1590 sp<SharedZip> zip = mZipFile[idx];
1591 if (zip == NULL) {
1592 zip = SharedZip::get(path);
1593 mZipFile.editItemAt(idx) = zip;
1594 }
Dianne Hackbornca3872c2017-10-30 14:19:32 -07001595 return zip;
Adam Lesinski16c4d152014-01-24 13:27:13 -08001596}
1597
1598Asset* AssetManager::ZipSet::getZipResourceTableAsset(const String8& path)
1599{
1600 int idx = getIndex(path);
1601 sp<SharedZip> zip = mZipFile[idx];
1602 if (zip == NULL) {
1603 zip = SharedZip::get(path);
1604 mZipFile.editItemAt(idx) = zip;
1605 }
1606 return zip->getResourceTableAsset();
1607}
1608
1609Asset* AssetManager::ZipSet::setZipResourceTableAsset(const String8& path,
1610 Asset* asset)
1611{
1612 int idx = getIndex(path);
1613 sp<SharedZip> zip = mZipFile[idx];
1614 // doesn't make sense to call before previously accessing.
1615 return zip->setResourceTableAsset(asset);
1616}
1617
1618ResTable* AssetManager::ZipSet::getZipResourceTable(const String8& path)
1619{
1620 int idx = getIndex(path);
1621 sp<SharedZip> zip = mZipFile[idx];
1622 if (zip == NULL) {
1623 zip = SharedZip::get(path);
1624 mZipFile.editItemAt(idx) = zip;
1625 }
1626 return zip->getResourceTable();
1627}
1628
1629ResTable* AssetManager::ZipSet::setZipResourceTable(const String8& path,
1630 ResTable* res)
1631{
1632 int idx = getIndex(path);
1633 sp<SharedZip> zip = mZipFile[idx];
1634 // doesn't make sense to call before previously accessing.
1635 return zip->setResourceTable(res);
1636}
1637
1638/*
1639 * Generate the partial pathname for the specified archive. The caller
1640 * gets to prepend the asset root directory.
1641 *
1642 * Returns something like "common/en-US-noogle.jar".
1643 */
1644/*static*/ String8 AssetManager::ZipSet::getPathName(const char* zipPath)
1645{
1646 return String8(zipPath);
1647}
1648
1649bool AssetManager::ZipSet::isUpToDate()
1650{
1651 const size_t N = mZipFile.size();
1652 for (size_t i=0; i<N; i++) {
1653 if (mZipFile[i] != NULL && !mZipFile[i]->isUpToDate()) {
1654 return false;
1655 }
1656 }
1657 return true;
1658}
1659
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +09001660void AssetManager::ZipSet::addOverlay(const String8& path, const asset_path& overlay)
1661{
1662 int idx = getIndex(path);
1663 sp<SharedZip> zip = mZipFile[idx];
1664 zip->addOverlay(overlay);
1665}
1666
1667bool AssetManager::ZipSet::getOverlay(const String8& path, size_t idx, asset_path* out) const
1668{
1669 sp<SharedZip> zip = SharedZip::get(path, false);
1670 if (zip == NULL) {
1671 return false;
1672 }
1673 return zip->getOverlay(idx, out);
1674}
1675
Adam Lesinski16c4d152014-01-24 13:27:13 -08001676/*
1677 * Compute the zip file's index.
1678 *
1679 * "appName", "locale", and "vendor" should be set to NULL to indicate the
1680 * default directory.
1681 */
1682int AssetManager::ZipSet::getIndex(const String8& zip) const
1683{
1684 const size_t N = mZipPath.size();
1685 for (size_t i=0; i<N; i++) {
1686 if (mZipPath[i] == zip) {
1687 return i;
1688 }
1689 }
1690
1691 mZipPath.add(zip);
1692 mZipFile.add(NULL);
1693
1694 return mZipPath.size()-1;
1695}