The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -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" |
Dianne Hackborn | f7be480 | 2013-04-12 14:52:58 -0700 | [diff] [blame] | 22 | #define ATRACE_TAG ATRACE_TAG_RESOURCES |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | //#define LOG_NDEBUG 0 |
| 24 | |
Mathias Agopian | b13b9bd | 2012-02-17 18:27:36 -0800 | [diff] [blame] | 25 | #include <androidfw/Asset.h> |
| 26 | #include <androidfw/AssetDir.h> |
| 27 | #include <androidfw/AssetManager.h> |
Mathias Agopian | 1f5762e | 2013-05-06 20:20:34 -0700 | [diff] [blame] | 28 | #include <androidfw/misc.h> |
Mathias Agopian | b13b9bd | 2012-02-17 18:27:36 -0800 | [diff] [blame] | 29 | #include <androidfw/ResourceTypes.h> |
Mathias Agopian | 1f5762e | 2013-05-06 20:20:34 -0700 | [diff] [blame] | 30 | #include <androidfw/ZipFileRO.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | #include <utils/Atomic.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | #include <utils/Log.h> |
Mathias Agopian | b13b9bd | 2012-02-17 18:27:36 -0800 | [diff] [blame] | 33 | #include <utils/String8.h> |
| 34 | #include <utils/String8.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | #include <utils/threads.h> |
Mathias Agopian | b13b9bd | 2012-02-17 18:27:36 -0800 | [diff] [blame] | 36 | #include <utils/Timers.h> |
Dianne Hackborn | c51d050 | 2013-04-15 15:36:53 -0700 | [diff] [blame] | 37 | #ifdef HAVE_ANDROID_OS |
Dianne Hackborn | f7be480 | 2013-04-12 14:52:58 -0700 | [diff] [blame] | 38 | #include <cutils/trace.h> |
Dianne Hackborn | c51d050 | 2013-04-15 15:36:53 -0700 | [diff] [blame] | 39 | #endif |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | |
Mathias Agopian | b13b9bd | 2012-02-17 18:27:36 -0800 | [diff] [blame] | 41 | #include <assert.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | #include <dirent.h> |
| 43 | #include <errno.h> |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 44 | #include <string.h> // strerror |
Mathias Agopian | b13b9bd | 2012-02-17 18:27:36 -0800 | [diff] [blame] | 45 | #include <strings.h> |
Mårten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 46 | |
| 47 | #ifndef TEMP_FAILURE_RETRY |
| 48 | /* Used to retry syscalls that can return EINTR. */ |
| 49 | #define TEMP_FAILURE_RETRY(exp) ({ \ |
| 50 | typeof (exp) _rc; \ |
| 51 | do { \ |
| 52 | _rc = (exp); \ |
| 53 | } while (_rc == -1 && errno == EINTR); \ |
| 54 | _rc; }) |
| 55 | #endif |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | |
Dianne Hackborn | f7be480 | 2013-04-12 14:52:58 -0700 | [diff] [blame] | 57 | #ifdef HAVE_ANDROID_OS |
| 58 | #define MY_TRACE_BEGIN(x) ATRACE_BEGIN(x) |
| 59 | #define MY_TRACE_END() ATRACE_END() |
| 60 | #else |
| 61 | #define MY_TRACE_BEGIN(x) |
| 62 | #define MY_TRACE_END() |
| 63 | #endif |
| 64 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | using namespace android; |
| 66 | |
| 67 | /* |
| 68 | * Names for default app, locale, and vendor. We might want to change |
| 69 | * these to be an actual locale, e.g. always use en-US as the default. |
| 70 | */ |
| 71 | static const char* kDefaultLocale = "default"; |
| 72 | static const char* kDefaultVendor = "default"; |
| 73 | static const char* kAssetsRoot = "assets"; |
| 74 | static const char* kAppZipName = NULL; //"classes.jar"; |
| 75 | static const char* kSystemAssets = "framework/framework-res.apk"; |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 76 | static const char* kResourceCache = "resource-cache"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | |
| 78 | static const char* kExcludeExtension = ".EXCLUDE"; |
| 79 | |
| 80 | static Asset* const kExcludedAsset = (Asset*) 0xd000000d; |
| 81 | |
| 82 | static volatile int32_t gCount = 0; |
| 83 | |
Mårten Kongstad | 65a05fd | 2014-01-31 14:01:52 +0100 | [diff] [blame] | 84 | const char* AssetManager::RESOURCES_FILENAME = "resources.arsc"; |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 85 | const char* AssetManager::IDMAP_BIN = "/system/bin/idmap"; |
| 86 | const char* AssetManager::OVERLAY_DIR = "/vendor/overlay"; |
| 87 | const char* AssetManager::TARGET_PACKAGE_NAME = "android"; |
| 88 | const char* AssetManager::TARGET_APK_PATH = "/system/framework/framework-res.apk"; |
| 89 | const char* AssetManager::IDMAP_DIR = "/data/resource-cache"; |
Mårten Kongstad | 65a05fd | 2014-01-31 14:01:52 +0100 | [diff] [blame] | 90 | |
Mårten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 91 | namespace { |
Mårten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 92 | String8 idmapPathForPackagePath(const String8& pkgPath) |
| 93 | { |
| 94 | const char* root = getenv("ANDROID_DATA"); |
| 95 | LOG_ALWAYS_FATAL_IF(root == NULL, "ANDROID_DATA not set"); |
| 96 | String8 path(root); |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 97 | path.appendPath(kResourceCache); |
Mårten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 98 | |
| 99 | char buf[256]; // 256 chars should be enough for anyone... |
| 100 | strncpy(buf, pkgPath.string(), 255); |
| 101 | buf[255] = '\0'; |
| 102 | char* filename = buf; |
| 103 | while (*filename && *filename == '/') { |
| 104 | ++filename; |
| 105 | } |
| 106 | char* p = filename; |
| 107 | while (*p) { |
| 108 | if (*p == '/') { |
| 109 | *p = '@'; |
| 110 | } |
| 111 | ++p; |
| 112 | } |
| 113 | path.appendPath(filename); |
| 114 | path.append("@idmap"); |
| 115 | |
| 116 | return path; |
| 117 | } |
Mathias Agopian | 69a3ce1 | 2012-08-05 12:38:51 -0700 | [diff] [blame] | 118 | |
| 119 | /* |
| 120 | * Like strdup(), but uses C++ "new" operator instead of malloc. |
| 121 | */ |
| 122 | static char* strdupNew(const char* str) |
| 123 | { |
| 124 | char* newStr; |
| 125 | int len; |
| 126 | |
| 127 | if (str == NULL) |
| 128 | return NULL; |
| 129 | |
| 130 | len = strlen(str); |
| 131 | newStr = new char[len+1]; |
| 132 | memcpy(newStr, str, len+1); |
| 133 | |
| 134 | return newStr; |
| 135 | } |
Mårten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 136 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | |
| 138 | /* |
| 139 | * =========================================================================== |
| 140 | * AssetManager |
| 141 | * =========================================================================== |
| 142 | */ |
| 143 | |
| 144 | int32_t AssetManager::getGlobalCount() |
| 145 | { |
| 146 | return gCount; |
| 147 | } |
| 148 | |
| 149 | AssetManager::AssetManager(CacheMode cacheMode) |
| 150 | : mLocale(NULL), mVendor(NULL), |
| 151 | mResources(NULL), mConfig(new ResTable_config), |
| 152 | mCacheMode(cacheMode), mCacheValid(false) |
| 153 | { |
| 154 | int count = android_atomic_inc(&gCount)+1; |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 155 | //ALOGI("Creating AssetManager %p #%d\n", this, count); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | memset(mConfig, 0, sizeof(ResTable_config)); |
| 157 | } |
| 158 | |
| 159 | AssetManager::~AssetManager(void) |
| 160 | { |
| 161 | int count = android_atomic_dec(&gCount); |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 162 | //ALOGI("Destroying AssetManager in %p #%d\n", this, count); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | |
| 164 | delete mConfig; |
| 165 | delete mResources; |
| 166 | |
| 167 | // don't have a String class yet, so make sure we clean up |
| 168 | delete[] mLocale; |
| 169 | delete[] mVendor; |
| 170 | } |
| 171 | |
Narayan Kamath | 745d4ef | 2014-01-27 11:17:22 +0000 | [diff] [blame] | 172 | bool AssetManager::addAssetPath(const String8& path, int32_t* cookie) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 173 | { |
| 174 | AutoMutex _l(mLock); |
| 175 | |
| 176 | asset_path ap; |
| 177 | |
| 178 | String8 realPath(path); |
| 179 | if (kAppZipName) { |
| 180 | realPath.appendPath(kAppZipName); |
| 181 | } |
| 182 | ap.type = ::getFileType(realPath.string()); |
| 183 | if (ap.type == kFileTypeRegular) { |
| 184 | ap.path = realPath; |
| 185 | } else { |
| 186 | ap.path = path; |
| 187 | ap.type = ::getFileType(path.string()); |
| 188 | if (ap.type != kFileTypeDirectory && ap.type != kFileTypeRegular) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 189 | ALOGW("Asset path %s is neither a directory nor file (type=%d).", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | path.string(), (int)ap.type); |
| 191 | return false; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | // Skip if we have it already. |
| 196 | for (size_t i=0; i<mAssetPaths.size(); i++) { |
| 197 | if (mAssetPaths[i].path == ap.path) { |
| 198 | if (cookie) { |
Narayan Kamath | 745d4ef | 2014-01-27 11:17:22 +0000 | [diff] [blame] | 199 | *cookie = static_cast<int32_t>(i+1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 200 | } |
| 201 | return true; |
| 202 | } |
| 203 | } |
Mårten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 204 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 205 | ALOGV("In %p Asset %s path: %s", this, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | ap.type == kFileTypeDirectory ? "dir" : "zip", ap.path.string()); |
| 207 | |
| 208 | mAssetPaths.add(ap); |
| 209 | |
| 210 | // new paths are always added at the end |
| 211 | if (cookie) { |
Narayan Kamath | 745d4ef | 2014-01-27 11:17:22 +0000 | [diff] [blame] | 212 | *cookie = static_cast<int32_t>(mAssetPaths.size()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 | } |
| 214 | |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 215 | #ifdef HAVE_ANDROID_OS |
| 216 | // Load overlays, if any |
| 217 | asset_path oap; |
| 218 | for (size_t idx = 0; mZipSet.getOverlay(ap.path, idx, &oap); idx++) { |
| 219 | mAssetPaths.add(oap); |
Mårten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 220 | } |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 221 | #endif |
Mårten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 222 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | return true; |
| 224 | } |
| 225 | |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 226 | bool AssetManager::addOverlayPath(const String8& packagePath, int32_t* cookie) |
| 227 | { |
| 228 | const String8 idmapPath = idmapPathForPackagePath(packagePath); |
| 229 | |
| 230 | AutoMutex _l(mLock); |
| 231 | |
| 232 | for (size_t i = 0; i < mAssetPaths.size(); ++i) { |
| 233 | if (mAssetPaths[i].idmap == idmapPath) { |
| 234 | *cookie = static_cast<int32_t>(i + 1); |
| 235 | return true; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | Asset* idmap = NULL; |
| 240 | if ((idmap = openAssetFromFileLocked(idmapPath, Asset::ACCESS_BUFFER)) == NULL) { |
| 241 | ALOGW("failed to open idmap file %s\n", idmapPath.string()); |
| 242 | return false; |
| 243 | } |
| 244 | |
| 245 | String8 targetPath; |
| 246 | String8 overlayPath; |
| 247 | if (!ResTable::getIdmapInfo(idmap->getBuffer(false), idmap->getLength(), |
| 248 | NULL, NULL, &targetPath, &overlayPath)) { |
| 249 | ALOGW("failed to read idmap file %s\n", idmapPath.string()); |
| 250 | delete idmap; |
| 251 | return false; |
| 252 | } |
| 253 | delete idmap; |
| 254 | |
| 255 | if (overlayPath != packagePath) { |
| 256 | ALOGW("idmap file %s inconcistent: expected path %s does not match actual path %s\n", |
| 257 | idmapPath.string(), packagePath.string(), overlayPath.string()); |
| 258 | return false; |
| 259 | } |
| 260 | if (access(targetPath.string(), R_OK) != 0) { |
| 261 | ALOGW("failed to access file %s: %s\n", targetPath.string(), strerror(errno)); |
| 262 | return false; |
| 263 | } |
| 264 | if (access(idmapPath.string(), R_OK) != 0) { |
| 265 | ALOGW("failed to access file %s: %s\n", idmapPath.string(), strerror(errno)); |
| 266 | return false; |
| 267 | } |
| 268 | if (access(overlayPath.string(), R_OK) != 0) { |
| 269 | ALOGW("failed to access file %s: %s\n", overlayPath.string(), strerror(errno)); |
| 270 | return false; |
| 271 | } |
| 272 | |
| 273 | asset_path oap; |
| 274 | oap.path = overlayPath; |
| 275 | oap.type = ::getFileType(overlayPath.string()); |
| 276 | oap.idmap = idmapPath; |
| 277 | #if 0 |
| 278 | ALOGD("Overlay added: targetPath=%s overlayPath=%s idmapPath=%s\n", |
| 279 | targetPath.string(), overlayPath.string(), idmapPath.string()); |
| 280 | #endif |
| 281 | mAssetPaths.add(oap); |
| 282 | *cookie = static_cast<int32_t>(mAssetPaths.size()); |
| 283 | |
| 284 | return true; |
| 285 | } |
| 286 | |
Mårten Kongstad | 65a05fd | 2014-01-31 14:01:52 +0100 | [diff] [blame] | 287 | bool AssetManager::createIdmap(const char* targetApkPath, const char* overlayApkPath, |
Colin Cross | ecbeae7 | 2014-02-11 18:02:06 -0800 | [diff] [blame] | 288 | 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] | 289 | { |
| 290 | AutoMutex _l(mLock); |
| 291 | const String8 paths[2] = { String8(targetApkPath), String8(overlayApkPath) }; |
| 292 | ResTable tables[2]; |
| 293 | |
| 294 | for (int i = 0; i < 2; ++i) { |
| 295 | asset_path ap; |
| 296 | ap.type = kFileTypeRegular; |
| 297 | ap.path = paths[i]; |
| 298 | Asset* ass = openNonAssetInPathLocked("resources.arsc", Asset::ACCESS_BUFFER, ap); |
| 299 | if (ass == NULL) { |
| 300 | ALOGW("failed to find resources.arsc in %s\n", ap.path.string()); |
| 301 | return false; |
| 302 | } |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 303 | tables[i].add(ass, 1, false /* copyData */, NULL /* idMap */); |
Mårten Kongstad | 65a05fd | 2014-01-31 14:01:52 +0100 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | return tables[0].createIdmap(tables[1], targetCrc, overlayCrc, |
| 307 | targetApkPath, overlayApkPath, (void**)outData, outSize) == NO_ERROR; |
| 308 | } |
| 309 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 310 | bool AssetManager::addDefaultAssets() |
| 311 | { |
| 312 | const char* root = getenv("ANDROID_ROOT"); |
| 313 | LOG_ALWAYS_FATAL_IF(root == NULL, "ANDROID_ROOT not set"); |
| 314 | |
| 315 | String8 path(root); |
| 316 | path.appendPath(kSystemAssets); |
| 317 | |
| 318 | return addAssetPath(path, NULL); |
| 319 | } |
| 320 | |
Narayan Kamath | 745d4ef | 2014-01-27 11:17:22 +0000 | [diff] [blame] | 321 | int32_t AssetManager::nextAssetPath(const int32_t cookie) const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 322 | { |
| 323 | AutoMutex _l(mLock); |
Narayan Kamath | 745d4ef | 2014-01-27 11:17:22 +0000 | [diff] [blame] | 324 | const size_t next = static_cast<size_t>(cookie) + 1; |
| 325 | return next > mAssetPaths.size() ? -1 : next; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 326 | } |
| 327 | |
Narayan Kamath | 745d4ef | 2014-01-27 11:17:22 +0000 | [diff] [blame] | 328 | String8 AssetManager::getAssetPath(const int32_t cookie) const |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 329 | { |
| 330 | AutoMutex _l(mLock); |
Narayan Kamath | 745d4ef | 2014-01-27 11:17:22 +0000 | [diff] [blame] | 331 | const size_t which = static_cast<size_t>(cookie) - 1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 332 | if (which < mAssetPaths.size()) { |
| 333 | return mAssetPaths[which].path; |
| 334 | } |
| 335 | return String8(); |
| 336 | } |
| 337 | |
| 338 | /* |
| 339 | * Set the current locale. Use NULL to indicate no locale. |
| 340 | * |
| 341 | * Close and reopen Zip archives as appropriate, and reset cached |
| 342 | * information in the locale-specific sections of the tree. |
| 343 | */ |
| 344 | void AssetManager::setLocale(const char* locale) |
| 345 | { |
| 346 | AutoMutex _l(mLock); |
| 347 | setLocaleLocked(locale); |
| 348 | } |
| 349 | |
| 350 | void AssetManager::setLocaleLocked(const char* locale) |
| 351 | { |
| 352 | if (mLocale != NULL) { |
| 353 | /* previously set, purge cached data */ |
| 354 | purgeFileNameCacheLocked(); |
| 355 | //mZipSet.purgeLocale(); |
| 356 | delete[] mLocale; |
| 357 | } |
| 358 | mLocale = strdupNew(locale); |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 359 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 360 | updateResourceParamsLocked(); |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | * Set the current vendor. Use NULL to indicate no vendor. |
| 365 | * |
| 366 | * Close and reopen Zip archives as appropriate, and reset cached |
| 367 | * information in the vendor-specific sections of the tree. |
| 368 | */ |
| 369 | void AssetManager::setVendor(const char* vendor) |
| 370 | { |
| 371 | AutoMutex _l(mLock); |
| 372 | |
| 373 | if (mVendor != NULL) { |
| 374 | /* previously set, purge cached data */ |
| 375 | purgeFileNameCacheLocked(); |
| 376 | //mZipSet.purgeVendor(); |
| 377 | delete[] mVendor; |
| 378 | } |
| 379 | mVendor = strdupNew(vendor); |
| 380 | } |
| 381 | |
| 382 | void AssetManager::setConfiguration(const ResTable_config& config, const char* locale) |
| 383 | { |
| 384 | AutoMutex _l(mLock); |
| 385 | *mConfig = config; |
| 386 | if (locale) { |
| 387 | setLocaleLocked(locale); |
| 388 | } else if (config.language[0] != 0) { |
Narayan Kamath | 788fa41 | 2014-01-21 15:32:36 +0000 | [diff] [blame] | 389 | char spec[RESTABLE_MAX_LOCALE_LEN]; |
| 390 | config.getBcp47Locale(spec); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 391 | setLocaleLocked(spec); |
| 392 | } else { |
| 393 | updateResourceParamsLocked(); |
| 394 | } |
| 395 | } |
| 396 | |
Dianne Hackborn | 08d5b8f | 2010-08-04 11:12:40 -0700 | [diff] [blame] | 397 | void AssetManager::getConfiguration(ResTable_config* outConfig) const |
| 398 | { |
| 399 | AutoMutex _l(mLock); |
| 400 | *outConfig = *mConfig; |
| 401 | } |
| 402 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 403 | /* |
| 404 | * Open an asset. |
| 405 | * |
| 406 | * The data could be; |
| 407 | * - In a file on disk (assetBase + fileName). |
| 408 | * - In a compressed file on disk (assetBase + fileName.gz). |
| 409 | * - In a Zip archive, uncompressed or compressed. |
| 410 | * |
| 411 | * It can be in a number of different directories and Zip archives. |
| 412 | * The search order is: |
| 413 | * - [appname] |
| 414 | * - locale + vendor |
| 415 | * - "default" + vendor |
| 416 | * - locale + "default" |
| 417 | * - "default + "default" |
| 418 | * - "common" |
| 419 | * - (same as above) |
| 420 | * |
| 421 | * To find a particular file, we have to try up to eight paths with |
| 422 | * all three forms of data. |
| 423 | * |
| 424 | * We should probably reject requests for "illegal" filenames, e.g. those |
| 425 | * with illegal characters or "../" backward relative paths. |
| 426 | */ |
| 427 | Asset* AssetManager::open(const char* fileName, AccessMode mode) |
| 428 | { |
| 429 | AutoMutex _l(mLock); |
| 430 | |
| 431 | LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager"); |
| 432 | |
| 433 | |
| 434 | if (mCacheMode != CACHE_OFF && !mCacheValid) |
| 435 | loadFileNameCacheLocked(); |
| 436 | |
| 437 | String8 assetName(kAssetsRoot); |
| 438 | assetName.appendPath(fileName); |
| 439 | |
| 440 | /* |
| 441 | * For each top-level asset path, search for the asset. |
| 442 | */ |
| 443 | |
| 444 | size_t i = mAssetPaths.size(); |
| 445 | while (i > 0) { |
| 446 | i--; |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 447 | ALOGV("Looking for asset '%s' in '%s'\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 448 | assetName.string(), mAssetPaths.itemAt(i).path.string()); |
| 449 | Asset* pAsset = openNonAssetInPathLocked(assetName.string(), mode, mAssetPaths.itemAt(i)); |
| 450 | if (pAsset != NULL) { |
| 451 | return pAsset != kExcludedAsset ? pAsset : NULL; |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | return NULL; |
| 456 | } |
| 457 | |
| 458 | /* |
| 459 | * Open a non-asset file as if it were an asset. |
| 460 | * |
| 461 | * The "fileName" is the partial path starting from the application |
| 462 | * name. |
| 463 | */ |
| 464 | Asset* AssetManager::openNonAsset(const char* fileName, AccessMode mode) |
| 465 | { |
| 466 | AutoMutex _l(mLock); |
| 467 | |
| 468 | LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager"); |
| 469 | |
| 470 | |
| 471 | if (mCacheMode != CACHE_OFF && !mCacheValid) |
| 472 | loadFileNameCacheLocked(); |
| 473 | |
| 474 | /* |
| 475 | * For each top-level asset path, search for the asset. |
| 476 | */ |
| 477 | |
| 478 | size_t i = mAssetPaths.size(); |
| 479 | while (i > 0) { |
| 480 | i--; |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 481 | ALOGV("Looking for non-asset '%s' in '%s'\n", fileName, mAssetPaths.itemAt(i).path.string()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 482 | Asset* pAsset = openNonAssetInPathLocked( |
| 483 | fileName, mode, mAssetPaths.itemAt(i)); |
| 484 | if (pAsset != NULL) { |
| 485 | return pAsset != kExcludedAsset ? pAsset : NULL; |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | return NULL; |
| 490 | } |
| 491 | |
Narayan Kamath | 745d4ef | 2014-01-27 11:17:22 +0000 | [diff] [blame] | 492 | Asset* AssetManager::openNonAsset(const int32_t cookie, const char* fileName, AccessMode mode) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 493 | { |
Narayan Kamath | 745d4ef | 2014-01-27 11:17:22 +0000 | [diff] [blame] | 494 | const size_t which = static_cast<size_t>(cookie) - 1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 495 | |
| 496 | AutoMutex _l(mLock); |
| 497 | |
| 498 | LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager"); |
| 499 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 500 | if (mCacheMode != CACHE_OFF && !mCacheValid) |
| 501 | loadFileNameCacheLocked(); |
| 502 | |
| 503 | if (which < mAssetPaths.size()) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 504 | ALOGV("Looking for non-asset '%s' in '%s'\n", fileName, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 505 | mAssetPaths.itemAt(which).path.string()); |
| 506 | Asset* pAsset = openNonAssetInPathLocked( |
| 507 | fileName, mode, mAssetPaths.itemAt(which)); |
| 508 | if (pAsset != NULL) { |
| 509 | return pAsset != kExcludedAsset ? pAsset : NULL; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | return NULL; |
| 514 | } |
| 515 | |
| 516 | /* |
| 517 | * Get the type of a file in the asset namespace. |
| 518 | * |
| 519 | * This currently only works for regular files. All others (including |
| 520 | * directories) will return kFileTypeNonexistent. |
| 521 | */ |
| 522 | FileType AssetManager::getFileType(const char* fileName) |
| 523 | { |
| 524 | Asset* pAsset = NULL; |
| 525 | |
| 526 | /* |
| 527 | * Open the asset. This is less efficient than simply finding the |
| 528 | * file, but it's not too bad (we don't uncompress or mmap data until |
| 529 | * the first read() call). |
| 530 | */ |
| 531 | pAsset = open(fileName, Asset::ACCESS_STREAMING); |
| 532 | delete pAsset; |
| 533 | |
| 534 | if (pAsset == NULL) |
| 535 | return kFileTypeNonexistent; |
| 536 | else |
| 537 | return kFileTypeRegular; |
| 538 | } |
| 539 | |
| 540 | const ResTable* AssetManager::getResTable(bool required) const |
| 541 | { |
| 542 | ResTable* rt = mResources; |
| 543 | if (rt) { |
| 544 | return rt; |
| 545 | } |
| 546 | |
| 547 | // Iterate through all asset packages, collecting resources from each. |
| 548 | |
| 549 | AutoMutex _l(mLock); |
| 550 | |
| 551 | if (mResources != NULL) { |
| 552 | return mResources; |
| 553 | } |
| 554 | |
| 555 | if (required) { |
| 556 | LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager"); |
| 557 | } |
| 558 | |
| 559 | if (mCacheMode != CACHE_OFF && !mCacheValid) |
| 560 | const_cast<AssetManager*>(this)->loadFileNameCacheLocked(); |
| 561 | |
| 562 | const size_t N = mAssetPaths.size(); |
| 563 | for (size_t i=0; i<N; i++) { |
| 564 | Asset* ass = NULL; |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 565 | ResTable* sharedRes = NULL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 566 | bool shared = true; |
| 567 | const asset_path& ap = mAssetPaths.itemAt(i); |
Dianne Hackborn | f7be480 | 2013-04-12 14:52:58 -0700 | [diff] [blame] | 568 | MY_TRACE_BEGIN(ap.path.string()); |
Mårten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 569 | Asset* idmap = openIdmapLocked(ap); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 570 | ALOGV("Looking for resource asset in '%s'\n", ap.path.string()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 571 | if (ap.type != kFileTypeDirectory) { |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 572 | if (i == 0) { |
| 573 | // The first item is typically the framework resources, |
| 574 | // which we want to avoid parsing every time. |
| 575 | sharedRes = const_cast<AssetManager*>(this)-> |
| 576 | mZipSet.getZipResourceTable(ap.path); |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 577 | if (sharedRes != NULL) { |
| 578 | // skip ahead the number of system overlay packages preloaded |
| 579 | i += sharedRes->getTableCount() - 1; |
| 580 | } |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 581 | } |
| 582 | if (sharedRes == NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 583 | ass = const_cast<AssetManager*>(this)-> |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 584 | mZipSet.getZipResourceTableAsset(ap.path); |
| 585 | if (ass == NULL) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 586 | ALOGV("loading resource table %s\n", ap.path.string()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 587 | ass = const_cast<AssetManager*>(this)-> |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 588 | openNonAssetInPathLocked("resources.arsc", |
| 589 | Asset::ACCESS_BUFFER, |
| 590 | ap); |
| 591 | if (ass != NULL && ass != kExcludedAsset) { |
| 592 | ass = const_cast<AssetManager*>(this)-> |
| 593 | mZipSet.setZipResourceTableAsset(ap.path, ass); |
| 594 | } |
| 595 | } |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 596 | |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 597 | if (i == 0 && ass != NULL) { |
| 598 | // If this is the first resource table in the asset |
| 599 | // manager, then we are going to cache it so that we |
| 600 | // can quickly copy it out for others. |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 601 | ALOGV("Creating shared resources for %s", ap.path.string()); |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 602 | sharedRes = new ResTable(); |
Narayan Kamath | 7c4887f | 2014-01-27 17:32:37 +0000 | [diff] [blame] | 603 | sharedRes->add(ass, i + 1, false, idmap); |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 604 | #ifdef HAVE_ANDROID_OS |
| 605 | const char* data = getenv("ANDROID_DATA"); |
| 606 | LOG_ALWAYS_FATAL_IF(data == NULL, "ANDROID_DATA not set"); |
| 607 | String8 overlaysListPath(data); |
| 608 | overlaysListPath.appendPath(kResourceCache); |
| 609 | overlaysListPath.appendPath("overlays.list"); |
| 610 | addSystemOverlays(overlaysListPath.string(), ap.path, sharedRes, i); |
| 611 | #endif |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 612 | sharedRes = const_cast<AssetManager*>(this)-> |
| 613 | mZipSet.setZipResourceTable(ap.path, sharedRes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 614 | } |
| 615 | } |
| 616 | } else { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 617 | ALOGV("loading resource table %s\n", ap.path.string()); |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 618 | ass = const_cast<AssetManager*>(this)-> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 619 | openNonAssetInPathLocked("resources.arsc", |
| 620 | Asset::ACCESS_BUFFER, |
| 621 | ap); |
| 622 | shared = false; |
| 623 | } |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 624 | if ((ass != NULL || sharedRes != NULL) && ass != kExcludedAsset) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 625 | if (rt == NULL) { |
| 626 | mResources = rt = new ResTable(); |
| 627 | updateResourceParamsLocked(); |
| 628 | } |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 629 | ALOGV("Installing resource asset %p in to table %p\n", ass, mResources); |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 630 | if (sharedRes != NULL) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 631 | ALOGV("Copying existing resources for %s", ap.path.string()); |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 632 | rt->add(sharedRes); |
| 633 | } else { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 634 | ALOGV("Parsing resources for %s", ap.path.string()); |
Narayan Kamath | 7c4887f | 2014-01-27 17:32:37 +0000 | [diff] [blame] | 635 | rt->add(ass, i + 1, !shared, idmap); |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 636 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 637 | |
| 638 | if (!shared) { |
| 639 | delete ass; |
| 640 | } |
| 641 | } |
Mårten Kongstad | 2b91c67 | 2011-05-05 10:40:42 +0200 | [diff] [blame] | 642 | if (idmap != NULL) { |
| 643 | delete idmap; |
| 644 | } |
Dianne Hackborn | f7be480 | 2013-04-12 14:52:58 -0700 | [diff] [blame] | 645 | MY_TRACE_END(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 646 | } |
| 647 | |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 648 | if (required && !rt) ALOGW("Unable to find resources file resources.arsc"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 649 | if (!rt) { |
| 650 | mResources = rt = new ResTable(); |
| 651 | } |
| 652 | return rt; |
| 653 | } |
| 654 | |
| 655 | void AssetManager::updateResourceParamsLocked() const |
| 656 | { |
| 657 | ResTable* res = mResources; |
| 658 | if (!res) { |
| 659 | return; |
| 660 | } |
| 661 | |
Narayan Kamath | 788fa41 | 2014-01-21 15:32:36 +0000 | [diff] [blame] | 662 | if (mLocale) { |
| 663 | mConfig->setBcp47Locale(mLocale); |
| 664 | } else { |
| 665 | mConfig->clearLocale(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 666 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 667 | |
| 668 | res->setParameters(mConfig); |
| 669 | } |
| 670 | |
Mårten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 671 | Asset* AssetManager::openIdmapLocked(const struct asset_path& ap) const |
| 672 | { |
| 673 | Asset* ass = NULL; |
| 674 | if (ap.idmap.size() != 0) { |
| 675 | ass = const_cast<AssetManager*>(this)-> |
| 676 | openAssetFromFileLocked(ap.idmap, Asset::ACCESS_BUFFER); |
| 677 | if (ass) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 678 | ALOGV("loading idmap %s\n", ap.idmap.string()); |
Mårten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 679 | } else { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 680 | ALOGW("failed to load idmap %s\n", ap.idmap.string()); |
Mårten Kongstad | 57f4b77 | 2011-03-17 14:13:41 +0100 | [diff] [blame] | 681 | } |
| 682 | } |
| 683 | return ass; |
| 684 | } |
| 685 | |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 686 | void AssetManager::addSystemOverlays(const char* pathOverlaysList, |
| 687 | const String8& targetPackagePath, ResTable* sharedRes, size_t offset) const |
| 688 | { |
| 689 | FILE* fin = fopen(pathOverlaysList, "r"); |
| 690 | if (fin == NULL) { |
| 691 | return; |
| 692 | } |
| 693 | |
| 694 | char buf[1024]; |
| 695 | while (fgets(buf, sizeof(buf), fin)) { |
| 696 | // format of each line: |
| 697 | // <path to apk><space><path to idmap><newline> |
| 698 | char* space = strchr(buf, ' '); |
| 699 | char* newline = strchr(buf, '\n'); |
| 700 | asset_path oap; |
| 701 | |
| 702 | if (space == NULL || newline == NULL || newline < space) { |
| 703 | continue; |
| 704 | } |
| 705 | |
| 706 | oap.path = String8(buf, space - buf); |
| 707 | oap.type = kFileTypeRegular; |
| 708 | oap.idmap = String8(space + 1, newline - space - 1); |
| 709 | |
| 710 | Asset* oass = const_cast<AssetManager*>(this)-> |
| 711 | openNonAssetInPathLocked("resources.arsc", |
| 712 | Asset::ACCESS_BUFFER, |
| 713 | oap); |
| 714 | |
| 715 | if (oass != NULL) { |
| 716 | Asset* oidmap = openIdmapLocked(oap); |
| 717 | offset++; |
| 718 | sharedRes->add(oass, offset + 1, false, oidmap); |
| 719 | const_cast<AssetManager*>(this)->mAssetPaths.add(oap); |
| 720 | const_cast<AssetManager*>(this)->mZipSet.addOverlay(targetPackagePath, oap); |
| 721 | } |
| 722 | } |
| 723 | fclose(fin); |
| 724 | } |
| 725 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 726 | const ResTable& AssetManager::getResources(bool required) const |
| 727 | { |
| 728 | const ResTable* rt = getResTable(required); |
| 729 | return *rt; |
| 730 | } |
| 731 | |
| 732 | bool AssetManager::isUpToDate() |
| 733 | { |
| 734 | AutoMutex _l(mLock); |
| 735 | return mZipSet.isUpToDate(); |
| 736 | } |
| 737 | |
| 738 | void AssetManager::getLocales(Vector<String8>* locales) const |
| 739 | { |
| 740 | ResTable* res = mResources; |
| 741 | if (res != NULL) { |
| 742 | res->getLocales(locales); |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | /* |
| 747 | * Open a non-asset file as if it were an asset, searching for it in the |
| 748 | * specified app. |
| 749 | * |
| 750 | * Pass in a NULL values for "appName" if the common app directory should |
| 751 | * be used. |
| 752 | */ |
| 753 | Asset* AssetManager::openNonAssetInPathLocked(const char* fileName, AccessMode mode, |
| 754 | const asset_path& ap) |
| 755 | { |
| 756 | Asset* pAsset = NULL; |
| 757 | |
| 758 | /* look at the filesystem on disk */ |
| 759 | if (ap.type == kFileTypeDirectory) { |
| 760 | String8 path(ap.path); |
| 761 | path.appendPath(fileName); |
| 762 | |
| 763 | pAsset = openAssetFromFileLocked(path, mode); |
| 764 | |
| 765 | if (pAsset == NULL) { |
| 766 | /* try again, this time with ".gz" */ |
| 767 | path.append(".gz"); |
| 768 | pAsset = openAssetFromFileLocked(path, mode); |
| 769 | } |
| 770 | |
| 771 | if (pAsset != NULL) { |
| 772 | //printf("FOUND NA '%s' on disk\n", fileName); |
| 773 | pAsset->setAssetSource(path); |
| 774 | } |
| 775 | |
| 776 | /* look inside the zip file */ |
| 777 | } else { |
| 778 | String8 path(fileName); |
| 779 | |
| 780 | /* check the appropriate Zip file */ |
Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 781 | ZipFileRO* pZip = getZipFileLocked(ap); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 782 | if (pZip != NULL) { |
| 783 | //printf("GOT zip, checking NA '%s'\n", (const char*) path); |
Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 784 | ZipEntryRO entry = pZip->findEntryByName(path.string()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 785 | if (entry != NULL) { |
| 786 | //printf("FOUND NA in Zip file for %s\n", appName ? appName : kAppCommon); |
| 787 | pAsset = openAssetFromZipLocked(pZip, entry, mode, path); |
Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 788 | pZip->releaseEntry(entry); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 789 | } |
| 790 | } |
| 791 | |
| 792 | if (pAsset != NULL) { |
| 793 | /* create a "source" name, for debug/display */ |
| 794 | pAsset->setAssetSource( |
| 795 | createZipSourceNameLocked(ZipSet::getPathName(ap.path.string()), String8(""), |
| 796 | String8(fileName))); |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | return pAsset; |
| 801 | } |
| 802 | |
| 803 | /* |
| 804 | * Open an asset, searching for it in the directory hierarchy for the |
| 805 | * specified app. |
| 806 | * |
| 807 | * Pass in a NULL values for "appName" if the common app directory should |
| 808 | * be used. |
| 809 | */ |
| 810 | Asset* AssetManager::openInPathLocked(const char* fileName, AccessMode mode, |
| 811 | const asset_path& ap) |
| 812 | { |
| 813 | Asset* pAsset = NULL; |
| 814 | |
| 815 | /* |
| 816 | * Try various combinations of locale and vendor. |
| 817 | */ |
| 818 | if (mLocale != NULL && mVendor != NULL) |
| 819 | pAsset = openInLocaleVendorLocked(fileName, mode, ap, mLocale, mVendor); |
| 820 | if (pAsset == NULL && mVendor != NULL) |
| 821 | pAsset = openInLocaleVendorLocked(fileName, mode, ap, NULL, mVendor); |
| 822 | if (pAsset == NULL && mLocale != NULL) |
| 823 | pAsset = openInLocaleVendorLocked(fileName, mode, ap, mLocale, NULL); |
| 824 | if (pAsset == NULL) |
| 825 | pAsset = openInLocaleVendorLocked(fileName, mode, ap, NULL, NULL); |
| 826 | |
| 827 | return pAsset; |
| 828 | } |
| 829 | |
| 830 | /* |
| 831 | * Open an asset, searching for it in the directory hierarchy for the |
| 832 | * specified locale and vendor. |
| 833 | * |
| 834 | * We also search in "app.jar". |
| 835 | * |
| 836 | * Pass in NULL values for "appName", "locale", and "vendor" if the |
| 837 | * defaults should be used. |
| 838 | */ |
| 839 | Asset* AssetManager::openInLocaleVendorLocked(const char* fileName, AccessMode mode, |
| 840 | const asset_path& ap, const char* locale, const char* vendor) |
| 841 | { |
| 842 | Asset* pAsset = NULL; |
| 843 | |
| 844 | if (ap.type == kFileTypeDirectory) { |
| 845 | if (mCacheMode == CACHE_OFF) { |
| 846 | /* look at the filesystem on disk */ |
| 847 | String8 path(createPathNameLocked(ap, locale, vendor)); |
| 848 | path.appendPath(fileName); |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 849 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 850 | String8 excludeName(path); |
| 851 | excludeName.append(kExcludeExtension); |
| 852 | if (::getFileType(excludeName.string()) != kFileTypeNonexistent) { |
| 853 | /* say no more */ |
| 854 | //printf("+++ excluding '%s'\n", (const char*) excludeName); |
| 855 | return kExcludedAsset; |
| 856 | } |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 857 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 858 | pAsset = openAssetFromFileLocked(path, mode); |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 859 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 860 | if (pAsset == NULL) { |
| 861 | /* try again, this time with ".gz" */ |
| 862 | path.append(".gz"); |
| 863 | pAsset = openAssetFromFileLocked(path, mode); |
| 864 | } |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 865 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 866 | if (pAsset != NULL) |
| 867 | pAsset->setAssetSource(path); |
| 868 | } else { |
| 869 | /* find in cache */ |
| 870 | String8 path(createPathNameLocked(ap, locale, vendor)); |
| 871 | path.appendPath(fileName); |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 872 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 873 | AssetDir::FileInfo tmpInfo; |
| 874 | bool found = false; |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 875 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 876 | String8 excludeName(path); |
| 877 | excludeName.append(kExcludeExtension); |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 878 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 879 | if (mCache.indexOf(excludeName) != NAME_NOT_FOUND) { |
| 880 | /* go no farther */ |
| 881 | //printf("+++ Excluding '%s'\n", (const char*) excludeName); |
| 882 | return kExcludedAsset; |
| 883 | } |
| 884 | |
| 885 | /* |
| 886 | * File compression extensions (".gz") don't get stored in the |
| 887 | * name cache, so we have to try both here. |
| 888 | */ |
| 889 | if (mCache.indexOf(path) != NAME_NOT_FOUND) { |
| 890 | found = true; |
| 891 | pAsset = openAssetFromFileLocked(path, mode); |
| 892 | if (pAsset == NULL) { |
| 893 | /* try again, this time with ".gz" */ |
| 894 | path.append(".gz"); |
| 895 | pAsset = openAssetFromFileLocked(path, mode); |
| 896 | } |
| 897 | } |
| 898 | |
| 899 | if (pAsset != NULL) |
| 900 | pAsset->setAssetSource(path); |
| 901 | |
| 902 | /* |
| 903 | * Don't continue the search into the Zip files. Our cached info |
| 904 | * said it was a file on disk; to be consistent with openDir() |
| 905 | * we want to return the loose asset. If the cached file gets |
| 906 | * removed, we fail. |
| 907 | * |
| 908 | * The alternative is to update our cache when files get deleted, |
| 909 | * or make some sort of "best effort" promise, but for now I'm |
| 910 | * taking the hard line. |
| 911 | */ |
| 912 | if (found) { |
| 913 | if (pAsset == NULL) |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 914 | ALOGD("Expected file not found: '%s'\n", path.string()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 915 | return pAsset; |
| 916 | } |
| 917 | } |
| 918 | } |
| 919 | |
| 920 | /* |
| 921 | * Either it wasn't found on disk or on the cached view of the disk. |
| 922 | * Dig through the currently-opened set of Zip files. If caching |
| 923 | * is disabled, the Zip file may get reopened. |
| 924 | */ |
| 925 | if (pAsset == NULL && ap.type == kFileTypeRegular) { |
| 926 | String8 path; |
| 927 | |
| 928 | path.appendPath((locale != NULL) ? locale : kDefaultLocale); |
| 929 | path.appendPath((vendor != NULL) ? vendor : kDefaultVendor); |
| 930 | path.appendPath(fileName); |
| 931 | |
| 932 | /* check the appropriate Zip file */ |
Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 933 | ZipFileRO* pZip = getZipFileLocked(ap); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 934 | if (pZip != NULL) { |
| 935 | //printf("GOT zip, checking '%s'\n", (const char*) path); |
Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 936 | ZipEntryRO entry = pZip->findEntryByName(path.string()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 937 | if (entry != NULL) { |
| 938 | //printf("FOUND in Zip file for %s/%s-%s\n", |
| 939 | // appName, locale, vendor); |
| 940 | pAsset = openAssetFromZipLocked(pZip, entry, mode, path); |
Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 941 | pZip->releaseEntry(entry); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 942 | } |
| 943 | } |
| 944 | |
| 945 | if (pAsset != NULL) { |
| 946 | /* create a "source" name, for debug/display */ |
| 947 | pAsset->setAssetSource(createZipSourceNameLocked(ZipSet::getPathName(ap.path.string()), |
| 948 | String8(""), String8(fileName))); |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | return pAsset; |
| 953 | } |
| 954 | |
| 955 | /* |
| 956 | * Create a "source name" for a file from a Zip archive. |
| 957 | */ |
| 958 | String8 AssetManager::createZipSourceNameLocked(const String8& zipFileName, |
| 959 | const String8& dirName, const String8& fileName) |
| 960 | { |
| 961 | String8 sourceName("zip:"); |
| 962 | sourceName.append(zipFileName); |
| 963 | sourceName.append(":"); |
| 964 | if (dirName.length() > 0) { |
| 965 | sourceName.appendPath(dirName); |
| 966 | } |
| 967 | sourceName.appendPath(fileName); |
| 968 | return sourceName; |
| 969 | } |
| 970 | |
| 971 | /* |
| 972 | * Create a path to a loose asset (asset-base/app/locale/vendor). |
| 973 | */ |
| 974 | String8 AssetManager::createPathNameLocked(const asset_path& ap, const char* locale, |
| 975 | const char* vendor) |
| 976 | { |
| 977 | String8 path(ap.path); |
| 978 | path.appendPath((locale != NULL) ? locale : kDefaultLocale); |
| 979 | path.appendPath((vendor != NULL) ? vendor : kDefaultVendor); |
| 980 | return path; |
| 981 | } |
| 982 | |
| 983 | /* |
| 984 | * Create a path to a loose asset (asset-base/app/rootDir). |
| 985 | */ |
| 986 | String8 AssetManager::createPathNameLocked(const asset_path& ap, const char* rootDir) |
| 987 | { |
| 988 | String8 path(ap.path); |
| 989 | if (rootDir != NULL) path.appendPath(rootDir); |
| 990 | return path; |
| 991 | } |
| 992 | |
| 993 | /* |
| 994 | * Return a pointer to one of our open Zip archives. Returns NULL if no |
| 995 | * matching Zip file exists. |
| 996 | * |
| 997 | * Right now we have 2 possible Zip files (1 each in app/"common"). |
| 998 | * |
| 999 | * If caching is set to CACHE_OFF, to get the expected behavior we |
| 1000 | * need to reopen the Zip file on every request. That would be silly |
| 1001 | * and expensive, so instead we just check the file modification date. |
| 1002 | * |
| 1003 | * Pass in NULL values for "appName", "locale", and "vendor" if the |
| 1004 | * generics should be used. |
| 1005 | */ |
| 1006 | ZipFileRO* AssetManager::getZipFileLocked(const asset_path& ap) |
| 1007 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1008 | ALOGV("getZipFileLocked() in %p\n", this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1009 | |
| 1010 | return mZipSet.getZip(ap.path); |
| 1011 | } |
| 1012 | |
| 1013 | /* |
| 1014 | * Try to open an asset from a file on disk. |
| 1015 | * |
| 1016 | * If the file is compressed with gzip, we seek to the start of the |
| 1017 | * deflated data and pass that in (just like we would for a Zip archive). |
| 1018 | * |
| 1019 | * For uncompressed data, we may already have an mmap()ed version sitting |
| 1020 | * around. If so, we want to hand that to the Asset instead. |
| 1021 | * |
| 1022 | * This returns NULL if the file doesn't exist, couldn't be opened, or |
| 1023 | * claims to be a ".gz" but isn't. |
| 1024 | */ |
| 1025 | Asset* AssetManager::openAssetFromFileLocked(const String8& pathName, |
| 1026 | AccessMode mode) |
| 1027 | { |
| 1028 | Asset* pAsset = NULL; |
| 1029 | |
| 1030 | if (strcasecmp(pathName.getPathExtension().string(), ".gz") == 0) { |
| 1031 | //printf("TRYING '%s'\n", (const char*) pathName); |
| 1032 | pAsset = Asset::createFromCompressedFile(pathName.string(), mode); |
| 1033 | } else { |
| 1034 | //printf("TRYING '%s'\n", (const char*) pathName); |
| 1035 | pAsset = Asset::createFromFile(pathName.string(), mode); |
| 1036 | } |
| 1037 | |
| 1038 | return pAsset; |
| 1039 | } |
| 1040 | |
| 1041 | /* |
| 1042 | * Given an entry in a Zip archive, create a new Asset object. |
| 1043 | * |
| 1044 | * If the entry is uncompressed, we may want to create or share a |
| 1045 | * slice of shared memory. |
| 1046 | */ |
| 1047 | Asset* AssetManager::openAssetFromZipLocked(const ZipFileRO* pZipFile, |
| 1048 | const ZipEntryRO entry, AccessMode mode, const String8& entryName) |
| 1049 | { |
| 1050 | Asset* pAsset = NULL; |
| 1051 | |
| 1052 | // TODO: look for previously-created shared memory slice? |
| 1053 | int method; |
Kenny Root | 68246dc | 2010-04-22 18:28:29 -0700 | [diff] [blame] | 1054 | size_t uncompressedLen; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1055 | |
| 1056 | //printf("USING Zip '%s'\n", pEntry->getFileName()); |
| 1057 | |
| 1058 | //pZipFile->getEntryInfo(entry, &method, &uncompressedLen, &compressedLen, |
| 1059 | // &offset); |
| 1060 | if (!pZipFile->getEntryInfo(entry, &method, &uncompressedLen, NULL, NULL, |
| 1061 | NULL, NULL)) |
| 1062 | { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1063 | ALOGW("getEntryInfo failed\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1064 | return NULL; |
| 1065 | } |
| 1066 | |
| 1067 | FileMap* dataMap = pZipFile->createEntryFileMap(entry); |
| 1068 | if (dataMap == NULL) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1069 | ALOGW("create map from entry failed\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1070 | return NULL; |
| 1071 | } |
| 1072 | |
| 1073 | if (method == ZipFileRO::kCompressStored) { |
| 1074 | pAsset = Asset::createFromUncompressedMap(dataMap, mode); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1075 | ALOGV("Opened uncompressed entry %s in zip %s mode %d: %p", entryName.string(), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1076 | dataMap->getFileName(), mode, pAsset); |
| 1077 | } else { |
| 1078 | pAsset = Asset::createFromCompressedMap(dataMap, method, |
| 1079 | uncompressedLen, mode); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1080 | ALOGV("Opened compressed entry %s in zip %s mode %d: %p", entryName.string(), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1081 | dataMap->getFileName(), mode, pAsset); |
| 1082 | } |
| 1083 | if (pAsset == NULL) { |
| 1084 | /* unexpected */ |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1085 | ALOGW("create from segment failed\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1086 | } |
| 1087 | |
| 1088 | return pAsset; |
| 1089 | } |
| 1090 | |
| 1091 | |
| 1092 | |
| 1093 | /* |
| 1094 | * Open a directory in the asset namespace. |
| 1095 | * |
| 1096 | * An "asset directory" is simply the combination of all files in all |
| 1097 | * locations, with ".gz" stripped for loose files. With app, locale, and |
| 1098 | * vendor defined, we have 8 directories and 2 Zip archives to scan. |
| 1099 | * |
| 1100 | * Pass in "" for the root dir. |
| 1101 | */ |
| 1102 | AssetDir* AssetManager::openDir(const char* dirName) |
| 1103 | { |
| 1104 | AutoMutex _l(mLock); |
| 1105 | |
| 1106 | AssetDir* pDir = NULL; |
| 1107 | SortedVector<AssetDir::FileInfo>* pMergedInfo = NULL; |
| 1108 | |
| 1109 | LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager"); |
| 1110 | assert(dirName != NULL); |
| 1111 | |
| 1112 | //printf("+++ openDir(%s) in '%s'\n", dirName, (const char*) mAssetBase); |
| 1113 | |
| 1114 | if (mCacheMode != CACHE_OFF && !mCacheValid) |
| 1115 | loadFileNameCacheLocked(); |
| 1116 | |
| 1117 | pDir = new AssetDir; |
| 1118 | |
| 1119 | /* |
| 1120 | * Scan the various directories, merging what we find into a single |
| 1121 | * vector. We want to scan them in reverse priority order so that |
| 1122 | * the ".EXCLUDE" processing works correctly. Also, if we decide we |
| 1123 | * want to remember where the file is coming from, we'll get the right |
| 1124 | * version. |
| 1125 | * |
| 1126 | * We start with Zip archives, then do loose files. |
| 1127 | */ |
| 1128 | pMergedInfo = new SortedVector<AssetDir::FileInfo>; |
| 1129 | |
| 1130 | size_t i = mAssetPaths.size(); |
| 1131 | while (i > 0) { |
| 1132 | i--; |
| 1133 | const asset_path& ap = mAssetPaths.itemAt(i); |
| 1134 | if (ap.type == kFileTypeRegular) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1135 | ALOGV("Adding directory %s from zip %s", dirName, ap.path.string()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1136 | scanAndMergeZipLocked(pMergedInfo, ap, kAssetsRoot, dirName); |
| 1137 | } else { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1138 | ALOGV("Adding directory %s from dir %s", dirName, ap.path.string()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1139 | scanAndMergeDirLocked(pMergedInfo, ap, kAssetsRoot, dirName); |
| 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | #if 0 |
| 1144 | printf("FILE LIST:\n"); |
| 1145 | for (i = 0; i < (size_t) pMergedInfo->size(); i++) { |
| 1146 | printf(" %d: (%d) '%s'\n", i, |
| 1147 | pMergedInfo->itemAt(i).getFileType(), |
| 1148 | (const char*) pMergedInfo->itemAt(i).getFileName()); |
| 1149 | } |
| 1150 | #endif |
| 1151 | |
| 1152 | pDir->setFileList(pMergedInfo); |
| 1153 | return pDir; |
| 1154 | } |
| 1155 | |
| 1156 | /* |
Dianne Hackborn | bb9ea30 | 2009-05-18 15:22:00 -0700 | [diff] [blame] | 1157 | * Open a directory in the non-asset namespace. |
| 1158 | * |
| 1159 | * An "asset directory" is simply the combination of all files in all |
| 1160 | * locations, with ".gz" stripped for loose files. With app, locale, and |
| 1161 | * vendor defined, we have 8 directories and 2 Zip archives to scan. |
| 1162 | * |
| 1163 | * Pass in "" for the root dir. |
| 1164 | */ |
Narayan Kamath | 745d4ef | 2014-01-27 11:17:22 +0000 | [diff] [blame] | 1165 | AssetDir* AssetManager::openNonAssetDir(const int32_t cookie, const char* dirName) |
Dianne Hackborn | bb9ea30 | 2009-05-18 15:22:00 -0700 | [diff] [blame] | 1166 | { |
| 1167 | AutoMutex _l(mLock); |
| 1168 | |
| 1169 | AssetDir* pDir = NULL; |
| 1170 | SortedVector<AssetDir::FileInfo>* pMergedInfo = NULL; |
| 1171 | |
| 1172 | LOG_FATAL_IF(mAssetPaths.size() == 0, "No assets added to AssetManager"); |
| 1173 | assert(dirName != NULL); |
| 1174 | |
| 1175 | //printf("+++ openDir(%s) in '%s'\n", dirName, (const char*) mAssetBase); |
| 1176 | |
| 1177 | if (mCacheMode != CACHE_OFF && !mCacheValid) |
| 1178 | loadFileNameCacheLocked(); |
| 1179 | |
| 1180 | pDir = new AssetDir; |
| 1181 | |
| 1182 | pMergedInfo = new SortedVector<AssetDir::FileInfo>; |
| 1183 | |
Narayan Kamath | 745d4ef | 2014-01-27 11:17:22 +0000 | [diff] [blame] | 1184 | const size_t which = static_cast<size_t>(cookie) - 1; |
Dianne Hackborn | bb9ea30 | 2009-05-18 15:22:00 -0700 | [diff] [blame] | 1185 | |
| 1186 | if (which < mAssetPaths.size()) { |
| 1187 | const asset_path& ap = mAssetPaths.itemAt(which); |
| 1188 | if (ap.type == kFileTypeRegular) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1189 | ALOGV("Adding directory %s from zip %s", dirName, ap.path.string()); |
Dianne Hackborn | bb9ea30 | 2009-05-18 15:22:00 -0700 | [diff] [blame] | 1190 | scanAndMergeZipLocked(pMergedInfo, ap, NULL, dirName); |
| 1191 | } else { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1192 | ALOGV("Adding directory %s from dir %s", dirName, ap.path.string()); |
Dianne Hackborn | bb9ea30 | 2009-05-18 15:22:00 -0700 | [diff] [blame] | 1193 | scanAndMergeDirLocked(pMergedInfo, ap, NULL, dirName); |
| 1194 | } |
| 1195 | } |
| 1196 | |
| 1197 | #if 0 |
| 1198 | printf("FILE LIST:\n"); |
| 1199 | for (i = 0; i < (size_t) pMergedInfo->size(); i++) { |
| 1200 | printf(" %d: (%d) '%s'\n", i, |
| 1201 | pMergedInfo->itemAt(i).getFileType(), |
| 1202 | (const char*) pMergedInfo->itemAt(i).getFileName()); |
| 1203 | } |
| 1204 | #endif |
| 1205 | |
| 1206 | pDir->setFileList(pMergedInfo); |
| 1207 | return pDir; |
| 1208 | } |
| 1209 | |
| 1210 | /* |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1211 | * Scan the contents of the specified directory and merge them into the |
| 1212 | * "pMergedInfo" vector, removing previous entries if we find "exclude" |
| 1213 | * directives. |
| 1214 | * |
| 1215 | * Returns "false" if we found nothing to contribute. |
| 1216 | */ |
| 1217 | bool AssetManager::scanAndMergeDirLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo, |
| 1218 | const asset_path& ap, const char* rootDir, const char* dirName) |
| 1219 | { |
| 1220 | SortedVector<AssetDir::FileInfo>* pContents; |
| 1221 | String8 path; |
| 1222 | |
| 1223 | assert(pMergedInfo != NULL); |
| 1224 | |
| 1225 | //printf("scanAndMergeDir: %s %s %s %s\n", appName, locale, vendor,dirName); |
| 1226 | |
| 1227 | if (mCacheValid) { |
| 1228 | int i, start, count; |
| 1229 | |
| 1230 | pContents = new SortedVector<AssetDir::FileInfo>; |
| 1231 | |
| 1232 | /* |
| 1233 | * Get the basic partial path and find it in the cache. That's |
| 1234 | * the start point for the search. |
| 1235 | */ |
| 1236 | path = createPathNameLocked(ap, rootDir); |
| 1237 | if (dirName[0] != '\0') |
| 1238 | path.appendPath(dirName); |
| 1239 | |
| 1240 | start = mCache.indexOf(path); |
| 1241 | if (start == NAME_NOT_FOUND) { |
| 1242 | //printf("+++ not found in cache: dir '%s'\n", (const char*) path); |
| 1243 | delete pContents; |
| 1244 | return false; |
| 1245 | } |
| 1246 | |
| 1247 | /* |
| 1248 | * The match string looks like "common/default/default/foo/bar/". |
| 1249 | * The '/' on the end ensures that we don't match on the directory |
| 1250 | * itself or on ".../foo/barfy/". |
| 1251 | */ |
| 1252 | path.append("/"); |
| 1253 | |
| 1254 | count = mCache.size(); |
| 1255 | |
| 1256 | /* |
| 1257 | * Pick out the stuff in the current dir by examining the pathname. |
| 1258 | * It needs to match the partial pathname prefix, and not have a '/' |
| 1259 | * (fssep) anywhere after the prefix. |
| 1260 | */ |
| 1261 | for (i = start+1; i < count; i++) { |
| 1262 | if (mCache[i].getFileName().length() > path.length() && |
| 1263 | strncmp(mCache[i].getFileName().string(), path.string(), path.length()) == 0) |
| 1264 | { |
| 1265 | const char* name = mCache[i].getFileName().string(); |
| 1266 | // XXX THIS IS BROKEN! Looks like we need to store the full |
| 1267 | // path prefix separately from the file path. |
| 1268 | if (strchr(name + path.length(), '/') == NULL) { |
| 1269 | /* grab it, reducing path to just the filename component */ |
| 1270 | AssetDir::FileInfo tmp = mCache[i]; |
| 1271 | tmp.setFileName(tmp.getFileName().getPathLeaf()); |
| 1272 | pContents->add(tmp); |
| 1273 | } |
| 1274 | } else { |
| 1275 | /* no longer in the dir or its subdirs */ |
| 1276 | break; |
| 1277 | } |
| 1278 | |
| 1279 | } |
| 1280 | } else { |
| 1281 | path = createPathNameLocked(ap, rootDir); |
| 1282 | if (dirName[0] != '\0') |
| 1283 | path.appendPath(dirName); |
| 1284 | pContents = scanDirLocked(path); |
| 1285 | if (pContents == NULL) |
| 1286 | return false; |
| 1287 | } |
| 1288 | |
| 1289 | // if we wanted to do an incremental cache fill, we would do it here |
| 1290 | |
| 1291 | /* |
| 1292 | * Process "exclude" directives. If we find a filename that ends with |
| 1293 | * ".EXCLUDE", we look for a matching entry in the "merged" set, and |
| 1294 | * remove it if we find it. We also delete the "exclude" entry. |
| 1295 | */ |
| 1296 | int i, count, exclExtLen; |
| 1297 | |
| 1298 | count = pContents->size(); |
| 1299 | exclExtLen = strlen(kExcludeExtension); |
| 1300 | for (i = 0; i < count; i++) { |
| 1301 | const char* name; |
| 1302 | int nameLen; |
| 1303 | |
| 1304 | name = pContents->itemAt(i).getFileName().string(); |
| 1305 | nameLen = strlen(name); |
| 1306 | if (nameLen > exclExtLen && |
| 1307 | strcmp(name + (nameLen - exclExtLen), kExcludeExtension) == 0) |
| 1308 | { |
| 1309 | String8 match(name, nameLen - exclExtLen); |
| 1310 | int matchIdx; |
| 1311 | |
| 1312 | matchIdx = AssetDir::FileInfo::findEntry(pMergedInfo, match); |
| 1313 | if (matchIdx > 0) { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1314 | ALOGV("Excluding '%s' [%s]\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1315 | pMergedInfo->itemAt(matchIdx).getFileName().string(), |
| 1316 | pMergedInfo->itemAt(matchIdx).getSourceName().string()); |
| 1317 | pMergedInfo->removeAt(matchIdx); |
| 1318 | } else { |
| 1319 | //printf("+++ no match on '%s'\n", (const char*) match); |
| 1320 | } |
| 1321 | |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1322 | ALOGD("HEY: size=%d removing %d\n", (int)pContents->size(), i); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1323 | pContents->removeAt(i); |
| 1324 | i--; // adjust "for" loop |
| 1325 | count--; // and loop limit |
| 1326 | } |
| 1327 | } |
| 1328 | |
| 1329 | mergeInfoLocked(pMergedInfo, pContents); |
| 1330 | |
| 1331 | delete pContents; |
| 1332 | |
| 1333 | return true; |
| 1334 | } |
| 1335 | |
| 1336 | /* |
| 1337 | * Scan the contents of the specified directory, and stuff what we find |
| 1338 | * into a newly-allocated vector. |
| 1339 | * |
| 1340 | * Files ending in ".gz" will have their extensions removed. |
| 1341 | * |
| 1342 | * We should probably think about skipping files with "illegal" names, |
| 1343 | * e.g. illegal characters (/\:) or excessive length. |
| 1344 | * |
| 1345 | * Returns NULL if the specified directory doesn't exist. |
| 1346 | */ |
| 1347 | SortedVector<AssetDir::FileInfo>* AssetManager::scanDirLocked(const String8& path) |
| 1348 | { |
| 1349 | SortedVector<AssetDir::FileInfo>* pContents = NULL; |
| 1350 | DIR* dir; |
| 1351 | struct dirent* entry; |
| 1352 | FileType fileType; |
| 1353 | |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1354 | ALOGV("Scanning dir '%s'\n", path.string()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1355 | |
| 1356 | dir = opendir(path.string()); |
| 1357 | if (dir == NULL) |
| 1358 | return NULL; |
| 1359 | |
| 1360 | pContents = new SortedVector<AssetDir::FileInfo>; |
| 1361 | |
| 1362 | while (1) { |
| 1363 | entry = readdir(dir); |
| 1364 | if (entry == NULL) |
| 1365 | break; |
| 1366 | |
| 1367 | if (strcmp(entry->d_name, ".") == 0 || |
| 1368 | strcmp(entry->d_name, "..") == 0) |
| 1369 | continue; |
| 1370 | |
| 1371 | #ifdef _DIRENT_HAVE_D_TYPE |
| 1372 | if (entry->d_type == DT_REG) |
| 1373 | fileType = kFileTypeRegular; |
| 1374 | else if (entry->d_type == DT_DIR) |
| 1375 | fileType = kFileTypeDirectory; |
| 1376 | else |
| 1377 | fileType = kFileTypeUnknown; |
| 1378 | #else |
| 1379 | // stat the file |
| 1380 | fileType = ::getFileType(path.appendPathCopy(entry->d_name).string()); |
| 1381 | #endif |
| 1382 | |
| 1383 | if (fileType != kFileTypeRegular && fileType != kFileTypeDirectory) |
| 1384 | continue; |
| 1385 | |
| 1386 | AssetDir::FileInfo info; |
| 1387 | info.set(String8(entry->d_name), fileType); |
| 1388 | if (strcasecmp(info.getFileName().getPathExtension().string(), ".gz") == 0) |
| 1389 | info.setFileName(info.getFileName().getBasePath()); |
| 1390 | info.setSourceName(path.appendPathCopy(info.getFileName())); |
| 1391 | pContents->add(info); |
| 1392 | } |
| 1393 | |
| 1394 | closedir(dir); |
| 1395 | return pContents; |
| 1396 | } |
| 1397 | |
| 1398 | /* |
| 1399 | * Scan the contents out of the specified Zip archive, and merge what we |
| 1400 | * find into "pMergedInfo". If the Zip archive in question doesn't exist, |
| 1401 | * we return immediately. |
| 1402 | * |
| 1403 | * Returns "false" if we found nothing to contribute. |
| 1404 | */ |
| 1405 | bool AssetManager::scanAndMergeZipLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo, |
| 1406 | const asset_path& ap, const char* rootDir, const char* baseDirName) |
| 1407 | { |
| 1408 | ZipFileRO* pZip; |
| 1409 | Vector<String8> dirs; |
| 1410 | AssetDir::FileInfo info; |
| 1411 | SortedVector<AssetDir::FileInfo> contents; |
| 1412 | String8 sourceName, zipName, dirName; |
| 1413 | |
| 1414 | pZip = mZipSet.getZip(ap.path); |
| 1415 | if (pZip == NULL) { |
Steve Block | 8564c8d | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1416 | ALOGW("Failure opening zip %s\n", ap.path.string()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1417 | return false; |
| 1418 | } |
| 1419 | |
| 1420 | zipName = ZipSet::getPathName(ap.path.string()); |
| 1421 | |
| 1422 | /* convert "sounds" to "rootDir/sounds" */ |
| 1423 | if (rootDir != NULL) dirName = rootDir; |
| 1424 | dirName.appendPath(baseDirName); |
| 1425 | |
| 1426 | /* |
| 1427 | * Scan through the list of files, looking for a match. The files in |
| 1428 | * the Zip table of contents are not in sorted order, so we have to |
| 1429 | * process the entire list. We're looking for a string that begins |
| 1430 | * with the characters in "dirName", is followed by a '/', and has no |
| 1431 | * subsequent '/' in the stuff that follows. |
| 1432 | * |
| 1433 | * What makes this especially fun is that directories are not stored |
| 1434 | * explicitly in Zip archives, so we have to infer them from context. |
| 1435 | * When we see "sounds/foo.wav" we have to leave a note to ourselves |
| 1436 | * to insert a directory called "sounds" into the list. We store |
| 1437 | * these in temporary vector so that we only return each one once. |
| 1438 | * |
| 1439 | * Name comparisons are case-sensitive to match UNIX filesystem |
| 1440 | * semantics. |
| 1441 | */ |
| 1442 | int dirNameLen = dirName.length(); |
Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 1443 | void *iterationCookie; |
| 1444 | if (!pZip->startIteration(&iterationCookie)) { |
| 1445 | ALOGW("ZipFileRO::startIteration returned false"); |
| 1446 | return false; |
| 1447 | } |
| 1448 | |
| 1449 | ZipEntryRO entry; |
| 1450 | while ((entry = pZip->nextEntry(iterationCookie)) != NULL) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1451 | char nameBuf[256]; |
| 1452 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1453 | if (pZip->getEntryFileName(entry, nameBuf, sizeof(nameBuf)) != 0) { |
| 1454 | // TODO: fix this if we expect to have long names |
Steve Block | 3762c31 | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 1455 | ALOGE("ARGH: name too long?\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1456 | continue; |
| 1457 | } |
Dianne Hackborn | bb9ea30 | 2009-05-18 15:22:00 -0700 | [diff] [blame] | 1458 | //printf("Comparing %s in %s?\n", nameBuf, dirName.string()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1459 | if (dirNameLen == 0 || |
| 1460 | (strncmp(nameBuf, dirName.string(), dirNameLen) == 0 && |
| 1461 | nameBuf[dirNameLen] == '/')) |
| 1462 | { |
| 1463 | const char* cp; |
| 1464 | const char* nextSlash; |
| 1465 | |
| 1466 | cp = nameBuf + dirNameLen; |
| 1467 | if (dirNameLen != 0) |
| 1468 | cp++; // advance past the '/' |
| 1469 | |
| 1470 | nextSlash = strchr(cp, '/'); |
| 1471 | //xxx this may break if there are bare directory entries |
| 1472 | if (nextSlash == NULL) { |
| 1473 | /* this is a file in the requested directory */ |
| 1474 | |
| 1475 | info.set(String8(nameBuf).getPathLeaf(), kFileTypeRegular); |
| 1476 | |
| 1477 | info.setSourceName( |
| 1478 | createZipSourceNameLocked(zipName, dirName, info.getFileName())); |
| 1479 | |
| 1480 | contents.add(info); |
Dianne Hackborn | bb9ea30 | 2009-05-18 15:22:00 -0700 | [diff] [blame] | 1481 | //printf("FOUND: file '%s'\n", info.getFileName().string()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1482 | } else { |
| 1483 | /* this is a subdir; add it if we don't already have it*/ |
| 1484 | String8 subdirName(cp, nextSlash - cp); |
| 1485 | size_t j; |
| 1486 | size_t N = dirs.size(); |
| 1487 | |
| 1488 | for (j = 0; j < N; j++) { |
| 1489 | if (subdirName == dirs[j]) { |
| 1490 | break; |
| 1491 | } |
| 1492 | } |
| 1493 | if (j == N) { |
| 1494 | dirs.add(subdirName); |
| 1495 | } |
| 1496 | |
Dianne Hackborn | bb9ea30 | 2009-05-18 15:22:00 -0700 | [diff] [blame] | 1497 | //printf("FOUND: dir '%s'\n", subdirName.string()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1498 | } |
| 1499 | } |
| 1500 | } |
| 1501 | |
Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 1502 | pZip->endIteration(iterationCookie); |
| 1503 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1504 | /* |
| 1505 | * Add the set of unique directories. |
| 1506 | */ |
| 1507 | for (int i = 0; i < (int) dirs.size(); i++) { |
| 1508 | info.set(dirs[i], kFileTypeDirectory); |
| 1509 | info.setSourceName( |
| 1510 | createZipSourceNameLocked(zipName, dirName, info.getFileName())); |
| 1511 | contents.add(info); |
| 1512 | } |
| 1513 | |
| 1514 | mergeInfoLocked(pMergedInfo, &contents); |
| 1515 | |
| 1516 | return true; |
| 1517 | } |
| 1518 | |
| 1519 | |
| 1520 | /* |
| 1521 | * Merge two vectors of FileInfo. |
| 1522 | * |
| 1523 | * The merged contents will be stuffed into *pMergedInfo. |
| 1524 | * |
| 1525 | * If an entry for a file exists in both "pMergedInfo" and "pContents", |
| 1526 | * we use the newer "pContents" entry. |
| 1527 | */ |
| 1528 | void AssetManager::mergeInfoLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo, |
| 1529 | const SortedVector<AssetDir::FileInfo>* pContents) |
| 1530 | { |
| 1531 | /* |
| 1532 | * Merge what we found in this directory with what we found in |
| 1533 | * other places. |
| 1534 | * |
| 1535 | * Two basic approaches: |
| 1536 | * (1) Create a new array that holds the unique values of the two |
| 1537 | * arrays. |
| 1538 | * (2) Take the elements from pContents and shove them into pMergedInfo. |
| 1539 | * |
| 1540 | * Because these are vectors of complex objects, moving elements around |
| 1541 | * inside the vector requires constructing new objects and allocating |
| 1542 | * storage for members. With approach #1, we're always adding to the |
| 1543 | * end, whereas with #2 we could be inserting multiple elements at the |
| 1544 | * front of the vector. Approach #1 requires a full copy of the |
| 1545 | * contents of pMergedInfo, but approach #2 requires the same copy for |
| 1546 | * every insertion at the front of pMergedInfo. |
| 1547 | * |
| 1548 | * (We should probably use a SortedVector interface that allows us to |
| 1549 | * just stuff items in, trusting us to maintain the sort order.) |
| 1550 | */ |
| 1551 | SortedVector<AssetDir::FileInfo>* pNewSorted; |
| 1552 | int mergeMax, contMax; |
| 1553 | int mergeIdx, contIdx; |
| 1554 | |
| 1555 | pNewSorted = new SortedVector<AssetDir::FileInfo>; |
| 1556 | mergeMax = pMergedInfo->size(); |
| 1557 | contMax = pContents->size(); |
| 1558 | mergeIdx = contIdx = 0; |
| 1559 | |
| 1560 | while (mergeIdx < mergeMax || contIdx < contMax) { |
| 1561 | if (mergeIdx == mergeMax) { |
| 1562 | /* hit end of "merge" list, copy rest of "contents" */ |
| 1563 | pNewSorted->add(pContents->itemAt(contIdx)); |
| 1564 | contIdx++; |
| 1565 | } else if (contIdx == contMax) { |
| 1566 | /* hit end of "cont" list, copy rest of "merge" */ |
| 1567 | pNewSorted->add(pMergedInfo->itemAt(mergeIdx)); |
| 1568 | mergeIdx++; |
| 1569 | } else if (pMergedInfo->itemAt(mergeIdx) == pContents->itemAt(contIdx)) |
| 1570 | { |
| 1571 | /* items are identical, add newer and advance both indices */ |
| 1572 | pNewSorted->add(pContents->itemAt(contIdx)); |
| 1573 | mergeIdx++; |
| 1574 | contIdx++; |
| 1575 | } else if (pMergedInfo->itemAt(mergeIdx) < pContents->itemAt(contIdx)) |
| 1576 | { |
| 1577 | /* "merge" is lower, add that one */ |
| 1578 | pNewSorted->add(pMergedInfo->itemAt(mergeIdx)); |
| 1579 | mergeIdx++; |
| 1580 | } else { |
| 1581 | /* "cont" is lower, add that one */ |
| 1582 | assert(pContents->itemAt(contIdx) < pMergedInfo->itemAt(mergeIdx)); |
| 1583 | pNewSorted->add(pContents->itemAt(contIdx)); |
| 1584 | contIdx++; |
| 1585 | } |
| 1586 | } |
| 1587 | |
| 1588 | /* |
| 1589 | * Overwrite the "merged" list with the new stuff. |
| 1590 | */ |
| 1591 | *pMergedInfo = *pNewSorted; |
| 1592 | delete pNewSorted; |
| 1593 | |
| 1594 | #if 0 // for Vector, rather than SortedVector |
| 1595 | int i, j; |
| 1596 | for (i = pContents->size() -1; i >= 0; i--) { |
| 1597 | bool add = true; |
| 1598 | |
| 1599 | for (j = pMergedInfo->size() -1; j >= 0; j--) { |
| 1600 | /* case-sensitive comparisons, to behave like UNIX fs */ |
| 1601 | if (strcmp(pContents->itemAt(i).mFileName, |
| 1602 | pMergedInfo->itemAt(j).mFileName) == 0) |
| 1603 | { |
| 1604 | /* match, don't add this entry */ |
| 1605 | add = false; |
| 1606 | break; |
| 1607 | } |
| 1608 | } |
| 1609 | |
| 1610 | if (add) |
| 1611 | pMergedInfo->add(pContents->itemAt(i)); |
| 1612 | } |
| 1613 | #endif |
| 1614 | } |
| 1615 | |
| 1616 | |
| 1617 | /* |
| 1618 | * Load all files into the file name cache. We want to do this across |
| 1619 | * all combinations of { appname, locale, vendor }, performing a recursive |
| 1620 | * directory traversal. |
| 1621 | * |
| 1622 | * This is not the most efficient data structure. Also, gathering the |
| 1623 | * information as we needed it (file-by-file or directory-by-directory) |
| 1624 | * would be faster. However, on the actual device, 99% of the files will |
| 1625 | * live in Zip archives, so this list will be very small. The trouble |
| 1626 | * is that we have to check the "loose" files first, so it's important |
| 1627 | * that we don't beat the filesystem silly looking for files that aren't |
| 1628 | * there. |
| 1629 | * |
| 1630 | * Note on thread safety: this is the only function that causes updates |
| 1631 | * to mCache, and anybody who tries to use it will call here if !mCacheValid, |
| 1632 | * so we need to employ a mutex here. |
| 1633 | */ |
| 1634 | void AssetManager::loadFileNameCacheLocked(void) |
| 1635 | { |
| 1636 | assert(!mCacheValid); |
| 1637 | assert(mCache.size() == 0); |
| 1638 | |
| 1639 | #ifdef DO_TIMINGS // need to link against -lrt for this now |
| 1640 | DurationTimer timer; |
| 1641 | timer.start(); |
| 1642 | #endif |
| 1643 | |
| 1644 | fncScanLocked(&mCache, ""); |
| 1645 | |
| 1646 | #ifdef DO_TIMINGS |
| 1647 | timer.stop(); |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1648 | ALOGD("Cache scan took %.3fms\n", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1649 | timer.durationUsecs() / 1000.0); |
| 1650 | #endif |
| 1651 | |
| 1652 | #if 0 |
| 1653 | int i; |
| 1654 | printf("CACHED FILE LIST (%d entries):\n", mCache.size()); |
| 1655 | for (i = 0; i < (int) mCache.size(); i++) { |
| 1656 | printf(" %d: (%d) '%s'\n", i, |
| 1657 | mCache.itemAt(i).getFileType(), |
| 1658 | (const char*) mCache.itemAt(i).getFileName()); |
| 1659 | } |
| 1660 | #endif |
| 1661 | |
| 1662 | mCacheValid = true; |
| 1663 | } |
| 1664 | |
| 1665 | /* |
| 1666 | * Scan up to 8 versions of the specified directory. |
| 1667 | */ |
| 1668 | void AssetManager::fncScanLocked(SortedVector<AssetDir::FileInfo>* pMergedInfo, |
| 1669 | const char* dirName) |
| 1670 | { |
| 1671 | size_t i = mAssetPaths.size(); |
| 1672 | while (i > 0) { |
| 1673 | i--; |
| 1674 | const asset_path& ap = mAssetPaths.itemAt(i); |
| 1675 | fncScanAndMergeDirLocked(pMergedInfo, ap, NULL, NULL, dirName); |
| 1676 | if (mLocale != NULL) |
| 1677 | fncScanAndMergeDirLocked(pMergedInfo, ap, mLocale, NULL, dirName); |
| 1678 | if (mVendor != NULL) |
| 1679 | fncScanAndMergeDirLocked(pMergedInfo, ap, NULL, mVendor, dirName); |
| 1680 | if (mLocale != NULL && mVendor != NULL) |
| 1681 | fncScanAndMergeDirLocked(pMergedInfo, ap, mLocale, mVendor, dirName); |
| 1682 | } |
| 1683 | } |
| 1684 | |
| 1685 | /* |
| 1686 | * Recursively scan this directory and all subdirs. |
| 1687 | * |
| 1688 | * This is similar to scanAndMergeDir, but we don't remove the .EXCLUDE |
| 1689 | * files, and we prepend the extended partial path to the filenames. |
| 1690 | */ |
| 1691 | bool AssetManager::fncScanAndMergeDirLocked( |
| 1692 | SortedVector<AssetDir::FileInfo>* pMergedInfo, |
| 1693 | const asset_path& ap, const char* locale, const char* vendor, |
| 1694 | const char* dirName) |
| 1695 | { |
| 1696 | SortedVector<AssetDir::FileInfo>* pContents; |
| 1697 | String8 partialPath; |
| 1698 | String8 fullPath; |
| 1699 | |
| 1700 | // XXX This is broken -- the filename cache needs to hold the base |
| 1701 | // asset path separately from its filename. |
Elliott Hughes | c367d48 | 2013-10-29 13:12:55 -0700 | [diff] [blame] | 1702 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1703 | partialPath = createPathNameLocked(ap, locale, vendor); |
| 1704 | if (dirName[0] != '\0') { |
| 1705 | partialPath.appendPath(dirName); |
| 1706 | } |
| 1707 | |
| 1708 | fullPath = partialPath; |
| 1709 | pContents = scanDirLocked(fullPath); |
| 1710 | if (pContents == NULL) { |
| 1711 | return false; // directory did not exist |
| 1712 | } |
| 1713 | |
| 1714 | /* |
| 1715 | * Scan all subdirectories of the current dir, merging what we find |
| 1716 | * into "pMergedInfo". |
| 1717 | */ |
| 1718 | for (int i = 0; i < (int) pContents->size(); i++) { |
| 1719 | if (pContents->itemAt(i).getFileType() == kFileTypeDirectory) { |
| 1720 | String8 subdir(dirName); |
| 1721 | subdir.appendPath(pContents->itemAt(i).getFileName()); |
| 1722 | |
| 1723 | fncScanAndMergeDirLocked(pMergedInfo, ap, locale, vendor, subdir.string()); |
| 1724 | } |
| 1725 | } |
| 1726 | |
| 1727 | /* |
| 1728 | * To be consistent, we want entries for the root directory. If |
| 1729 | * we're the root, add one now. |
| 1730 | */ |
| 1731 | if (dirName[0] == '\0') { |
| 1732 | AssetDir::FileInfo tmpInfo; |
| 1733 | |
| 1734 | tmpInfo.set(String8(""), kFileTypeDirectory); |
| 1735 | tmpInfo.setSourceName(createPathNameLocked(ap, locale, vendor)); |
| 1736 | pContents->add(tmpInfo); |
| 1737 | } |
| 1738 | |
| 1739 | /* |
| 1740 | * We want to prepend the extended partial path to every entry in |
| 1741 | * "pContents". It's the same value for each entry, so this will |
| 1742 | * not change the sorting order of the vector contents. |
| 1743 | */ |
| 1744 | for (int i = 0; i < (int) pContents->size(); i++) { |
| 1745 | const AssetDir::FileInfo& info = pContents->itemAt(i); |
| 1746 | pContents->editItemAt(i).setFileName(partialPath.appendPathCopy(info.getFileName())); |
| 1747 | } |
| 1748 | |
| 1749 | mergeInfoLocked(pMergedInfo, pContents); |
| 1750 | return true; |
| 1751 | } |
| 1752 | |
| 1753 | /* |
| 1754 | * Trash the cache. |
| 1755 | */ |
| 1756 | void AssetManager::purgeFileNameCacheLocked(void) |
| 1757 | { |
| 1758 | mCacheValid = false; |
| 1759 | mCache.clear(); |
| 1760 | } |
| 1761 | |
| 1762 | /* |
| 1763 | * =========================================================================== |
| 1764 | * AssetManager::SharedZip |
| 1765 | * =========================================================================== |
| 1766 | */ |
| 1767 | |
| 1768 | |
| 1769 | Mutex AssetManager::SharedZip::gLock; |
| 1770 | DefaultKeyedVector<String8, wp<AssetManager::SharedZip> > AssetManager::SharedZip::gOpen; |
| 1771 | |
| 1772 | AssetManager::SharedZip::SharedZip(const String8& path, time_t modWhen) |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 1773 | : mPath(path), mZipFile(NULL), mModWhen(modWhen), |
| 1774 | mResourceTableAsset(NULL), mResourceTable(NULL) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1775 | { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1776 | //ALOGI("Creating SharedZip %p %s\n", this, (const char*)mPath); |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1777 | ALOGV("+++ opening zip '%s'\n", mPath.string()); |
Narayan Kamath | afd31e0 | 2013-12-03 13:16:03 +0000 | [diff] [blame] | 1778 | mZipFile = ZipFileRO::open(mPath.string()); |
| 1779 | if (mZipFile == NULL) { |
Steve Block | 5baa3a6 | 2011-12-20 16:23:08 +0000 | [diff] [blame] | 1780 | ALOGD("failed to open Zip archive '%s'\n", mPath.string()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1781 | } |
| 1782 | } |
| 1783 | |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 1784 | sp<AssetManager::SharedZip> AssetManager::SharedZip::get(const String8& path, |
| 1785 | bool createIfNotPresent) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1786 | { |
| 1787 | AutoMutex _l(gLock); |
| 1788 | time_t modWhen = getFileModDate(path); |
| 1789 | sp<SharedZip> zip = gOpen.valueFor(path).promote(); |
| 1790 | if (zip != NULL && zip->mModWhen == modWhen) { |
| 1791 | return zip; |
| 1792 | } |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 1793 | if (zip == NULL && !createIfNotPresent) { |
| 1794 | return NULL; |
| 1795 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1796 | zip = new SharedZip(path, modWhen); |
| 1797 | gOpen.add(path, zip); |
| 1798 | return zip; |
| 1799 | |
| 1800 | } |
| 1801 | |
| 1802 | ZipFileRO* AssetManager::SharedZip::getZip() |
| 1803 | { |
| 1804 | return mZipFile; |
| 1805 | } |
| 1806 | |
| 1807 | Asset* AssetManager::SharedZip::getResourceTableAsset() |
| 1808 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1809 | ALOGV("Getting from SharedZip %p resource asset %p\n", this, mResourceTableAsset); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1810 | return mResourceTableAsset; |
| 1811 | } |
| 1812 | |
| 1813 | Asset* AssetManager::SharedZip::setResourceTableAsset(Asset* asset) |
| 1814 | { |
| 1815 | { |
| 1816 | AutoMutex _l(gLock); |
| 1817 | if (mResourceTableAsset == NULL) { |
| 1818 | mResourceTableAsset = asset; |
| 1819 | // This is not thread safe the first time it is called, so |
| 1820 | // do it here with the global lock held. |
| 1821 | asset->getBuffer(true); |
| 1822 | return asset; |
| 1823 | } |
| 1824 | } |
| 1825 | delete asset; |
| 1826 | return mResourceTableAsset; |
| 1827 | } |
| 1828 | |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 1829 | ResTable* AssetManager::SharedZip::getResourceTable() |
| 1830 | { |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1831 | ALOGV("Getting from SharedZip %p resource table %p\n", this, mResourceTable); |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 1832 | return mResourceTable; |
| 1833 | } |
| 1834 | |
| 1835 | ResTable* AssetManager::SharedZip::setResourceTable(ResTable* res) |
| 1836 | { |
| 1837 | { |
| 1838 | AutoMutex _l(gLock); |
| 1839 | if (mResourceTable == NULL) { |
| 1840 | mResourceTable = res; |
| 1841 | return res; |
| 1842 | } |
| 1843 | } |
| 1844 | delete res; |
| 1845 | return mResourceTable; |
| 1846 | } |
| 1847 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1848 | bool AssetManager::SharedZip::isUpToDate() |
| 1849 | { |
| 1850 | time_t modWhen = getFileModDate(mPath.string()); |
| 1851 | return mModWhen == modWhen; |
| 1852 | } |
| 1853 | |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 1854 | void AssetManager::SharedZip::addOverlay(const asset_path& ap) |
| 1855 | { |
| 1856 | mOverlays.add(ap); |
| 1857 | } |
| 1858 | |
| 1859 | bool AssetManager::SharedZip::getOverlay(size_t idx, asset_path* out) const |
| 1860 | { |
| 1861 | if (idx >= mOverlays.size()) { |
| 1862 | return false; |
| 1863 | } |
| 1864 | *out = mOverlays[idx]; |
| 1865 | return true; |
| 1866 | } |
| 1867 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1868 | AssetManager::SharedZip::~SharedZip() |
| 1869 | { |
Steve Block | 6215d3f | 2012-01-04 20:05:49 +0000 | [diff] [blame] | 1870 | //ALOGI("Destroying SharedZip %p %s\n", this, (const char*)mPath); |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 1871 | if (mResourceTable != NULL) { |
| 1872 | delete mResourceTable; |
| 1873 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1874 | if (mResourceTableAsset != NULL) { |
| 1875 | delete mResourceTableAsset; |
| 1876 | } |
| 1877 | if (mZipFile != NULL) { |
| 1878 | delete mZipFile; |
Steve Block | 71f2cf1 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1879 | ALOGV("Closed '%s'\n", mPath.string()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1880 | } |
| 1881 | } |
| 1882 | |
| 1883 | /* |
| 1884 | * =========================================================================== |
| 1885 | * AssetManager::ZipSet |
| 1886 | * =========================================================================== |
| 1887 | */ |
| 1888 | |
| 1889 | /* |
| 1890 | * Constructor. |
| 1891 | */ |
| 1892 | AssetManager::ZipSet::ZipSet(void) |
| 1893 | { |
| 1894 | } |
| 1895 | |
| 1896 | /* |
| 1897 | * Destructor. Close any open archives. |
| 1898 | */ |
| 1899 | AssetManager::ZipSet::~ZipSet(void) |
| 1900 | { |
| 1901 | size_t N = mZipFile.size(); |
| 1902 | for (size_t i = 0; i < N; i++) |
| 1903 | closeZip(i); |
| 1904 | } |
| 1905 | |
| 1906 | /* |
| 1907 | * Close a Zip file and reset the entry. |
| 1908 | */ |
| 1909 | void AssetManager::ZipSet::closeZip(int idx) |
| 1910 | { |
| 1911 | mZipFile.editItemAt(idx) = NULL; |
| 1912 | } |
| 1913 | |
| 1914 | |
| 1915 | /* |
| 1916 | * Retrieve the appropriate Zip file from the set. |
| 1917 | */ |
| 1918 | ZipFileRO* AssetManager::ZipSet::getZip(const String8& path) |
| 1919 | { |
| 1920 | int idx = getIndex(path); |
| 1921 | sp<SharedZip> zip = mZipFile[idx]; |
| 1922 | if (zip == NULL) { |
| 1923 | zip = SharedZip::get(path); |
| 1924 | mZipFile.editItemAt(idx) = zip; |
| 1925 | } |
| 1926 | return zip->getZip(); |
| 1927 | } |
| 1928 | |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 1929 | Asset* AssetManager::ZipSet::getZipResourceTableAsset(const String8& path) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1930 | { |
| 1931 | int idx = getIndex(path); |
| 1932 | sp<SharedZip> zip = mZipFile[idx]; |
| 1933 | if (zip == NULL) { |
| 1934 | zip = SharedZip::get(path); |
| 1935 | mZipFile.editItemAt(idx) = zip; |
| 1936 | } |
| 1937 | return zip->getResourceTableAsset(); |
| 1938 | } |
| 1939 | |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 1940 | Asset* AssetManager::ZipSet::setZipResourceTableAsset(const String8& path, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1941 | Asset* asset) |
| 1942 | { |
| 1943 | int idx = getIndex(path); |
| 1944 | sp<SharedZip> zip = mZipFile[idx]; |
| 1945 | // doesn't make sense to call before previously accessing. |
| 1946 | return zip->setResourceTableAsset(asset); |
| 1947 | } |
| 1948 | |
Dianne Hackborn | 78c4051 | 2009-07-06 11:07:40 -0700 | [diff] [blame] | 1949 | ResTable* AssetManager::ZipSet::getZipResourceTable(const String8& path) |
| 1950 | { |
| 1951 | int idx = getIndex(path); |
| 1952 | sp<SharedZip> zip = mZipFile[idx]; |
| 1953 | if (zip == NULL) { |
| 1954 | zip = SharedZip::get(path); |
| 1955 | mZipFile.editItemAt(idx) = zip; |
| 1956 | } |
| 1957 | return zip->getResourceTable(); |
| 1958 | } |
| 1959 | |
| 1960 | ResTable* AssetManager::ZipSet::setZipResourceTable(const String8& path, |
| 1961 | ResTable* res) |
| 1962 | { |
| 1963 | int idx = getIndex(path); |
| 1964 | sp<SharedZip> zip = mZipFile[idx]; |
| 1965 | // doesn't make sense to call before previously accessing. |
| 1966 | return zip->setResourceTable(res); |
| 1967 | } |
| 1968 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1969 | /* |
| 1970 | * Generate the partial pathname for the specified archive. The caller |
| 1971 | * gets to prepend the asset root directory. |
| 1972 | * |
| 1973 | * Returns something like "common/en-US-noogle.jar". |
| 1974 | */ |
| 1975 | /*static*/ String8 AssetManager::ZipSet::getPathName(const char* zipPath) |
| 1976 | { |
| 1977 | return String8(zipPath); |
| 1978 | } |
| 1979 | |
| 1980 | bool AssetManager::ZipSet::isUpToDate() |
| 1981 | { |
| 1982 | const size_t N = mZipFile.size(); |
| 1983 | for (size_t i=0; i<N; i++) { |
| 1984 | if (mZipFile[i] != NULL && !mZipFile[i]->isUpToDate()) { |
| 1985 | return false; |
| 1986 | } |
| 1987 | } |
| 1988 | return true; |
| 1989 | } |
| 1990 | |
Mårten Kongstad | 48d2232 | 2014-01-31 14:43:27 +0100 | [diff] [blame] | 1991 | void AssetManager::ZipSet::addOverlay(const String8& path, const asset_path& overlay) |
| 1992 | { |
| 1993 | int idx = getIndex(path); |
| 1994 | sp<SharedZip> zip = mZipFile[idx]; |
| 1995 | zip->addOverlay(overlay); |
| 1996 | } |
| 1997 | |
| 1998 | bool AssetManager::ZipSet::getOverlay(const String8& path, size_t idx, asset_path* out) const |
| 1999 | { |
| 2000 | sp<SharedZip> zip = SharedZip::get(path, false); |
| 2001 | if (zip == NULL) { |
| 2002 | return false; |
| 2003 | } |
| 2004 | return zip->getOverlay(idx, out); |
| 2005 | } |
| 2006 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2007 | /* |
| 2008 | * Compute the zip file's index. |
| 2009 | * |
| 2010 | * "appName", "locale", and "vendor" should be set to NULL to indicate the |
| 2011 | * default directory. |
| 2012 | */ |
| 2013 | int AssetManager::ZipSet::getIndex(const String8& zip) const |
| 2014 | { |
| 2015 | const size_t N = mZipPath.size(); |
| 2016 | for (size_t i=0; i<N; i++) { |
| 2017 | if (mZipPath[i] == zip) { |
| 2018 | return i; |
| 2019 | } |
| 2020 | } |
| 2021 | |
| 2022 | mZipPath.add(zip); |
| 2023 | mZipFile.add(NULL); |
| 2024 | |
| 2025 | return mZipPath.size()-1; |
| 2026 | } |