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 * Some utility functions for use with command-line utilities.
20 #include "ZipArchive.h"
34 * Extract "classes.dex" from archive file.
36 * If "quiet" is set, don't report common errors.
38 UnzipToFileResult dexUnzipToFile(const char* zipFileName,
39 const char* outFileName, bool quiet)
41 UnzipToFileResult result = kUTFRSuccess;
42 static const char* kFileToExtract = "classes.dex";
45 bool unlinkOnFailure = false;
48 if (dexZipOpenArchive(zipFileName, &archive) != 0) {
50 fprintf(stderr, "Unable to open '%s' as zip archive\n",
57 fd = open(outFileName, O_WRONLY | O_CREAT | O_EXCL, 0600);
59 fprintf(stderr, "Unable to create output file '%s': %s\n",
60 outFileName, strerror(errno));
61 result = kUTFROutputFileProblem;
65 unlinkOnFailure = true;
67 entry = dexZipFindEntry(&archive, kFileToExtract);
70 fprintf(stderr, "Unable to find '%s' in '%s'\n",
71 kFileToExtract, zipFileName);
73 result = kUTFRNoClassesDex;
77 if (dexZipExtractEntryToFile(&archive, entry, fd) != 0) {
78 fprintf(stderr, "Extract of '%s' from '%s' failed\n",
79 kFileToExtract, zipFileName);
87 if (unlinkOnFailure && result != kUTFRSuccess)
89 dexZipCloseArchive(&archive);
94 * Map the specified DEX file read-only (possibly after expanding it into a
95 * temp file from a Jar). Pass in a MemMapping struct to hold the info.
96 * If the file is an unoptimized DEX file, then byte-swapping and structural
97 * verification are performed on it before the memory is made read-only.
99 * The temp file is deleted after the map succeeds.
101 * This is intended for use by tools (e.g. dexdump) that need to get a
102 * read-only copy of a DEX file that could be in a number of different states.
104 * If "tempFileName" is NULL, a default value is used. The temp file is
105 * deleted after the map succeeds.
107 * If "quiet" is set, don't report common errors.
109 * Returns 0 (kUTFRSuccess) on success.
111 UnzipToFileResult dexOpenAndMap(const char* fileName, const char* tempFileName,
112 MemMapping* pMap, bool quiet)
114 UnzipToFileResult result = kUTFRGenericFailure;
115 int len = strlen(fileName);
116 char tempNameBuf[32];
117 bool removeTemp = false;
123 "ERROR: filename must end in .dex, .zip, .jar, or .apk\n");
125 result = kUTFRBadArgs;
129 if (strcasecmp(fileName + len -3, "dex") != 0) {
130 if (tempFileName == NULL) {
132 * Try .zip/.jar/.apk, all of which are Zip archives with
133 * "classes.dex" inside. We need to extract the compressed
134 * data to a temp file, the location of which varies.
136 * On the device we must use /sdcard because most other
137 * directories aren't writable (either because of permissions
138 * or because the volume is mounted read-only). On desktop
139 * it's nice to use the designated temp directory.
141 if (access("/tmp", W_OK) == 0) {
142 sprintf(tempNameBuf, "/tmp/dex-temp-%d", getpid());
143 } else if (access("/sdcard", W_OK) == 0) {
144 sprintf(tempNameBuf, "/sdcard/dex-temp-%d", getpid());
147 "NOTE: /tmp and /sdcard unavailable for temp files\n");
148 sprintf(tempNameBuf, "dex-temp-%d", getpid());
151 tempFileName = tempNameBuf;
154 result = dexUnzipToFile(fileName, tempFileName, quiet);
156 if (result == kUTFRSuccess) {
157 //printf("+++ Good unzip to '%s'\n", tempFileName);
158 fileName = tempFileName;
160 } else if (result == kUTFRNotZip) {
162 fprintf(stderr, "Not Zip, retrying as DEX\n");
165 if (!quiet && result == kUTFRNoClassesDex) {
166 fprintf(stderr, "Zip has no classes.dex\n");
172 result = kUTFRGenericFailure;
175 * Pop open the (presumed) DEX file.
177 fd = open(fileName, O_RDONLY | O_BINARY);
180 fprintf(stderr, "ERROR: unable to open '%s': %s\n",
181 fileName, strerror(errno));
186 if (sysMapFileInShmemWritableReadOnly(fd, pMap) != 0) {
187 fprintf(stderr, "ERROR: Unable to map '%s'\n", fileName);
192 * This call will fail if the file exists on a filesystem that
193 * doesn't support mprotect(). If that's the case, then the file
194 * will have already been mapped private-writable by the previous
195 * call, so we don't need to do anything special if this call
198 sysChangeMapAccess(pMap->addr, pMap->length, true, pMap);
200 if (dexSwapAndVerifyIfNecessary((u1*) pMap->addr, pMap->length)) {
201 fprintf(stderr, "ERROR: Failed structural verification of '%s'\n",
207 * Similar to above, this call will fail if the file wasn't ever
208 * read-only to begin with. This is innocuous, though it is
209 * undesirable from a memory hygiene perspective.
211 sysChangeMapAccess(pMap->addr, pMap->length, false, pMap);
214 * Success! Close the file and return with the start/length in pMap.
216 result = kUTFRSuccess;
222 /* this will fail if the OS doesn't allow removal of a mapped file */
223 if (unlink(tempFileName) != 0) {
224 fprintf(stderr, "WARNING: unable to remove temp '%s'\n",