blob: f4a77ec6c4682d21bd648fe45cb0287d2983edf6 [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";
Adnan Begovic14e307c12015-07-06 20:06:36 -070066static const char* kOmniRomAssets = "framework/omnirom-res.apk";
Mårten Kongstad48d22322014-01-31 14:43:27 +010067static const char* kResourceCache = "resource-cache";
Adam Lesinski16c4d152014-01-24 13:27:13 -080068
69static const char* kExcludeExtension = ".EXCLUDE";
70
71static Asset* const kExcludedAsset = (Asset*) 0xd000000d;
72
73static volatile int32_t gCount = 0;
74
Mårten Kongstad65a05fd2014-01-31 14:01:52 +010075const char* AssetManager::RESOURCES_FILENAME = "resources.arsc";
Mårten Kongstad48d22322014-01-31 14:43:27 +010076const char* AssetManager::IDMAP_BIN = "/system/bin/idmap";
Mårten Kongstad06a1ac82018-09-20 13:09:47 +020077const char* AssetManager::VENDOR_OVERLAY_DIR = "/vendor/overlay";
Jaekyun Seok1713d9e2018-01-12 21:47:26 +090078const char* AssetManager::PRODUCT_OVERLAY_DIR = "/product/overlay";
Jeongik Cha3e725f22019-07-09 23:58:01 +090079const char* AssetManager::SYSTEM_EXT_OVERLAY_DIR = "/system_ext/overlay";
Mårten Kongstad48c24cf2019-02-25 10:54:09 +010080const char* AssetManager::ODM_OVERLAY_DIR = "/odm/overlay";
Mårten Kongstadeb8a5c02019-02-25 14:18:17 +010081const char* AssetManager::OEM_OVERLAY_DIR = "/oem/overlay";
Jakub Adamek54dcaab2016-10-19 11:46:13 +010082const char* AssetManager::OVERLAY_THEME_DIR_PROPERTY = "ro.boot.vendor.overlay.theme";
Mårten Kongstad48d22322014-01-31 14:43:27 +010083const char* AssetManager::TARGET_PACKAGE_NAME = "android";
84const char* AssetManager::TARGET_APK_PATH = "/system/framework/framework-res.apk";
85const char* AssetManager::IDMAP_DIR = "/data/resource-cache";
Mårten Kongstad65a05fd2014-01-31 14:01:52 +010086
Adam Lesinski16c4d152014-01-24 13:27:13 -080087namespace {
Adam Lesinski16c4d152014-01-24 13:27:13 -080088
Adam Lesinskia77685f2016-10-03 16:26:28 -070089String8 idmapPathForPackagePath(const String8& pkgPath) {
90 const char* root = getenv("ANDROID_DATA");
91 LOG_ALWAYS_FATAL_IF(root == NULL, "ANDROID_DATA not set");
92 String8 path(root);
Tomasz Wasilczyk804e8192023-08-23 02:22:53 +000093 appendPath(path, kResourceCache);
Adam Lesinski16c4d152014-01-24 13:27:13 -080094
Adam Lesinskia77685f2016-10-03 16:26:28 -070095 char buf[256]; // 256 chars should be enough for anyone...
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +000096 strncpy(buf, pkgPath.c_str(), 255);
Adam Lesinskia77685f2016-10-03 16:26:28 -070097 buf[255] = '\0';
98 char* filename = buf;
99 while (*filename && *filename == '/') {
100 ++filename;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800101 }
Adam Lesinskia77685f2016-10-03 16:26:28 -0700102 char* p = filename;
103 while (*p) {
104 if (*p == '/') {
105 *p = '@';
106 }
107 ++p;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800108 }
Tomasz Wasilczyk804e8192023-08-23 02:22:53 +0000109 appendPath(path, filename);
Adam Lesinskia77685f2016-10-03 16:26:28 -0700110 path.append("@idmap");
111
112 return path;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800113}
114
115/*
Adam Lesinskia77685f2016-10-03 16:26:28 -0700116 * Like strdup(), but uses C++ "new" operator instead of malloc.
117 */
118static char* strdupNew(const char* str) {
119 char* newStr;
120 int len;
121
122 if (str == NULL)
123 return NULL;
124
125 len = strlen(str);
126 newStr = new char[len+1];
127 memcpy(newStr, str, len+1);
128
129 return newStr;
130}
131
132} // namespace
133
134/*
Adam Lesinski16c4d152014-01-24 13:27:13 -0800135 * ===========================================================================
136 * AssetManager
137 * ===========================================================================
138 */
139
Adam Lesinskia77685f2016-10-03 16:26:28 -0700140int32_t AssetManager::getGlobalCount() {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800141 return gCount;
142}
143
Adam Lesinskia77685f2016-10-03 16:26:28 -0700144AssetManager::AssetManager() :
145 mLocale(NULL), mResources(NULL), mConfig(new ResTable_config) {
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700146 int count = android_atomic_inc(&gCount) + 1;
147 if (kIsDebug) {
148 ALOGI("Creating AssetManager %p #%d\n", this, count);
149 }
Adam Lesinski16c4d152014-01-24 13:27:13 -0800150 memset(mConfig, 0, sizeof(ResTable_config));
151}
152
Adam Lesinskia77685f2016-10-03 16:26:28 -0700153AssetManager::~AssetManager() {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800154 int count = android_atomic_dec(&gCount);
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700155 if (kIsDebug) {
156 ALOGI("Destroying AssetManager in %p #%d\n", this, count);
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000157 } else {
158 ALOGV("Destroying AssetManager in %p #%d\n", this, count);
Andreas Gampe2204f0b2014-10-21 23:04:54 -0700159 }
Adam Lesinski16c4d152014-01-24 13:27:13 -0800160
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700161 // Manually close any fd paths for which we have not yet opened their zip (which
162 // will take ownership of the fd and close it when done).
163 for (size_t i=0; i<mAssetPaths.size(); i++) {
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000164 ALOGV("Cleaning path #%d: fd=%d, zip=%p", (int)i, mAssetPaths[i].rawFd,
165 mAssetPaths[i].zip.get());
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700166 if (mAssetPaths[i].rawFd >= 0 && mAssetPaths[i].zip == NULL) {
167 close(mAssetPaths[i].rawFd);
168 }
169 }
170
Adam Lesinski16c4d152014-01-24 13:27:13 -0800171 delete mConfig;
172 delete mResources;
173
174 // don't have a String class yet, so make sure we clean up
175 delete[] mLocale;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800176}
177
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800178bool AssetManager::addAssetPath(
Adam Lesinskia77685f2016-10-03 16:26:28 -0700179 const String8& path, int32_t* cookie, bool appAsLib, bool isSystemAsset) {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800180 AutoMutex _l(mLock);
181
182 asset_path ap;
183
184 String8 realPath(path);
185 if (kAppZipName) {
Tomasz Wasilczyk804e8192023-08-23 02:22:53 +0000186 appendPath(realPath, kAppZipName);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800187 }
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000188 ap.type = ::getFileType(realPath.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800189 if (ap.type == kFileTypeRegular) {
190 ap.path = realPath;
191 } else {
192 ap.path = path;
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000193 ap.type = ::getFileType(path.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800194 if (ap.type != kFileTypeDirectory && ap.type != kFileTypeRegular) {
195 ALOGW("Asset path %s is neither a directory nor file (type=%d).",
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000196 path.c_str(), (int)ap.type);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800197 return false;
198 }
199 }
200
201 // Skip if we have it already.
202 for (size_t i=0; i<mAssetPaths.size(); i++) {
203 if (mAssetPaths[i].path == ap.path) {
204 if (cookie) {
Narayan Kamatha0c62602014-01-24 13:51:51 +0000205 *cookie = static_cast<int32_t>(i+1);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800206 }
207 return true;
208 }
209 }
210
211 ALOGV("In %p Asset %s path: %s", this,
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000212 ap.type == kFileTypeDirectory ? "dir" : "zip", ap.path.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800213
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800214 ap.isSystemAsset = isSystemAsset;
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000215 ssize_t apPos = mAssetPaths.add(ap);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800216
217 // new paths are always added at the end
218 if (cookie) {
Narayan Kamatha0c62602014-01-24 13:51:51 +0000219 *cookie = static_cast<int32_t>(mAssetPaths.size());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800220 }
221
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +0900222#ifdef __ANDROID__
223 // Load overlays, if any
224 asset_path oap;
225 for (size_t idx = 0; mZipSet.getOverlay(ap.path, idx, &oap); idx++) {
226 oap.isSystemAsset = isSystemAsset;
227 mAssetPaths.add(oap);
228 }
229#endif
230
Martin Kosiba7df36252014-01-16 16:25:56 +0000231 if (mResources != NULL) {
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000232 appendPathToResTable(mAssetPaths.editItemAt(apPos), appAsLib);
Martin Kosiba7df36252014-01-16 16:25:56 +0000233 }
234
Adam Lesinski16c4d152014-01-24 13:27:13 -0800235 return true;
236}
237
Mårten Kongstad48d22322014-01-31 14:43:27 +0100238bool AssetManager::addOverlayPath(const String8& packagePath, int32_t* cookie)
239{
240 const String8 idmapPath = idmapPathForPackagePath(packagePath);
241
242 AutoMutex _l(mLock);
243
244 for (size_t i = 0; i < mAssetPaths.size(); ++i) {
245 if (mAssetPaths[i].idmap == idmapPath) {
246 *cookie = static_cast<int32_t>(i + 1);
247 return true;
248 }
249 }
250
251 Asset* idmap = NULL;
252 if ((idmap = openAssetFromFileLocked(idmapPath, Asset::ACCESS_BUFFER)) == NULL) {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000253 ALOGW("failed to open idmap file %s\n", idmapPath.c_str());
Mårten Kongstad48d22322014-01-31 14:43:27 +0100254 return false;
255 }
256
257 String8 targetPath;
258 String8 overlayPath;
259 if (!ResTable::getIdmapInfo(idmap->getBuffer(false), idmap->getLength(),
Adam Lesinskif90f2f8d2014-06-06 14:27:00 -0700260 NULL, NULL, NULL, &targetPath, &overlayPath)) {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000261 ALOGW("failed to read idmap file %s\n", idmapPath.c_str());
Mårten Kongstad48d22322014-01-31 14:43:27 +0100262 delete idmap;
263 return false;
264 }
265 delete idmap;
266
267 if (overlayPath != packagePath) {
268 ALOGW("idmap file %s inconcistent: expected path %s does not match actual path %s\n",
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000269 idmapPath.c_str(), packagePath.c_str(), overlayPath.c_str());
Mårten Kongstad48d22322014-01-31 14:43:27 +0100270 return false;
271 }
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000272 if (access(targetPath.c_str(), R_OK) != 0) {
273 ALOGW("failed to access file %s: %s\n", targetPath.c_str(), strerror(errno));
Mårten Kongstad48d22322014-01-31 14:43:27 +0100274 return false;
275 }
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000276 if (access(idmapPath.c_str(), R_OK) != 0) {
277 ALOGW("failed to access file %s: %s\n", idmapPath.c_str(), strerror(errno));
Mårten Kongstad48d22322014-01-31 14:43:27 +0100278 return false;
279 }
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000280 if (access(overlayPath.c_str(), R_OK) != 0) {
281 ALOGW("failed to access file %s: %s\n", overlayPath.c_str(), strerror(errno));
Mårten Kongstad48d22322014-01-31 14:43:27 +0100282 return false;
283 }
284
285 asset_path oap;
286 oap.path = overlayPath;
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000287 oap.type = ::getFileType(overlayPath.c_str());
Mårten Kongstad48d22322014-01-31 14:43:27 +0100288 oap.idmap = idmapPath;
289#if 0
290 ALOGD("Overlay added: targetPath=%s overlayPath=%s idmapPath=%s\n",
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000291 targetPath.c_str(), overlayPath.c_str(), idmapPath.c_str());
Mårten Kongstad48d22322014-01-31 14:43:27 +0100292#endif
293 mAssetPaths.add(oap);
294 *cookie = static_cast<int32_t>(mAssetPaths.size());
295
Mårten Kongstad30113132014-11-07 10:52:17 +0100296 if (mResources != NULL) {
297 appendPathToResTable(oap);
298 }
299
Mårten Kongstad48d22322014-01-31 14:43:27 +0100300 return true;
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700301}
302
303bool AssetManager::addAssetFd(
304 int fd, const String8& debugPathName, int32_t* cookie, bool appAsLib,
305 bool assume_ownership) {
306 AutoMutex _l(mLock);
307
308 asset_path ap;
309
310 ap.path = debugPathName;
311 ap.rawFd = fd;
312 ap.type = kFileTypeRegular;
313 ap.assumeOwnership = assume_ownership;
314
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000315 ALOGV("In %p Asset fd %d name: %s", this, fd, ap.path.c_str());
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700316
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000317 ssize_t apPos = mAssetPaths.add(ap);
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700318
319 // new paths are always added at the end
320 if (cookie) {
321 *cookie = static_cast<int32_t>(mAssetPaths.size());
322 }
323
324 if (mResources != NULL) {
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000325 appendPathToResTable(mAssetPaths.editItemAt(apPos), appAsLib);
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700326 }
327
328 return true;
329}
Mårten Kongstad48d22322014-01-31 14:43:27 +0100330
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100331bool AssetManager::createIdmap(const char* targetApkPath, const char* overlayApkPath,
Dianne Hackborn32bb5fa2014-02-11 13:56:21 -0800332 uint32_t targetCrc, uint32_t overlayCrc, uint32_t** outData, size_t* outSize)
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100333{
334 AutoMutex _l(mLock);
335 const String8 paths[2] = { String8(targetApkPath), String8(overlayApkPath) };
Mårten Kongstad6bb13da2016-06-02 09:34:36 +0200336 Asset* assets[2] = {NULL, NULL};
337 bool ret = false;
338 {
339 ResTable tables[2];
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100340
Mårten Kongstad6bb13da2016-06-02 09:34:36 +0200341 for (int i = 0; i < 2; ++i) {
342 asset_path ap;
343 ap.type = kFileTypeRegular;
344 ap.path = paths[i];
345 assets[i] = openNonAssetInPathLocked("resources.arsc",
346 Asset::ACCESS_BUFFER, ap);
347 if (assets[i] == NULL) {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000348 ALOGW("failed to find resources.arsc in %s\n", ap.path.c_str());
Mårten Kongstad6bb13da2016-06-02 09:34:36 +0200349 goto exit;
350 }
351 if (tables[i].add(assets[i]) != NO_ERROR) {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000352 ALOGW("failed to add %s to resource table", paths[i].c_str());
Mårten Kongstad6bb13da2016-06-02 09:34:36 +0200353 goto exit;
354 }
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100355 }
Mårten Kongstad67d5c932018-05-25 15:58:17 +0200356 ret = tables[1].createIdmap(tables[0], targetCrc, overlayCrc,
Mårten Kongstad6bb13da2016-06-02 09:34:36 +0200357 targetApkPath, overlayApkPath, (void**)outData, outSize) == NO_ERROR;
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100358 }
359
Mårten Kongstad6bb13da2016-06-02 09:34:36 +0200360exit:
361 delete assets[0];
362 delete assets[1];
363 return ret;
Mårten Kongstad65a05fd2014-01-31 14:01:52 +0100364}
365
Adam Lesinski16c4d152014-01-24 13:27:13 -0800366bool AssetManager::addDefaultAssets()
367{
368 const char* root = getenv("ANDROID_ROOT");
369 LOG_ALWAYS_FATAL_IF(root == NULL, "ANDROID_ROOT not set");
370
Adnan Begovic14e307c12015-07-06 20:06:36 -0700371 bool success = true;
372 {
373 String8 path(root);
374 appendPath(path, kSystemAssets);
375 success &= addAssetPath(path, NULL, false /* appAsLib */, true /* isSystemAsset */);
376 }
377 {
378 String8 path(root);
379 appendPath(path, kOmniRomAssets);
380 success &= addAssetPath(path, NULL, false /* appAsLib */, true /* isSystemAsset */);
381 }
Adam Lesinski16c4d152014-01-24 13:27:13 -0800382
Adnan Begovic14e307c12015-07-06 20:06:36 -0700383 return success;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800384}
385
Narayan Kamatha0c62602014-01-24 13:51:51 +0000386int32_t AssetManager::nextAssetPath(const int32_t cookie) const
Adam Lesinski16c4d152014-01-24 13:27:13 -0800387{
388 AutoMutex _l(mLock);
Narayan Kamatha0c62602014-01-24 13:51:51 +0000389 const size_t next = static_cast<size_t>(cookie) + 1;
390 return next > mAssetPaths.size() ? -1 : next;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800391}
392
Narayan Kamatha0c62602014-01-24 13:51:51 +0000393String8 AssetManager::getAssetPath(const int32_t cookie) const
Adam Lesinski16c4d152014-01-24 13:27:13 -0800394{
395 AutoMutex _l(mLock);
Narayan Kamatha0c62602014-01-24 13:51:51 +0000396 const size_t which = static_cast<size_t>(cookie) - 1;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800397 if (which < mAssetPaths.size()) {
398 return mAssetPaths[which].path;
399 }
400 return String8();
401}
402
Adam Lesinski16c4d152014-01-24 13:27:13 -0800403void AssetManager::setLocaleLocked(const char* locale)
404{
405 if (mLocale != NULL) {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800406 delete[] mLocale;
407 }
Elliott Hughesc367d482013-10-29 13:12:55 -0700408
Adam Lesinski16c4d152014-01-24 13:27:13 -0800409 mLocale = strdupNew(locale);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800410 updateResourceParamsLocked();
411}
412
Adam Lesinski16c4d152014-01-24 13:27:13 -0800413void AssetManager::setConfiguration(const ResTable_config& config, const char* locale)
414{
415 AutoMutex _l(mLock);
416 *mConfig = config;
417 if (locale) {
418 setLocaleLocked(locale);
419 } else if (config.language[0] != 0) {
Narayan Kamath91447d82014-01-21 15:32:36 +0000420 char spec[RESTABLE_MAX_LOCALE_LEN];
421 config.getBcp47Locale(spec);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800422 setLocaleLocked(spec);
423 } else {
424 updateResourceParamsLocked();
425 }
426}
427
428void AssetManager::getConfiguration(ResTable_config* outConfig) const
429{
430 AutoMutex _l(mLock);
431 *outConfig = *mConfig;
432}
433
434/*
435 * Open an asset.
436 *
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700437 * The data could be in any asset path. Each asset path could be:
438 * - A directory on disk.
439 * - A Zip archive, uncompressed or compressed.
Adam Lesinski16c4d152014-01-24 13:27:13 -0800440 *
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700441 * If the file is in a directory, it could have a .gz suffix, meaning it is compressed.
Adam Lesinski16c4d152014-01-24 13:27:13 -0800442 *
443 * We should probably reject requests for "illegal" filenames, e.g. those
444 * with illegal characters or "../" backward relative paths.
445 */
446Asset* AssetManager::open(const char* fileName, AccessMode mode)
447{
448 AutoMutex _l(mLock);
449
450 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
451
Adam Lesinski16c4d152014-01-24 13:27:13 -0800452 String8 assetName(kAssetsRoot);
Tomasz Wasilczyk804e8192023-08-23 02:22:53 +0000453 appendPath(assetName, fileName);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800454
455 /*
456 * For each top-level asset path, search for the asset.
457 */
458
459 size_t i = mAssetPaths.size();
460 while (i > 0) {
461 i--;
462 ALOGV("Looking for asset '%s' in '%s'\n",
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000463 assetName.c_str(), mAssetPaths.itemAt(i).path.c_str());
464 Asset* pAsset = openNonAssetInPathLocked(assetName.c_str(), mode,
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000465 mAssetPaths.editItemAt(i));
Adam Lesinski16c4d152014-01-24 13:27:13 -0800466 if (pAsset != NULL) {
467 return pAsset != kExcludedAsset ? pAsset : NULL;
468 }
469 }
470
471 return NULL;
472}
473
474/*
475 * Open a non-asset file as if it were an asset.
476 *
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700477 * The "fileName" is the partial path starting from the application name.
Adam Lesinski16c4d152014-01-24 13:27:13 -0800478 */
Adam Lesinskide898ff2014-01-29 18:20:45 -0800479Asset* AssetManager::openNonAsset(const char* fileName, AccessMode mode, int32_t* outCookie)
Adam Lesinski16c4d152014-01-24 13:27:13 -0800480{
481 AutoMutex _l(mLock);
482
483 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
484
Adam Lesinski16c4d152014-01-24 13:27:13 -0800485 /*
486 * For each top-level asset path, search for the asset.
487 */
488
489 size_t i = mAssetPaths.size();
490 while (i > 0) {
491 i--;
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000492 ALOGV("Looking for non-asset '%s' in '%s'\n", fileName, mAssetPaths.itemAt(i).path.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800493 Asset* pAsset = openNonAssetInPathLocked(
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000494 fileName, mode, mAssetPaths.editItemAt(i));
Adam Lesinski16c4d152014-01-24 13:27:13 -0800495 if (pAsset != NULL) {
Adam Lesinskide898ff2014-01-29 18:20:45 -0800496 if (outCookie != NULL) *outCookie = static_cast<int32_t>(i + 1);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800497 return pAsset != kExcludedAsset ? pAsset : NULL;
498 }
499 }
500
501 return NULL;
502}
503
Narayan Kamatha0c62602014-01-24 13:51:51 +0000504Asset* AssetManager::openNonAsset(const int32_t cookie, const char* fileName, AccessMode mode)
Adam Lesinski16c4d152014-01-24 13:27:13 -0800505{
Narayan Kamatha0c62602014-01-24 13:51:51 +0000506 const size_t which = static_cast<size_t>(cookie) - 1;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800507
508 AutoMutex _l(mLock);
509
510 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
511
Adam Lesinski16c4d152014-01-24 13:27:13 -0800512 if (which < mAssetPaths.size()) {
513 ALOGV("Looking for non-asset '%s' in '%s'\n", fileName,
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000514 mAssetPaths.itemAt(which).path.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800515 Asset* pAsset = openNonAssetInPathLocked(
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000516 fileName, mode, mAssetPaths.editItemAt(which));
Adam Lesinski16c4d152014-01-24 13:27:13 -0800517 if (pAsset != NULL) {
518 return pAsset != kExcludedAsset ? pAsset : NULL;
519 }
520 }
521
522 return NULL;
523}
524
525/*
526 * Get the type of a file in the asset namespace.
527 *
528 * This currently only works for regular files. All others (including
529 * directories) will return kFileTypeNonexistent.
530 */
531FileType AssetManager::getFileType(const char* fileName)
532{
533 Asset* pAsset = NULL;
534
535 /*
536 * Open the asset. This is less efficient than simply finding the
537 * file, but it's not too bad (we don't uncompress or mmap data until
538 * the first read() call).
539 */
540 pAsset = open(fileName, Asset::ACCESS_STREAMING);
541 delete pAsset;
542
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700543 if (pAsset == NULL) {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800544 return kFileTypeNonexistent;
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700545 } else {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800546 return kFileTypeRegular;
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700547 }
Adam Lesinski16c4d152014-01-24 13:27:13 -0800548}
549
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000550bool AssetManager::appendPathToResTable(asset_path& ap, bool appAsLib) const {
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +0900551 // skip those ap's that correspond to system overlays
552 if (ap.isSystemOverlay) {
553 return true;
554 }
555
Martin Kosiba7df36252014-01-16 16:25:56 +0000556 Asset* ass = NULL;
557 ResTable* sharedRes = NULL;
558 bool shared = true;
559 bool onlyEmptyResources = true;
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000560 ATRACE_NAME(ap.path.c_str());
Martin Kosiba7df36252014-01-16 16:25:56 +0000561 Asset* idmap = openIdmapLocked(ap);
562 size_t nextEntryIdx = mResources->getTableCount();
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000563 ALOGV("Looking for resource asset in '%s'\n", ap.path.c_str());
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700564 if (ap.type != kFileTypeDirectory && ap.rawFd < 0) {
Martin Kosiba7df36252014-01-16 16:25:56 +0000565 if (nextEntryIdx == 0) {
566 // The first item is typically the framework resources,
567 // which we want to avoid parsing every time.
568 sharedRes = const_cast<AssetManager*>(this)->
569 mZipSet.getZipResourceTable(ap.path);
570 if (sharedRes != NULL) {
571 // skip ahead the number of system overlay packages preloaded
572 nextEntryIdx = sharedRes->getTableCount();
573 }
574 }
575 if (sharedRes == NULL) {
576 ass = const_cast<AssetManager*>(this)->
577 mZipSet.getZipResourceTableAsset(ap.path);
578 if (ass == NULL) {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000579 ALOGV("loading resource table %s\n", ap.path.c_str());
Martin Kosiba7df36252014-01-16 16:25:56 +0000580 ass = const_cast<AssetManager*>(this)->
581 openNonAssetInPathLocked("resources.arsc",
582 Asset::ACCESS_BUFFER,
583 ap);
584 if (ass != NULL && ass != kExcludedAsset) {
585 ass = const_cast<AssetManager*>(this)->
586 mZipSet.setZipResourceTableAsset(ap.path, ass);
587 }
588 }
Jeongik Cha3e725f22019-07-09 23:58:01 +0900589
Martin Kosiba7df36252014-01-16 16:25:56 +0000590 if (nextEntryIdx == 0 && ass != NULL) {
591 // If this is the first resource table in the asset
592 // manager, then we are going to cache it so that we
593 // can quickly copy it out for others.
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000594 ALOGV("Creating shared resources for %s", ap.path.c_str());
Martin Kosiba7df36252014-01-16 16:25:56 +0000595 sharedRes = new ResTable();
596 sharedRes->add(ass, idmap, nextEntryIdx + 1, false);
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +0900597#ifdef __ANDROID__
598 const char* data = getenv("ANDROID_DATA");
599 LOG_ALWAYS_FATAL_IF(data == NULL, "ANDROID_DATA not set");
600 String8 overlaysListPath(data);
Tomasz Wasilczyk804e8192023-08-23 02:22:53 +0000601 appendPath(overlaysListPath, kResourceCache);
602 appendPath(overlaysListPath, "overlays.list");
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000603 addSystemOverlays(overlaysListPath.c_str(), ap.path, sharedRes, nextEntryIdx);
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +0900604#endif
Martin Kosiba7df36252014-01-16 16:25:56 +0000605 sharedRes = const_cast<AssetManager*>(this)->
606 mZipSet.setZipResourceTable(ap.path, sharedRes);
607 }
608 }
609 } else {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000610 ALOGV("loading resource table %s\n", ap.path.c_str());
Martin Kosiba7df36252014-01-16 16:25:56 +0000611 ass = const_cast<AssetManager*>(this)->
612 openNonAssetInPathLocked("resources.arsc",
613 Asset::ACCESS_BUFFER,
614 ap);
615 shared = false;
616 }
617
618 if ((ass != NULL || sharedRes != NULL) && ass != kExcludedAsset) {
619 ALOGV("Installing resource asset %p in to table %p\n", ass, mResources);
620 if (sharedRes != NULL) {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000621 ALOGV("Copying existing resources for %s", ap.path.c_str());
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800622 mResources->add(sharedRes, ap.isSystemAsset);
Martin Kosiba7df36252014-01-16 16:25:56 +0000623 } else {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000624 ALOGV("Parsing resources for %s", ap.path.c_str());
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800625 mResources->add(ass, idmap, nextEntryIdx + 1, !shared, appAsLib, ap.isSystemAsset);
Martin Kosiba7df36252014-01-16 16:25:56 +0000626 }
627 onlyEmptyResources = false;
628
629 if (!shared) {
630 delete ass;
631 }
632 } else {
633 ALOGV("Installing empty resources in to table %p\n", mResources);
634 mResources->addEmpty(nextEntryIdx + 1);
635 }
636
637 if (idmap != NULL) {
638 delete idmap;
639 }
Martin Kosiba7df36252014-01-16 16:25:56 +0000640 return onlyEmptyResources;
641}
642
Adam Lesinski16c4d152014-01-24 13:27:13 -0800643const ResTable* AssetManager::getResTable(bool required) const
644{
645 ResTable* rt = mResources;
646 if (rt) {
647 return rt;
648 }
649
650 // Iterate through all asset packages, collecting resources from each.
651
652 AutoMutex _l(mLock);
653
654 if (mResources != NULL) {
655 return mResources;
656 }
657
658 if (required) {
659 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
660 }
661
Adam Lesinskide898ff2014-01-29 18:20:45 -0800662 mResources = new ResTable();
663 updateResourceParamsLocked();
664
665 bool onlyEmptyResources = true;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800666 const size_t N = mAssetPaths.size();
667 for (size_t i=0; i<N; i++) {
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000668 bool empty = appendPathToResTable(
669 const_cast<AssetManager*>(this)->mAssetPaths.editItemAt(i));
Martin Kosiba7df36252014-01-16 16:25:56 +0000670 onlyEmptyResources = onlyEmptyResources && empty;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800671 }
672
Adam Lesinskide898ff2014-01-29 18:20:45 -0800673 if (required && onlyEmptyResources) {
674 ALOGW("Unable to find resources file resources.arsc");
675 delete mResources;
676 mResources = NULL;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800677 }
Adam Lesinskide898ff2014-01-29 18:20:45 -0800678
679 return mResources;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800680}
681
682void AssetManager::updateResourceParamsLocked() const
683{
Adam Lesinskib7e1ce02016-04-11 20:03:01 -0700684 ATRACE_CALL();
Adam Lesinski16c4d152014-01-24 13:27:13 -0800685 ResTable* res = mResources;
686 if (!res) {
687 return;
688 }
689
Narayan Kamath91447d82014-01-21 15:32:36 +0000690 if (mLocale) {
691 mConfig->setBcp47Locale(mLocale);
692 } else {
693 mConfig->clearLocale();
Adam Lesinski16c4d152014-01-24 13:27:13 -0800694 }
Adam Lesinski16c4d152014-01-24 13:27:13 -0800695
696 res->setParameters(mConfig);
697}
698
699Asset* AssetManager::openIdmapLocked(const struct asset_path& ap) const
700{
701 Asset* ass = NULL;
702 if (ap.idmap.size() != 0) {
703 ass = const_cast<AssetManager*>(this)->
704 openAssetFromFileLocked(ap.idmap, Asset::ACCESS_BUFFER);
705 if (ass) {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000706 ALOGV("loading idmap %s\n", ap.idmap.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800707 } else {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000708 ALOGW("failed to load idmap %s\n", ap.idmap.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800709 }
710 }
711 return ass;
712}
713
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +0900714void AssetManager::addSystemOverlays(const char* pathOverlaysList,
715 const String8& targetPackagePath, ResTable* sharedRes, size_t offset) const
716{
717 FILE* fin = fopen(pathOverlaysList, "r");
718 if (fin == NULL) {
719 return;
720 }
721
722#ifndef _WIN32
723 if (TEMP_FAILURE_RETRY(flock(fileno(fin), LOCK_SH)) != 0) {
724 fclose(fin);
725 return;
726 }
727#endif
728 char buf[1024];
729 while (fgets(buf, sizeof(buf), fin)) {
730 // format of each line:
731 // <path to apk><space><path to idmap><newline>
732 char* space = strchr(buf, ' ');
733 char* newline = strchr(buf, '\n');
734 asset_path oap;
735
736 if (space == NULL || newline == NULL || newline < space) {
737 continue;
738 }
739
740 oap.path = String8(buf, space - buf);
741 oap.type = kFileTypeRegular;
742 oap.idmap = String8(space + 1, newline - space - 1);
743 oap.isSystemOverlay = true;
744
745 Asset* oass = const_cast<AssetManager*>(this)->
746 openNonAssetInPathLocked("resources.arsc",
747 Asset::ACCESS_BUFFER,
748 oap);
749
750 if (oass != NULL) {
751 Asset* oidmap = openIdmapLocked(oap);
752 offset++;
753 sharedRes->add(oass, oidmap, offset + 1, false);
754 const_cast<AssetManager*>(this)->mAssetPaths.add(oap);
755 const_cast<AssetManager*>(this)->mZipSet.addOverlay(targetPackagePath, oap);
756 delete oidmap;
757 }
758 }
759
760#ifndef _WIN32
761 TEMP_FAILURE_RETRY(flock(fileno(fin), LOCK_UN));
762#endif
763 fclose(fin);
764}
765
Adam Lesinski16c4d152014-01-24 13:27:13 -0800766const ResTable& AssetManager::getResources(bool required) const
767{
768 const ResTable* rt = getResTable(required);
769 return *rt;
770}
771
772bool AssetManager::isUpToDate()
773{
774 AutoMutex _l(mLock);
775 return mZipSet.isUpToDate();
776}
777
Roozbeh Pournader1c686f22015-12-18 14:22:14 -0800778void AssetManager::getLocales(Vector<String8>* locales, bool includeSystemLocales) const
Adam Lesinski16c4d152014-01-24 13:27:13 -0800779{
780 ResTable* res = mResources;
781 if (res != NULL) {
Roozbeh Pournader7e5f96f2016-06-13 18:10:49 -0700782 res->getLocales(locales, includeSystemLocales, true /* mergeEquivalentLangs */);
Narayan Kamathe4345db2014-06-26 16:01:28 +0100783 }
Adam Lesinski16c4d152014-01-24 13:27:13 -0800784}
785
786/*
787 * Open a non-asset file as if it were an asset, searching for it in the
788 * specified app.
789 *
790 * Pass in a NULL values for "appName" if the common app directory should
791 * be used.
792 */
793Asset* AssetManager::openNonAssetInPathLocked(const char* fileName, AccessMode mode,
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000794 asset_path& ap)
Adam Lesinski16c4d152014-01-24 13:27:13 -0800795{
796 Asset* pAsset = NULL;
797
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700798 ALOGV("openNonAssetInPath: name=%s type=%d fd=%d", fileName, ap.type, ap.rawFd);
799
Adam Lesinski16c4d152014-01-24 13:27:13 -0800800 /* look at the filesystem on disk */
801 if (ap.type == kFileTypeDirectory) {
802 String8 path(ap.path);
Tomasz Wasilczyk804e8192023-08-23 02:22:53 +0000803 appendPath(path, fileName);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800804
805 pAsset = openAssetFromFileLocked(path, mode);
806
807 if (pAsset == NULL) {
808 /* try again, this time with ".gz" */
809 path.append(".gz");
810 pAsset = openAssetFromFileLocked(path, mode);
811 }
812
813 if (pAsset != NULL) {
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700814 ALOGV("FOUND NA '%s' on disk", fileName);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800815 pAsset->setAssetSource(path);
816 }
817
818 /* look inside the zip file */
819 } else {
820 String8 path(fileName);
821
822 /* check the appropriate Zip file */
Narayan Kamath560566d2013-12-03 13:16:03 +0000823 ZipFileRO* pZip = getZipFileLocked(ap);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800824 if (pZip != NULL) {
Tomasz Wasilczyk835dfe52023-08-17 16:27:22 +0000825 ALOGV("GOT zip, checking NA '%s'", path.c_str());
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000826 ZipEntryRO entry = pZip->findEntryByName(path.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800827 if (entry != NULL) {
Tomasz Wasilczyk835dfe52023-08-17 16:27:22 +0000828 ALOGV("FOUND NA in Zip file for %s", path.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800829 pAsset = openAssetFromZipLocked(pZip, entry, mode, path);
Narayan Kamath560566d2013-12-03 13:16:03 +0000830 pZip->releaseEntry(entry);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800831 }
832 }
833
834 if (pAsset != NULL) {
835 /* create a "source" name, for debug/display */
836 pAsset->setAssetSource(
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000837 createZipSourceNameLocked(ZipSet::getPathName(ap.path.c_str()), String8(""),
Adam Lesinski16c4d152014-01-24 13:27:13 -0800838 String8(fileName)));
839 }
840 }
841
842 return pAsset;
843}
844
845/*
Adam Lesinski16c4d152014-01-24 13:27:13 -0800846 * Create a "source name" for a file from a Zip archive.
847 */
848String8 AssetManager::createZipSourceNameLocked(const String8& zipFileName,
849 const String8& dirName, const String8& fileName)
850{
851 String8 sourceName("zip:");
852 sourceName.append(zipFileName);
853 sourceName.append(":");
854 if (dirName.length() > 0) {
Tomasz Wasilczyk804e8192023-08-23 02:22:53 +0000855 appendPath(sourceName, dirName);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800856 }
Tomasz Wasilczyk804e8192023-08-23 02:22:53 +0000857 appendPath(sourceName, fileName);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800858 return sourceName;
859}
860
861/*
Adam Lesinski16c4d152014-01-24 13:27:13 -0800862 * Create a path to a loose asset (asset-base/app/rootDir).
863 */
864String8 AssetManager::createPathNameLocked(const asset_path& ap, const char* rootDir)
865{
866 String8 path(ap.path);
Tomasz Wasilczyk804e8192023-08-23 02:22:53 +0000867 if (rootDir != NULL) appendPath(path, rootDir);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800868 return path;
869}
870
871/*
872 * Return a pointer to one of our open Zip archives. Returns NULL if no
873 * matching Zip file exists.
Adam Lesinski16c4d152014-01-24 13:27:13 -0800874 */
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000875ZipFileRO* AssetManager::getZipFileLocked(asset_path& ap)
Adam Lesinski16c4d152014-01-24 13:27:13 -0800876{
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000877 ALOGV("getZipFileLocked() in %p: ap=%p zip=%p", this, &ap, ap.zip.get());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800878
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700879 if (ap.zip != NULL) {
880 return ap.zip->getZip();
881 }
882
883 if (ap.rawFd < 0) {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000884 ALOGV("getZipFileLocked: Creating new zip from path %s", ap.path.c_str());
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700885 ap.zip = mZipSet.getSharedZip(ap.path);
886 } else {
Dianne Hackborn1704e3c2017-10-31 19:55:42 +0000887 ALOGV("getZipFileLocked: Creating new zip from fd %d", ap.rawFd);
Dianne Hackbornca3872c2017-10-30 14:19:32 -0700888 ap.zip = SharedZip::create(ap.rawFd, ap.path);
889
890 }
891 return ap.zip != NULL ? ap.zip->getZip() : NULL;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800892}
893
894/*
895 * Try to open an asset from a file on disk.
896 *
897 * If the file is compressed with gzip, we seek to the start of the
898 * deflated data and pass that in (just like we would for a Zip archive).
899 *
900 * For uncompressed data, we may already have an mmap()ed version sitting
901 * around. If so, we want to hand that to the Asset instead.
902 *
903 * This returns NULL if the file doesn't exist, couldn't be opened, or
904 * claims to be a ".gz" but isn't.
905 */
906Asset* AssetManager::openAssetFromFileLocked(const String8& pathName,
907 AccessMode mode)
908{
909 Asset* pAsset = NULL;
910
Tomasz Wasilczyk804e8192023-08-23 02:22:53 +0000911 if (strcasecmp(getPathExtension(pathName).c_str(), ".gz") == 0) {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800912 //printf("TRYING '%s'\n", (const char*) pathName);
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000913 pAsset = Asset::createFromCompressedFile(pathName.c_str(), mode);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800914 } else {
915 //printf("TRYING '%s'\n", (const char*) pathName);
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000916 pAsset = Asset::createFromFile(pathName.c_str(), mode);
Adam Lesinski16c4d152014-01-24 13:27:13 -0800917 }
918
919 return pAsset;
920}
921
922/*
923 * Given an entry in a Zip archive, create a new Asset object.
924 *
925 * If the entry is uncompressed, we may want to create or share a
926 * slice of shared memory.
927 */
928Asset* AssetManager::openAssetFromZipLocked(const ZipFileRO* pZipFile,
929 const ZipEntryRO entry, AccessMode mode, const String8& entryName)
930{
Ryan Mitchell80094e32020-11-16 23:08:18 +0000931 std::unique_ptr<Asset> pAsset;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800932
933 // TODO: look for previously-created shared memory slice?
Narayan Kamath407753c2015-06-16 12:02:57 +0100934 uint16_t method;
935 uint32_t uncompressedLen;
Adam Lesinski16c4d152014-01-24 13:27:13 -0800936
937 //printf("USING Zip '%s'\n", pEntry->getFileName());
938
Pawan Waghfbe2bd32024-06-03 21:39:07 +0000939 if (!pZipFile->getEntryInfo(entry, &method, &uncompressedLen, nullptr, nullptr,
940 nullptr, nullptr, nullptr))
Adam Lesinski16c4d152014-01-24 13:27:13 -0800941 {
942 ALOGW("getEntryInfo failed\n");
943 return NULL;
944 }
945
Ryan Mitchell80094e32020-11-16 23:08:18 +0000946 std::optional<incfs::IncFsFileMap> dataMap = pZipFile->createEntryIncFsFileMap(entry);
947 if (!dataMap.has_value()) {
Adam Lesinski16c4d152014-01-24 13:27:13 -0800948 ALOGW("create map from entry failed\n");
949 return NULL;
950 }
951
952 if (method == ZipFileRO::kCompressStored) {
Ryan Mitchell80094e32020-11-16 23:08:18 +0000953 pAsset = Asset::createFromUncompressedMap(std::move(*dataMap), mode);
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000954 ALOGV("Opened uncompressed entry %s in zip %s mode %d: %p", entryName.c_str(),
Ryan Mitchell80094e32020-11-16 23:08:18 +0000955 dataMap->file_name(), mode, pAsset.get());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800956 } else {
Ryan Mitchell80094e32020-11-16 23:08:18 +0000957 pAsset = Asset::createFromCompressedMap(std::move(*dataMap),
Narayan Kamath407753c2015-06-16 12:02:57 +0100958 static_cast<size_t>(uncompressedLen), mode);
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +0000959 ALOGV("Opened compressed entry %s in zip %s mode %d: %p", entryName.c_str(),
Ryan Mitchell80094e32020-11-16 23:08:18 +0000960 dataMap->file_name(), mode, pAsset.get());
Adam Lesinski16c4d152014-01-24 13:27:13 -0800961 }
962 if (pAsset == NULL) {
963 /* unexpected */
964 ALOGW("create from segment failed\n");
965 }
966
Ryan Mitchell80094e32020-11-16 23:08:18 +0000967 return pAsset.release();
Adam Lesinski16c4d152014-01-24 13:27:13 -0800968}
969
Adam Lesinski16c4d152014-01-24 13:27:13 -0800970/*
971 * Open a directory in the asset namespace.
972 *
Adam Lesinskife90eaf2016-10-04 13:31:31 -0700973 * An "asset directory" is simply the combination of all asset paths' "assets/" directories.
Adam Lesinski16c4d152014-01-24 13:27:13 -0800974 *
975 * Pass in "" for the root dir.
976 */
977AssetDir* AssetManager::openDir(const char* dirName)
978{
979 AutoMutex _l(mLock);
980
981 AssetDir* pDir = NULL;
982 SortedVector<AssetDir::FileInfo>* pMergedInfo = NULL;
983
984 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
985 assert(dirName != NULL);
986
987 //printf("+++ openDir(%s) in '%s'\n", dirName, (const char*) mAssetBase);
988
Adam Lesinski16c4d152014-01-24 13:27:13 -0800989 pDir = new AssetDir;
990
991 /*
992 * Scan the various directories, merging what we find into a single
993 * vector. We want to scan them in reverse priority order so that
994 * the ".EXCLUDE" processing works correctly. Also, if we decide we
995 * want to remember where the file is coming from, we'll get the right
996 * version.
997 *
998 * We start with Zip archives, then do loose files.
999 */
1000 pMergedInfo = new SortedVector<AssetDir::FileInfo>;
1001
1002 size_t i = mAssetPaths.size();
1003 while (i > 0) {
1004 i--;
1005 const asset_path& ap = mAssetPaths.itemAt(i);
1006 if (ap.type == kFileTypeRegular) {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +00001007 ALOGV("Adding directory %s from zip %s", dirName, ap.path.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -08001008 scanAndMergeZipLocked(pMergedInfo, ap, kAssetsRoot, dirName);
1009 } else {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +00001010 ALOGV("Adding directory %s from dir %s", dirName, ap.path.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -08001011 scanAndMergeDirLocked(pMergedInfo, ap, kAssetsRoot, dirName);
1012 }
1013 }
1014
1015#if 0
1016 printf("FILE LIST:\n");
1017 for (i = 0; i < (size_t) pMergedInfo->size(); i++) {
1018 printf(" %d: (%d) '%s'\n", i,
1019 pMergedInfo->itemAt(i).getFileType(),
1020 (const char*) pMergedInfo->itemAt(i).getFileName());
1021 }
1022#endif
1023
1024 pDir->setFileList(pMergedInfo);
1025 return pDir;
1026}
1027
1028/*
1029 * Open a directory in the non-asset namespace.
1030 *
Adam Lesinskife90eaf2016-10-04 13:31:31 -07001031 * An "asset directory" is simply the combination of all asset paths' "assets/" directories.
Adam Lesinski16c4d152014-01-24 13:27:13 -08001032 *
1033 * Pass in "" for the root dir.
1034 */
Narayan Kamatha0c62602014-01-24 13:51:51 +00001035AssetDir* AssetManager::openNonAssetDir(const int32_t cookie, const char* dirName)
Adam Lesinski16c4d152014-01-24 13:27:13 -08001036{
1037 AutoMutex _l(mLock);
1038
1039 AssetDir* pDir = NULL;
1040 SortedVector<AssetDir::FileInfo>* pMergedInfo = NULL;
1041
1042 LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager");
1043 assert(dirName != NULL);
1044
1045 //printf("+++ openDir(%s) in '%s'\n", dirName, (const char*) mAssetBase);
1046
Adam Lesinski16c4d152014-01-24 13:27:13 -08001047 pDir = new AssetDir;
1048
1049 pMergedInfo = new SortedVector<AssetDir::FileInfo>;
1050
Narayan Kamatha0c62602014-01-24 13:51:51 +00001051 const size_t which = static_cast<size_t>(cookie) - 1;
Adam Lesinski16c4d152014-01-24 13:27:13 -08001052
1053 if (which < mAssetPaths.size()) {
1054 const asset_path& ap = mAssetPaths.itemAt(which);
1055 if (ap.type == kFileTypeRegular) {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +00001056 ALOGV("Adding directory %s from zip %s", dirName, ap.path.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -08001057 scanAndMergeZipLocked(pMergedInfo, ap, NULL, dirName);
1058 } else {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +00001059 ALOGV("Adding directory %s from dir %s", dirName, ap.path.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -08001060 scanAndMergeDirLocked(pMergedInfo, ap, NULL, dirName);
1061 }
1062 }
1063
1064#if 0
1065 printf("FILE LIST:\n");
1066 for (i = 0; i < (size_t) pMergedInfo->size(); i++) {
1067 printf(" %d: (%d) '%s'\n", i,
1068 pMergedInfo->itemAt(i).getFileType(),
1069 (const char*) pMergedInfo->itemAt(i).getFileName());
1070 }
1071#endif
1072
1073 pDir->setFileList(pMergedInfo);
1074 return pDir;
1075}
1076
1077/*
1078 * Scan the contents of the specified directory and merge them into the
1079 * "pMergedInfo" vector, removing previous entries if we find "exclude"
1080 * directives.
1081 *
1082 * Returns "false" if we found nothing to contribute.
1083 */
1084bool AssetManager::scanAndMergeDirLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
1085 const asset_path& ap, const char* rootDir, const char* dirName)
1086{
Adam Lesinski16c4d152014-01-24 13:27:13 -08001087 assert(pMergedInfo != NULL);
1088
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +00001089 //printf("scanAndMergeDir: %s %s %s\n", ap.path.c_str(), rootDir, dirName);
Adam Lesinski16c4d152014-01-24 13:27:13 -08001090
Adam Lesinskia77685f2016-10-03 16:26:28 -07001091 String8 path = createPathNameLocked(ap, rootDir);
Tomasz Wasilczyk804e8192023-08-23 02:22:53 +00001092 if (dirName[0] != '\0') appendPath(path, dirName);
Adam Lesinski16c4d152014-01-24 13:27:13 -08001093
Adam Lesinskia77685f2016-10-03 16:26:28 -07001094 SortedVector<AssetDir::FileInfo>* pContents = scanDirLocked(path);
1095 if (pContents == NULL)
1096 return false;
Adam Lesinski16c4d152014-01-24 13:27:13 -08001097
1098 // if we wanted to do an incremental cache fill, we would do it here
1099
1100 /*
1101 * Process "exclude" directives. If we find a filename that ends with
1102 * ".EXCLUDE", we look for a matching entry in the "merged" set, and
1103 * remove it if we find it. We also delete the "exclude" entry.
1104 */
1105 int i, count, exclExtLen;
1106
1107 count = pContents->size();
1108 exclExtLen = strlen(kExcludeExtension);
1109 for (i = 0; i < count; i++) {
1110 const char* name;
1111 int nameLen;
1112
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +00001113 name = pContents->itemAt(i).getFileName().c_str();
Adam Lesinski16c4d152014-01-24 13:27:13 -08001114 nameLen = strlen(name);
1115 if (nameLen > exclExtLen &&
1116 strcmp(name + (nameLen - exclExtLen), kExcludeExtension) == 0)
1117 {
1118 String8 match(name, nameLen - exclExtLen);
1119 int matchIdx;
1120
1121 matchIdx = AssetDir::FileInfo::findEntry(pMergedInfo, match);
1122 if (matchIdx > 0) {
1123 ALOGV("Excluding '%s' [%s]\n",
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +00001124 pMergedInfo->itemAt(matchIdx).getFileName().c_str(),
1125 pMergedInfo->itemAt(matchIdx).getSourceName().c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -08001126 pMergedInfo->removeAt(matchIdx);
1127 } else {
1128 //printf("+++ no match on '%s'\n", (const char*) match);
1129 }
1130
1131 ALOGD("HEY: size=%d removing %d\n", (int)pContents->size(), i);
1132 pContents->removeAt(i);
1133 i--; // adjust "for" loop
1134 count--; // and loop limit
1135 }
1136 }
1137
1138 mergeInfoLocked(pMergedInfo, pContents);
1139
1140 delete pContents;
1141
1142 return true;
1143}
1144
1145/*
1146 * Scan the contents of the specified directory, and stuff what we find
1147 * into a newly-allocated vector.
1148 *
1149 * Files ending in ".gz" will have their extensions removed.
1150 *
1151 * We should probably think about skipping files with "illegal" names,
1152 * e.g. illegal characters (/\:) or excessive length.
1153 *
1154 * Returns NULL if the specified directory doesn't exist.
1155 */
1156SortedVector<AssetDir::FileInfo>* AssetManager::scanDirLocked(const String8& path)
1157{
1158 SortedVector<AssetDir::FileInfo>* pContents = NULL;
1159 DIR* dir;
1160 struct dirent* entry;
1161 FileType fileType;
1162
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +00001163 ALOGV("Scanning dir '%s'\n", path.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -08001164
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +00001165 dir = opendir(path.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -08001166 if (dir == NULL)
1167 return NULL;
1168
1169 pContents = new SortedVector<AssetDir::FileInfo>;
1170
1171 while (1) {
1172 entry = readdir(dir);
1173 if (entry == NULL)
1174 break;
1175
1176 if (strcmp(entry->d_name, ".") == 0 ||
1177 strcmp(entry->d_name, "..") == 0)
1178 continue;
1179
1180#ifdef _DIRENT_HAVE_D_TYPE
1181 if (entry->d_type == DT_REG)
1182 fileType = kFileTypeRegular;
1183 else if (entry->d_type == DT_DIR)
1184 fileType = kFileTypeDirectory;
1185 else
1186 fileType = kFileTypeUnknown;
1187#else
1188 // stat the file
Tomasz Wasilczyk804e8192023-08-23 02:22:53 +00001189 fileType = ::getFileType(appendPathCopy(path, entry->d_name).c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -08001190#endif
1191
1192 if (fileType != kFileTypeRegular && fileType != kFileTypeDirectory)
1193 continue;
1194
1195 AssetDir::FileInfo info;
1196 info.set(String8(entry->d_name), fileType);
Tomasz Wasilczyk804e8192023-08-23 02:22:53 +00001197 if (strcasecmp(getPathExtension(info.getFileName()).c_str(), ".gz") == 0)
1198 info.setFileName(getBasePath(info.getFileName()));
1199 info.setSourceName(appendPathCopy(path, info.getFileName()));
Adam Lesinski16c4d152014-01-24 13:27:13 -08001200 pContents->add(info);
1201 }
1202
1203 closedir(dir);
1204 return pContents;
1205}
1206
1207/*
1208 * Scan the contents out of the specified Zip archive, and merge what we
1209 * find into "pMergedInfo". If the Zip archive in question doesn't exist,
1210 * we return immediately.
1211 *
1212 * Returns "false" if we found nothing to contribute.
1213 */
1214bool AssetManager::scanAndMergeZipLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
1215 const asset_path& ap, const char* rootDir, const char* baseDirName)
1216{
1217 ZipFileRO* pZip;
1218 Vector<String8> dirs;
1219 AssetDir::FileInfo info;
1220 SortedVector<AssetDir::FileInfo> contents;
1221 String8 sourceName, zipName, dirName;
1222
1223 pZip = mZipSet.getZip(ap.path);
1224 if (pZip == NULL) {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +00001225 ALOGW("Failure opening zip %s\n", ap.path.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -08001226 return false;
1227 }
1228
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +00001229 zipName = ZipSet::getPathName(ap.path.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -08001230
1231 /* convert "sounds" to "rootDir/sounds" */
1232 if (rootDir != NULL) dirName = rootDir;
Tomasz Wasilczyk804e8192023-08-23 02:22:53 +00001233 appendPath(dirName, baseDirName);
Adam Lesinski16c4d152014-01-24 13:27:13 -08001234
1235 /*
1236 * Scan through the list of files, looking for a match. The files in
1237 * the Zip table of contents are not in sorted order, so we have to
1238 * process the entire list. We're looking for a string that begins
1239 * with the characters in "dirName", is followed by a '/', and has no
1240 * subsequent '/' in the stuff that follows.
1241 *
1242 * What makes this especially fun is that directories are not stored
1243 * explicitly in Zip archives, so we have to infer them from context.
1244 * When we see "sounds/foo.wav" we have to leave a note to ourselves
1245 * to insert a directory called "sounds" into the list. We store
1246 * these in temporary vector so that we only return each one once.
1247 *
1248 * Name comparisons are case-sensitive to match UNIX filesystem
1249 * semantics.
1250 */
1251 int dirNameLen = dirName.length();
Narayan Kamath560566d2013-12-03 13:16:03 +00001252 void *iterationCookie;
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +00001253 if (!pZip->startIteration(&iterationCookie, dirName.c_str(), NULL)) {
Narayan Kamath560566d2013-12-03 13:16:03 +00001254 ALOGW("ZipFileRO::startIteration returned false");
1255 return false;
1256 }
1257
1258 ZipEntryRO entry;
1259 while ((entry = pZip->nextEntry(iterationCookie)) != NULL) {
Adam Lesinski16c4d152014-01-24 13:27:13 -08001260 char nameBuf[256];
1261
Adam Lesinski16c4d152014-01-24 13:27:13 -08001262 if (pZip->getEntryFileName(entry, nameBuf, sizeof(nameBuf)) != 0) {
1263 // TODO: fix this if we expect to have long names
1264 ALOGE("ARGH: name too long?\n");
1265 continue;
1266 }
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +00001267 //printf("Comparing %s in %s?\n", nameBuf, dirName.c_str());
Yusuke Sato05f648e2015-08-03 16:21:10 -07001268 if (dirNameLen == 0 || nameBuf[dirNameLen] == '/')
Adam Lesinski16c4d152014-01-24 13:27:13 -08001269 {
1270 const char* cp;
1271 const char* nextSlash;
1272
1273 cp = nameBuf + dirNameLen;
1274 if (dirNameLen != 0)
1275 cp++; // advance past the '/'
1276
1277 nextSlash = strchr(cp, '/');
1278//xxx this may break if there are bare directory entries
1279 if (nextSlash == NULL) {
1280 /* this is a file in the requested directory */
1281
Tomasz Wasilczyk804e8192023-08-23 02:22:53 +00001282 info.set(getPathLeaf(String8(nameBuf)), kFileTypeRegular);
Adam Lesinski16c4d152014-01-24 13:27:13 -08001283
1284 info.setSourceName(
1285 createZipSourceNameLocked(zipName, dirName, info.getFileName()));
1286
1287 contents.add(info);
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +00001288 //printf("FOUND: file '%s'\n", info.getFileName().c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -08001289 } else {
1290 /* this is a subdir; add it if we don't already have it*/
1291 String8 subdirName(cp, nextSlash - cp);
1292 size_t j;
1293 size_t N = dirs.size();
1294
1295 for (j = 0; j < N; j++) {
1296 if (subdirName == dirs[j]) {
1297 break;
1298 }
1299 }
1300 if (j == N) {
1301 dirs.add(subdirName);
1302 }
1303
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +00001304 //printf("FOUND: dir '%s'\n", subdirName.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -08001305 }
1306 }
1307 }
1308
Narayan Kamath560566d2013-12-03 13:16:03 +00001309 pZip->endIteration(iterationCookie);
1310
Adam Lesinski16c4d152014-01-24 13:27:13 -08001311 /*
1312 * Add the set of unique directories.
1313 */
1314 for (int i = 0; i < (int) dirs.size(); i++) {
1315 info.set(dirs[i], kFileTypeDirectory);
1316 info.setSourceName(
1317 createZipSourceNameLocked(zipName, dirName, info.getFileName()));
1318 contents.add(info);
1319 }
1320
1321 mergeInfoLocked(pMergedInfo, &contents);
1322
1323 return true;
1324}
1325
1326
1327/*
1328 * Merge two vectors of FileInfo.
1329 *
1330 * The merged contents will be stuffed into *pMergedInfo.
1331 *
1332 * If an entry for a file exists in both "pMergedInfo" and "pContents",
1333 * we use the newer "pContents" entry.
1334 */
1335void AssetManager::mergeInfoLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo,
1336 const SortedVector<AssetDir::FileInfo>* pContents)
1337{
1338 /*
1339 * Merge what we found in this directory with what we found in
1340 * other places.
1341 *
1342 * Two basic approaches:
1343 * (1) Create a new array that holds the unique values of the two
1344 * arrays.
1345 * (2) Take the elements from pContents and shove them into pMergedInfo.
1346 *
1347 * Because these are vectors of complex objects, moving elements around
1348 * inside the vector requires constructing new objects and allocating
1349 * storage for members. With approach #1, we're always adding to the
1350 * end, whereas with #2 we could be inserting multiple elements at the
1351 * front of the vector. Approach #1 requires a full copy of the
1352 * contents of pMergedInfo, but approach #2 requires the same copy for
1353 * every insertion at the front of pMergedInfo.
1354 *
1355 * (We should probably use a SortedVector interface that allows us to
1356 * just stuff items in, trusting us to maintain the sort order.)
1357 */
1358 SortedVector<AssetDir::FileInfo>* pNewSorted;
1359 int mergeMax, contMax;
1360 int mergeIdx, contIdx;
1361
1362 pNewSorted = new SortedVector<AssetDir::FileInfo>;
1363 mergeMax = pMergedInfo->size();
1364 contMax = pContents->size();
1365 mergeIdx = contIdx = 0;
1366
1367 while (mergeIdx < mergeMax || contIdx < contMax) {
1368 if (mergeIdx == mergeMax) {
1369 /* hit end of "merge" list, copy rest of "contents" */
1370 pNewSorted->add(pContents->itemAt(contIdx));
1371 contIdx++;
1372 } else if (contIdx == contMax) {
1373 /* hit end of "cont" list, copy rest of "merge" */
1374 pNewSorted->add(pMergedInfo->itemAt(mergeIdx));
1375 mergeIdx++;
1376 } else if (pMergedInfo->itemAt(mergeIdx) == pContents->itemAt(contIdx))
1377 {
1378 /* items are identical, add newer and advance both indices */
1379 pNewSorted->add(pContents->itemAt(contIdx));
1380 mergeIdx++;
1381 contIdx++;
1382 } else if (pMergedInfo->itemAt(mergeIdx) < pContents->itemAt(contIdx))
1383 {
1384 /* "merge" is lower, add that one */
1385 pNewSorted->add(pMergedInfo->itemAt(mergeIdx));
1386 mergeIdx++;
1387 } else {
1388 /* "cont" is lower, add that one */
1389 assert(pContents->itemAt(contIdx) < pMergedInfo->itemAt(mergeIdx));
1390 pNewSorted->add(pContents->itemAt(contIdx));
1391 contIdx++;
1392 }
1393 }
1394
1395 /*
1396 * Overwrite the "merged" list with the new stuff.
1397 */
1398 *pMergedInfo = *pNewSorted;
1399 delete pNewSorted;
1400
1401#if 0 // for Vector, rather than SortedVector
1402 int i, j;
1403 for (i = pContents->size() -1; i >= 0; i--) {
1404 bool add = true;
1405
1406 for (j = pMergedInfo->size() -1; j >= 0; j--) {
1407 /* case-sensitive comparisons, to behave like UNIX fs */
1408 if (strcmp(pContents->itemAt(i).mFileName,
1409 pMergedInfo->itemAt(j).mFileName) == 0)
1410 {
1411 /* match, don't add this entry */
1412 add = false;
1413 break;
1414 }
1415 }
1416
1417 if (add)
1418 pMergedInfo->add(pContents->itemAt(i));
1419 }
1420#endif
1421}
1422
Adam Lesinski16c4d152014-01-24 13:27:13 -08001423/*
1424 * ===========================================================================
1425 * AssetManager::SharedZip
1426 * ===========================================================================
1427 */
1428
1429
1430Mutex AssetManager::SharedZip::gLock;
1431DefaultKeyedVector<String8, wp<AssetManager::SharedZip> > AssetManager::SharedZip::gOpen;
1432
1433AssetManager::SharedZip::SharedZip(const String8& path, time_t modWhen)
1434 : mPath(path), mZipFile(NULL), mModWhen(modWhen),
1435 mResourceTableAsset(NULL), mResourceTable(NULL)
1436{
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001437 if (kIsDebug) {
Tomasz Wasilczyk835dfe52023-08-17 16:27:22 +00001438 ALOGI("Creating SharedZip %p %s\n", this, mPath.c_str());
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001439 }
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +00001440 ALOGV("+++ opening zip '%s'\n", mPath.c_str());
1441 mZipFile = ZipFileRO::open(mPath.c_str());
Narayan Kamath560566d2013-12-03 13:16:03 +00001442 if (mZipFile == NULL) {
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +00001443 ALOGD("failed to open Zip archive '%s'\n", mPath.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -08001444 }
1445}
1446
Dianne Hackbornca3872c2017-10-30 14:19:32 -07001447AssetManager::SharedZip::SharedZip(int fd, const String8& path)
1448 : mPath(path), mZipFile(NULL), mModWhen(0),
1449 mResourceTableAsset(NULL), mResourceTable(NULL)
1450{
1451 if (kIsDebug) {
Tomasz Wasilczyk835dfe52023-08-17 16:27:22 +00001452 ALOGI("Creating SharedZip %p fd=%d %s\n", this, fd, mPath.c_str());
Dianne Hackbornca3872c2017-10-30 14:19:32 -07001453 }
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +00001454 ALOGV("+++ opening zip fd=%d '%s'\n", fd, mPath.c_str());
1455 mZipFile = ZipFileRO::openFd(fd, mPath.c_str());
Dianne Hackbornca3872c2017-10-30 14:19:32 -07001456 if (mZipFile == NULL) {
1457 ::close(fd);
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +00001458 ALOGD("failed to open Zip archive fd=%d '%s'\n", fd, mPath.c_str());
Dianne Hackbornca3872c2017-10-30 14:19:32 -07001459 }
1460}
1461
Mårten Kongstad48d22322014-01-31 14:43:27 +01001462sp<AssetManager::SharedZip> AssetManager::SharedZip::get(const String8& path,
1463 bool createIfNotPresent)
Adam Lesinski16c4d152014-01-24 13:27:13 -08001464{
1465 AutoMutex _l(gLock);
Tomasz Wasilczyk835dfe52023-08-17 16:27:22 +00001466 time_t modWhen = getFileModDate(path.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -08001467 sp<SharedZip> zip = gOpen.valueFor(path).promote();
1468 if (zip != NULL && zip->mModWhen == modWhen) {
1469 return zip;
1470 }
Mårten Kongstad48d22322014-01-31 14:43:27 +01001471 if (zip == NULL && !createIfNotPresent) {
1472 return NULL;
1473 }
Adam Lesinski16c4d152014-01-24 13:27:13 -08001474 zip = new SharedZip(path, modWhen);
1475 gOpen.add(path, zip);
1476 return zip;
Dianne Hackbornca3872c2017-10-30 14:19:32 -07001477}
Adam Lesinski16c4d152014-01-24 13:27:13 -08001478
Dianne Hackbornca3872c2017-10-30 14:19:32 -07001479sp<AssetManager::SharedZip> AssetManager::SharedZip::create(int fd, const String8& path)
1480{
1481 return new SharedZip(fd, path);
Adam Lesinski16c4d152014-01-24 13:27:13 -08001482}
1483
1484ZipFileRO* AssetManager::SharedZip::getZip()
1485{
1486 return mZipFile;
1487}
1488
1489Asset* AssetManager::SharedZip::getResourceTableAsset()
1490{
songjinshi49921f22016-09-08 15:24:30 +08001491 AutoMutex _l(gLock);
Adam Lesinski16c4d152014-01-24 13:27:13 -08001492 ALOGV("Getting from SharedZip %p resource asset %p\n", this, mResourceTableAsset);
1493 return mResourceTableAsset;
1494}
1495
1496Asset* AssetManager::SharedZip::setResourceTableAsset(Asset* asset)
1497{
1498 {
1499 AutoMutex _l(gLock);
1500 if (mResourceTableAsset == NULL) {
Adam Lesinski16c4d152014-01-24 13:27:13 -08001501 // This is not thread safe the first time it is called, so
1502 // do it here with the global lock held.
1503 asset->getBuffer(true);
songjinshi49921f22016-09-08 15:24:30 +08001504 mResourceTableAsset = asset;
Adam Lesinski16c4d152014-01-24 13:27:13 -08001505 return asset;
1506 }
1507 }
1508 delete asset;
1509 return mResourceTableAsset;
1510}
1511
1512ResTable* AssetManager::SharedZip::getResourceTable()
1513{
1514 ALOGV("Getting from SharedZip %p resource table %p\n", this, mResourceTable);
1515 return mResourceTable;
1516}
1517
1518ResTable* AssetManager::SharedZip::setResourceTable(ResTable* res)
1519{
1520 {
1521 AutoMutex _l(gLock);
1522 if (mResourceTable == NULL) {
1523 mResourceTable = res;
1524 return res;
1525 }
1526 }
1527 delete res;
1528 return mResourceTable;
1529}
1530
1531bool AssetManager::SharedZip::isUpToDate()
1532{
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +00001533 time_t modWhen = getFileModDate(mPath.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -08001534 return mModWhen == modWhen;
1535}
1536
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +09001537void AssetManager::SharedZip::addOverlay(const asset_path& ap)
1538{
1539 mOverlays.add(ap);
1540}
1541
1542bool AssetManager::SharedZip::getOverlay(size_t idx, asset_path* out) const
1543{
1544 if (idx >= mOverlays.size()) {
1545 return false;
1546 }
1547 *out = mOverlays[idx];
1548 return true;
1549}
1550
Adam Lesinski16c4d152014-01-24 13:27:13 -08001551AssetManager::SharedZip::~SharedZip()
1552{
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001553 if (kIsDebug) {
Tomasz Wasilczyk835dfe52023-08-17 16:27:22 +00001554 ALOGI("Destroying SharedZip %p %s\n", this, mPath.c_str());
Andreas Gampe2204f0b2014-10-21 23:04:54 -07001555 }
Adam Lesinski16c4d152014-01-24 13:27:13 -08001556 if (mResourceTable != NULL) {
1557 delete mResourceTable;
1558 }
1559 if (mResourceTableAsset != NULL) {
1560 delete mResourceTableAsset;
1561 }
1562 if (mZipFile != NULL) {
1563 delete mZipFile;
Tomasz Wasilczykd2a69832023-08-10 23:54:44 +00001564 ALOGV("Closed '%s'\n", mPath.c_str());
Adam Lesinski16c4d152014-01-24 13:27:13 -08001565 }
1566}
1567
1568/*
1569 * ===========================================================================
1570 * AssetManager::ZipSet
1571 * ===========================================================================
1572 */
1573
1574/*
Adam Lesinski16c4d152014-01-24 13:27:13 -08001575 * Destructor. Close any open archives.
1576 */
1577AssetManager::ZipSet::~ZipSet(void)
1578{
1579 size_t N = mZipFile.size();
1580 for (size_t i = 0; i < N; i++)
1581 closeZip(i);
1582}
1583
1584/*
1585 * Close a Zip file and reset the entry.
1586 */
1587void AssetManager::ZipSet::closeZip(int idx)
1588{
1589 mZipFile.editItemAt(idx) = NULL;
1590}
1591
Adam Lesinski16c4d152014-01-24 13:27:13 -08001592/*
1593 * Retrieve the appropriate Zip file from the set.
1594 */
1595ZipFileRO* AssetManager::ZipSet::getZip(const String8& path)
1596{
Dianne Hackbornca3872c2017-10-30 14:19:32 -07001597 return getSharedZip(path)->getZip();
1598}
1599
1600const sp<AssetManager::SharedZip> AssetManager::ZipSet::getSharedZip(const String8& path)
1601{
Adam Lesinski16c4d152014-01-24 13:27:13 -08001602 int idx = getIndex(path);
1603 sp<SharedZip> zip = mZipFile[idx];
1604 if (zip == NULL) {
1605 zip = SharedZip::get(path);
1606 mZipFile.editItemAt(idx) = zip;
1607 }
Dianne Hackbornca3872c2017-10-30 14:19:32 -07001608 return zip;
Adam Lesinski16c4d152014-01-24 13:27:13 -08001609}
1610
1611Asset* AssetManager::ZipSet::getZipResourceTableAsset(const String8& path)
1612{
1613 int idx = getIndex(path);
1614 sp<SharedZip> zip = mZipFile[idx];
1615 if (zip == NULL) {
1616 zip = SharedZip::get(path);
1617 mZipFile.editItemAt(idx) = zip;
1618 }
1619 return zip->getResourceTableAsset();
1620}
1621
1622Asset* AssetManager::ZipSet::setZipResourceTableAsset(const String8& path,
1623 Asset* asset)
1624{
1625 int idx = getIndex(path);
1626 sp<SharedZip> zip = mZipFile[idx];
1627 // doesn't make sense to call before previously accessing.
1628 return zip->setResourceTableAsset(asset);
1629}
1630
1631ResTable* AssetManager::ZipSet::getZipResourceTable(const String8& path)
1632{
1633 int idx = getIndex(path);
1634 sp<SharedZip> zip = mZipFile[idx];
1635 if (zip == NULL) {
1636 zip = SharedZip::get(path);
1637 mZipFile.editItemAt(idx) = zip;
1638 }
1639 return zip->getResourceTable();
1640}
1641
1642ResTable* AssetManager::ZipSet::setZipResourceTable(const String8& path,
1643 ResTable* res)
1644{
1645 int idx = getIndex(path);
1646 sp<SharedZip> zip = mZipFile[idx];
1647 // doesn't make sense to call before previously accessing.
1648 return zip->setResourceTable(res);
1649}
1650
1651/*
1652 * Generate the partial pathname for the specified archive. The caller
1653 * gets to prepend the asset root directory.
1654 *
1655 * Returns something like "common/en-US-noogle.jar".
1656 */
1657/*static*/ String8 AssetManager::ZipSet::getPathName(const char* zipPath)
1658{
1659 return String8(zipPath);
1660}
1661
1662bool AssetManager::ZipSet::isUpToDate()
1663{
1664 const size_t N = mZipFile.size();
1665 for (size_t i=0; i<N; i++) {
1666 if (mZipFile[i] != NULL && !mZipFile[i]->isUpToDate()) {
1667 return false;
1668 }
1669 }
1670 return true;
1671}
1672
Jaekyun Seok7de2f9c2017-03-02 12:45:10 +09001673void AssetManager::ZipSet::addOverlay(const String8& path, const asset_path& overlay)
1674{
1675 int idx = getIndex(path);
1676 sp<SharedZip> zip = mZipFile[idx];
1677 zip->addOverlay(overlay);
1678}
1679
1680bool AssetManager::ZipSet::getOverlay(const String8& path, size_t idx, asset_path* out) const
1681{
1682 sp<SharedZip> zip = SharedZip::get(path, false);
1683 if (zip == NULL) {
1684 return false;
1685 }
1686 return zip->getOverlay(idx, out);
1687}
1688
Adam Lesinski16c4d152014-01-24 13:27:13 -08001689/*
1690 * Compute the zip file's index.
1691 *
1692 * "appName", "locale", and "vendor" should be set to NULL to indicate the
1693 * default directory.
1694 */
1695int AssetManager::ZipSet::getIndex(const String8& zip) const
1696{
1697 const size_t N = mZipPath.size();
1698 for (size_t i=0; i<N; i++) {
1699 if (mZipPath[i] == zip) {
1700 return i;
1701 }
1702 }
1703
1704 mZipPath.add(zip);
1705 mZipFile.add(NULL);
1706
1707 return mZipPath.size()-1;
1708}