2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * Access the contents of a Jar file.
19 * This isn't actually concerned with any of the Jar-like elements; it
20 * just wants a zip archive with "classes.dex" inside. In Android the
21 * most common example is ".apk".
24 #include "libdex/OptInvocation.h"
32 static const char* kDexInJarName = "classes.dex";
35 * Attempt to open a file whose name is similar to <fileName>,
36 * but with the supplied suffix. E.g.,
37 * openAlternateSuffix("Home.apk", "dex", O_RDONLY) will attempt
38 * to open "Home.dex". If the open succeeds, a pointer to a
39 * malloc()ed copy of the opened file name will be put in <*pCachedName>.
41 * <flags> is passed directly to open(). O_CREAT is not supported.
43 static int openAlternateSuffix(const char *fileName, const char *suffix,
44 int flags, char **pCachedName)
47 size_t fileNameLen = strlen(fileName);
48 size_t suffixLen = strlen(suffix);
49 size_t bufLen = fileNameLen + suffixLen + 1;
58 /* Copy the original filename into the buffer, find
59 * the last dot, and copy the suffix to just after it.
61 memcpy(buf, fileName, fileNameLen + 1);
62 c = strrchr(buf, '.');
67 memcpy(c + 1, suffix, suffixLen + 1);
69 fd = open(buf, flags);
74 LOGV("Couldn't open %s: %s\n", buf, strerror(errno));
81 * Checks the dependencies of the dex cache file corresponding
82 * to the jar file at the absolute path "fileName".
84 DexCacheStatus dvmDexCacheStatus(const char *fileName)
87 char* cachedName = NULL;
89 DexCacheStatus result = DEX_CACHE_ERROR;
92 /* Always treat elements of the bootclasspath as up-to-date.
93 * The fact that interpreted code is running at all means that this
96 if (dvmClassPathContains(gDvm.bootClassPath, fileName)) {
100 //TODO: match dvmJarFileOpen()'s logic. Not super-important
101 // (the odex-first logic is only necessary for dexpreopt)
102 // but it would be nice to be consistent.
104 /* Try to find the dex file inside of the archive.
106 if (dexZipOpenArchive(fileName, &archive) != 0) {
107 return DEX_CACHE_BAD_ARCHIVE;
109 entry = dexZipFindEntry(&archive, kDexInJarName);
111 bool newFile = false;
114 * See if there's an up-to-date copy of the optimized dex
115 * in the cache, but don't create one if there isn't.
117 LOGV("dvmDexCacheStatus: Checking cache for %s\n", fileName);
118 cachedName = dexOptGenerateCacheFileName(fileName, kDexInJarName);
119 if (cachedName == NULL)
122 fd = dvmOpenCachedDexFile(fileName, cachedName,
123 dexGetZipEntryModTime(&archive, entry),
124 dexGetZipEntryCrc32(&archive, entry),
125 /*isBootstrap=*/false, &newFile, /*createIfMissing=*/false);
126 LOGV("dvmOpenCachedDexFile returned fd %d\n", fd);
128 result = DEX_CACHE_STALE;
132 /* dvmOpenCachedDexFile locks the file as a side-effect.
133 * Unlock and close it.
135 if (!dvmUnlockCachedDexFile(fd)) {
136 /* uh oh -- this process needs to exit or we'll wedge the system */
137 LOGE("Unable to unlock DEX file\n");
141 /* When createIfMissing is false, dvmOpenCachedDexFile() only
142 * returns a valid fd if the cache file is up-to-date.
146 * There's no dex file in the jar file. See if there's an
147 * optimized dex file living alongside the jar.
149 fd = openAlternateSuffix(fileName, "odex", O_RDONLY, &cachedName);
151 LOGI("Zip is good, but no %s inside, and no .odex "
152 "file in the same directory\n", kDexInJarName);
153 result = DEX_CACHE_BAD_ARCHIVE;
157 LOGV("Using alternate file (odex) for %s ...\n", fileName);
158 if (!dvmCheckOptHeaderAndDependencies(fd, false, 0, 0, true, true)) {
159 LOGE("%s odex has stale dependencies\n", fileName);
160 LOGE("odex source not available -- failing\n");
161 result = DEX_CACHE_STALE_ODEX;
164 LOGV("%s odex has good dependencies\n", fileName);
167 result = DEX_CACHE_OK;
170 dexZipCloseArchive(&archive);
179 * Open a Jar file. It's okay if it's just a Zip archive without all of
180 * the Jar trimmings, but we do insist on finding "classes.dex" inside
181 * or an appropriately-named ".odex" file alongside.
183 * If "isBootstrap" is not set, the optimizer/verifier regards this DEX as
184 * being part of a different class loader.
186 int dvmJarFileOpen(const char* fileName, const char* odexOutputName,
187 JarFile** ppJarFile, bool isBootstrap)
190 DvmDex* pDvmDex = NULL;
191 char* cachedName = NULL;
192 bool archiveOpen = false;
197 /* Even if we're not going to look at the archive, we need to
198 * open it so we can stuff it into ppJarFile.
200 if (dexZipOpenArchive(fileName, &archive) != 0)
204 /* If we fork/exec into dexopt, don't let it inherit the archive's fd.
206 dvmSetCloseOnExec(dexZipGetArchiveFd(&archive));
208 /* First, look for a ".odex" alongside the jar file. It will
209 * have the same name/path except for the extension.
211 fd = openAlternateSuffix(fileName, "odex", O_RDONLY, &cachedName);
213 LOGV("Using alternate file (odex) for %s ...\n", fileName);
214 if (!dvmCheckOptHeaderAndDependencies(fd, false, 0, 0, true, true)) {
215 LOGE("%s odex has stale dependencies\n", fileName);
221 LOGV("%s odex has good dependencies\n", fileName);
222 //TODO: make sure that the .odex actually corresponds
223 // to the classes.dex inside the archive (if present).
224 // For typical use there will be no classes.dex.
231 * Pre-created .odex absent or stale. Look inside the jar for a
234 entry = dexZipFindEntry(&archive, kDexInJarName);
236 bool newFile = false;
239 * We've found the one we want. See if there's an up-to-date copy
242 * On return, "fd" will be seeked just past the "opt" header.
244 * If a stale .odex file is present and classes.dex exists in
245 * the archive, this will *not* return an fd pointing to the
246 * .odex file; the fd will point into dalvik-cache like any
249 if (odexOutputName == NULL) {
250 cachedName = dexOptGenerateCacheFileName(fileName,
252 if (cachedName == NULL)
255 cachedName = strdup(odexOutputName);
257 LOGV("dvmDexCacheStatus: Checking cache for %s (%s)\n",
258 fileName, cachedName);
259 fd = dvmOpenCachedDexFile(fileName, cachedName,
260 dexGetZipEntryModTime(&archive, entry),
261 dexGetZipEntryCrc32(&archive, entry),
262 isBootstrap, &newFile, /*createIfMissing=*/true);
264 LOGI("Unable to open or create cache for %s (%s)\n",
265 fileName, cachedName);
271 * If fd points to a new file (because there was no cached version,
272 * or the cached version was stale), generate the optimized DEX.
273 * The file descriptor returned is still locked, and is positioned
274 * just past the optimization header.
277 u8 startWhen, extractWhen, endWhen;
279 off_t dexOffset, fileLen;
281 dexOffset = lseek(fd, 0, SEEK_CUR);
282 result = (dexOffset > 0);
285 startWhen = dvmGetRelativeTimeUsec();
286 result = dexZipExtractEntryToFile(&archive, entry, fd);
287 extractWhen = dvmGetRelativeTimeUsec();
290 result = dvmOptimizeDexFile(fd, dexOffset,
291 dexGetZipEntryUncompLen(&archive, entry),
293 dexGetZipEntryModTime(&archive, entry),
294 dexGetZipEntryCrc32(&archive, entry),
299 LOGE("Unable to extract+optimize DEX from '%s'\n",
304 endWhen = dvmGetRelativeTimeUsec();
305 LOGD("DEX prep '%s': unzip in %dms, rewrite %dms\n",
307 (int) (extractWhen - startWhen) / 1000,
308 (int) (endWhen - extractWhen) / 1000);
311 LOGI("Zip is good, but no %s inside, and no valid .odex "
312 "file in the same directory\n", kDexInJarName);
318 * Map the cached version. This immediately rewinds the fd, so it
319 * doesn't have to be seeked anywhere in particular.
321 if (dvmDexFileOpenFromFd(fd, &pDvmDex) != 0) {
322 LOGI("Unable to map %s in %s\n", kDexInJarName, fileName);
328 if (!dvmUnlockCachedDexFile(fd)) {
329 /* uh oh -- this process needs to exit or we'll wedge the system */
330 LOGE("Unable to unlock DEX file\n");
336 LOGV("Successfully opened '%s' in '%s'\n", kDexInJarName, fileName);
338 *ppJarFile = (JarFile*) calloc(1, sizeof(JarFile));
339 (*ppJarFile)->archive = archive;
340 (*ppJarFile)->cacheFileName = cachedName;
341 (*ppJarFile)->pDvmDex = pDvmDex;
342 cachedName = NULL; // don't free it below
346 /* clean up, closing the open file */
347 if (archiveOpen && result != 0)
348 dexZipCloseArchive(&archive);
352 (void) dvmUnlockCachedDexFile(fd);
359 * Close a Jar file and free the struct.
361 void dvmJarFileFree(JarFile* pJarFile)
363 if (pJarFile == NULL)
366 dvmDexFileFree(pJarFile->pDvmDex);
367 dexZipCloseArchive(&pJarFile->archive);
368 free(pJarFile->cacheFileName);