Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | // |
| 18 | // Provide access to read-only assets. |
| 19 | // |
| 20 | |
| 21 | #define LOG_TAG "asset" |
| 22 | #define ATRACE_TAG ATRACE_TAG_RESOURCES |
| 23 | //#define LOG_NDEBUG 0 |
| 24 | |
| 25 | #include <androidfw/Asset.h> |
| 26 | #include <androidfw/AssetDir.h> |
| 27 | #include <androidfw/AssetManager.h> |
| 28 | #include <androidfw/misc.h> |
| 29 | #include <androidfw/ResourceTypes.h> |
| 30 | #include <androidfw/ZipFileRO.h> |
Steven Moreland | fb7952f | 2018-02-23 14:58:50 -0800 | [diff] [blame] | 31 | #include <cutils/atomic.h> |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 32 | #include <utils/Log.h> |
| 33 | #include <utils/String8.h> |
| 34 | #include <utils/String8.h> |
| 35 | #include <utils/threads.h> |
| 36 | #include <utils/Timers.h> |
Adam Lesinski | b7e1ce0 | 2016-04-11 20:03:01 -0700 | [diff] [blame] | 37 | #include <utils/Trace.h> |
Martin Wallgren | 0fbb608 | 2015-08-11 15:10:31 +0200 | [diff] [blame] | 38 | #ifndef _WIN32 |
| 39 | #include <sys/file.h> |
| 40 | #endif |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 41 | |
| 42 | #include <assert.h> |
| 43 | #include <dirent.h> |
| 44 | #include <errno.h> |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 45 | #include <string.h> // strerror |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 46 | #include <strings.h> |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 47 | |
| 48 | #ifndef TEMP_FAILURE_RETRY |
| 49 | /* Used to retry syscalls that can return EINTR. */ |
| 50 | #define TEMP_FAILURE_RETRY(exp) ({ \ |
| 51 | typeof (exp) _rc; \ |
| 52 | do { \ |
| 53 | _rc = (exp); \ |
| 54 | } while (_rc == -1 && errno == EINTR); \ |
| 55 | _rc; }) |
| 56 | #endif |
| 57 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 58 | using namespace android; |
| 59 | |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 60 | static const bool kIsDebug = false; |
| 61 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 62 | static const char* kAssetsRoot = "assets"; |
| 63 | static const char* kAppZipName = NULL; //"classes.jar"; |
| 64 | static const char* kSystemAssets = "framework/framework-res.apk"; |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 65 | static const char* kResourceCache = "resource-cache"; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 66 | |
| 67 | static const char* kExcludeExtension = ".EXCLUDE"; |
| 68 | |
| 69 | static Asset* const kExcludedAsset = (Asset*) 0xd000000d; |
| 70 | |
| 71 | static volatile int32_t gCount = 0; |
| 72 | |
Mårten Kongstad | 65a05fd | 2014-01-31 14:01:52 +0100 | [diff] [blame] | 73 | const char* AssetManager::RESOURCES_FILENAME = "resources.arsc"; |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 74 | const char* AssetManager::IDMAP_BIN = "/system/bin/idmap"; |
Mårten Kongstad | 06a1ac8 | 2018-09-20 13:09:47 +0200 | [diff] [blame] | 75 | const char* AssetManager::VENDOR_OVERLAY_DIR = "/vendor/overlay"; |
Jaekyun Seok | 1713d9e | 2018-01-12 21:47:26 +0900 | [diff] [blame] | 76 | const char* AssetManager::PRODUCT_OVERLAY_DIR = "/product/overlay"; |
Dario Freni | 4ce4679 | 2018-06-01 14:02:08 +0100 | [diff] [blame] | 77 | const char* AssetManager::PRODUCT_SERVICES_OVERLAY_DIR = "/product_services/overlay"; |
Mårten Kongstad | 48c24cf | 2019-02-25 10:54:09 +0100 | [diff] [blame] | 78 | const char* AssetManager::ODM_OVERLAY_DIR = "/odm/overlay"; |
Jakub Adamek | 54dcaab | 2016-10-19 11:46:13 +0100 | [diff] [blame] | 79 | const char* AssetManager::OVERLAY_THEME_DIR_PROPERTY = "ro.boot.vendor.overlay.theme"; |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 80 | const char* AssetManager::TARGET_PACKAGE_NAME = "android"; |
| 81 | const char* AssetManager::TARGET_APK_PATH = "/system/framework/framework-res.apk"; |
| 82 | const char* AssetManager::IDMAP_DIR = "/data/resource-cache"; |
Mårten Kongstad | 65a05fd | 2014-01-31 14:01:52 +0100 | [diff] [blame] | 83 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 84 | namespace { |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 85 | |
Adam Lesinski | a77685f | 2016-10-03 16:26:28 -0700 | [diff] [blame] | 86 | String8 idmapPathForPackagePath(const String8& pkgPath) { |
| 87 | const char* root = getenv("ANDROID_DATA"); |
| 88 | LOG_ALWAYS_FATAL_IF(root == NULL, "ANDROID_DATA not set"); |
| 89 | String8 path(root); |
| 90 | path.appendPath(kResourceCache); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 91 | |
Adam Lesinski | a77685f | 2016-10-03 16:26:28 -0700 | [diff] [blame] | 92 | char buf[256]; // 256 chars should be enough for anyone... |
| 93 | strncpy(buf, pkgPath.string(), 255); |
| 94 | buf[255] = '\0'; |
| 95 | char* filename = buf; |
| 96 | while (*filename && *filename == '/') { |
| 97 | ++filename; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 98 | } |
Adam Lesinski | a77685f | 2016-10-03 16:26:28 -0700 | [diff] [blame] | 99 | char* p = filename; |
| 100 | while (*p) { |
| 101 | if (*p == '/') { |
| 102 | *p = '@'; |
| 103 | } |
| 104 | ++p; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 105 | } |
Adam Lesinski | a77685f | 2016-10-03 16:26:28 -0700 | [diff] [blame] | 106 | path.appendPath(filename); |
| 107 | path.append("@idmap"); |
| 108 | |
| 109 | return path; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /* |
Adam Lesinski | a77685f | 2016-10-03 16:26:28 -0700 | [diff] [blame] | 113 | * Like strdup(), but uses C++ "new" operator instead of malloc. |
| 114 | */ |
| 115 | static char* strdupNew(const char* str) { |
| 116 | char* newStr; |
| 117 | int len; |
| 118 | |
| 119 | if (str == NULL) |
| 120 | return NULL; |
| 121 | |
| 122 | len = strlen(str); |
| 123 | newStr = new char[len+1]; |
| 124 | memcpy(newStr, str, len+1); |
| 125 | |
| 126 | return newStr; |
| 127 | } |
| 128 | |
| 129 | } // namespace |
| 130 | |
| 131 | /* |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 132 | * =========================================================================== |
| 133 | * AssetManager |
| 134 | * =========================================================================== |
| 135 | */ |
| 136 | |
Adam Lesinski | a77685f | 2016-10-03 16:26:28 -0700 | [diff] [blame] | 137 | int32_t AssetManager::getGlobalCount() { |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 138 | return gCount; |
| 139 | } |
| 140 | |
Adam Lesinski | a77685f | 2016-10-03 16:26:28 -0700 | [diff] [blame] | 141 | AssetManager::AssetManager() : |
| 142 | mLocale(NULL), mResources(NULL), mConfig(new ResTable_config) { |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 143 | int count = android_atomic_inc(&gCount) + 1; |
| 144 | if (kIsDebug) { |
| 145 | ALOGI("Creating AssetManager %p #%d\n", this, count); |
| 146 | } |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 147 | memset(mConfig, 0, sizeof(ResTable_config)); |
| 148 | } |
| 149 | |
Adam Lesinski | a77685f | 2016-10-03 16:26:28 -0700 | [diff] [blame] | 150 | AssetManager::~AssetManager() { |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 151 | int count = android_atomic_dec(&gCount); |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 152 | if (kIsDebug) { |
| 153 | ALOGI("Destroying AssetManager in %p #%d\n", this, count); |
Dianne Hackborn | 1704e3c | 2017-10-31 19:55:42 +0000 | [diff] [blame] | 154 | } else { |
| 155 | ALOGV("Destroying AssetManager in %p #%d\n", this, count); |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 156 | } |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 157 | |
Dianne Hackborn | ca3872c | 2017-10-30 14:19:32 -0700 | [diff] [blame] | 158 | // Manually close any fd paths for which we have not yet opened their zip (which |
| 159 | // will take ownership of the fd and close it when done). |
| 160 | for (size_t i=0; i<mAssetPaths.size(); i++) { |
Dianne Hackborn | 1704e3c | 2017-10-31 19:55:42 +0000 | [diff] [blame] | 161 | ALOGV("Cleaning path #%d: fd=%d, zip=%p", (int)i, mAssetPaths[i].rawFd, |
| 162 | mAssetPaths[i].zip.get()); |
Dianne Hackborn | ca3872c | 2017-10-30 14:19:32 -0700 | [diff] [blame] | 163 | if (mAssetPaths[i].rawFd >= 0 && mAssetPaths[i].zip == NULL) { |
| 164 | close(mAssetPaths[i].rawFd); |
| 165 | } |
| 166 | } |
| 167 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 168 | delete mConfig; |
| 169 | delete mResources; |
| 170 | |
| 171 | // don't have a String class yet, so make sure we clean up |
| 172 | delete[] mLocale; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 173 | } |
| 174 | |
Roozbeh Pournader | 1c686f2 | 2015-12-18 14:22:14 -0800 | [diff] [blame] | 175 | bool AssetManager::addAssetPath( |
Adam Lesinski | a77685f | 2016-10-03 16:26:28 -0700 | [diff] [blame] | 176 | const String8& path, int32_t* cookie, bool appAsLib, bool isSystemAsset) { |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 177 | AutoMutex _l(mLock); |
| 178 | |
| 179 | asset_path ap; |
| 180 | |
| 181 | String8 realPath(path); |
| 182 | if (kAppZipName) { |
| 183 | realPath.appendPath(kAppZipName); |
| 184 | } |
| 185 | ap.type = ::getFileType(realPath.string()); |
| 186 | if (ap.type == kFileTypeRegular) { |
| 187 | ap.path = realPath; |
| 188 | } else { |
| 189 | ap.path = path; |
| 190 | ap.type = ::getFileType(path.string()); |
| 191 | if (ap.type != kFileTypeDirectory && ap.type != kFileTypeRegular) { |
| 192 | ALOGW("Asset path %s is neither a directory nor file (type=%d).", |
| 193 | path.string(), (int)ap.type); |
| 194 | return false; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | // Skip if we have it already. |
| 199 | for (size_t i=0; i<mAssetPaths.size(); i++) { |
| 200 | if (mAssetPaths[i].path == ap.path) { |
| 201 | if (cookie) { |
Narayan Kamath | a0c6260 | 2014-01-24 13:51:51 +0000 | [diff] [blame] | 202 | *cookie = static_cast<int32_t>(i+1); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 203 | } |
| 204 | return true; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | ALOGV("In %p Asset %s path: %s", this, |
| 209 | ap.type == kFileTypeDirectory ? "dir" : "zip", ap.path.string()); |
| 210 | |
Roozbeh Pournader | 1c686f2 | 2015-12-18 14:22:14 -0800 | [diff] [blame] | 211 | ap.isSystemAsset = isSystemAsset; |
Dianne Hackborn | 1704e3c | 2017-10-31 19:55:42 +0000 | [diff] [blame] | 212 | ssize_t apPos = mAssetPaths.add(ap); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 213 | |
| 214 | // new paths are always added at the end |
| 215 | if (cookie) { |
Narayan Kamath | a0c6260 | 2014-01-24 13:51:51 +0000 | [diff] [blame] | 216 | *cookie = static_cast<int32_t>(mAssetPaths.size()); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 217 | } |
| 218 | |
Jaekyun Seok | 7de2f9c | 2017-03-02 12:45:10 +0900 | [diff] [blame] | 219 | #ifdef __ANDROID__ |
| 220 | // Load overlays, if any |
| 221 | asset_path oap; |
| 222 | for (size_t idx = 0; mZipSet.getOverlay(ap.path, idx, &oap); idx++) { |
| 223 | oap.isSystemAsset = isSystemAsset; |
| 224 | mAssetPaths.add(oap); |
| 225 | } |
| 226 | #endif |
| 227 | |
Martin Kosiba | 7df3625 | 2014-01-16 16:25:56 +0000 | [diff] [blame] | 228 | if (mResources != NULL) { |
Dianne Hackborn | 1704e3c | 2017-10-31 19:55:42 +0000 | [diff] [blame] | 229 | appendPathToResTable(mAssetPaths.editItemAt(apPos), appAsLib); |
Martin Kosiba | 7df3625 | 2014-01-16 16:25:56 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 232 | return true; |
| 233 | } |
| 234 | |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 235 | bool AssetManager::addOverlayPath(const String8& packagePath, int32_t* cookie) |
| 236 | { |
| 237 | const String8 idmapPath = idmapPathForPackagePath(packagePath); |
| 238 | |
| 239 | AutoMutex _l(mLock); |
| 240 | |
| 241 | for (size_t i = 0; i < mAssetPaths.size(); ++i) { |
| 242 | if (mAssetPaths[i].idmap == idmapPath) { |
| 243 | *cookie = static_cast<int32_t>(i + 1); |
| 244 | return true; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | Asset* idmap = NULL; |
| 249 | if ((idmap = openAssetFromFileLocked(idmapPath, Asset::ACCESS_BUFFER)) == NULL) { |
| 250 | ALOGW("failed to open idmap file %s\n", idmapPath.string()); |
| 251 | return false; |
| 252 | } |
| 253 | |
| 254 | String8 targetPath; |
| 255 | String8 overlayPath; |
| 256 | if (!ResTable::getIdmapInfo(idmap->getBuffer(false), idmap->getLength(), |
Adam Lesinski | f90f2f8d | 2014-06-06 14:27:00 -0700 | [diff] [blame] | 257 | NULL, NULL, NULL, &targetPath, &overlayPath)) { |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 258 | ALOGW("failed to read idmap file %s\n", idmapPath.string()); |
| 259 | delete idmap; |
| 260 | return false; |
| 261 | } |
| 262 | delete idmap; |
| 263 | |
| 264 | if (overlayPath != packagePath) { |
| 265 | ALOGW("idmap file %s inconcistent: expected path %s does not match actual path %s\n", |
| 266 | idmapPath.string(), packagePath.string(), overlayPath.string()); |
| 267 | return false; |
| 268 | } |
| 269 | if (access(targetPath.string(), R_OK) != 0) { |
| 270 | ALOGW("failed to access file %s: %s\n", targetPath.string(), strerror(errno)); |
| 271 | return false; |
| 272 | } |
| 273 | if (access(idmapPath.string(), R_OK) != 0) { |
| 274 | ALOGW("failed to access file %s: %s\n", idmapPath.string(), strerror(errno)); |
| 275 | return false; |
| 276 | } |
| 277 | if (access(overlayPath.string(), R_OK) != 0) { |
| 278 | ALOGW("failed to access file %s: %s\n", overlayPath.string(), strerror(errno)); |
| 279 | return false; |
| 280 | } |
| 281 | |
| 282 | asset_path oap; |
| 283 | oap.path = overlayPath; |
| 284 | oap.type = ::getFileType(overlayPath.string()); |
| 285 | oap.idmap = idmapPath; |
| 286 | #if 0 |
| 287 | ALOGD("Overlay added: targetPath=%s overlayPath=%s idmapPath=%s\n", |
| 288 | targetPath.string(), overlayPath.string(), idmapPath.string()); |
| 289 | #endif |
| 290 | mAssetPaths.add(oap); |
| 291 | *cookie = static_cast<int32_t>(mAssetPaths.size()); |
| 292 | |
Mårten Kongstad | 3011313 | 2014-11-07 10:52:17 +0100 | [diff] [blame] | 293 | if (mResources != NULL) { |
| 294 | appendPathToResTable(oap); |
| 295 | } |
| 296 | |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 297 | return true; |
Dianne Hackborn | ca3872c | 2017-10-30 14:19:32 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | bool AssetManager::addAssetFd( |
| 301 | int fd, const String8& debugPathName, int32_t* cookie, bool appAsLib, |
| 302 | bool assume_ownership) { |
| 303 | AutoMutex _l(mLock); |
| 304 | |
| 305 | asset_path ap; |
| 306 | |
| 307 | ap.path = debugPathName; |
| 308 | ap.rawFd = fd; |
| 309 | ap.type = kFileTypeRegular; |
| 310 | ap.assumeOwnership = assume_ownership; |
| 311 | |
| 312 | ALOGV("In %p Asset fd %d name: %s", this, fd, ap.path.string()); |
| 313 | |
Dianne Hackborn | 1704e3c | 2017-10-31 19:55:42 +0000 | [diff] [blame] | 314 | ssize_t apPos = mAssetPaths.add(ap); |
Dianne Hackborn | ca3872c | 2017-10-30 14:19:32 -0700 | [diff] [blame] | 315 | |
| 316 | // new paths are always added at the end |
| 317 | if (cookie) { |
| 318 | *cookie = static_cast<int32_t>(mAssetPaths.size()); |
| 319 | } |
| 320 | |
| 321 | if (mResources != NULL) { |
Dianne Hackborn | 1704e3c | 2017-10-31 19:55:42 +0000 | [diff] [blame] | 322 | appendPathToResTable(mAssetPaths.editItemAt(apPos), appAsLib); |
Dianne Hackborn | ca3872c | 2017-10-30 14:19:32 -0700 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | return true; |
| 326 | } |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 327 | |
Mårten Kongstad | 65a05fd | 2014-01-31 14:01:52 +0100 | [diff] [blame] | 328 | bool AssetManager::createIdmap(const char* targetApkPath, const char* overlayApkPath, |
Dianne Hackborn | 32bb5fa | 2014-02-11 13:56:21 -0800 | [diff] [blame] | 329 | uint32_t targetCrc, uint32_t overlayCrc, uint32_t** outData, size_t* outSize) |
Mårten Kongstad | 65a05fd | 2014-01-31 14:01:52 +0100 | [diff] [blame] | 330 | { |
| 331 | AutoMutex _l(mLock); |
| 332 | const String8 paths[2] = { String8(targetApkPath), String8(overlayApkPath) }; |
Mårten Kongstad | 6bb13da | 2016-06-02 09:34:36 +0200 | [diff] [blame] | 333 | Asset* assets[2] = {NULL, NULL}; |
| 334 | bool ret = false; |
| 335 | { |
| 336 | ResTable tables[2]; |
Mårten Kongstad | 65a05fd | 2014-01-31 14:01:52 +0100 | [diff] [blame] | 337 | |
Mårten Kongstad | 6bb13da | 2016-06-02 09:34:36 +0200 | [diff] [blame] | 338 | for (int i = 0; i < 2; ++i) { |
| 339 | asset_path ap; |
| 340 | ap.type = kFileTypeRegular; |
| 341 | ap.path = paths[i]; |
| 342 | assets[i] = openNonAssetInPathLocked("resources.arsc", |
| 343 | Asset::ACCESS_BUFFER, ap); |
| 344 | if (assets[i] == NULL) { |
| 345 | ALOGW("failed to find resources.arsc in %s\n", ap.path.string()); |
| 346 | goto exit; |
| 347 | } |
| 348 | if (tables[i].add(assets[i]) != NO_ERROR) { |
| 349 | ALOGW("failed to add %s to resource table", paths[i].string()); |
| 350 | goto exit; |
| 351 | } |
Mårten Kongstad | 65a05fd | 2014-01-31 14:01:52 +0100 | [diff] [blame] | 352 | } |
Mårten Kongstad | 67d5c93 | 2018-05-25 15:58:17 +0200 | [diff] [blame] | 353 | ret = tables[1].createIdmap(tables[0], targetCrc, overlayCrc, |
Mårten Kongstad | 6bb13da | 2016-06-02 09:34:36 +0200 | [diff] [blame] | 354 | targetApkPath, overlayApkPath, (void**)outData, outSize) == NO_ERROR; |
Mårten Kongstad | 65a05fd | 2014-01-31 14:01:52 +0100 | [diff] [blame] | 355 | } |
| 356 | |
Mårten Kongstad | 6bb13da | 2016-06-02 09:34:36 +0200 | [diff] [blame] | 357 | exit: |
| 358 | delete assets[0]; |
| 359 | delete assets[1]; |
| 360 | return ret; |
Mårten Kongstad | 65a05fd | 2014-01-31 14:01:52 +0100 | [diff] [blame] | 361 | } |
| 362 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 363 | bool AssetManager::addDefaultAssets() |
| 364 | { |
| 365 | const char* root = getenv("ANDROID_ROOT"); |
| 366 | LOG_ALWAYS_FATAL_IF(root == NULL, "ANDROID_ROOT not set"); |
| 367 | |
| 368 | String8 path(root); |
| 369 | path.appendPath(kSystemAssets); |
| 370 | |
Roozbeh Pournader | 1c686f2 | 2015-12-18 14:22:14 -0800 | [diff] [blame] | 371 | return addAssetPath(path, NULL, false /* appAsLib */, true /* isSystemAsset */); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 372 | } |
| 373 | |
Narayan Kamath | a0c6260 | 2014-01-24 13:51:51 +0000 | [diff] [blame] | 374 | int32_t AssetManager::nextAssetPath(const int32_t cookie) const |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 375 | { |
| 376 | AutoMutex _l(mLock); |
Narayan Kamath | a0c6260 | 2014-01-24 13:51:51 +0000 | [diff] [blame] | 377 | const size_t next = static_cast<size_t>(cookie) + 1; |
| 378 | return next > mAssetPaths.size() ? -1 : next; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 379 | } |
| 380 | |
Narayan Kamath | a0c6260 | 2014-01-24 13:51:51 +0000 | [diff] [blame] | 381 | String8 AssetManager::getAssetPath(const int32_t cookie) const |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 382 | { |
| 383 | AutoMutex _l(mLock); |
Narayan Kamath | a0c6260 | 2014-01-24 13:51:51 +0000 | [diff] [blame] | 384 | const size_t which = static_cast<size_t>(cookie) - 1; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 385 | if (which < mAssetPaths.size()) { |
| 386 | return mAssetPaths[which].path; |
| 387 | } |
| 388 | return String8(); |
| 389 | } |
| 390 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 391 | void AssetManager::setLocaleLocked(const char* locale) |
| 392 | { |
| 393 | if (mLocale != NULL) { |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 394 | delete[] mLocale; |
| 395 | } |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 396 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 397 | mLocale = strdupNew(locale); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 398 | updateResourceParamsLocked(); |
| 399 | } |
| 400 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 401 | void AssetManager::setConfiguration(const ResTable_config& config, const char* locale) |
| 402 | { |
| 403 | AutoMutex _l(mLock); |
| 404 | *mConfig = config; |
| 405 | if (locale) { |
| 406 | setLocaleLocked(locale); |
| 407 | } else if (config.language[0] != 0) { |
Narayan Kamath | 91447d8 | 2014-01-21 15:32:36 +0000 | [diff] [blame] | 408 | char spec[RESTABLE_MAX_LOCALE_LEN]; |
| 409 | config.getBcp47Locale(spec); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 410 | setLocaleLocked(spec); |
| 411 | } else { |
| 412 | updateResourceParamsLocked(); |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | void AssetManager::getConfiguration(ResTable_config* outConfig) const |
| 417 | { |
| 418 | AutoMutex _l(mLock); |
| 419 | *outConfig = *mConfig; |
| 420 | } |
| 421 | |
| 422 | /* |
| 423 | * Open an asset. |
| 424 | * |
Adam Lesinski | fe90eaf | 2016-10-04 13:31:31 -0700 | [diff] [blame] | 425 | * The data could be in any asset path. Each asset path could be: |
| 426 | * - A directory on disk. |
| 427 | * - A Zip archive, uncompressed or compressed. |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 428 | * |
Adam Lesinski | fe90eaf | 2016-10-04 13:31:31 -0700 | [diff] [blame] | 429 | * If the file is in a directory, it could have a .gz suffix, meaning it is compressed. |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 430 | * |
| 431 | * We should probably reject requests for "illegal" filenames, e.g. those |
| 432 | * with illegal characters or "../" backward relative paths. |
| 433 | */ |
| 434 | Asset* AssetManager::open(const char* fileName, AccessMode mode) |
| 435 | { |
| 436 | AutoMutex _l(mLock); |
| 437 | |
| 438 | LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager"); |
| 439 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 440 | String8 assetName(kAssetsRoot); |
| 441 | assetName.appendPath(fileName); |
| 442 | |
| 443 | /* |
| 444 | * For each top-level asset path, search for the asset. |
| 445 | */ |
| 446 | |
| 447 | size_t i = mAssetPaths.size(); |
| 448 | while (i > 0) { |
| 449 | i--; |
| 450 | ALOGV("Looking for asset '%s' in '%s'\n", |
| 451 | assetName.string(), mAssetPaths.itemAt(i).path.string()); |
Dianne Hackborn | 1704e3c | 2017-10-31 19:55:42 +0000 | [diff] [blame] | 452 | Asset* pAsset = openNonAssetInPathLocked(assetName.string(), mode, |
| 453 | mAssetPaths.editItemAt(i)); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 454 | if (pAsset != NULL) { |
| 455 | return pAsset != kExcludedAsset ? pAsset : NULL; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | return NULL; |
| 460 | } |
| 461 | |
| 462 | /* |
| 463 | * Open a non-asset file as if it were an asset. |
| 464 | * |
Adam Lesinski | fe90eaf | 2016-10-04 13:31:31 -0700 | [diff] [blame] | 465 | * The "fileName" is the partial path starting from the application name. |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 466 | */ |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 467 | Asset* AssetManager::openNonAsset(const char* fileName, AccessMode mode, int32_t* outCookie) |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 468 | { |
| 469 | AutoMutex _l(mLock); |
| 470 | |
| 471 | LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager"); |
| 472 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 473 | /* |
| 474 | * For each top-level asset path, search for the asset. |
| 475 | */ |
| 476 | |
| 477 | size_t i = mAssetPaths.size(); |
| 478 | while (i > 0) { |
| 479 | i--; |
| 480 | ALOGV("Looking for non-asset '%s' in '%s'\n", fileName, mAssetPaths.itemAt(i).path.string()); |
| 481 | Asset* pAsset = openNonAssetInPathLocked( |
Dianne Hackborn | 1704e3c | 2017-10-31 19:55:42 +0000 | [diff] [blame] | 482 | fileName, mode, mAssetPaths.editItemAt(i)); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 483 | if (pAsset != NULL) { |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 484 | if (outCookie != NULL) *outCookie = static_cast<int32_t>(i + 1); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 485 | return pAsset != kExcludedAsset ? pAsset : NULL; |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | return NULL; |
| 490 | } |
| 491 | |
Narayan Kamath | a0c6260 | 2014-01-24 13:51:51 +0000 | [diff] [blame] | 492 | Asset* AssetManager::openNonAsset(const int32_t cookie, const char* fileName, AccessMode mode) |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 493 | { |
Narayan Kamath | a0c6260 | 2014-01-24 13:51:51 +0000 | [diff] [blame] | 494 | const size_t which = static_cast<size_t>(cookie) - 1; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 495 | |
| 496 | AutoMutex _l(mLock); |
| 497 | |
| 498 | LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager"); |
| 499 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 500 | if (which < mAssetPaths.size()) { |
| 501 | ALOGV("Looking for non-asset '%s' in '%s'\n", fileName, |
| 502 | mAssetPaths.itemAt(which).path.string()); |
| 503 | Asset* pAsset = openNonAssetInPathLocked( |
Dianne Hackborn | 1704e3c | 2017-10-31 19:55:42 +0000 | [diff] [blame] | 504 | fileName, mode, mAssetPaths.editItemAt(which)); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 505 | if (pAsset != NULL) { |
| 506 | return pAsset != kExcludedAsset ? pAsset : NULL; |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | return NULL; |
| 511 | } |
| 512 | |
| 513 | /* |
| 514 | * Get the type of a file in the asset namespace. |
| 515 | * |
| 516 | * This currently only works for regular files. All others (including |
| 517 | * directories) will return kFileTypeNonexistent. |
| 518 | */ |
| 519 | FileType AssetManager::getFileType(const char* fileName) |
| 520 | { |
| 521 | Asset* pAsset = NULL; |
| 522 | |
| 523 | /* |
| 524 | * Open the asset. This is less efficient than simply finding the |
| 525 | * file, but it's not too bad (we don't uncompress or mmap data until |
| 526 | * the first read() call). |
| 527 | */ |
| 528 | pAsset = open(fileName, Asset::ACCESS_STREAMING); |
| 529 | delete pAsset; |
| 530 | |
Adam Lesinski | fe90eaf | 2016-10-04 13:31:31 -0700 | [diff] [blame] | 531 | if (pAsset == NULL) { |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 532 | return kFileTypeNonexistent; |
Adam Lesinski | fe90eaf | 2016-10-04 13:31:31 -0700 | [diff] [blame] | 533 | } else { |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 534 | return kFileTypeRegular; |
Adam Lesinski | fe90eaf | 2016-10-04 13:31:31 -0700 | [diff] [blame] | 535 | } |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 536 | } |
| 537 | |
Dianne Hackborn | 1704e3c | 2017-10-31 19:55:42 +0000 | [diff] [blame] | 538 | bool AssetManager::appendPathToResTable(asset_path& ap, bool appAsLib) const { |
Jaekyun Seok | 7de2f9c | 2017-03-02 12:45:10 +0900 | [diff] [blame] | 539 | // skip those ap's that correspond to system overlays |
| 540 | if (ap.isSystemOverlay) { |
| 541 | return true; |
| 542 | } |
| 543 | |
Martin Kosiba | 7df3625 | 2014-01-16 16:25:56 +0000 | [diff] [blame] | 544 | Asset* ass = NULL; |
| 545 | ResTable* sharedRes = NULL; |
| 546 | bool shared = true; |
| 547 | bool onlyEmptyResources = true; |
Adam Lesinski | b7e1ce0 | 2016-04-11 20:03:01 -0700 | [diff] [blame] | 548 | ATRACE_NAME(ap.path.string()); |
Martin Kosiba | 7df3625 | 2014-01-16 16:25:56 +0000 | [diff] [blame] | 549 | Asset* idmap = openIdmapLocked(ap); |
| 550 | size_t nextEntryIdx = mResources->getTableCount(); |
| 551 | ALOGV("Looking for resource asset in '%s'\n", ap.path.string()); |
Dianne Hackborn | ca3872c | 2017-10-30 14:19:32 -0700 | [diff] [blame] | 552 | if (ap.type != kFileTypeDirectory && ap.rawFd < 0) { |
Martin Kosiba | 7df3625 | 2014-01-16 16:25:56 +0000 | [diff] [blame] | 553 | if (nextEntryIdx == 0) { |
| 554 | // The first item is typically the framework resources, |
| 555 | // which we want to avoid parsing every time. |
| 556 | sharedRes = const_cast<AssetManager*>(this)-> |
| 557 | mZipSet.getZipResourceTable(ap.path); |
| 558 | if (sharedRes != NULL) { |
| 559 | // skip ahead the number of system overlay packages preloaded |
| 560 | nextEntryIdx = sharedRes->getTableCount(); |
| 561 | } |
| 562 | } |
| 563 | if (sharedRes == NULL) { |
| 564 | ass = const_cast<AssetManager*>(this)-> |
| 565 | mZipSet.getZipResourceTableAsset(ap.path); |
| 566 | if (ass == NULL) { |
| 567 | ALOGV("loading resource table %s\n", ap.path.string()); |
| 568 | ass = const_cast<AssetManager*>(this)-> |
| 569 | openNonAssetInPathLocked("resources.arsc", |
| 570 | Asset::ACCESS_BUFFER, |
| 571 | ap); |
| 572 | if (ass != NULL && ass != kExcludedAsset) { |
| 573 | ass = const_cast<AssetManager*>(this)-> |
| 574 | mZipSet.setZipResourceTableAsset(ap.path, ass); |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | if (nextEntryIdx == 0 && ass != NULL) { |
| 579 | // If this is the first resource table in the asset |
| 580 | // manager, then we are going to cache it so that we |
| 581 | // can quickly copy it out for others. |
| 582 | ALOGV("Creating shared resources for %s", ap.path.string()); |
| 583 | sharedRes = new ResTable(); |
| 584 | sharedRes->add(ass, idmap, nextEntryIdx + 1, false); |
Jaekyun Seok | 7de2f9c | 2017-03-02 12:45:10 +0900 | [diff] [blame] | 585 | #ifdef __ANDROID__ |
| 586 | const char* data = getenv("ANDROID_DATA"); |
| 587 | LOG_ALWAYS_FATAL_IF(data == NULL, "ANDROID_DATA not set"); |
| 588 | String8 overlaysListPath(data); |
| 589 | overlaysListPath.appendPath(kResourceCache); |
| 590 | overlaysListPath.appendPath("overlays.list"); |
| 591 | addSystemOverlays(overlaysListPath.string(), ap.path, sharedRes, nextEntryIdx); |
| 592 | #endif |
Martin Kosiba | 7df3625 | 2014-01-16 16:25:56 +0000 | [diff] [blame] | 593 | sharedRes = const_cast<AssetManager*>(this)-> |
| 594 | mZipSet.setZipResourceTable(ap.path, sharedRes); |
| 595 | } |
| 596 | } |
| 597 | } else { |
| 598 | ALOGV("loading resource table %s\n", ap.path.string()); |
| 599 | ass = const_cast<AssetManager*>(this)-> |
| 600 | openNonAssetInPathLocked("resources.arsc", |
| 601 | Asset::ACCESS_BUFFER, |
| 602 | ap); |
| 603 | shared = false; |
| 604 | } |
| 605 | |
| 606 | if ((ass != NULL || sharedRes != NULL) && ass != kExcludedAsset) { |
| 607 | ALOGV("Installing resource asset %p in to table %p\n", ass, mResources); |
| 608 | if (sharedRes != NULL) { |
| 609 | ALOGV("Copying existing resources for %s", ap.path.string()); |
Roozbeh Pournader | 1c686f2 | 2015-12-18 14:22:14 -0800 | [diff] [blame] | 610 | mResources->add(sharedRes, ap.isSystemAsset); |
Martin Kosiba | 7df3625 | 2014-01-16 16:25:56 +0000 | [diff] [blame] | 611 | } else { |
| 612 | ALOGV("Parsing resources for %s", ap.path.string()); |
Roozbeh Pournader | 1c686f2 | 2015-12-18 14:22:14 -0800 | [diff] [blame] | 613 | mResources->add(ass, idmap, nextEntryIdx + 1, !shared, appAsLib, ap.isSystemAsset); |
Martin Kosiba | 7df3625 | 2014-01-16 16:25:56 +0000 | [diff] [blame] | 614 | } |
| 615 | onlyEmptyResources = false; |
| 616 | |
| 617 | if (!shared) { |
| 618 | delete ass; |
| 619 | } |
| 620 | } else { |
| 621 | ALOGV("Installing empty resources in to table %p\n", mResources); |
| 622 | mResources->addEmpty(nextEntryIdx + 1); |
| 623 | } |
| 624 | |
| 625 | if (idmap != NULL) { |
| 626 | delete idmap; |
| 627 | } |
Martin Kosiba | 7df3625 | 2014-01-16 16:25:56 +0000 | [diff] [blame] | 628 | return onlyEmptyResources; |
| 629 | } |
| 630 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 631 | const ResTable* AssetManager::getResTable(bool required) const |
| 632 | { |
| 633 | ResTable* rt = mResources; |
| 634 | if (rt) { |
| 635 | return rt; |
| 636 | } |
| 637 | |
| 638 | // Iterate through all asset packages, collecting resources from each. |
| 639 | |
| 640 | AutoMutex _l(mLock); |
| 641 | |
| 642 | if (mResources != NULL) { |
| 643 | return mResources; |
| 644 | } |
| 645 | |
| 646 | if (required) { |
| 647 | LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager"); |
| 648 | } |
| 649 | |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 650 | mResources = new ResTable(); |
| 651 | updateResourceParamsLocked(); |
| 652 | |
| 653 | bool onlyEmptyResources = true; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 654 | const size_t N = mAssetPaths.size(); |
| 655 | for (size_t i=0; i<N; i++) { |
Dianne Hackborn | 1704e3c | 2017-10-31 19:55:42 +0000 | [diff] [blame] | 656 | bool empty = appendPathToResTable( |
| 657 | const_cast<AssetManager*>(this)->mAssetPaths.editItemAt(i)); |
Martin Kosiba | 7df3625 | 2014-01-16 16:25:56 +0000 | [diff] [blame] | 658 | onlyEmptyResources = onlyEmptyResources && empty; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 659 | } |
| 660 | |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 661 | if (required && onlyEmptyResources) { |
| 662 | ALOGW("Unable to find resources file resources.arsc"); |
| 663 | delete mResources; |
| 664 | mResources = NULL; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 665 | } |
Adam Lesinski | de898ff | 2014-01-29 18:20:45 -0800 | [diff] [blame] | 666 | |
| 667 | return mResources; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | void AssetManager::updateResourceParamsLocked() const |
| 671 | { |
Adam Lesinski | b7e1ce0 | 2016-04-11 20:03:01 -0700 | [diff] [blame] | 672 | ATRACE_CALL(); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 673 | ResTable* res = mResources; |
| 674 | if (!res) { |
| 675 | return; |
| 676 | } |
| 677 | |
Narayan Kamath | 91447d8 | 2014-01-21 15:32:36 +0000 | [diff] [blame] | 678 | if (mLocale) { |
| 679 | mConfig->setBcp47Locale(mLocale); |
| 680 | } else { |
| 681 | mConfig->clearLocale(); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 682 | } |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 683 | |
| 684 | res->setParameters(mConfig); |
| 685 | } |
| 686 | |
| 687 | Asset* AssetManager::openIdmapLocked(const struct asset_path& ap) const |
| 688 | { |
| 689 | Asset* ass = NULL; |
| 690 | if (ap.idmap.size() != 0) { |
| 691 | ass = const_cast<AssetManager*>(this)-> |
| 692 | openAssetFromFileLocked(ap.idmap, Asset::ACCESS_BUFFER); |
| 693 | if (ass) { |
| 694 | ALOGV("loading idmap %s\n", ap.idmap.string()); |
| 695 | } else { |
| 696 | ALOGW("failed to load idmap %s\n", ap.idmap.string()); |
| 697 | } |
| 698 | } |
| 699 | return ass; |
| 700 | } |
| 701 | |
Jaekyun Seok | 7de2f9c | 2017-03-02 12:45:10 +0900 | [diff] [blame] | 702 | void AssetManager::addSystemOverlays(const char* pathOverlaysList, |
| 703 | const String8& targetPackagePath, ResTable* sharedRes, size_t offset) const |
| 704 | { |
| 705 | FILE* fin = fopen(pathOverlaysList, "r"); |
| 706 | if (fin == NULL) { |
| 707 | return; |
| 708 | } |
| 709 | |
| 710 | #ifndef _WIN32 |
| 711 | if (TEMP_FAILURE_RETRY(flock(fileno(fin), LOCK_SH)) != 0) { |
| 712 | fclose(fin); |
| 713 | return; |
| 714 | } |
| 715 | #endif |
| 716 | char buf[1024]; |
| 717 | while (fgets(buf, sizeof(buf), fin)) { |
| 718 | // format of each line: |
| 719 | // <path to apk><space><path to idmap><newline> |
| 720 | char* space = strchr(buf, ' '); |
| 721 | char* newline = strchr(buf, '\n'); |
| 722 | asset_path oap; |
| 723 | |
| 724 | if (space == NULL || newline == NULL || newline < space) { |
| 725 | continue; |
| 726 | } |
| 727 | |
| 728 | oap.path = String8(buf, space - buf); |
| 729 | oap.type = kFileTypeRegular; |
| 730 | oap.idmap = String8(space + 1, newline - space - 1); |
| 731 | oap.isSystemOverlay = true; |
| 732 | |
| 733 | Asset* oass = const_cast<AssetManager*>(this)-> |
| 734 | openNonAssetInPathLocked("resources.arsc", |
| 735 | Asset::ACCESS_BUFFER, |
| 736 | oap); |
| 737 | |
| 738 | if (oass != NULL) { |
| 739 | Asset* oidmap = openIdmapLocked(oap); |
| 740 | offset++; |
| 741 | sharedRes->add(oass, oidmap, offset + 1, false); |
| 742 | const_cast<AssetManager*>(this)->mAssetPaths.add(oap); |
| 743 | const_cast<AssetManager*>(this)->mZipSet.addOverlay(targetPackagePath, oap); |
| 744 | delete oidmap; |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | #ifndef _WIN32 |
| 749 | TEMP_FAILURE_RETRY(flock(fileno(fin), LOCK_UN)); |
| 750 | #endif |
| 751 | fclose(fin); |
| 752 | } |
| 753 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 754 | const ResTable& AssetManager::getResources(bool required) const |
| 755 | { |
| 756 | const ResTable* rt = getResTable(required); |
| 757 | return *rt; |
| 758 | } |
| 759 | |
| 760 | bool AssetManager::isUpToDate() |
| 761 | { |
| 762 | AutoMutex _l(mLock); |
| 763 | return mZipSet.isUpToDate(); |
| 764 | } |
| 765 | |
Roozbeh Pournader | 1c686f2 | 2015-12-18 14:22:14 -0800 | [diff] [blame] | 766 | void AssetManager::getLocales(Vector<String8>* locales, bool includeSystemLocales) const |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 767 | { |
| 768 | ResTable* res = mResources; |
| 769 | if (res != NULL) { |
Roozbeh Pournader | 7e5f96f | 2016-06-13 18:10:49 -0700 | [diff] [blame] | 770 | res->getLocales(locales, includeSystemLocales, true /* mergeEquivalentLangs */); |
Narayan Kamath | e4345db | 2014-06-26 16:01:28 +0100 | [diff] [blame] | 771 | } |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 772 | } |
| 773 | |
| 774 | /* |
| 775 | * Open a non-asset file as if it were an asset, searching for it in the |
| 776 | * specified app. |
| 777 | * |
| 778 | * Pass in a NULL values for "appName" if the common app directory should |
| 779 | * be used. |
| 780 | */ |
| 781 | Asset* AssetManager::openNonAssetInPathLocked(const char* fileName, AccessMode mode, |
Dianne Hackborn | 1704e3c | 2017-10-31 19:55:42 +0000 | [diff] [blame] | 782 | asset_path& ap) |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 783 | { |
| 784 | Asset* pAsset = NULL; |
| 785 | |
Dianne Hackborn | ca3872c | 2017-10-30 14:19:32 -0700 | [diff] [blame] | 786 | ALOGV("openNonAssetInPath: name=%s type=%d fd=%d", fileName, ap.type, ap.rawFd); |
| 787 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 788 | /* look at the filesystem on disk */ |
| 789 | if (ap.type == kFileTypeDirectory) { |
| 790 | String8 path(ap.path); |
| 791 | path.appendPath(fileName); |
| 792 | |
| 793 | pAsset = openAssetFromFileLocked(path, mode); |
| 794 | |
| 795 | if (pAsset == NULL) { |
| 796 | /* try again, this time with ".gz" */ |
| 797 | path.append(".gz"); |
| 798 | pAsset = openAssetFromFileLocked(path, mode); |
| 799 | } |
| 800 | |
| 801 | if (pAsset != NULL) { |
Dianne Hackborn | ca3872c | 2017-10-30 14:19:32 -0700 | [diff] [blame] | 802 | ALOGV("FOUND NA '%s' on disk", fileName); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 803 | pAsset->setAssetSource(path); |
| 804 | } |
| 805 | |
| 806 | /* look inside the zip file */ |
| 807 | } else { |
| 808 | String8 path(fileName); |
| 809 | |
| 810 | /* check the appropriate Zip file */ |
Narayan Kamath | 560566d | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 811 | ZipFileRO* pZip = getZipFileLocked(ap); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 812 | if (pZip != NULL) { |
Dianne Hackborn | ca3872c | 2017-10-30 14:19:32 -0700 | [diff] [blame] | 813 | ALOGV("GOT zip, checking NA '%s'", (const char*) path); |
Narayan Kamath | 560566d | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 814 | ZipEntryRO entry = pZip->findEntryByName(path.string()); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 815 | if (entry != NULL) { |
Dianne Hackborn | ca3872c | 2017-10-30 14:19:32 -0700 | [diff] [blame] | 816 | ALOGV("FOUND NA in Zip file for %s", (const char*) path); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 817 | pAsset = openAssetFromZipLocked(pZip, entry, mode, path); |
Narayan Kamath | 560566d | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 818 | pZip->releaseEntry(entry); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 819 | } |
| 820 | } |
| 821 | |
| 822 | if (pAsset != NULL) { |
| 823 | /* create a "source" name, for debug/display */ |
| 824 | pAsset->setAssetSource( |
| 825 | createZipSourceNameLocked(ZipSet::getPathName(ap.path.string()), String8(""), |
| 826 | String8(fileName))); |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | return pAsset; |
| 831 | } |
| 832 | |
| 833 | /* |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 834 | * Create a "source name" for a file from a Zip archive. |
| 835 | */ |
| 836 | String8 AssetManager::createZipSourceNameLocked(const String8& zipFileName, |
| 837 | const String8& dirName, const String8& fileName) |
| 838 | { |
| 839 | String8 sourceName("zip:"); |
| 840 | sourceName.append(zipFileName); |
| 841 | sourceName.append(":"); |
| 842 | if (dirName.length() > 0) { |
| 843 | sourceName.appendPath(dirName); |
| 844 | } |
| 845 | sourceName.appendPath(fileName); |
| 846 | return sourceName; |
| 847 | } |
| 848 | |
| 849 | /* |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 850 | * Create a path to a loose asset (asset-base/app/rootDir). |
| 851 | */ |
| 852 | String8 AssetManager::createPathNameLocked(const asset_path& ap, const char* rootDir) |
| 853 | { |
| 854 | String8 path(ap.path); |
| 855 | if (rootDir != NULL) path.appendPath(rootDir); |
| 856 | return path; |
| 857 | } |
| 858 | |
| 859 | /* |
| 860 | * Return a pointer to one of our open Zip archives. Returns NULL if no |
| 861 | * matching Zip file exists. |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 862 | */ |
Dianne Hackborn | 1704e3c | 2017-10-31 19:55:42 +0000 | [diff] [blame] | 863 | ZipFileRO* AssetManager::getZipFileLocked(asset_path& ap) |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 864 | { |
Dianne Hackborn | 1704e3c | 2017-10-31 19:55:42 +0000 | [diff] [blame] | 865 | ALOGV("getZipFileLocked() in %p: ap=%p zip=%p", this, &ap, ap.zip.get()); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 866 | |
Dianne Hackborn | ca3872c | 2017-10-30 14:19:32 -0700 | [diff] [blame] | 867 | if (ap.zip != NULL) { |
| 868 | return ap.zip->getZip(); |
| 869 | } |
| 870 | |
| 871 | if (ap.rawFd < 0) { |
Dianne Hackborn | 1704e3c | 2017-10-31 19:55:42 +0000 | [diff] [blame] | 872 | ALOGV("getZipFileLocked: Creating new zip from path %s", ap.path.string()); |
Dianne Hackborn | ca3872c | 2017-10-30 14:19:32 -0700 | [diff] [blame] | 873 | ap.zip = mZipSet.getSharedZip(ap.path); |
| 874 | } else { |
Dianne Hackborn | 1704e3c | 2017-10-31 19:55:42 +0000 | [diff] [blame] | 875 | ALOGV("getZipFileLocked: Creating new zip from fd %d", ap.rawFd); |
Dianne Hackborn | ca3872c | 2017-10-30 14:19:32 -0700 | [diff] [blame] | 876 | ap.zip = SharedZip::create(ap.rawFd, ap.path); |
| 877 | |
| 878 | } |
| 879 | return ap.zip != NULL ? ap.zip->getZip() : NULL; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | /* |
| 883 | * Try to open an asset from a file on disk. |
| 884 | * |
| 885 | * If the file is compressed with gzip, we seek to the start of the |
| 886 | * deflated data and pass that in (just like we would for a Zip archive). |
| 887 | * |
| 888 | * For uncompressed data, we may already have an mmap()ed version sitting |
| 889 | * around. If so, we want to hand that to the Asset instead. |
| 890 | * |
| 891 | * This returns NULL if the file doesn't exist, couldn't be opened, or |
| 892 | * claims to be a ".gz" but isn't. |
| 893 | */ |
| 894 | Asset* AssetManager::openAssetFromFileLocked(const String8& pathName, |
| 895 | AccessMode mode) |
| 896 | { |
| 897 | Asset* pAsset = NULL; |
| 898 | |
| 899 | if (strcasecmp(pathName.getPathExtension().string(), ".gz") == 0) { |
| 900 | //printf("TRYING '%s'\n", (const char*) pathName); |
| 901 | pAsset = Asset::createFromCompressedFile(pathName.string(), mode); |
| 902 | } else { |
| 903 | //printf("TRYING '%s'\n", (const char*) pathName); |
| 904 | pAsset = Asset::createFromFile(pathName.string(), mode); |
| 905 | } |
| 906 | |
| 907 | return pAsset; |
| 908 | } |
| 909 | |
| 910 | /* |
| 911 | * Given an entry in a Zip archive, create a new Asset object. |
| 912 | * |
| 913 | * If the entry is uncompressed, we may want to create or share a |
| 914 | * slice of shared memory. |
| 915 | */ |
| 916 | Asset* AssetManager::openAssetFromZipLocked(const ZipFileRO* pZipFile, |
| 917 | const ZipEntryRO entry, AccessMode mode, const String8& entryName) |
| 918 | { |
| 919 | Asset* pAsset = NULL; |
| 920 | |
| 921 | // TODO: look for previously-created shared memory slice? |
Narayan Kamath | 407753c | 2015-06-16 12:02:57 +0100 | [diff] [blame] | 922 | uint16_t method; |
| 923 | uint32_t uncompressedLen; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 924 | |
| 925 | //printf("USING Zip '%s'\n", pEntry->getFileName()); |
| 926 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 927 | if (!pZipFile->getEntryInfo(entry, &method, &uncompressedLen, NULL, NULL, |
| 928 | NULL, NULL)) |
| 929 | { |
| 930 | ALOGW("getEntryInfo failed\n"); |
| 931 | return NULL; |
| 932 | } |
| 933 | |
| 934 | FileMap* dataMap = pZipFile->createEntryFileMap(entry); |
| 935 | if (dataMap == NULL) { |
| 936 | ALOGW("create map from entry failed\n"); |
| 937 | return NULL; |
| 938 | } |
| 939 | |
| 940 | if (method == ZipFileRO::kCompressStored) { |
| 941 | pAsset = Asset::createFromUncompressedMap(dataMap, mode); |
| 942 | ALOGV("Opened uncompressed entry %s in zip %s mode %d: %p", entryName.string(), |
| 943 | dataMap->getFileName(), mode, pAsset); |
| 944 | } else { |
Narayan Kamath | 407753c | 2015-06-16 12:02:57 +0100 | [diff] [blame] | 945 | pAsset = Asset::createFromCompressedMap(dataMap, |
| 946 | static_cast<size_t>(uncompressedLen), mode); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 947 | ALOGV("Opened compressed entry %s in zip %s mode %d: %p", entryName.string(), |
| 948 | dataMap->getFileName(), mode, pAsset); |
| 949 | } |
| 950 | if (pAsset == NULL) { |
| 951 | /* unexpected */ |
| 952 | ALOGW("create from segment failed\n"); |
| 953 | } |
| 954 | |
| 955 | return pAsset; |
| 956 | } |
| 957 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 958 | /* |
| 959 | * Open a directory in the asset namespace. |
| 960 | * |
Adam Lesinski | fe90eaf | 2016-10-04 13:31:31 -0700 | [diff] [blame] | 961 | * An "asset directory" is simply the combination of all asset paths' "assets/" directories. |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 962 | * |
| 963 | * Pass in "" for the root dir. |
| 964 | */ |
| 965 | AssetDir* AssetManager::openDir(const char* dirName) |
| 966 | { |
| 967 | AutoMutex _l(mLock); |
| 968 | |
| 969 | AssetDir* pDir = NULL; |
| 970 | SortedVector<AssetDir::FileInfo>* pMergedInfo = NULL; |
| 971 | |
| 972 | LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager"); |
| 973 | assert(dirName != NULL); |
| 974 | |
| 975 | //printf("+++ openDir(%s) in '%s'\n", dirName, (const char*) mAssetBase); |
| 976 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 977 | pDir = new AssetDir; |
| 978 | |
| 979 | /* |
| 980 | * Scan the various directories, merging what we find into a single |
| 981 | * vector. We want to scan them in reverse priority order so that |
| 982 | * the ".EXCLUDE" processing works correctly. Also, if we decide we |
| 983 | * want to remember where the file is coming from, we'll get the right |
| 984 | * version. |
| 985 | * |
| 986 | * We start with Zip archives, then do loose files. |
| 987 | */ |
| 988 | pMergedInfo = new SortedVector<AssetDir::FileInfo>; |
| 989 | |
| 990 | size_t i = mAssetPaths.size(); |
| 991 | while (i > 0) { |
| 992 | i--; |
| 993 | const asset_path& ap = mAssetPaths.itemAt(i); |
| 994 | if (ap.type == kFileTypeRegular) { |
| 995 | ALOGV("Adding directory %s from zip %s", dirName, ap.path.string()); |
| 996 | scanAndMergeZipLocked(pMergedInfo, ap, kAssetsRoot, dirName); |
| 997 | } else { |
| 998 | ALOGV("Adding directory %s from dir %s", dirName, ap.path.string()); |
| 999 | scanAndMergeDirLocked(pMergedInfo, ap, kAssetsRoot, dirName); |
| 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | #if 0 |
| 1004 | printf("FILE LIST:\n"); |
| 1005 | for (i = 0; i < (size_t) pMergedInfo->size(); i++) { |
| 1006 | printf(" %d: (%d) '%s'\n", i, |
| 1007 | pMergedInfo->itemAt(i).getFileType(), |
| 1008 | (const char*) pMergedInfo->itemAt(i).getFileName()); |
| 1009 | } |
| 1010 | #endif |
| 1011 | |
| 1012 | pDir->setFileList(pMergedInfo); |
| 1013 | return pDir; |
| 1014 | } |
| 1015 | |
| 1016 | /* |
| 1017 | * Open a directory in the non-asset namespace. |
| 1018 | * |
Adam Lesinski | fe90eaf | 2016-10-04 13:31:31 -0700 | [diff] [blame] | 1019 | * An "asset directory" is simply the combination of all asset paths' "assets/" directories. |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1020 | * |
| 1021 | * Pass in "" for the root dir. |
| 1022 | */ |
Narayan Kamath | a0c6260 | 2014-01-24 13:51:51 +0000 | [diff] [blame] | 1023 | AssetDir* AssetManager::openNonAssetDir(const int32_t cookie, const char* dirName) |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1024 | { |
| 1025 | AutoMutex _l(mLock); |
| 1026 | |
| 1027 | AssetDir* pDir = NULL; |
| 1028 | SortedVector<AssetDir::FileInfo>* pMergedInfo = NULL; |
| 1029 | |
| 1030 | LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager"); |
| 1031 | assert(dirName != NULL); |
| 1032 | |
| 1033 | //printf("+++ openDir(%s) in '%s'\n", dirName, (const char*) mAssetBase); |
| 1034 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1035 | pDir = new AssetDir; |
| 1036 | |
| 1037 | pMergedInfo = new SortedVector<AssetDir::FileInfo>; |
| 1038 | |
Narayan Kamath | a0c6260 | 2014-01-24 13:51:51 +0000 | [diff] [blame] | 1039 | const size_t which = static_cast<size_t>(cookie) - 1; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1040 | |
| 1041 | if (which < mAssetPaths.size()) { |
| 1042 | const asset_path& ap = mAssetPaths.itemAt(which); |
| 1043 | if (ap.type == kFileTypeRegular) { |
| 1044 | ALOGV("Adding directory %s from zip %s", dirName, ap.path.string()); |
| 1045 | scanAndMergeZipLocked(pMergedInfo, ap, NULL, dirName); |
| 1046 | } else { |
| 1047 | ALOGV("Adding directory %s from dir %s", dirName, ap.path.string()); |
| 1048 | scanAndMergeDirLocked(pMergedInfo, ap, NULL, dirName); |
| 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | #if 0 |
| 1053 | printf("FILE LIST:\n"); |
| 1054 | for (i = 0; i < (size_t) pMergedInfo->size(); i++) { |
| 1055 | printf(" %d: (%d) '%s'\n", i, |
| 1056 | pMergedInfo->itemAt(i).getFileType(), |
| 1057 | (const char*) pMergedInfo->itemAt(i).getFileName()); |
| 1058 | } |
| 1059 | #endif |
| 1060 | |
| 1061 | pDir->setFileList(pMergedInfo); |
| 1062 | return pDir; |
| 1063 | } |
| 1064 | |
| 1065 | /* |
| 1066 | * Scan the contents of the specified directory and merge them into the |
| 1067 | * "pMergedInfo" vector, removing previous entries if we find "exclude" |
| 1068 | * directives. |
| 1069 | * |
| 1070 | * Returns "false" if we found nothing to contribute. |
| 1071 | */ |
| 1072 | bool AssetManager::scanAndMergeDirLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo, |
| 1073 | const asset_path& ap, const char* rootDir, const char* dirName) |
| 1074 | { |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1075 | assert(pMergedInfo != NULL); |
| 1076 | |
Adam Lesinski | a77685f | 2016-10-03 16:26:28 -0700 | [diff] [blame] | 1077 | //printf("scanAndMergeDir: %s %s %s\n", ap.path.string(), rootDir, dirName); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1078 | |
Adam Lesinski | a77685f | 2016-10-03 16:26:28 -0700 | [diff] [blame] | 1079 | String8 path = createPathNameLocked(ap, rootDir); |
| 1080 | if (dirName[0] != '\0') |
| 1081 | path.appendPath(dirName); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1082 | |
Adam Lesinski | a77685f | 2016-10-03 16:26:28 -0700 | [diff] [blame] | 1083 | SortedVector<AssetDir::FileInfo>* pContents = scanDirLocked(path); |
| 1084 | if (pContents == NULL) |
| 1085 | return false; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1086 | |
| 1087 | // if we wanted to do an incremental cache fill, we would do it here |
| 1088 | |
| 1089 | /* |
| 1090 | * Process "exclude" directives. If we find a filename that ends with |
| 1091 | * ".EXCLUDE", we look for a matching entry in the "merged" set, and |
| 1092 | * remove it if we find it. We also delete the "exclude" entry. |
| 1093 | */ |
| 1094 | int i, count, exclExtLen; |
| 1095 | |
| 1096 | count = pContents->size(); |
| 1097 | exclExtLen = strlen(kExcludeExtension); |
| 1098 | for (i = 0; i < count; i++) { |
| 1099 | const char* name; |
| 1100 | int nameLen; |
| 1101 | |
| 1102 | name = pContents->itemAt(i).getFileName().string(); |
| 1103 | nameLen = strlen(name); |
| 1104 | if (nameLen > exclExtLen && |
| 1105 | strcmp(name + (nameLen - exclExtLen), kExcludeExtension) == 0) |
| 1106 | { |
| 1107 | String8 match(name, nameLen - exclExtLen); |
| 1108 | int matchIdx; |
| 1109 | |
| 1110 | matchIdx = AssetDir::FileInfo::findEntry(pMergedInfo, match); |
| 1111 | if (matchIdx > 0) { |
| 1112 | ALOGV("Excluding '%s' [%s]\n", |
| 1113 | pMergedInfo->itemAt(matchIdx).getFileName().string(), |
| 1114 | pMergedInfo->itemAt(matchIdx).getSourceName().string()); |
| 1115 | pMergedInfo->removeAt(matchIdx); |
| 1116 | } else { |
| 1117 | //printf("+++ no match on '%s'\n", (const char*) match); |
| 1118 | } |
| 1119 | |
| 1120 | ALOGD("HEY: size=%d removing %d\n", (int)pContents->size(), i); |
| 1121 | pContents->removeAt(i); |
| 1122 | i--; // adjust "for" loop |
| 1123 | count--; // and loop limit |
| 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | mergeInfoLocked(pMergedInfo, pContents); |
| 1128 | |
| 1129 | delete pContents; |
| 1130 | |
| 1131 | return true; |
| 1132 | } |
| 1133 | |
| 1134 | /* |
| 1135 | * Scan the contents of the specified directory, and stuff what we find |
| 1136 | * into a newly-allocated vector. |
| 1137 | * |
| 1138 | * Files ending in ".gz" will have their extensions removed. |
| 1139 | * |
| 1140 | * We should probably think about skipping files with "illegal" names, |
| 1141 | * e.g. illegal characters (/\:) or excessive length. |
| 1142 | * |
| 1143 | * Returns NULL if the specified directory doesn't exist. |
| 1144 | */ |
| 1145 | SortedVector<AssetDir::FileInfo>* AssetManager::scanDirLocked(const String8& path) |
| 1146 | { |
| 1147 | SortedVector<AssetDir::FileInfo>* pContents = NULL; |
| 1148 | DIR* dir; |
| 1149 | struct dirent* entry; |
| 1150 | FileType fileType; |
| 1151 | |
| 1152 | ALOGV("Scanning dir '%s'\n", path.string()); |
| 1153 | |
| 1154 | dir = opendir(path.string()); |
| 1155 | if (dir == NULL) |
| 1156 | return NULL; |
| 1157 | |
| 1158 | pContents = new SortedVector<AssetDir::FileInfo>; |
| 1159 | |
| 1160 | while (1) { |
| 1161 | entry = readdir(dir); |
| 1162 | if (entry == NULL) |
| 1163 | break; |
| 1164 | |
| 1165 | if (strcmp(entry->d_name, ".") == 0 || |
| 1166 | strcmp(entry->d_name, "..") == 0) |
| 1167 | continue; |
| 1168 | |
| 1169 | #ifdef _DIRENT_HAVE_D_TYPE |
| 1170 | if (entry->d_type == DT_REG) |
| 1171 | fileType = kFileTypeRegular; |
| 1172 | else if (entry->d_type == DT_DIR) |
| 1173 | fileType = kFileTypeDirectory; |
| 1174 | else |
| 1175 | fileType = kFileTypeUnknown; |
| 1176 | #else |
| 1177 | // stat the file |
| 1178 | fileType = ::getFileType(path.appendPathCopy(entry->d_name).string()); |
| 1179 | #endif |
| 1180 | |
| 1181 | if (fileType != kFileTypeRegular && fileType != kFileTypeDirectory) |
| 1182 | continue; |
| 1183 | |
| 1184 | AssetDir::FileInfo info; |
| 1185 | info.set(String8(entry->d_name), fileType); |
| 1186 | if (strcasecmp(info.getFileName().getPathExtension().string(), ".gz") == 0) |
| 1187 | info.setFileName(info.getFileName().getBasePath()); |
| 1188 | info.setSourceName(path.appendPathCopy(info.getFileName())); |
| 1189 | pContents->add(info); |
| 1190 | } |
| 1191 | |
| 1192 | closedir(dir); |
| 1193 | return pContents; |
| 1194 | } |
| 1195 | |
| 1196 | /* |
| 1197 | * Scan the contents out of the specified Zip archive, and merge what we |
| 1198 | * find into "pMergedInfo". If the Zip archive in question doesn't exist, |
| 1199 | * we return immediately. |
| 1200 | * |
| 1201 | * Returns "false" if we found nothing to contribute. |
| 1202 | */ |
| 1203 | bool AssetManager::scanAndMergeZipLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo, |
| 1204 | const asset_path& ap, const char* rootDir, const char* baseDirName) |
| 1205 | { |
| 1206 | ZipFileRO* pZip; |
| 1207 | Vector<String8> dirs; |
| 1208 | AssetDir::FileInfo info; |
| 1209 | SortedVector<AssetDir::FileInfo> contents; |
| 1210 | String8 sourceName, zipName, dirName; |
| 1211 | |
| 1212 | pZip = mZipSet.getZip(ap.path); |
| 1213 | if (pZip == NULL) { |
| 1214 | ALOGW("Failure opening zip %s\n", ap.path.string()); |
| 1215 | return false; |
| 1216 | } |
| 1217 | |
| 1218 | zipName = ZipSet::getPathName(ap.path.string()); |
| 1219 | |
| 1220 | /* convert "sounds" to "rootDir/sounds" */ |
| 1221 | if (rootDir != NULL) dirName = rootDir; |
| 1222 | dirName.appendPath(baseDirName); |
| 1223 | |
| 1224 | /* |
| 1225 | * Scan through the list of files, looking for a match. The files in |
| 1226 | * the Zip table of contents are not in sorted order, so we have to |
| 1227 | * process the entire list. We're looking for a string that begins |
| 1228 | * with the characters in "dirName", is followed by a '/', and has no |
| 1229 | * subsequent '/' in the stuff that follows. |
| 1230 | * |
| 1231 | * What makes this especially fun is that directories are not stored |
| 1232 | * explicitly in Zip archives, so we have to infer them from context. |
| 1233 | * When we see "sounds/foo.wav" we have to leave a note to ourselves |
| 1234 | * to insert a directory called "sounds" into the list. We store |
| 1235 | * these in temporary vector so that we only return each one once. |
| 1236 | * |
| 1237 | * Name comparisons are case-sensitive to match UNIX filesystem |
| 1238 | * semantics. |
| 1239 | */ |
| 1240 | int dirNameLen = dirName.length(); |
Narayan Kamath | 560566d | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 1241 | void *iterationCookie; |
Yusuke Sato | 05f648e | 2015-08-03 16:21:10 -0700 | [diff] [blame] | 1242 | if (!pZip->startIteration(&iterationCookie, dirName.string(), NULL)) { |
Narayan Kamath | 560566d | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 1243 | ALOGW("ZipFileRO::startIteration returned false"); |
| 1244 | return false; |
| 1245 | } |
| 1246 | |
| 1247 | ZipEntryRO entry; |
| 1248 | while ((entry = pZip->nextEntry(iterationCookie)) != NULL) { |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1249 | char nameBuf[256]; |
| 1250 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1251 | if (pZip->getEntryFileName(entry, nameBuf, sizeof(nameBuf)) != 0) { |
| 1252 | // TODO: fix this if we expect to have long names |
| 1253 | ALOGE("ARGH: name too long?\n"); |
| 1254 | continue; |
| 1255 | } |
| 1256 | //printf("Comparing %s in %s?\n", nameBuf, dirName.string()); |
Yusuke Sato | 05f648e | 2015-08-03 16:21:10 -0700 | [diff] [blame] | 1257 | if (dirNameLen == 0 || nameBuf[dirNameLen] == '/') |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1258 | { |
| 1259 | const char* cp; |
| 1260 | const char* nextSlash; |
| 1261 | |
| 1262 | cp = nameBuf + dirNameLen; |
| 1263 | if (dirNameLen != 0) |
| 1264 | cp++; // advance past the '/' |
| 1265 | |
| 1266 | nextSlash = strchr(cp, '/'); |
| 1267 | //xxx this may break if there are bare directory entries |
| 1268 | if (nextSlash == NULL) { |
| 1269 | /* this is a file in the requested directory */ |
| 1270 | |
| 1271 | info.set(String8(nameBuf).getPathLeaf(), kFileTypeRegular); |
| 1272 | |
| 1273 | info.setSourceName( |
| 1274 | createZipSourceNameLocked(zipName, dirName, info.getFileName())); |
| 1275 | |
| 1276 | contents.add(info); |
| 1277 | //printf("FOUND: file '%s'\n", info.getFileName().string()); |
| 1278 | } else { |
| 1279 | /* this is a subdir; add it if we don't already have it*/ |
| 1280 | String8 subdirName(cp, nextSlash - cp); |
| 1281 | size_t j; |
| 1282 | size_t N = dirs.size(); |
| 1283 | |
| 1284 | for (j = 0; j < N; j++) { |
| 1285 | if (subdirName == dirs[j]) { |
| 1286 | break; |
| 1287 | } |
| 1288 | } |
| 1289 | if (j == N) { |
| 1290 | dirs.add(subdirName); |
| 1291 | } |
| 1292 | |
| 1293 | //printf("FOUND: dir '%s'\n", subdirName.string()); |
| 1294 | } |
| 1295 | } |
| 1296 | } |
| 1297 | |
Narayan Kamath | 560566d | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 1298 | pZip->endIteration(iterationCookie); |
| 1299 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1300 | /* |
| 1301 | * Add the set of unique directories. |
| 1302 | */ |
| 1303 | for (int i = 0; i < (int) dirs.size(); i++) { |
| 1304 | info.set(dirs[i], kFileTypeDirectory); |
| 1305 | info.setSourceName( |
| 1306 | createZipSourceNameLocked(zipName, dirName, info.getFileName())); |
| 1307 | contents.add(info); |
| 1308 | } |
| 1309 | |
| 1310 | mergeInfoLocked(pMergedInfo, &contents); |
| 1311 | |
| 1312 | return true; |
| 1313 | } |
| 1314 | |
| 1315 | |
| 1316 | /* |
| 1317 | * Merge two vectors of FileInfo. |
| 1318 | * |
| 1319 | * The merged contents will be stuffed into *pMergedInfo. |
| 1320 | * |
| 1321 | * If an entry for a file exists in both "pMergedInfo" and "pContents", |
| 1322 | * we use the newer "pContents" entry. |
| 1323 | */ |
| 1324 | void AssetManager::mergeInfoLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo, |
| 1325 | const SortedVector<AssetDir::FileInfo>* pContents) |
| 1326 | { |
| 1327 | /* |
| 1328 | * Merge what we found in this directory with what we found in |
| 1329 | * other places. |
| 1330 | * |
| 1331 | * Two basic approaches: |
| 1332 | * (1) Create a new array that holds the unique values of the two |
| 1333 | * arrays. |
| 1334 | * (2) Take the elements from pContents and shove them into pMergedInfo. |
| 1335 | * |
| 1336 | * Because these are vectors of complex objects, moving elements around |
| 1337 | * inside the vector requires constructing new objects and allocating |
| 1338 | * storage for members. With approach #1, we're always adding to the |
| 1339 | * end, whereas with #2 we could be inserting multiple elements at the |
| 1340 | * front of the vector. Approach #1 requires a full copy of the |
| 1341 | * contents of pMergedInfo, but approach #2 requires the same copy for |
| 1342 | * every insertion at the front of pMergedInfo. |
| 1343 | * |
| 1344 | * (We should probably use a SortedVector interface that allows us to |
| 1345 | * just stuff items in, trusting us to maintain the sort order.) |
| 1346 | */ |
| 1347 | SortedVector<AssetDir::FileInfo>* pNewSorted; |
| 1348 | int mergeMax, contMax; |
| 1349 | int mergeIdx, contIdx; |
| 1350 | |
| 1351 | pNewSorted = new SortedVector<AssetDir::FileInfo>; |
| 1352 | mergeMax = pMergedInfo->size(); |
| 1353 | contMax = pContents->size(); |
| 1354 | mergeIdx = contIdx = 0; |
| 1355 | |
| 1356 | while (mergeIdx < mergeMax || contIdx < contMax) { |
| 1357 | if (mergeIdx == mergeMax) { |
| 1358 | /* hit end of "merge" list, copy rest of "contents" */ |
| 1359 | pNewSorted->add(pContents->itemAt(contIdx)); |
| 1360 | contIdx++; |
| 1361 | } else if (contIdx == contMax) { |
| 1362 | /* hit end of "cont" list, copy rest of "merge" */ |
| 1363 | pNewSorted->add(pMergedInfo->itemAt(mergeIdx)); |
| 1364 | mergeIdx++; |
| 1365 | } else if (pMergedInfo->itemAt(mergeIdx) == pContents->itemAt(contIdx)) |
| 1366 | { |
| 1367 | /* items are identical, add newer and advance both indices */ |
| 1368 | pNewSorted->add(pContents->itemAt(contIdx)); |
| 1369 | mergeIdx++; |
| 1370 | contIdx++; |
| 1371 | } else if (pMergedInfo->itemAt(mergeIdx) < pContents->itemAt(contIdx)) |
| 1372 | { |
| 1373 | /* "merge" is lower, add that one */ |
| 1374 | pNewSorted->add(pMergedInfo->itemAt(mergeIdx)); |
| 1375 | mergeIdx++; |
| 1376 | } else { |
| 1377 | /* "cont" is lower, add that one */ |
| 1378 | assert(pContents->itemAt(contIdx) < pMergedInfo->itemAt(mergeIdx)); |
| 1379 | pNewSorted->add(pContents->itemAt(contIdx)); |
| 1380 | contIdx++; |
| 1381 | } |
| 1382 | } |
| 1383 | |
| 1384 | /* |
| 1385 | * Overwrite the "merged" list with the new stuff. |
| 1386 | */ |
| 1387 | *pMergedInfo = *pNewSorted; |
| 1388 | delete pNewSorted; |
| 1389 | |
| 1390 | #if 0 // for Vector, rather than SortedVector |
| 1391 | int i, j; |
| 1392 | for (i = pContents->size() -1; i >= 0; i--) { |
| 1393 | bool add = true; |
| 1394 | |
| 1395 | for (j = pMergedInfo->size() -1; j >= 0; j--) { |
| 1396 | /* case-sensitive comparisons, to behave like UNIX fs */ |
| 1397 | if (strcmp(pContents->itemAt(i).mFileName, |
| 1398 | pMergedInfo->itemAt(j).mFileName) == 0) |
| 1399 | { |
| 1400 | /* match, don't add this entry */ |
| 1401 | add = false; |
| 1402 | break; |
| 1403 | } |
| 1404 | } |
| 1405 | |
| 1406 | if (add) |
| 1407 | pMergedInfo->add(pContents->itemAt(i)); |
| 1408 | } |
| 1409 | #endif |
| 1410 | } |
| 1411 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1412 | /* |
| 1413 | * =========================================================================== |
| 1414 | * AssetManager::SharedZip |
| 1415 | * =========================================================================== |
| 1416 | */ |
| 1417 | |
| 1418 | |
| 1419 | Mutex AssetManager::SharedZip::gLock; |
| 1420 | DefaultKeyedVector<String8, wp<AssetManager::SharedZip> > AssetManager::SharedZip::gOpen; |
| 1421 | |
| 1422 | AssetManager::SharedZip::SharedZip(const String8& path, time_t modWhen) |
| 1423 | : mPath(path), mZipFile(NULL), mModWhen(modWhen), |
| 1424 | mResourceTableAsset(NULL), mResourceTable(NULL) |
| 1425 | { |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 1426 | if (kIsDebug) { |
| 1427 | ALOGI("Creating SharedZip %p %s\n", this, (const char*)mPath); |
| 1428 | } |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1429 | ALOGV("+++ opening zip '%s'\n", mPath.string()); |
Narayan Kamath | 560566d | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 1430 | mZipFile = ZipFileRO::open(mPath.string()); |
| 1431 | if (mZipFile == NULL) { |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1432 | ALOGD("failed to open Zip archive '%s'\n", mPath.string()); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1433 | } |
| 1434 | } |
| 1435 | |
Dianne Hackborn | ca3872c | 2017-10-30 14:19:32 -0700 | [diff] [blame] | 1436 | AssetManager::SharedZip::SharedZip(int fd, const String8& path) |
| 1437 | : mPath(path), mZipFile(NULL), mModWhen(0), |
| 1438 | mResourceTableAsset(NULL), mResourceTable(NULL) |
| 1439 | { |
| 1440 | if (kIsDebug) { |
| 1441 | ALOGI("Creating SharedZip %p fd=%d %s\n", this, fd, (const char*)mPath); |
| 1442 | } |
| 1443 | ALOGV("+++ opening zip fd=%d '%s'\n", fd, mPath.string()); |
| 1444 | mZipFile = ZipFileRO::openFd(fd, mPath.string()); |
| 1445 | if (mZipFile == NULL) { |
| 1446 | ::close(fd); |
| 1447 | ALOGD("failed to open Zip archive fd=%d '%s'\n", fd, mPath.string()); |
| 1448 | } |
| 1449 | } |
| 1450 | |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 1451 | sp<AssetManager::SharedZip> AssetManager::SharedZip::get(const String8& path, |
| 1452 | bool createIfNotPresent) |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1453 | { |
| 1454 | AutoMutex _l(gLock); |
| 1455 | time_t modWhen = getFileModDate(path); |
| 1456 | sp<SharedZip> zip = gOpen.valueFor(path).promote(); |
| 1457 | if (zip != NULL && zip->mModWhen == modWhen) { |
| 1458 | return zip; |
| 1459 | } |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 1460 | if (zip == NULL && !createIfNotPresent) { |
| 1461 | return NULL; |
| 1462 | } |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1463 | zip = new SharedZip(path, modWhen); |
| 1464 | gOpen.add(path, zip); |
| 1465 | return zip; |
Dianne Hackborn | ca3872c | 2017-10-30 14:19:32 -0700 | [diff] [blame] | 1466 | } |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1467 | |
Dianne Hackborn | ca3872c | 2017-10-30 14:19:32 -0700 | [diff] [blame] | 1468 | sp<AssetManager::SharedZip> AssetManager::SharedZip::create(int fd, const String8& path) |
| 1469 | { |
| 1470 | return new SharedZip(fd, path); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1471 | } |
| 1472 | |
| 1473 | ZipFileRO* AssetManager::SharedZip::getZip() |
| 1474 | { |
| 1475 | return mZipFile; |
| 1476 | } |
| 1477 | |
| 1478 | Asset* AssetManager::SharedZip::getResourceTableAsset() |
| 1479 | { |
songjinshi | 49921f2 | 2016-09-08 15:24:30 +0800 | [diff] [blame] | 1480 | AutoMutex _l(gLock); |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1481 | ALOGV("Getting from SharedZip %p resource asset %p\n", this, mResourceTableAsset); |
| 1482 | return mResourceTableAsset; |
| 1483 | } |
| 1484 | |
| 1485 | Asset* AssetManager::SharedZip::setResourceTableAsset(Asset* asset) |
| 1486 | { |
| 1487 | { |
| 1488 | AutoMutex _l(gLock); |
| 1489 | if (mResourceTableAsset == NULL) { |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1490 | // This is not thread safe the first time it is called, so |
| 1491 | // do it here with the global lock held. |
| 1492 | asset->getBuffer(true); |
songjinshi | 49921f2 | 2016-09-08 15:24:30 +0800 | [diff] [blame] | 1493 | mResourceTableAsset = asset; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1494 | return asset; |
| 1495 | } |
| 1496 | } |
| 1497 | delete asset; |
| 1498 | return mResourceTableAsset; |
| 1499 | } |
| 1500 | |
| 1501 | ResTable* AssetManager::SharedZip::getResourceTable() |
| 1502 | { |
| 1503 | ALOGV("Getting from SharedZip %p resource table %p\n", this, mResourceTable); |
| 1504 | return mResourceTable; |
| 1505 | } |
| 1506 | |
| 1507 | ResTable* AssetManager::SharedZip::setResourceTable(ResTable* res) |
| 1508 | { |
| 1509 | { |
| 1510 | AutoMutex _l(gLock); |
| 1511 | if (mResourceTable == NULL) { |
| 1512 | mResourceTable = res; |
| 1513 | return res; |
| 1514 | } |
| 1515 | } |
| 1516 | delete res; |
| 1517 | return mResourceTable; |
| 1518 | } |
| 1519 | |
| 1520 | bool AssetManager::SharedZip::isUpToDate() |
| 1521 | { |
| 1522 | time_t modWhen = getFileModDate(mPath.string()); |
| 1523 | return mModWhen == modWhen; |
| 1524 | } |
| 1525 | |
Jaekyun Seok | 7de2f9c | 2017-03-02 12:45:10 +0900 | [diff] [blame] | 1526 | void AssetManager::SharedZip::addOverlay(const asset_path& ap) |
| 1527 | { |
| 1528 | mOverlays.add(ap); |
| 1529 | } |
| 1530 | |
| 1531 | bool AssetManager::SharedZip::getOverlay(size_t idx, asset_path* out) const |
| 1532 | { |
| 1533 | if (idx >= mOverlays.size()) { |
| 1534 | return false; |
| 1535 | } |
| 1536 | *out = mOverlays[idx]; |
| 1537 | return true; |
| 1538 | } |
| 1539 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1540 | AssetManager::SharedZip::~SharedZip() |
| 1541 | { |
Andreas Gampe | 2204f0b | 2014-10-21 23:04:54 -0700 | [diff] [blame] | 1542 | if (kIsDebug) { |
| 1543 | ALOGI("Destroying SharedZip %p %s\n", this, (const char*)mPath); |
| 1544 | } |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1545 | if (mResourceTable != NULL) { |
| 1546 | delete mResourceTable; |
| 1547 | } |
| 1548 | if (mResourceTableAsset != NULL) { |
| 1549 | delete mResourceTableAsset; |
| 1550 | } |
| 1551 | if (mZipFile != NULL) { |
| 1552 | delete mZipFile; |
| 1553 | ALOGV("Closed '%s'\n", mPath.string()); |
| 1554 | } |
| 1555 | } |
| 1556 | |
| 1557 | /* |
| 1558 | * =========================================================================== |
| 1559 | * AssetManager::ZipSet |
| 1560 | * =========================================================================== |
| 1561 | */ |
| 1562 | |
| 1563 | /* |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1564 | * Destructor. Close any open archives. |
| 1565 | */ |
| 1566 | AssetManager::ZipSet::~ZipSet(void) |
| 1567 | { |
| 1568 | size_t N = mZipFile.size(); |
| 1569 | for (size_t i = 0; i < N; i++) |
| 1570 | closeZip(i); |
| 1571 | } |
| 1572 | |
| 1573 | /* |
| 1574 | * Close a Zip file and reset the entry. |
| 1575 | */ |
| 1576 | void AssetManager::ZipSet::closeZip(int idx) |
| 1577 | { |
| 1578 | mZipFile.editItemAt(idx) = NULL; |
| 1579 | } |
| 1580 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1581 | /* |
| 1582 | * Retrieve the appropriate Zip file from the set. |
| 1583 | */ |
| 1584 | ZipFileRO* AssetManager::ZipSet::getZip(const String8& path) |
| 1585 | { |
Dianne Hackborn | ca3872c | 2017-10-30 14:19:32 -0700 | [diff] [blame] | 1586 | return getSharedZip(path)->getZip(); |
| 1587 | } |
| 1588 | |
| 1589 | const sp<AssetManager::SharedZip> AssetManager::ZipSet::getSharedZip(const String8& path) |
| 1590 | { |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1591 | int idx = getIndex(path); |
| 1592 | sp<SharedZip> zip = mZipFile[idx]; |
| 1593 | if (zip == NULL) { |
| 1594 | zip = SharedZip::get(path); |
| 1595 | mZipFile.editItemAt(idx) = zip; |
| 1596 | } |
Dianne Hackborn | ca3872c | 2017-10-30 14:19:32 -0700 | [diff] [blame] | 1597 | return zip; |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1598 | } |
| 1599 | |
| 1600 | Asset* AssetManager::ZipSet::getZipResourceTableAsset(const String8& path) |
| 1601 | { |
| 1602 | 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 | } |
| 1608 | return zip->getResourceTableAsset(); |
| 1609 | } |
| 1610 | |
| 1611 | Asset* AssetManager::ZipSet::setZipResourceTableAsset(const String8& path, |
| 1612 | Asset* asset) |
| 1613 | { |
| 1614 | int idx = getIndex(path); |
| 1615 | sp<SharedZip> zip = mZipFile[idx]; |
| 1616 | // doesn't make sense to call before previously accessing. |
| 1617 | return zip->setResourceTableAsset(asset); |
| 1618 | } |
| 1619 | |
| 1620 | ResTable* AssetManager::ZipSet::getZipResourceTable(const String8& path) |
| 1621 | { |
| 1622 | int idx = getIndex(path); |
| 1623 | sp<SharedZip> zip = mZipFile[idx]; |
| 1624 | if (zip == NULL) { |
| 1625 | zip = SharedZip::get(path); |
| 1626 | mZipFile.editItemAt(idx) = zip; |
| 1627 | } |
| 1628 | return zip->getResourceTable(); |
| 1629 | } |
| 1630 | |
| 1631 | ResTable* AssetManager::ZipSet::setZipResourceTable(const String8& path, |
| 1632 | ResTable* res) |
| 1633 | { |
| 1634 | int idx = getIndex(path); |
| 1635 | sp<SharedZip> zip = mZipFile[idx]; |
| 1636 | // doesn't make sense to call before previously accessing. |
| 1637 | return zip->setResourceTable(res); |
| 1638 | } |
| 1639 | |
| 1640 | /* |
| 1641 | * Generate the partial pathname for the specified archive. The caller |
| 1642 | * gets to prepend the asset root directory. |
| 1643 | * |
| 1644 | * Returns something like "common/en-US-noogle.jar". |
| 1645 | */ |
| 1646 | /*static*/ String8 AssetManager::ZipSet::getPathName(const char* zipPath) |
| 1647 | { |
| 1648 | return String8(zipPath); |
| 1649 | } |
| 1650 | |
| 1651 | bool AssetManager::ZipSet::isUpToDate() |
| 1652 | { |
| 1653 | const size_t N = mZipFile.size(); |
| 1654 | for (size_t i=0; i<N; i++) { |
| 1655 | if (mZipFile[i] != NULL && !mZipFile[i]->isUpToDate()) { |
| 1656 | return false; |
| 1657 | } |
| 1658 | } |
| 1659 | return true; |
| 1660 | } |
| 1661 | |
Jaekyun Seok | 7de2f9c | 2017-03-02 12:45:10 +0900 | [diff] [blame] | 1662 | void AssetManager::ZipSet::addOverlay(const String8& path, const asset_path& overlay) |
| 1663 | { |
| 1664 | int idx = getIndex(path); |
| 1665 | sp<SharedZip> zip = mZipFile[idx]; |
| 1666 | zip->addOverlay(overlay); |
| 1667 | } |
| 1668 | |
| 1669 | bool AssetManager::ZipSet::getOverlay(const String8& path, size_t idx, asset_path* out) const |
| 1670 | { |
| 1671 | sp<SharedZip> zip = SharedZip::get(path, false); |
| 1672 | if (zip == NULL) { |
| 1673 | return false; |
| 1674 | } |
| 1675 | return zip->getOverlay(idx, out); |
| 1676 | } |
| 1677 | |
Adam Lesinski | 16c4d15 | 2014-01-24 13:27:13 -0800 | [diff] [blame] | 1678 | /* |
| 1679 | * Compute the zip file's index. |
| 1680 | * |
| 1681 | * "appName", "locale", and "vendor" should be set to NULL to indicate the |
| 1682 | * default directory. |
| 1683 | */ |
| 1684 | int AssetManager::ZipSet::getIndex(const String8& zip) const |
| 1685 | { |
| 1686 | const size_t N = mZipPath.size(); |
| 1687 | for (size_t i=0; i<N; i++) { |
| 1688 | if (mZipPath[i] == zip) { |
| 1689 | return i; |
| 1690 | } |
| 1691 | } |
| 1692 | |
| 1693 | mZipPath.add(zip); |
| 1694 | mZipFile.add(NULL); |
| 1695 | |
| 1696 | return mZipPath.size()-1; |
| 1697 | } |