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.
18 * Access the contents of a Jar file.
20 * This isn't actually concerned with any of the Jar-like elements; it
21 * just wants a zip archive with "classes.dex" inside. In Android the
22 * most common example is ".apk".
26 #include "libdex/OptInvocation.h"
34 static const char* kDexInJarName = "classes.dex";
37 * Attempt to open a file whose name is similar to <fileName>,
38 * but with the supplied suffix. E.g.,
39 * openAlternateSuffix("Home.apk", "dex", O_RDONLY) will attempt
40 * to open "Home.dex". If the open succeeds, a pointer to a
41 * malloc()ed copy of the opened file name will be put in <*pCachedName>.
43 * <flags> is passed directly to open(). O_CREAT is not supported.
45 static int openAlternateSuffix(const char *fileName, const char *suffix,
46 int flags, char **pCachedName)
49 size_t fileNameLen = strlen(fileName);
50 size_t suffixLen = strlen(suffix);
51 size_t bufLen = fileNameLen + suffixLen + 1;
54 buf = (char*)malloc(bufLen);
60 /* Copy the original filename into the buffer, find
61 * the last dot, and copy the suffix to just after it.
63 memcpy(buf, fileName, fileNameLen + 1);
64 c = strrchr(buf, '.');
69 memcpy(c + 1, suffix, suffixLen + 1);
71 fd = open(buf, flags);
76 LOGV("Couldn't open %s: %s\n", buf, strerror(errno));
83 * Checks the dependencies of the dex cache file corresponding
84 * to the jar file at the absolute path "fileName".
86 DexCacheStatus dvmDexCacheStatus(const char *fileName)
89 char* cachedName = NULL;
91 DexCacheStatus result = DEX_CACHE_ERROR;
94 /* Always treat elements of the bootclasspath as up-to-date.
95 * The fact that interpreted code is running at all means that this
98 if (dvmClassPathContains(gDvm.bootClassPath, fileName)) {
102 //TODO: match dvmJarFileOpen()'s logic. Not super-important
103 // (the odex-first logic is only necessary for dexpreopt)
104 // but it would be nice to be consistent.
106 /* Try to find the dex file inside of the archive.
108 if (dexZipOpenArchive(fileName, &archive) != 0) {
109 return DEX_CACHE_BAD_ARCHIVE;
111 entry = dexZipFindEntry(&archive, kDexInJarName);
113 bool newFile = false;
116 * See if there's an up-to-date copy of the optimized dex
117 * in the cache, but don't create one if there isn't.
119 LOGV("dvmDexCacheStatus: Checking cache for %s\n", fileName);
120 cachedName = dexOptGenerateCacheFileName(fileName, kDexInJarName);
121 if (cachedName == NULL)
122 return DEX_CACHE_BAD_ARCHIVE;
124 fd = dvmOpenCachedDexFile(fileName, cachedName,
125 dexGetZipEntryModTime(&archive, entry),
126 dexGetZipEntryCrc32(&archive, entry),
127 /*isBootstrap=*/false, &newFile, /*createIfMissing=*/false);
128 LOGV("dvmOpenCachedDexFile returned fd %d\n", fd);
130 result = DEX_CACHE_STALE;
134 /* dvmOpenCachedDexFile locks the file as a side-effect.
135 * Unlock and close it.
137 if (!dvmUnlockCachedDexFile(fd)) {
138 /* uh oh -- this process needs to exit or we'll wedge the system */
139 LOGE("Unable to unlock DEX file\n");
143 /* When createIfMissing is false, dvmOpenCachedDexFile() only
144 * returns a valid fd if the cache file is up-to-date.
148 * There's no dex file in the jar file. See if there's an
149 * optimized dex file living alongside the jar.
151 fd = openAlternateSuffix(fileName, "odex", O_RDONLY, &cachedName);
153 LOGI("Zip is good, but no %s inside, and no .odex "
154 "file in the same directory\n", kDexInJarName);
155 result = DEX_CACHE_BAD_ARCHIVE;
159 LOGV("Using alternate file (odex) for %s ...\n", fileName);
160 if (!dvmCheckOptHeaderAndDependencies(fd, false, 0, 0, true, true)) {
161 LOGE("%s odex has stale dependencies\n", fileName);
162 LOGE("odex source not available -- failing\n");
163 result = DEX_CACHE_STALE_ODEX;
166 LOGV("%s odex has good dependencies\n", fileName);
169 result = DEX_CACHE_OK;
172 dexZipCloseArchive(&archive);
181 * Open a Jar file. It's okay if it's just a Zip archive without all of
182 * the Jar trimmings, but we do insist on finding "classes.dex" inside
183 * or an appropriately-named ".odex" file alongside.
185 * If "isBootstrap" is not set, the optimizer/verifier regards this DEX as
186 * being part of a different class loader.
188 int dvmJarFileOpen(const char* fileName, const char* odexOutputName,
189 JarFile** ppJarFile, bool isBootstrap)
192 * TODO: This function has been duplicated and modified to become
193 * dvmRawDexFileOpen() in RawDexFile.c. This should be refactored.
197 DvmDex* pDvmDex = NULL;
198 char* cachedName = NULL;
199 bool archiveOpen = false;
204 /* Even if we're not going to look at the archive, we need to
205 * open it so we can stuff it into ppJarFile.
207 if (dexZipOpenArchive(fileName, &archive) != 0)
211 /* If we fork/exec into dexopt, don't let it inherit the archive's fd.
213 dvmSetCloseOnExec(dexZipGetArchiveFd(&archive));
215 /* First, look for a ".odex" alongside the jar file. It will
216 * have the same name/path except for the extension.
218 fd = openAlternateSuffix(fileName, "odex", O_RDONLY, &cachedName);
220 LOGV("Using alternate file (odex) for %s ...\n", fileName);
221 if (!dvmCheckOptHeaderAndDependencies(fd, false, 0, 0, true, true)) {
222 LOGE("%s odex has stale dependencies\n", fileName);
229 LOGV("%s odex has good dependencies\n", fileName);
230 //TODO: make sure that the .odex actually corresponds
231 // to the classes.dex inside the archive (if present).
232 // For typical use there will be no classes.dex.
239 * Pre-created .odex absent or stale. Look inside the jar for a
242 entry = dexZipFindEntry(&archive, kDexInJarName);
244 bool newFile = false;
247 * We've found the one we want. See if there's an up-to-date copy
250 * On return, "fd" will be seeked just past the "opt" header.
252 * If a stale .odex file is present and classes.dex exists in
253 * the archive, this will *not* return an fd pointing to the
254 * .odex file; the fd will point into dalvik-cache like any
257 if (odexOutputName == NULL) {
258 cachedName = dexOptGenerateCacheFileName(fileName,
260 if (cachedName == NULL)
263 cachedName = strdup(odexOutputName);
265 LOGV("dvmJarFileOpen: Checking cache for %s (%s)\n",
266 fileName, cachedName);
267 fd = dvmOpenCachedDexFile(fileName, cachedName,
268 dexGetZipEntryModTime(&archive, entry),
269 dexGetZipEntryCrc32(&archive, entry),
270 isBootstrap, &newFile, /*createIfMissing=*/true);
272 LOGI("Unable to open or create cache for %s (%s)\n",
273 fileName, cachedName);
279 * If fd points to a new file (because there was no cached version,
280 * or the cached version was stale), generate the optimized DEX.
281 * The file descriptor returned is still locked, and is positioned
282 * just past the optimization header.
285 u8 startWhen, extractWhen, endWhen;
289 dexOffset = lseek(fd, 0, SEEK_CUR);
290 result = (dexOffset > 0);
293 startWhen = dvmGetRelativeTimeUsec();
294 result = dexZipExtractEntryToFile(&archive, entry, fd) == 0;
295 extractWhen = dvmGetRelativeTimeUsec();
298 result = dvmOptimizeDexFile(fd, dexOffset,
299 dexGetZipEntryUncompLen(&archive, entry),
301 dexGetZipEntryModTime(&archive, entry),
302 dexGetZipEntryCrc32(&archive, entry),
307 LOGE("Unable to extract+optimize DEX from '%s'\n",
312 endWhen = dvmGetRelativeTimeUsec();
313 LOGD("DEX prep '%s': unzip in %dms, rewrite %dms\n",
315 (int) (extractWhen - startWhen) / 1000,
316 (int) (endWhen - extractWhen) / 1000);
319 LOGI("Zip is good, but no %s inside, and no valid .odex "
320 "file in the same directory\n", kDexInJarName);
326 * Map the cached version. This immediately rewinds the fd, so it
327 * doesn't have to be seeked anywhere in particular.
329 if (dvmDexFileOpenFromFd(fd, &pDvmDex) != 0) {
330 LOGI("Unable to map %s in %s\n", kDexInJarName, fileName);
336 if (!dvmUnlockCachedDexFile(fd)) {
337 /* uh oh -- this process needs to exit or we'll wedge the system */
338 LOGE("Unable to unlock DEX file\n");
344 LOGV("Successfully opened '%s' in '%s'\n", kDexInJarName, fileName);
346 *ppJarFile = (JarFile*) calloc(1, sizeof(JarFile));
347 (*ppJarFile)->archive = archive;
348 (*ppJarFile)->cacheFileName = cachedName;
349 (*ppJarFile)->pDvmDex = pDvmDex;
350 cachedName = NULL; // don't free it below
354 /* clean up, closing the open file */
355 if (archiveOpen && result != 0)
356 dexZipCloseArchive(&archive);
360 (void) dvmUnlockCachedDexFile(fd);
367 * Close a Jar file and free the struct.
369 void dvmJarFileFree(JarFile* pJarFile)
371 if (pJarFile == NULL)
374 dvmDexFileFree(pJarFile->pDvmDex);
375 dexZipCloseArchive(&pJarFile->archive);
376 free(pJarFile->cacheFileName);