blob: e6182454ad8a0eee267eb54b17d651c566a3edce [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>
Tomasz Wasilczyk804e8192023-08-23 02:22:53 +000029#include <androidfw/PathUtils.h>
Adam Lesinski16c4d152014-01-24 13:27:13 -080030#include <androidfw/ResourceTypes.h>
31#include <androidfw/ZipFileRO.h>
Steven Morelandfb7952f2018-02-23 14:58:50 -080032#include <cutils/atomic.h>
Adam Lesinski16c4d152014-01-24 13:27:13 -080033#include <utils/Log.h>
34#include <utils/String8.h>
35#include <utils/String8.h>
36#include <utils/threads.h>
37#include <utils/Timers.h>
Adam Lesinskib7e1ce02016-04-11 20:03:01 -070038#include <utils/Trace.h>
Martin Wallgren0fbb6082015-08-11 15:10:31 +020039#ifndef _WIN32
40#include <sys/file.h>
41#endif
Adam Lesinski16c4d152014-01-24 13:27:13 -080042
43#include <assert.h>
44#include <dirent.h>
45#include <errno.h>
Mårten Kongstad48d22322014-01-31 14:43:27 +010046#include <string.h> // strerror
Adam Lesinski16c4d152014-01-24 13:27:13 -080047#include <strings.h>
Adam Lesinski16c4d152014-01-24 13:27:13 -080048
49#ifndef TEMP_FAILURE_RETRY
50/* Used to retry syscalls that can return EINTR. */
51#define TEMP_FAILURE_RETRY(exp) ({ \
52 typeof (exp) _rc; \
53 do { \
54 _rc = (exp); \
55 } while (_rc == -1 && errno == EINTR); \
56 _rc; })
57#endif
58
Adam Lesinski16c4d152014-01-24 13:27:13 -080059using namespace android;
60
Andreas Gampe2204f0b2014-10-21 23:04:54 -070061static const bool kIsDebug = false;
62
Adam Lesinski16c4d152014-01-24 13:27:13 -080063static const char* kAssetsRoot = "assets";
64static const char* kAppZipName = NULL; //"classes.jar";
65static const char* kSystemAssets = "framework/framework-res.apk";
Mårten Kongstad48d22322014-01-31 14:43:27 +010066static const char* kResourceCache = "resource-cache";
Adam Lesinski16c4d152014-01-24 13:27:13 -080067
68static const char* kExcludeExtension = ".EXCLUDE";
69
70static Asset* const kExcludedAsset = (Asset*) 0xd000000d;
71
72static volatile int32_t gCount = 0;
73
Mårten Kongstad65a05fd2014-01-31 14:01:52 +010074const char* AssetManager::RESOURCES_FILENAME = "resources.arsc";
Mårten Kongstad48d22322014-01-31 14:43:27 +010075const char* AssetManager::IDMAP_BIN = "/system/bin/idmap";
Mårten Kongstad06a1ac82018-09-20 13:09:47 +020076const char* AssetManager::VENDOR_OVERLAY_DIR = "/vendor/overlay";
Jaekyun Seok1713d9e2018-01-12 21:47:26 +090077const char* AssetManager::PRODUCT_OVERLAY_DIR = "/product/overlay";
Jeongik Cha3e725f22019-07-09 23:58:01 +090078const char* AssetManager::SYSTEM_EXT_OVERLAY_DIR = "/system_ext/overlay";
Mårten Kongstad48c24cf2019-02-25 10:54:09 +010079const char* AssetManager::ODM_OVERLAY_DIR = "/odm/overlay";
Mårten Kongstadeb8a5c02019-02-25 14:18:17 +010080const char* AssetManager::OEM_OVERLAY_DIR = "/oem/overlay";
Jakub Adamek54dcaab2016-10-19 11:46:13 +010081const char* AssetManager::OVERLAY_THEME_DIR_PROPERTY = "ro.boot.vendor.overlay.theme";
Mårten Kongstad48d22322014-01-31 14:43:27 +010082const char* AssetManager::TARGET_PACKAGE_NAME = "android";
83const char* AssetManager::TARGET_APK_PATH = "/system/framework/framework-res.apk";
84const char* AssetManager::IDMAP_DIR = "/data/resource-cache";
Mårten Kongstad65a05fd2014-01-31 14:01:52 +010085
Adam Lesinski16c4d152014-01-24 13:27:13 -080086namespace {
Adam Lesinski16c4d152014-01-24 13:27:13 -080087
Adam Lesinskia77685f2016-10-03 16:26:28 -070088String8 idmapPathForPackagePath(const String8& pkgPath) {
89 const char* root = getenv("ANDROID_DATA");
90 LOG_ALWAYS_FATAL_IF(root == NULL, "ANDROID_DATA not set");
91 String8 path(root);
Tomasz Wasilczyk804e8192023-08-23 02:22:53 +000092 appendPath(path, kResourceCache);
Adam Lesinski16c4d152014-01-24 13:27:13 -080093
Adam Lesinskia77685f2016-10-03 16:26:28 -070094 char buf[256]; // 256 chars should be enough for anyone...
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +000095 strncpy(buf, pkgPath.c_str(), 255);
Adam Lesinskia77685f2016-10-03 16:26:28 -070096 buf[255] = '\0';
97 char* filename = buf;
98 while (*filename && *filename == '/') {
99 ++filename;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800100 }
Adam Lesinskia77685f2016-10-03 16:26:28 -0700101 char* p = filename;
102 while (*p) {
103 if (*p == '/') {
104 *p = '@';
105 }
106 ++p;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800107 }
Tomasz Wasilczyk804e8192023-08-23 02:22:53 +0000108 appendPath(path, filename);
Adam Lesinskia77685f2016-10-03 16:26:28 -0700109 path.append("@idmap");
110
111 return path;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800112}
113
114/*
Adam Lesinskia77685f2016-10-03 16:26:28 -0700115 * Like strdup(), but uses C++ "new" operator instead of malloc.
116 */
117static char* strdupNew(const char* str) {
118 char* newStr;
119 int len;
120
121 if (str == NULL)
122 return NULL;
123
124 len = strlen(str);
125 newStr = new char[len+1];
126 memcpy(newStr, str, len+1);
127
128 return newStr;
129}
130
131} // namespace
132
133/*
Adam Lesinski16c4d152014-01-24 13:27:13 -0800134 * ===========================================================================
135 * AssetManager
136 * ===========================================================================
137 */
138
Adam Lesinskia77685f2016-10-03 16:26:28 -0700139int32_t AssetManager::getGlobalCount() {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800140 return gCount;
141}
142
Adam Lesinskia77685f2016-10-03 16:26:28 -0700143AssetManager::AssetManager() :
144 mLocale(NULL), mResources(NULL), mConfig(new ResTable_config) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700145 int count = android_atomic_inc(&gCount) + 1;
146 if (kIsDebug) {
147 ALOGI("Creating AssetManager %p #%d\n", this, count);
148 }
Adam Lesinski16c4d152014-01-24 13:27:13 -0800149 memset(mConfig, 0, sizeof(ResTable_config));
150}
151
Adam Lesinskia77685f2016-10-03 16:26:28 -0700152AssetManager::~AssetManager() {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800153 int count = android_atomic_dec(&gCount);
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700154 if (kIsDebug) {
155 ALOGI("Destroying AssetManager in %p #%d\n", this, count);
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000156 } else {
157 ALOGV("Destroying AssetManager in %p #%d\n", this, count);
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700158 }
Adam Lesinski16c4d152014-01-24 13:27:13 -0800159
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700160 // Manually close any fd paths for which we have not yet opened their zip (which
161 // will take ownership of the fd and close it when done).
162 for (size_t i=0; i<mAssetPaths.size(); i++) {
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000163 ALOGV("Cleaning path #%d: fd=%d, zip=%p", (int)i, mAssetPaths[i].rawFd,
164 mAssetPaths[i].zip.get());
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700165 if (mAssetPaths[i].rawFd >= 0 && mAssetPaths[i].zip == NULL) {
166 close(mAssetPaths[i].rawFd);
167 }
168 }
169
Adam Lesinski16c4d152014-01-24 13:27:13 -0800170 delete mConfig;
171 delete mResources;
172
173 // don't have a String class yet, so make sure we clean up
174 delete[] mLocale;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800175}
176
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800177bool AssetManager::addAssetPath(
Adam Lesinskia77685f2016-10-03 16:26:28 -0700178 const String8& path, int32_t* cookie, bool appAsLib, bool isSystemAsset) {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800179 AutoMutex _l(mLock);
180
181 asset_path ap;
182
183 String8 realPath(path);
184 if (kAppZipName) {
Tomasz Wasilczyk804e8192023-08-23 02:22:53 +0000185 appendPath(realPath, kAppZipName);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800186 }
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000187 ap.type = ::getFileType(realPath.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800188 if (ap.type == kFileTypeRegular) {
189 ap.path = realPath;
190 } else {
191 ap.path = path;
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000192 ap.type = ::getFileType(path.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800193 if (ap.type != kFileTypeDirectory && ap.type != kFileTypeRegular) {
194 ALOGW("Asset path %s is neither a directory nor file (type=%d).",
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000195 path.c_str(), (int)ap.type);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800196 return false;
197 }
198 }
199
200 // Skip if we have it already.
201 for (size_t i=0; i<mAssetPaths.size(); i++) {
202 if (mAssetPaths[i].path == ap.path) {
203 if (cookie) {
Narayan Kamatha0c62602014-01-24 13:51:51 +0000204 *cookie = static_cast<int32_t>(i+1);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800205 }
206 return true;
207 }
208 }
209
210 ALOGV("In %p Asset %s path: %s", this,
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000211 ap.type == kFileTypeDirectory ? "dir" : "zip", ap.path.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800212
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800213 ap.isSystemAsset = isSystemAsset;
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000214 ssize_t apPos = mAssetPaths.add(ap);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800215
216 // new paths are always added at the end
217 if (cookie) {
Narayan Kamatha0c62602014-01-24 13:51:51 +0000218 *cookie = static_cast<int32_t>(mAssetPaths.size());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800219 }
220
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +0900221#ifdef __ANDROID__
222 // Load overlays, if any
223 asset_path oap;
224 for (size_t idx = 0; mZipSet.getOverlay(ap.path, idx, &oap); idx++) {
225 oap.isSystemAsset = isSystemAsset;
226 mAssetPaths.add(oap);
227 }
228#endif
229
Martin Kosiba7df36252014-01-16 16:25:56 +0000230 if (mResources != NULL) {
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000231 appendPathToResTable(mAssetPaths.editItemAt(apPos), appAsLib);
Martin Kosiba7df36252014-01-16 16:25:56 +0000232 }
233
Adam Lesinski16c4d152014-01-24 13:27:13 -0800234 return true;
235}
236
Mårten Kongstad48d22322014-01-31 14:43:27 +0100237bool AssetManager::addOverlayPath(const String8& packagePath, int32_t* cookie)
238{
239 const String8 idmapPath = idmapPathForPackagePath(packagePath);
240
241 AutoMutex _l(mLock);
242
243 for (size_t i = 0; i < mAssetPaths.size(); ++i) {
244 if (mAssetPaths[i].idmap == idmapPath) {
245 *cookie = static_cast<int32_t>(i + 1);
246 return true;
247 }
248 }
249
250 Asset* idmap = NULL;
251 if ((idmap = openAssetFromFileLocked(idmapPath, Asset::ACCESS_BUFFER)) == NULL) {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000252 ALOGW("failed to open idmap file %s\n", idmapPath.c_str());
Mårten Kongstad48d22322014-01-31 14:43:27 +0100253 return false;
254 }
255
256 String8 targetPath;
257 String8 overlayPath;
258 if (!ResTable::getIdmapInfo(idmap->getBuffer(false), idmap->getLength(),
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700259 NULL, NULL, NULL, &targetPath, &overlayPath)) {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000260 ALOGW("failed to read idmap file %s\n", idmapPath.c_str());
Mårten Kongstad48d22322014-01-31 14:43:27 +0100261 delete idmap;
262 return false;
263 }
264 delete idmap;
265
266 if (overlayPath != packagePath) {
267 ALOGW("idmap file %s inconcistent: expected path %s does not match actual path %s\n",
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000268 idmapPath.c_str(), packagePath.c_str(), overlayPath.c_str());
Mårten Kongstad48d22322014-01-31 14:43:27 +0100269 return false;
270 }
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000271 if (access(targetPath.c_str(), R_OK) != 0) {
272 ALOGW("failed to access file %s: %s\n", targetPath.c_str(), strerror(errno));
Mårten Kongstad48d22322014-01-31 14:43:27 +0100273 return false;
274 }
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000275 if (access(idmapPath.c_str(), R_OK) != 0) {
276 ALOGW("failed to access file %s: %s\n", idmapPath.c_str(), strerror(errno));
Mårten Kongstad48d22322014-01-31 14:43:27 +0100277 return false;
278 }
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000279 if (access(overlayPath.c_str(), R_OK) != 0) {
280 ALOGW("failed to access file %s: %s\n", overlayPath.c_str(), strerror(errno));
Mårten Kongstad48d22322014-01-31 14:43:27 +0100281 return false;
282 }
283
284 asset_path oap;
285 oap.path = overlayPath;
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000286 oap.type = ::getFileType(overlayPath.c_str());
Mårten Kongstad48d22322014-01-31 14:43:27 +0100287 oap.idmap = idmapPath;
288#if 0
289 ALOGD("Overlay added: targetPath=%s overlayPath=%s idmapPath=%s\n",
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000290 targetPath.c_str(), overlayPath.c_str(), idmapPath.c_str());
Mårten Kongstad48d22322014-01-31 14:43:27 +0100291#endif
292 mAssetPaths.add(oap);
293 *cookie = static_cast<int32_t>(mAssetPaths.size());
294
Mårten Kongstad30113132014-11-07 10:52:17 +0100295 if (mResources != NULL) {
296 appendPathToResTable(oap);
297 }
298
Mårten Kongstad48d22322014-01-31 14:43:27 +0100299 return true;
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700300}
301
302bool AssetManager::addAssetFd(
303 int fd, const String8& debugPathName, int32_t* cookie, bool appAsLib,
304 bool assume_ownership) {
305 AutoMutex _l(mLock);
306
307 asset_path ap;
308
309 ap.path = debugPathName;
310 ap.rawFd = fd;
311 ap.type = kFileTypeRegular;
312 ap.assumeOwnership = assume_ownership;
313
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000314 ALOGV("In %p Asset fd %d name: %s", this, fd, ap.path.c_str());
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700315
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000316 ssize_t apPos = mAssetPaths.add(ap);
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700317
318 // new paths are always added at the end
319 if (cookie) {
320 *cookie = static_cast<int32_t>(mAssetPaths.size());
321 }
322
323 if (mResources != NULL) {
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000324 appendPathToResTable(mAssetPaths.editItemAt(apPos), appAsLib);
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700325 }
326
327 return true;
328}
Mårten Kongstad48d22322014-01-31 14:43:27 +0100329
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100330bool AssetManager::createIdmap(const char* targetApkPath, const char* overlayApkPath,
Dianne Hackborn32bb5fa2014-02-11 13:56:21 -0800331 uint32_t targetCrc, uint32_t overlayCrc, uint32_t** outData, size_t* outSize)
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100332{
333 AutoMutex _l(mLock);
334 const String8 paths[2] = { String8(targetApkPath), String8(overlayApkPath) };
Mårten Kongstad6bb13da2016-06-02 09:34:36 +0200335 Asset* assets[2] = {NULL, NULL};
336 bool ret = false;
337 {
338 ResTable tables[2];
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100339
Mårten Kongstad6bb13da2016-06-02 09:34:36 +0200340 for (int i = 0; i < 2; ++i) {
341 asset_path ap;
342 ap.type = kFileTypeRegular;
343 ap.path = paths[i];
344 assets[i] = openNonAssetInPathLocked("resources.arsc",
345 Asset::ACCESS_BUFFER, ap);
346 if (assets[i] == NULL) {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000347 ALOGW("failed to find resources.arsc in %s\n", ap.path.c_str());
Mårten Kongstad6bb13da2016-06-02 09:34:36 +0200348 goto exit;
349 }
350 if (tables[i].add(assets[i]) != NO_ERROR) {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000351 ALOGW("failed to add %s to resource table", paths[i].c_str());
Mårten Kongstad6bb13da2016-06-02 09:34:36 +0200352 goto exit;
353 }
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100354 }
Mårten Kongstad67d5c932018-05-25 15:58:17 +0200355 ret = tables[1].createIdmap(tables[0], targetCrc, overlayCrc,
Mårten Kongstad6bb13da2016-06-02 09:34:36 +0200356 targetApkPath, overlayApkPath, (void**)outData, outSize) == NO_ERROR;
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100357 }
358
Mårten Kongstad6bb13da2016-06-02 09:34:36 +0200359exit:
360 delete assets[0];
361 delete assets[1];
362 return ret;
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100363}
364
Adam Lesinski16c4d152014-01-24 13:27:13 -0800365bool AssetManager::addDefaultAssets()
366{
367 const char* root = getenv("ANDROID_ROOT");
368 LOG_ALWAYS_FATAL_IF(root == NULL, "ANDROID_ROOT not set");
369
370 String8 path(root);
Tomasz Wasilczyk804e8192023-08-23 02:22:53 +0000371 appendPath(path, kSystemAssets);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800372
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800373 return addAssetPath(path, NULL, false /* appAsLib */, true /* isSystemAsset */);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800374}
375
Narayan Kamatha0c62602014-01-24 13:51:51 +0000376int32_t AssetManager::nextAssetPath(const int32_t cookie) const
Adam Lesinski16c4d152014-01-24 13:27:13 -0800377{
378 AutoMutex _l(mLock);
Narayan Kamatha0c62602014-01-24 13:51:51 +0000379 const size_t next = static_cast<size_t>(cookie) + 1;
380 return next > mAssetPaths.size() ? -1 : next;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800381}
382
Narayan Kamatha0c62602014-01-24 13:51:51 +0000383String8 AssetManager::getAssetPath(const int32_t cookie) const
Adam Lesinski16c4d152014-01-24 13:27:13 -0800384{
385 AutoMutex _l(mLock);
Narayan Kamatha0c62602014-01-24 13:51:51 +0000386 const size_t which = static_cast<size_t>(cookie) - 1;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800387 if (which < mAssetPaths.size()) {
388 return mAssetPaths[which].path;
389 }
390 return String8();
391}
392
Adam Lesinski16c4d152014-01-24 13:27:13 -0800393void AssetManager::setLocaleLocked(const char* locale)
394{
395 if (mLocale != NULL) {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800396 delete[] mLocale;
397 }
Elliott Hughesc367d482013-10-29 13:12:55 -0700398
Adam Lesinski16c4d152014-01-24 13:27:13 -0800399 mLocale = strdupNew(locale);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800400 updateResourceParamsLocked();
401}
402
Adam Lesinski16c4d152014-01-24 13:27:13 -0800403void AssetManager::setConfiguration(const ResTable_config& config, const char* locale)
404{
405 AutoMutex _l(mLock);
406 *mConfig = config;
407 if (locale) {
408 setLocaleLocked(locale);
409 } else if (config.language[0] != 0) {
Narayan Kamath91447d82014-01-21 15:32:36 +0000410 char spec[RESTABLE_MAX_LOCALE_LEN];
411 config.getBcp47Locale(spec);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800412 setLocaleLocked(spec);
413 } else {
414 updateResourceParamsLocked();
415 }
416}
417
418void AssetManager::getConfiguration(ResTable_config* outConfig) const
419{
420 AutoMutex _l(mLock);
421 *outConfig = *mConfig;
422}
423
424/*
425 * Open an asset.
426 *
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700427 * The data could be in any asset path. Each asset path could be:
428 * - A directory on disk.
429 * - A Zip archive, uncompressed or compressed.
Adam Lesinski16c4d152014-01-24 13:27:13 -0800430 *
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700431 * If the file is in a directory, it could have a .gz suffix, meaning it is compressed.
Adam Lesinski16c4d152014-01-24 13:27:13 -0800432 *
433 * We should probably reject requests for "illegal" filenames, e.g. those
434 * with illegal characters or "../" backward relative paths.
435 */
436Asset* AssetManager::open(const char* fileName, AccessMode mode)
437{
438 AutoMutex _l(mLock);
439
440 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
441
Adam Lesinski16c4d152014-01-24 13:27:13 -0800442 String8 assetName(kAssetsRoot);
Tomasz Wasilczyk804e8192023-08-23 02:22:53 +0000443 appendPath(assetName, fileName);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800444
445 /*
446 * For each top-level asset path, search for the asset.
447 */
448
449 size_t i = mAssetPaths.size();
450 while (i > 0) {
451 i--;
452 ALOGV("Looking for asset '%s' in '%s'\n",
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000453 assetName.c_str(), mAssetPaths.itemAt(i).path.c_str());
454 Asset* pAsset = openNonAssetInPathLocked(assetName.c_str(), mode,
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000455 mAssetPaths.editItemAt(i));
Adam Lesinski16c4d152014-01-24 13:27:13 -0800456 if (pAsset != NULL) {
457 return pAsset != kExcludedAsset ? pAsset : NULL;
458 }
459 }
460
461 return NULL;
462}
463
464/*
465 * Open a non-asset file as if it were an asset.
466 *
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700467 * The "fileName" is the partial path starting from the application name.
Adam Lesinski16c4d152014-01-24 13:27:13 -0800468 */
Adam Lesinskide898ff2014-01-29 18:20:45 -0800469Asset* AssetManager::openNonAsset(const char* fileName, AccessMode mode, int32_t* outCookie)
Adam Lesinski16c4d152014-01-24 13:27:13 -0800470{
471 AutoMutex _l(mLock);
472
473 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
474
Adam Lesinski16c4d152014-01-24 13:27:13 -0800475 /*
476 * For each top-level asset path, search for the asset.
477 */
478
479 size_t i = mAssetPaths.size();
480 while (i > 0) {
481 i--;
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000482 ALOGV("Looking for non-asset '%s' in '%s'\n", fileName, mAssetPaths.itemAt(i).path.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800483 Asset* pAsset = openNonAssetInPathLocked(
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000484 fileName, mode, mAssetPaths.editItemAt(i));
Adam Lesinski16c4d152014-01-24 13:27:13 -0800485 if (pAsset != NULL) {
Adam Lesinskide898ff2014-01-29 18:20:45 -0800486 if (outCookie != NULL) *outCookie = static_cast<int32_t>(i + 1);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800487 return pAsset != kExcludedAsset ? pAsset : NULL;
488 }
489 }
490
491 return NULL;
492}
493
Narayan Kamatha0c62602014-01-24 13:51:51 +0000494Asset* AssetManager::openNonAsset(const int32_t cookie, const char* fileName, AccessMode mode)
Adam Lesinski16c4d152014-01-24 13:27:13 -0800495{
Narayan Kamatha0c62602014-01-24 13:51:51 +0000496 const size_t which = static_cast<size_t>(cookie) - 1;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800497
498 AutoMutex _l(mLock);
499
500 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
501
Adam Lesinski16c4d152014-01-24 13:27:13 -0800502 if (which < mAssetPaths.size()) {
503 ALOGV("Looking for non-asset '%s' in '%s'\n", fileName,
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000504 mAssetPaths.itemAt(which).path.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800505 Asset* pAsset = openNonAssetInPathLocked(
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000506 fileName, mode, mAssetPaths.editItemAt(which));
Adam Lesinski16c4d152014-01-24 13:27:13 -0800507 if (pAsset != NULL) {
508 return pAsset != kExcludedAsset ? pAsset : NULL;
509 }
510 }
511
512 return NULL;
513}
514
515/*
516 * Get the type of a file in the asset namespace.
517 *
518 * This currently only works for regular files. All others (including
519 * directories) will return kFileTypeNonexistent.
520 */
521FileType AssetManager::getFileType(const char* fileName)
522{
523 Asset* pAsset = NULL;
524
525 /*
526 * Open the asset. This is less efficient than simply finding the
527 * file, but it's not too bad (we don't uncompress or mmap data until
528 * the first read() call).
529 */
530 pAsset = open(fileName, Asset::ACCESS_STREAMING);
531 delete pAsset;
532
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700533 if (pAsset == NULL) {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800534 return kFileTypeNonexistent;
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700535 } else {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800536 return kFileTypeRegular;
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700537 }
Adam Lesinski16c4d152014-01-24 13:27:13 -0800538}
539
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000540bool AssetManager::appendPathToResTable(asset_path& ap, bool appAsLib) const {
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +0900541 // skip those ap's that correspond to system overlays
542 if (ap.isSystemOverlay) {
543 return true;
544 }
545
Martin Kosiba7df36252014-01-16 16:25:56 +0000546 Asset* ass = NULL;
547 ResTable* sharedRes = NULL;
548 bool shared = true;
549 bool onlyEmptyResources = true;
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000550 ATRACE_NAME(ap.path.c_str());
Martin Kosiba7df36252014-01-16 16:25:56 +0000551 Asset* idmap = openIdmapLocked(ap);
552 size_t nextEntryIdx = mResources->getTableCount();
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000553 ALOGV("Looking for resource asset in '%s'\n", ap.path.c_str());
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700554 if (ap.type != kFileTypeDirectory && ap.rawFd < 0) {
Martin Kosiba7df36252014-01-16 16:25:56 +0000555 if (nextEntryIdx == 0) {
556 // The first item is typically the framework resources,
557 // which we want to avoid parsing every time.
558 sharedRes = const_cast<AssetManager*>(this)->
559 mZipSet.getZipResourceTable(ap.path);
560 if (sharedRes != NULL) {
561 // skip ahead the number of system overlay packages preloaded
562 nextEntryIdx = sharedRes->getTableCount();
563 }
564 }
565 if (sharedRes == NULL) {
566 ass = const_cast<AssetManager*>(this)->
567 mZipSet.getZipResourceTableAsset(ap.path);
568 if (ass == NULL) {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000569 ALOGV("loading resource table %s\n", ap.path.c_str());
Martin Kosiba7df36252014-01-16 16:25:56 +0000570 ass = const_cast<AssetManager*>(this)->
571 openNonAssetInPathLocked("resources.arsc",
572 Asset::ACCESS_BUFFER,
573 ap);
574 if (ass != NULL && ass != kExcludedAsset) {
575 ass = const_cast<AssetManager*>(this)->
576 mZipSet.setZipResourceTableAsset(ap.path, ass);
577 }
578 }
Jeongik Cha3e725f22019-07-09 23:58:01 +0900579
Martin Kosiba7df36252014-01-16 16:25:56 +0000580 if (nextEntryIdx == 0 && ass != NULL) {
581 // If this is the first resource table in the asset
582 // manager, then we are going to cache it so that we
583 // can quickly copy it out for others.
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000584 ALOGV("Creating shared resources for %s", ap.path.c_str());
Martin Kosiba7df36252014-01-16 16:25:56 +0000585 sharedRes = new ResTable();
586 sharedRes->add(ass, idmap, nextEntryIdx + 1, false);
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +0900587#ifdef __ANDROID__
588 const char* data = getenv("ANDROID_DATA");
589 LOG_ALWAYS_FATAL_IF(data == NULL, "ANDROID_DATA not set");
590 String8 overlaysListPath(data);
Tomasz Wasilczyk804e8192023-08-23 02:22:53 +0000591 appendPath(overlaysListPath, kResourceCache);
592 appendPath(overlaysListPath, "overlays.list");
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000593 addSystemOverlays(overlaysListPath.c_str(), ap.path, sharedRes, nextEntryIdx);
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +0900594#endif
Martin Kosiba7df36252014-01-16 16:25:56 +0000595 sharedRes = const_cast<AssetManager*>(this)->
596 mZipSet.setZipResourceTable(ap.path, sharedRes);
597 }
598 }
599 } else {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000600 ALOGV("loading resource table %s\n", ap.path.c_str());
Martin Kosiba7df36252014-01-16 16:25:56 +0000601 ass = const_cast<AssetManager*>(this)->
602 openNonAssetInPathLocked("resources.arsc",
603 Asset::ACCESS_BUFFER,
604 ap);
605 shared = false;
606 }
607
608 if ((ass != NULL || sharedRes != NULL) && ass != kExcludedAsset) {
609 ALOGV("Installing resource asset %p in to table %p\n", ass, mResources);
610 if (sharedRes != NULL) {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000611 ALOGV("Copying existing resources for %s", ap.path.c_str());
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800612 mResources->add(sharedRes, ap.isSystemAsset);
Martin Kosiba7df36252014-01-16 16:25:56 +0000613 } else {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000614 ALOGV("Parsing resources for %s", ap.path.c_str());
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800615 mResources->add(ass, idmap, nextEntryIdx + 1, !shared, appAsLib, ap.isSystemAsset);
Martin Kosiba7df36252014-01-16 16:25:56 +0000616 }
617 onlyEmptyResources = false;
618
619 if (!shared) {
620 delete ass;
621 }
622 } else {
623 ALOGV("Installing empty resources in to table %p\n", mResources);
624 mResources->addEmpty(nextEntryIdx + 1);
625 }
626
627 if (idmap != NULL) {
628 delete idmap;
629 }
Martin Kosiba7df36252014-01-16 16:25:56 +0000630 return onlyEmptyResources;
631}
632
Adam Lesinski16c4d152014-01-24 13:27:13 -0800633const ResTable* AssetManager::getResTable(bool required) const
634{
635 ResTable* rt = mResources;
636 if (rt) {
637 return rt;
638 }
639
640 // Iterate through all asset packages, collecting resources from each.
641
642 AutoMutex _l(mLock);
643
644 if (mResources != NULL) {
645 return mResources;
646 }
647
648 if (required) {
649 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
650 }
651
Adam Lesinskide898ff2014-01-29 18:20:45 -0800652 mResources = new ResTable();
653 updateResourceParamsLocked();
654
655 bool onlyEmptyResources = true;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800656 const size_t N = mAssetPaths.size();
657 for (size_t i=0; i<N; i++) {
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000658 bool empty = appendPathToResTable(
659 const_cast<AssetManager*>(this)->mAssetPaths.editItemAt(i));
Martin Kosiba7df36252014-01-16 16:25:56 +0000660 onlyEmptyResources = onlyEmptyResources && empty;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800661 }
662
Adam Lesinskide898ff2014-01-29 18:20:45 -0800663 if (required && onlyEmptyResources) {
664 ALOGW("Unable to find resources file resources.arsc");
665 delete mResources;
666 mResources = NULL;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800667 }
Adam Lesinskide898ff2014-01-29 18:20:45 -0800668
669 return mResources;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800670}
671
672void AssetManager::updateResourceParamsLocked() const
673{
Adam Lesinskib7e1ce02016-04-11 20:03:01 -0700674 ATRACE_CALL();
Adam Lesinski16c4d152014-01-24 13:27:13 -0800675 ResTable* res = mResources;
676 if (!res) {
677 return;
678 }
679
Narayan Kamath91447d82014-01-21 15:32:36 +0000680 if (mLocale) {
681 mConfig->setBcp47Locale(mLocale);
682 } else {
683 mConfig->clearLocale();
Adam Lesinski16c4d152014-01-24 13:27:13 -0800684 }
Adam Lesinski16c4d152014-01-24 13:27:13 -0800685
686 res->setParameters(mConfig);
687}
688
689Asset* AssetManager::openIdmapLocked(const struct asset_path& ap) const
690{
691 Asset* ass = NULL;
692 if (ap.idmap.size() != 0) {
693 ass = const_cast<AssetManager*>(this)->
694 openAssetFromFileLocked(ap.idmap, Asset::ACCESS_BUFFER);
695 if (ass) {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000696 ALOGV("loading idmap %s\n", ap.idmap.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800697 } else {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000698 ALOGW("failed to load idmap %s\n", ap.idmap.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800699 }
700 }
701 return ass;
702}
703
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +0900704void AssetManager::addSystemOverlays(const char* pathOverlaysList,
705 const String8& targetPackagePath, ResTable* sharedRes, size_t offset) const
706{
707 FILE* fin = fopen(pathOverlaysList, "r");
708 if (fin == NULL) {
709 return;
710 }
711
712#ifndef _WIN32
713 if (TEMP_FAILURE_RETRY(flock(fileno(fin), LOCK_SH)) != 0) {
714 fclose(fin);
715 return;
716 }
717#endif
718 char buf[1024];
719 while (fgets(buf, sizeof(buf), fin)) {
720 // format of each line:
721 // <path to apk><space><path to idmap><newline>
722 char* space = strchr(buf, ' ');
723 char* newline = strchr(buf, '\n');
724 asset_path oap;
725
726 if (space == NULL || newline == NULL || newline < space) {
727 continue;
728 }
729
730 oap.path = String8(buf, space - buf);
731 oap.type = kFileTypeRegular;
732 oap.idmap = String8(space + 1, newline - space - 1);
733 oap.isSystemOverlay = true;
734
735 Asset* oass = const_cast<AssetManager*>(this)->
736 openNonAssetInPathLocked("resources.arsc",
737 Asset::ACCESS_BUFFER,
738 oap);
739
740 if (oass != NULL) {
741 Asset* oidmap = openIdmapLocked(oap);
742 offset++;
743 sharedRes->add(oass, oidmap, offset + 1, false);
744 const_cast<AssetManager*>(this)->mAssetPaths.add(oap);
745 const_cast<AssetManager*>(this)->mZipSet.addOverlay(targetPackagePath, oap);
746 delete oidmap;
747 }
748 }
749
750#ifndef _WIN32
751 TEMP_FAILURE_RETRY(flock(fileno(fin), LOCK_UN));
752#endif
753 fclose(fin);
754}
755
Adam Lesinski16c4d152014-01-24 13:27:13 -0800756const ResTable& AssetManager::getResources(bool required) const
757{
758 const ResTable* rt = getResTable(required);
759 return *rt;
760}
761
762bool AssetManager::isUpToDate()
763{
764 AutoMutex _l(mLock);
765 return mZipSet.isUpToDate();
766}
767
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800768void AssetManager::getLocales(Vector<String8>* locales, bool includeSystemLocales) const
Adam Lesinski16c4d152014-01-24 13:27:13 -0800769{
770 ResTable* res = mResources;
771 if (res != NULL) {
Roozbeh Pournader7e5f96f2016-06-13 18:10:49 -0700772 res->getLocales(locales, includeSystemLocales, true /* mergeEquivalentLangs */);
Narayan Kamathe4345db2014-06-26 16:01:28 +0100773 }
Adam Lesinski16c4d152014-01-24 13:27:13 -0800774}
775
776/*
777 * Open a non-asset file as if it were an asset, searching for it in the
778 * specified app.
779 *
780 * Pass in a NULL values for "appName" if the common app directory should
781 * be used.
782 */
783Asset* AssetManager::openNonAssetInPathLocked(const char* fileName, AccessMode mode,
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000784 asset_path& ap)
Adam Lesinski16c4d152014-01-24 13:27:13 -0800785{
786 Asset* pAsset = NULL;
787
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700788 ALOGV("openNonAssetInPath: name=%s type=%d fd=%d", fileName, ap.type, ap.rawFd);
789
Adam Lesinski16c4d152014-01-24 13:27:13 -0800790 /* look at the filesystem on disk */
791 if (ap.type == kFileTypeDirectory) {
792 String8 path(ap.path);
Tomasz Wasilczyk804e8192023-08-23 02:22:53 +0000793 appendPath(path, fileName);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800794
795 pAsset = openAssetFromFileLocked(path, mode);
796
797 if (pAsset == NULL) {
798 /* try again, this time with ".gz" */
799 path.append(".gz");
800 pAsset = openAssetFromFileLocked(path, mode);
801 }
802
803 if (pAsset != NULL) {
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700804 ALOGV("FOUND NA '%s' on disk", fileName);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800805 pAsset->setAssetSource(path);
806 }
807
808 /* look inside the zip file */
809 } else {
810 String8 path(fileName);
811
812 /* check the appropriate Zip file */
Narayan Kamath560566d2013-12-03 13:16:03 +0000813 ZipFileRO* pZip = getZipFileLocked(ap);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800814 if (pZip != NULL) {
Tomasz Wasilczyk835dfe52023-08-17 16:27:22 +0000815 ALOGV("GOT zip, checking NA '%s'", path.c_str());
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000816 ZipEntryRO entry = pZip->findEntryByName(path.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800817 if (entry != NULL) {
Tomasz Wasilczyk835dfe52023-08-17 16:27:22 +0000818 ALOGV("FOUND NA in Zip file for %s", path.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800819 pAsset = openAssetFromZipLocked(pZip, entry, mode, path);
Narayan Kamath560566d2013-12-03 13:16:03 +0000820 pZip->releaseEntry(entry);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800821 }
822 }
823
824 if (pAsset != NULL) {
825 /* create a "source" name, for debug/display */
826 pAsset->setAssetSource(
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000827 createZipSourceNameLocked(ZipSet::getPathName(ap.path.c_str()), String8(""),
Adam Lesinski16c4d152014-01-24 13:27:13 -0800828 String8(fileName)));
829 }
830 }
831
832 return pAsset;
833}
834
835/*
Adam Lesinski16c4d152014-01-24 13:27:13 -0800836 * Create a "source name" for a file from a Zip archive.
837 */
838String8 AssetManager::createZipSourceNameLocked(const String8& zipFileName,
839 const String8& dirName, const String8& fileName)
840{
841 String8 sourceName("zip:");
842 sourceName.append(zipFileName);
843 sourceName.append(":");
844 if (dirName.length() > 0) {
Tomasz Wasilczyk804e8192023-08-23 02:22:53 +0000845 appendPath(sourceName, dirName);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800846 }
Tomasz Wasilczyk804e8192023-08-23 02:22:53 +0000847 appendPath(sourceName, fileName);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800848 return sourceName;
849}
850
851/*
Adam Lesinski16c4d152014-01-24 13:27:13 -0800852 * Create a path to a loose asset (asset-base/app/rootDir).
853 */
854String8 AssetManager::createPathNameLocked(const asset_path& ap, const char* rootDir)
855{
856 String8 path(ap.path);
Tomasz Wasilczyk804e8192023-08-23 02:22:53 +0000857 if (rootDir != NULL) appendPath(path, rootDir);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800858 return path;
859}
860
861/*
862 * Return a pointer to one of our open Zip archives. Returns NULL if no
863 * matching Zip file exists.
Adam Lesinski16c4d152014-01-24 13:27:13 -0800864 */
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000865ZipFileRO* AssetManager::getZipFileLocked(asset_path& ap)
Adam Lesinski16c4d152014-01-24 13:27:13 -0800866{
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000867 ALOGV("getZipFileLocked() in %p: ap=%p zip=%p", this, &ap, ap.zip.get());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800868
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700869 if (ap.zip != NULL) {
870 return ap.zip->getZip();
871 }
872
873 if (ap.rawFd < 0) {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000874 ALOGV("getZipFileLocked: Creating new zip from path %s", ap.path.c_str());
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700875 ap.zip = mZipSet.getSharedZip(ap.path);
876 } else {
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000877 ALOGV("getZipFileLocked: Creating new zip from fd %d", ap.rawFd);
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700878 ap.zip = SharedZip::create(ap.rawFd, ap.path);
879
880 }
881 return ap.zip != NULL ? ap.zip->getZip() : NULL;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800882}
883
884/*
885 * Try to open an asset from a file on disk.
886 *
887 * If the file is compressed with gzip, we seek to the start of the
888 * deflated data and pass that in (just like we would for a Zip archive).
889 *
890 * For uncompressed data, we may already have an mmap()ed version sitting
891 * around. If so, we want to hand that to the Asset instead.
892 *
893 * This returns NULL if the file doesn't exist, couldn't be opened, or
894 * claims to be a ".gz" but isn't.
895 */
896Asset* AssetManager::openAssetFromFileLocked(const String8& pathName,
897 AccessMode mode)
898{
899 Asset* pAsset = NULL;
900
Tomasz Wasilczyk804e8192023-08-23 02:22:53 +0000901 if (strcasecmp(getPathExtension(pathName).c_str(), ".gz") == 0) {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800902 //printf("TRYING '%s'\n", (const char*) pathName);
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000903 pAsset = Asset::createFromCompressedFile(pathName.c_str(), mode);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800904 } else {
905 //printf("TRYING '%s'\n", (const char*) pathName);
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000906 pAsset = Asset::createFromFile(pathName.c_str(), mode);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800907 }
908
909 return pAsset;
910}
911
912/*
913 * Given an entry in a Zip archive, create a new Asset object.
914 *
915 * If the entry is uncompressed, we may want to create or share a
916 * slice of shared memory.
917 */
918Asset* AssetManager::openAssetFromZipLocked(const ZipFileRO* pZipFile,
919 const ZipEntryRO entry, AccessMode mode, const String8& entryName)
920{
Ryan Mitchell80094e32020-11-16 23:08:18 +0000921 std::unique_ptr<Asset> pAsset;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800922
923 // TODO: look for previously-created shared memory slice?
Narayan Kamath407753c2015-06-16 12:02:57 +0100924 uint16_t method;
925 uint32_t uncompressedLen;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800926
927 //printf("USING Zip '%s'\n", pEntry->getFileName());
928
Pawan Waghfbe2bd32024-06-03 21:39:07 +0000929 if (!pZipFile->getEntryInfo(entry, &method, &uncompressedLen, nullptr, nullptr,
930 nullptr, nullptr, nullptr))
Adam Lesinski16c4d152014-01-24 13:27:13 -0800931 {
932 ALOGW("getEntryInfo failed\n");
933 return NULL;
934 }
935
Ryan Mitchell80094e32020-11-16 23:08:18 +0000936 std::optional<incfs::IncFsFileMap> dataMap = pZipFile->createEntryIncFsFileMap(entry);
937 if (!dataMap.has_value()) {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800938 ALOGW("create map from entry failed\n");
939 return NULL;
940 }
941
942 if (method == ZipFileRO::kCompressStored) {
Ryan Mitchell80094e32020-11-16 23:08:18 +0000943 pAsset = Asset::createFromUncompressedMap(std::move(*dataMap), mode);
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000944 ALOGV("Opened uncompressed entry %s in zip %s mode %d: %p", entryName.c_str(),
Ryan Mitchell80094e32020-11-16 23:08:18 +0000945 dataMap->file_name(), mode, pAsset.get());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800946 } else {
Ryan Mitchell80094e32020-11-16 23:08:18 +0000947 pAsset = Asset::createFromCompressedMap(std::move(*dataMap),
Narayan Kamath407753c2015-06-16 12:02:57 +0100948 static_cast<size_t>(uncompressedLen), mode);
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000949 ALOGV("Opened compressed entry %s in zip %s mode %d: %p", entryName.c_str(),
Ryan Mitchell80094e32020-11-16 23:08:18 +0000950 dataMap->file_name(), mode, pAsset.get());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800951 }
952 if (pAsset == NULL) {
953 /* unexpected */
954 ALOGW("create from segment failed\n");
955 }
956
Ryan Mitchell80094e32020-11-16 23:08:18 +0000957 return pAsset.release();
Adam Lesinski16c4d152014-01-24 13:27:13 -0800958}
959
Adam Lesinski16c4d152014-01-24 13:27:13 -0800960/*
961 * Open a directory in the asset namespace.
962 *
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700963 * An "asset directory" is simply the combination of all asset paths' "assets/" directories.
Adam Lesinski16c4d152014-01-24 13:27:13 -0800964 *
965 * Pass in "" for the root dir.
966 */
967AssetDir* AssetManager::openDir(const char* dirName)
968{
969 AutoMutex _l(mLock);
970
971 AssetDir* pDir = NULL;
972 SortedVector<AssetDir::FileInfo>* pMergedInfo = NULL;
973
974 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
975 assert(dirName != NULL);
976
977 //printf("+++ openDir(%s) in '%s'\n", dirName, (const char*) mAssetBase);
978
Adam Lesinski16c4d152014-01-24 13:27:13 -0800979 pDir = new AssetDir;
980
981 /*
982 * Scan the various directories, merging what we find into a single
983 * vector. We want to scan them in reverse priority order so that
984 * the ".EXCLUDE" processing works correctly. Also, if we decide we
985 * want to remember where the file is coming from, we'll get the right
986 * version.
987 *
988 * We start with Zip archives, then do loose files.
989 */
990 pMergedInfo = new SortedVector<AssetDir::FileInfo>;
991
992 size_t i = mAssetPaths.size();
993 while (i > 0) {
994 i--;
995 const asset_path& ap = mAssetPaths.itemAt(i);
996 if (ap.type == kFileTypeRegular) {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000997 ALOGV("Adding directory %s from zip %s", dirName, ap.path.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800998 scanAndMergeZipLocked(pMergedInfo, ap, kAssetsRoot, dirName);
999 } else {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +00001000 ALOGV("Adding directory %s from dir %s", dirName, ap.path.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -08001001 scanAndMergeDirLocked(pMergedInfo, ap, kAssetsRoot, dirName);
1002 }
1003 }
1004
1005#if 0
1006 printf("FILE LIST:\n");
1007 for (i = 0; i < (size_t) pMergedInfo->size(); i++) {
1008 printf(" %d: (%d) '%s'\n", i,
1009 pMergedInfo->itemAt(i).getFileType(),
1010 (const char*) pMergedInfo->itemAt(i).getFileName());
1011 }
1012#endif
1013
1014 pDir->setFileList(pMergedInfo);
1015 return pDir;
1016}
1017
1018/*
1019 * Open a directory in the non-asset namespace.
1020 *
Adam Lesinskife90eaf2016-10-04 13:31:31 -07001021 * An "asset directory" is simply the combination of all asset paths' "assets/" directories.
Adam Lesinski16c4d152014-01-24 13:27:13 -08001022 *
1023 * Pass in "" for the root dir.
1024 */
Narayan Kamatha0c62602014-01-24 13:51:51 +00001025AssetDir* AssetManager::openNonAssetDir(const int32_t cookie, const char* dirName)
Adam Lesinski16c4d152014-01-24 13:27:13 -08001026{
1027 AutoMutex _l(mLock);
1028
1029 AssetDir* pDir = NULL;
1030 SortedVector<AssetDir::FileInfo>* pMergedInfo = NULL;
1031
1032 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
1033 assert(dirName != NULL);
1034
1035 //printf("+++ openDir(%s) in '%s'\n", dirName, (const char*) mAssetBase);
1036
Adam Lesinski16c4d152014-01-24 13:27:13 -08001037 pDir = new AssetDir;
1038
1039 pMergedInfo = new SortedVector<AssetDir::FileInfo>;
1040
Narayan Kamatha0c62602014-01-24 13:51:51 +00001041 const size_t which = static_cast<size_t>(cookie) - 1;
Adam Lesinski16c4d152014-01-24 13:27:13 -08001042
1043 if (which < mAssetPaths.size()) {
1044 const asset_path& ap = mAssetPaths.itemAt(which);
1045 if (ap.type == kFileTypeRegular) {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +00001046 ALOGV("Adding directory %s from zip %s", dirName, ap.path.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -08001047 scanAndMergeZipLocked(pMergedInfo, ap, NULL, dirName);
1048 } else {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +00001049 ALOGV("Adding directory %s from dir %s", dirName, ap.path.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -08001050 scanAndMergeDirLocked(pMergedInfo, ap, NULL, dirName);
1051 }
1052 }
1053
1054#if 0
1055 printf("FILE LIST:\n");
1056 for (i = 0; i < (size_t) pMergedInfo->size(); i++) {
1057 printf(" %d: (%d) '%s'\n", i,
1058 pMergedInfo->itemAt(i).getFileType(),
1059 (const char*) pMergedInfo->itemAt(i).getFileName());
1060 }
1061#endif
1062
1063 pDir->setFileList(pMergedInfo);
1064 return pDir;
1065}
1066
1067/*
1068 * Scan the contents of the specified directory and merge them into the
1069 * "pMergedInfo" vector, removing previous entries if we find "exclude"
1070 * directives.
1071 *
1072 * Returns "false" if we found nothing to contribute.
1073 */
1074bool AssetManager::scanAndMergeDirLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
1075 const asset_path& ap, const char* rootDir, const char* dirName)
1076{
Adam Lesinski16c4d152014-01-24 13:27:13 -08001077 assert(pMergedInfo != NULL);
1078
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +00001079 //printf("scanAndMergeDir: %s %s %s\n", ap.path.c_str(), rootDir, dirName);
Adam Lesinski16c4d152014-01-24 13:27:13 -08001080
Adam Lesinskia77685f2016-10-03 16:26:28 -07001081 String8 path = createPathNameLocked(ap, rootDir);
Tomasz Wasilczyk804e8192023-08-23 02:22:53 +00001082 if (dirName[0] != '\0') appendPath(path, dirName);
Adam Lesinski16c4d152014-01-24 13:27:13 -08001083
Adam Lesinskia77685f2016-10-03 16:26:28 -07001084 SortedVector<AssetDir::FileInfo>* pContents = scanDirLocked(path);
1085 if (pContents == NULL)
1086 return false;
Adam Lesinski16c4d152014-01-24 13:27:13 -08001087
1088 // if we wanted to do an incremental cache fill, we would do it here
1089
1090 /*
1091 * Process "exclude" directives. If we find a filename that ends with
1092 * ".EXCLUDE", we look for a matching entry in the "merged" set, and
1093 * remove it if we find it. We also delete the "exclude" entry.
1094 */
1095 int i, count, exclExtLen;
1096
1097 count = pContents->size();
1098 exclExtLen = strlen(kExcludeExtension);
1099 for (i = 0; i < count; i++) {
1100 const char* name;
1101 int nameLen;
1102
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +00001103 name = pContents->itemAt(i).getFileName().c_str();
Adam Lesinski16c4d152014-01-24 13:27:13 -08001104 nameLen = strlen(name);
1105 if (nameLen > exclExtLen &&
1106 strcmp(name + (nameLen - exclExtLen), kExcludeExtension) == 0)
1107 {
1108 String8 match(name, nameLen - exclExtLen);
1109 int matchIdx;
1110
1111 matchIdx = AssetDir::FileInfo::findEntry(pMergedInfo, match);
1112 if (matchIdx > 0) {
1113 ALOGV("Excluding '%s' [%s]\n",
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +00001114 pMergedInfo->itemAt(matchIdx).getFileName().c_str(),
1115 pMergedInfo->itemAt(matchIdx).getSourceName().c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -08001116 pMergedInfo->removeAt(matchIdx);
1117 } else {
1118 //printf("+++ no match on '%s'\n", (const char*) match);
1119 }
1120
1121 ALOGD("HEY: size=%d removing %d\n", (int)pContents->size(), i);
1122 pContents->removeAt(i);
1123 i--; // adjust "for" loop
1124 count--; // and loop limit
1125 }
1126 }
1127
1128 mergeInfoLocked(pMergedInfo, pContents);
1129
1130 delete pContents;
1131
1132 return true;
1133}
1134
1135/*
1136 * Scan the contents of the specified directory, and stuff what we find
1137 * into a newly-allocated vector.
1138 *
1139 * Files ending in ".gz" will have their extensions removed.
1140 *
1141 * We should probably think about skipping files with "illegal" names,
1142 * e.g. illegal characters (/\:) or excessive length.
1143 *
1144 * Returns NULL if the specified directory doesn't exist.
1145 */
1146SortedVector<AssetDir::FileInfo>* AssetManager::scanDirLocked(const String8& path)
1147{
1148 SortedVector<AssetDir::FileInfo>* pContents = NULL;
1149 DIR* dir;
1150 struct dirent* entry;
1151 FileType fileType;
1152
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +00001153 ALOGV("Scanning dir '%s'\n", path.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -08001154
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +00001155 dir = opendir(path.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -08001156 if (dir == NULL)
1157 return NULL;
1158
1159 pContents = new SortedVector<AssetDir::FileInfo>;
1160
1161 while (1) {
1162 entry = readdir(dir);
1163 if (entry == NULL)
1164 break;
1165
1166 if (strcmp(entry->d_name, ".") == 0 ||
1167 strcmp(entry->d_name, "..") == 0)
1168 continue;
1169
1170#ifdef _DIRENT_HAVE_D_TYPE
1171 if (entry->d_type == DT_REG)
1172 fileType = kFileTypeRegular;
1173 else if (entry->d_type == DT_DIR)
1174 fileType = kFileTypeDirectory;
1175 else
1176 fileType = kFileTypeUnknown;
1177#else
1178 // stat the file
Tomasz Wasilczyk804e8192023-08-23 02:22:53 +00001179 fileType = ::getFileType(appendPathCopy(path, entry->d_name).c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -08001180#endif
1181
1182 if (fileType != kFileTypeRegular && fileType != kFileTypeDirectory)
1183 continue;
1184
1185 AssetDir::FileInfo info;
1186 info.set(String8(entry->d_name), fileType);
Tomasz Wasilczyk804e8192023-08-23 02:22:53 +00001187 if (strcasecmp(getPathExtension(info.getFileName()).c_str(), ".gz") == 0)
1188 info.setFileName(getBasePath(info.getFileName()));
1189 info.setSourceName(appendPathCopy(path, info.getFileName()));
Adam Lesinski16c4d152014-01-24 13:27:13 -08001190 pContents->add(info);
1191 }
1192
1193 closedir(dir);
1194 return pContents;
1195}
1196
1197/*
1198 * Scan the contents out of the specified Zip archive, and merge what we
1199 * find into "pMergedInfo". If the Zip archive in question doesn't exist,
1200 * we return immediately.
1201 *
1202 * Returns "false" if we found nothing to contribute.
1203 */
1204bool AssetManager::scanAndMergeZipLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
1205 const asset_path& ap, const char* rootDir, const char* baseDirName)
1206{
1207 ZipFileRO* pZip;
1208 Vector<String8> dirs;
1209 AssetDir::FileInfo info;
1210 SortedVector<AssetDir::FileInfo> contents;
1211 String8 sourceName, zipName, dirName;
1212
1213 pZip = mZipSet.getZip(ap.path);
1214 if (pZip == NULL) {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +00001215 ALOGW("Failure opening zip %s\n", ap.path.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -08001216 return false;
1217 }
1218
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +00001219 zipName = ZipSet::getPathName(ap.path.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -08001220
1221 /* convert "sounds" to "rootDir/sounds" */
1222 if (rootDir != NULL) dirName = rootDir;
Tomasz Wasilczyk804e8192023-08-23 02:22:53 +00001223 appendPath(dirName, baseDirName);
Adam Lesinski16c4d152014-01-24 13:27:13 -08001224
1225 /*
1226 * Scan through the list of files, looking for a match. The files in
1227 * the Zip table of contents are not in sorted order, so we have to
1228 * process the entire list. We're looking for a string that begins
1229 * with the characters in "dirName", is followed by a '/', and has no
1230 * subsequent '/' in the stuff that follows.
1231 *
1232 * What makes this especially fun is that directories are not stored
1233 * explicitly in Zip archives, so we have to infer them from context.
1234 * When we see "sounds/foo.wav" we have to leave a note to ourselves
1235 * to insert a directory called "sounds" into the list. We store
1236 * these in temporary vector so that we only return each one once.
1237 *
1238 * Name comparisons are case-sensitive to match UNIX filesystem
1239 * semantics.
1240 */
1241 int dirNameLen = dirName.length();
Narayan Kamath560566d2013-12-03 13:16:03 +00001242 void *iterationCookie;
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +00001243 if (!pZip->startIteration(&iterationCookie, dirName.c_str(), NULL)) {
Narayan Kamath560566d2013-12-03 13:16:03 +00001244 ALOGW("ZipFileRO::startIteration returned false");
1245 return false;
1246 }
1247
1248 ZipEntryRO entry;
1249 while ((entry = pZip->nextEntry(iterationCookie)) != NULL) {
Adam Lesinski16c4d152014-01-24 13:27:13 -08001250 char nameBuf[256];
1251
Adam Lesinski16c4d152014-01-24 13:27:13 -08001252 if (pZip->getEntryFileName(entry, nameBuf, sizeof(nameBuf)) != 0) {
1253 // TODO: fix this if we expect to have long names
1254 ALOGE("ARGH: name too long?\n");
1255 continue;
1256 }
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +00001257 //printf("Comparing %s in %s?\n", nameBuf, dirName.c_str());
Yusuke Sato05f648e2015-08-03 16:21:10 -07001258 if (dirNameLen == 0 || nameBuf[dirNameLen] == '/')
Adam Lesinski16c4d152014-01-24 13:27:13 -08001259 {
1260 const char* cp;
1261 const char* nextSlash;
1262
1263 cp = nameBuf + dirNameLen;
1264 if (dirNameLen != 0)
1265 cp++; // advance past the '/'
1266
1267 nextSlash = strchr(cp, '/');
1268//xxx this may break if there are bare directory entries
1269 if (nextSlash == NULL) {
1270 /* this is a file in the requested directory */
1271
Tomasz Wasilczyk804e8192023-08-23 02:22:53 +00001272 info.set(getPathLeaf(String8(nameBuf)), kFileTypeRegular);
Adam Lesinski16c4d152014-01-24 13:27:13 -08001273
1274 info.setSourceName(
1275 createZipSourceNameLocked(zipName, dirName, info.getFileName()));
1276
1277 contents.add(info);
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +00001278 //printf("FOUND: file '%s'\n", info.getFileName().c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -08001279 } else {
1280 /* this is a subdir; add it if we don't already have it*/
1281 String8 subdirName(cp, nextSlash - cp);
1282 size_t j;
1283 size_t N = dirs.size();
1284
1285 for (j = 0; j < N; j++) {
1286 if (subdirName == dirs[j]) {
1287 break;
1288 }
1289 }
1290 if (j == N) {
1291 dirs.add(subdirName);
1292 }
1293
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +00001294 //printf("FOUND: dir '%s'\n", subdirName.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -08001295 }
1296 }
1297 }
1298
Narayan Kamath560566d2013-12-03 13:16:03 +00001299 pZip->endIteration(iterationCookie);
1300
Adam Lesinski16c4d152014-01-24 13:27:13 -08001301 /*
1302 * Add the set of unique directories.
1303 */
1304 for (int i = 0; i < (int) dirs.size(); i++) {
1305 info.set(dirs[i], kFileTypeDirectory);
1306 info.setSourceName(
1307 createZipSourceNameLocked(zipName, dirName, info.getFileName()));
1308 contents.add(info);
1309 }
1310
1311 mergeInfoLocked(pMergedInfo, &contents);
1312
1313 return true;
1314}
1315
1316
1317/*
1318 * Merge two vectors of FileInfo.
1319 *
1320 * The merged contents will be stuffed into *pMergedInfo.
1321 *
1322 * If an entry for a file exists in both "pMergedInfo" and "pContents",
1323 * we use the newer "pContents" entry.
1324 */
1325void AssetManager::mergeInfoLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
1326 const SortedVector<AssetDir::FileInfo>* pContents)
1327{
1328 /*
1329 * Merge what we found in this directory with what we found in
1330 * other places.
1331 *
1332 * Two basic approaches:
1333 * (1) Create a new array that holds the unique values of the two
1334 * arrays.
1335 * (2) Take the elements from pContents and shove them into pMergedInfo.
1336 *
1337 * Because these are vectors of complex objects, moving elements around
1338 * inside the vector requires constructing new objects and allocating
1339 * storage for members. With approach #1, we're always adding to the
1340 * end, whereas with #2 we could be inserting multiple elements at the
1341 * front of the vector. Approach #1 requires a full copy of the
1342 * contents of pMergedInfo, but approach #2 requires the same copy for
1343 * every insertion at the front of pMergedInfo.
1344 *
1345 * (We should probably use a SortedVector interface that allows us to
1346 * just stuff items in, trusting us to maintain the sort order.)
1347 */
1348 SortedVector<AssetDir::FileInfo>* pNewSorted;
1349 int mergeMax, contMax;
1350 int mergeIdx, contIdx;
1351
1352 pNewSorted = new SortedVector<AssetDir::FileInfo>;
1353 mergeMax = pMergedInfo->size();
1354 contMax = pContents->size();
1355 mergeIdx = contIdx = 0;
1356
1357 while (mergeIdx < mergeMax || contIdx < contMax) {
1358 if (mergeIdx == mergeMax) {
1359 /* hit end of "merge" list, copy rest of "contents" */
1360 pNewSorted->add(pContents->itemAt(contIdx));
1361 contIdx++;
1362 } else if (contIdx == contMax) {
1363 /* hit end of "cont" list, copy rest of "merge" */
1364 pNewSorted->add(pMergedInfo->itemAt(mergeIdx));
1365 mergeIdx++;
1366 } else if (pMergedInfo->itemAt(mergeIdx) == pContents->itemAt(contIdx))
1367 {
1368 /* items are identical, add newer and advance both indices */
1369 pNewSorted->add(pContents->itemAt(contIdx));
1370 mergeIdx++;
1371 contIdx++;
1372 } else if (pMergedInfo->itemAt(mergeIdx) < pContents->itemAt(contIdx))
1373 {
1374 /* "merge" is lower, add that one */
1375 pNewSorted->add(pMergedInfo->itemAt(mergeIdx));
1376 mergeIdx++;
1377 } else {
1378 /* "cont" is lower, add that one */
1379 assert(pContents->itemAt(contIdx) < pMergedInfo->itemAt(mergeIdx));
1380 pNewSorted->add(pContents->itemAt(contIdx));
1381 contIdx++;
1382 }
1383 }
1384
1385 /*
1386 * Overwrite the "merged" list with the new stuff.
1387 */
1388 *pMergedInfo = *pNewSorted;
1389 delete pNewSorted;
1390
1391#if 0 // for Vector, rather than SortedVector
1392 int i, j;
1393 for (i = pContents->size() -1; i >= 0; i--) {
1394 bool add = true;
1395
1396 for (j = pMergedInfo->size() -1; j >= 0; j--) {
1397 /* case-sensitive comparisons, to behave like UNIX fs */
1398 if (strcmp(pContents->itemAt(i).mFileName,
1399 pMergedInfo->itemAt(j).mFileName) == 0)
1400 {
1401 /* match, don't add this entry */
1402 add = false;
1403 break;
1404 }
1405 }
1406
1407 if (add)
1408 pMergedInfo->add(pContents->itemAt(i));
1409 }
1410#endif
1411}
1412
Adam Lesinski16c4d152014-01-24 13:27:13 -08001413/*
1414 * ===========================================================================
1415 * AssetManager::SharedZip
1416 * ===========================================================================
1417 */
1418
1419
1420Mutex AssetManager::SharedZip::gLock;
1421DefaultKeyedVector<String8, wp<AssetManager::SharedZip> > AssetManager::SharedZip::gOpen;
1422
1423AssetManager::SharedZip::SharedZip(const String8& path, time_t modWhen)
1424 : mPath(path), mZipFile(NULL), mModWhen(modWhen),
1425 mResourceTableAsset(NULL), mResourceTable(NULL)
1426{
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001427 if (kIsDebug) {
Tomasz Wasilczyk835dfe52023-08-17 16:27:22 +00001428 ALOGI("Creating SharedZip %p %s\n", this, mPath.c_str());
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001429 }
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +00001430 ALOGV("+++ opening zip '%s'\n", mPath.c_str());
1431 mZipFile = ZipFileRO::open(mPath.c_str());
Narayan Kamath560566d2013-12-03 13:16:03 +00001432 if (mZipFile == NULL) {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +00001433 ALOGD("failed to open Zip archive '%s'\n", mPath.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -08001434 }
1435}
1436
Dianne Hackbornca3872c2017-10-30 14:19:32 -07001437AssetManager::SharedZip::SharedZip(int fd, const String8& path)
1438 : mPath(path), mZipFile(NULL), mModWhen(0),
1439 mResourceTableAsset(NULL), mResourceTable(NULL)
1440{
1441 if (kIsDebug) {
Tomasz Wasilczyk835dfe52023-08-17 16:27:22 +00001442 ALOGI("Creating SharedZip %p fd=%d %s\n", this, fd, mPath.c_str());
Dianne Hackbornca3872c2017-10-30 14:19:32 -07001443 }
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +00001444 ALOGV("+++ opening zip fd=%d '%s'\n", fd, mPath.c_str());
1445 mZipFile = ZipFileRO::openFd(fd, mPath.c_str());
Dianne Hackbornca3872c2017-10-30 14:19:32 -07001446 if (mZipFile == NULL) {
1447 ::close(fd);
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +00001448 ALOGD("failed to open Zip archive fd=%d '%s'\n", fd, mPath.c_str());
Dianne Hackbornca3872c2017-10-30 14:19:32 -07001449 }
1450}
1451
Mårten Kongstad48d22322014-01-31 14:43:27 +01001452sp<AssetManager::SharedZip> AssetManager::SharedZip::get(const String8& path,
1453 bool createIfNotPresent)
Adam Lesinski16c4d152014-01-24 13:27:13 -08001454{
1455 AutoMutex _l(gLock);
Tomasz Wasilczyk835dfe52023-08-17 16:27:22 +00001456 time_t modWhen = getFileModDate(path.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -08001457 sp<SharedZip> zip = gOpen.valueFor(path).promote();
1458 if (zip != NULL && zip->mModWhen == modWhen) {
1459 return zip;
1460 }
Mårten Kongstad48d22322014-01-31 14:43:27 +01001461 if (zip == NULL && !createIfNotPresent) {
1462 return NULL;
1463 }
Adam Lesinski16c4d152014-01-24 13:27:13 -08001464 zip = new SharedZip(path, modWhen);
1465 gOpen.add(path, zip);
1466 return zip;
Dianne Hackbornca3872c2017-10-30 14:19:32 -07001467}
Adam Lesinski16c4d152014-01-24 13:27:13 -08001468
Dianne Hackbornca3872c2017-10-30 14:19:32 -07001469sp<AssetManager::SharedZip> AssetManager::SharedZip::create(int fd, const String8& path)
1470{
1471 return new SharedZip(fd, path);
Adam Lesinski16c4d152014-01-24 13:27:13 -08001472}
1473
1474ZipFileRO* AssetManager::SharedZip::getZip()
1475{
1476 return mZipFile;
1477}
1478
1479Asset* AssetManager::SharedZip::getResourceTableAsset()
1480{
songjinshi49921f22016-09-08 15:24:30 +08001481 AutoMutex _l(gLock);
Adam Lesinski16c4d152014-01-24 13:27:13 -08001482 ALOGV("Getting from SharedZip %p resource asset %p\n", this, mResourceTableAsset);
1483 return mResourceTableAsset;
1484}
1485
1486Asset* AssetManager::SharedZip::setResourceTableAsset(Asset* asset)
1487{
1488 {
1489 AutoMutex _l(gLock);
1490 if (mResourceTableAsset == NULL) {
Adam Lesinski16c4d152014-01-24 13:27:13 -08001491 // This is not thread safe the first time it is called, so
1492 // do it here with the global lock held.
1493 asset->getBuffer(true);
songjinshi49921f22016-09-08 15:24:30 +08001494 mResourceTableAsset = asset;
Adam Lesinski16c4d152014-01-24 13:27:13 -08001495 return asset;
1496 }
1497 }
1498 delete asset;
1499 return mResourceTableAsset;
1500}
1501
1502ResTable* AssetManager::SharedZip::getResourceTable()
1503{
1504 ALOGV("Getting from SharedZip %p resource table %p\n", this, mResourceTable);
1505 return mResourceTable;
1506}
1507
1508ResTable* AssetManager::SharedZip::setResourceTable(ResTable* res)
1509{
1510 {
1511 AutoMutex _l(gLock);
1512 if (mResourceTable == NULL) {
1513 mResourceTable = res;
1514 return res;
1515 }
1516 }
1517 delete res;
1518 return mResourceTable;
1519}
1520
1521bool AssetManager::SharedZip::isUpToDate()
1522{
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +00001523 time_t modWhen = getFileModDate(mPath.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -08001524 return mModWhen == modWhen;
1525}
1526
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +09001527void AssetManager::SharedZip::addOverlay(const asset_path& ap)
1528{
1529 mOverlays.add(ap);
1530}
1531
1532bool AssetManager::SharedZip::getOverlay(size_t idx, asset_path* out) const
1533{
1534 if (idx >= mOverlays.size()) {
1535 return false;
1536 }
1537 *out = mOverlays[idx];
1538 return true;
1539}
1540
Adam Lesinski16c4d152014-01-24 13:27:13 -08001541AssetManager::SharedZip::~SharedZip()
1542{
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001543 if (kIsDebug) {
Tomasz Wasilczyk835dfe52023-08-17 16:27:22 +00001544 ALOGI("Destroying SharedZip %p %s\n", this, mPath.c_str());
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001545 }
Adam Lesinski16c4d152014-01-24 13:27:13 -08001546 if (mResourceTable != NULL) {
1547 delete mResourceTable;
1548 }
1549 if (mResourceTableAsset != NULL) {
1550 delete mResourceTableAsset;
1551 }
1552 if (mZipFile != NULL) {
1553 delete mZipFile;
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +00001554 ALOGV("Closed '%s'\n", mPath.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -08001555 }
1556}
1557
1558/*
1559 * ===========================================================================
1560 * AssetManager::ZipSet
1561 * ===========================================================================
1562 */
1563
1564/*
Adam Lesinski16c4d152014-01-24 13:27:13 -08001565 * Destructor. Close any open archives.
1566 */
1567AssetManager::ZipSet::~ZipSet(void)
1568{
1569 size_t N = mZipFile.size();
1570 for (size_t i = 0; i < N; i++)
1571 closeZip(i);
1572}
1573
1574/*
1575 * Close a Zip file and reset the entry.
1576 */
1577void AssetManager::ZipSet::closeZip(int idx)
1578{
1579 mZipFile.editItemAt(idx) = NULL;
1580}
1581
Adam Lesinski16c4d152014-01-24 13:27:13 -08001582/*
1583 * Retrieve the appropriate Zip file from the set.
1584 */
1585ZipFileRO* AssetManager::ZipSet::getZip(const String8& path)
1586{
Dianne Hackbornca3872c2017-10-30 14:19:32 -07001587 return getSharedZip(path)->getZip();
1588}
1589
1590const sp<AssetManager::SharedZip> AssetManager::ZipSet::getSharedZip(const String8& path)
1591{
Adam Lesinski16c4d152014-01-24 13:27:13 -08001592 int idx = getIndex(path);
1593 sp<SharedZip> zip = mZipFile[idx];
1594 if (zip == NULL) {
1595 zip = SharedZip::get(path);
1596 mZipFile.editItemAt(idx) = zip;
1597 }
Dianne Hackbornca3872c2017-10-30 14:19:32 -07001598 return zip;
Adam Lesinski16c4d152014-01-24 13:27:13 -08001599}
1600
1601Asset* AssetManager::ZipSet::getZipResourceTableAsset(const String8& path)
1602{
1603 int idx = getIndex(path);
1604 sp<SharedZip> zip = mZipFile[idx];
1605 if (zip == NULL) {
1606 zip = SharedZip::get(path);
1607 mZipFile.editItemAt(idx) = zip;
1608 }
1609 return zip->getResourceTableAsset();
1610}
1611
1612Asset* AssetManager::ZipSet::setZipResourceTableAsset(const String8& path,
1613 Asset* asset)
1614{
1615 int idx = getIndex(path);
1616 sp<SharedZip> zip = mZipFile[idx];
1617 // doesn't make sense to call before previously accessing.
1618 return zip->setResourceTableAsset(asset);
1619}
1620
1621ResTable* AssetManager::ZipSet::getZipResourceTable(const String8& path)
1622{
1623 int idx = getIndex(path);
1624 sp<SharedZip> zip = mZipFile[idx];
1625 if (zip == NULL) {
1626 zip = SharedZip::get(path);
1627 mZipFile.editItemAt(idx) = zip;
1628 }
1629 return zip->getResourceTable();
1630}
1631
1632ResTable* AssetManager::ZipSet::setZipResourceTable(const String8& path,
1633 ResTable* res)
1634{
1635 int idx = getIndex(path);
1636 sp<SharedZip> zip = mZipFile[idx];
1637 // doesn't make sense to call before previously accessing.
1638 return zip->setResourceTable(res);
1639}
1640
1641/*
1642 * Generate the partial pathname for the specified archive. The caller
1643 * gets to prepend the asset root directory.
1644 *
1645 * Returns something like "common/en-US-noogle.jar".
1646 */
1647/*static*/ String8 AssetManager::ZipSet::getPathName(const char* zipPath)
1648{
1649 return String8(zipPath);
1650}
1651
1652bool AssetManager::ZipSet::isUpToDate()
1653{
1654 const size_t N = mZipFile.size();
1655 for (size_t i=0; i<N; i++) {
1656 if (mZipFile[i] != NULL && !mZipFile[i]->isUpToDate()) {
1657 return false;
1658 }
1659 }
1660 return true;
1661}
1662
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +09001663void AssetManager::ZipSet::addOverlay(const String8& path, const asset_path& overlay)
1664{
1665 int idx = getIndex(path);
1666 sp<SharedZip> zip = mZipFile[idx];
1667 zip->addOverlay(overlay);
1668}
1669
1670bool AssetManager::ZipSet::getOverlay(const String8& path, size_t idx, asset_path* out) const
1671{
1672 sp<SharedZip> zip = SharedZip::get(path, false);
1673 if (zip == NULL) {
1674 return false;
1675 }
1676 return zip->getOverlay(idx, out);
1677}
1678
Adam Lesinski16c4d152014-01-24 13:27:13 -08001679/*
1680 * Compute the zip file's index.
1681 *
1682 * "appName", "locale", and "vendor" should be set to NULL to indicate the
1683 * default directory.
1684 */
1685int AssetManager::ZipSet::getIndex(const String8& zip) const
1686{
1687 const size_t N = mZipPath.size();
1688 for (size_t i=0; i<N; i++) {
1689 if (mZipPath[i] == zip) {
1690 return i;
1691 }
1692 }
1693
1694 mZipPath.add(zip);
1695 mZipFile.add(NULL);
1696
1697 return mZipPath.size()-1;
1698}