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"
33 * Extract "classes.dex" from archive file.
35 * If "quiet" is set, don't report common errors.
37 UnzipToFileResult dexUnzipToFile(const char* zipFileName,
38 const char* outFileName, bool quiet)
40 UnzipToFileResult result = kUTFRSuccess;
41 static const char* kFileToExtract = "classes.dex";
44 bool unlinkOnFailure = false;
47 if (dexZipOpenArchive(zipFileName, &archive) != 0) {
49 fprintf(stderr, "Unable to open '%s' as zip archive\n",
56 fd = open(outFileName, O_WRONLY | O_CREAT | O_EXCL, 0600);
58 fprintf(stderr, "Unable to create output file '%s': %s\n",
59 outFileName, strerror(errno));
60 result = kUTFROutputFileProblem;
64 unlinkOnFailure = true;
66 entry = dexZipFindEntry(&archive, kFileToExtract);
69 fprintf(stderr, "Unable to find '%s' in '%s'\n",
70 kFileToExtract, zipFileName);
72 result = kUTFRNoClassesDex;
76 if (dexZipExtractEntryToFile(&archive, entry, fd) != 0) {
77 fprintf(stderr, "Extract of '%s' from '%s' failed\n",
78 kFileToExtract, zipFileName);
86 if (unlinkOnFailure && result != kUTFRSuccess)
88 dexZipCloseArchive(&archive);
93 * Map the specified DEX file read-only (possibly after expanding it into a
94 * temp file from a Jar). Pass in a MemMapping struct to hold the info.
95 * If the file is an unoptimized DEX file, then byte-swapping and structural
96 * verification are performed on it before the memory is made read-only.
98 * The temp file is deleted after the map succeeds.
100 * This is intended for use by tools (e.g. dexdump) that need to get a
101 * read-only copy of a DEX file that could be in a number of different states.
103 * If "tempFileName" is NULL, a default value is used. The temp file is
104 * deleted after the map succeeds.
106 * If "quiet" is set, don't report common errors.
108 * Returns 0 (kUTFRSuccess) on success.
110 UnzipToFileResult dexOpenAndMap(const char* fileName, const char* tempFileName,
111 MemMapping* pMap, bool quiet)
113 UnzipToFileResult result = kUTFRGenericFailure;
114 int len = strlen(fileName);
115 char tempNameBuf[32];
116 bool removeTemp = false;
122 "ERROR: filename must end in .dex, .zip, .jar, or .apk\n");
124 result = kUTFRBadArgs;
128 if (strcasecmp(fileName + len -3, "dex") != 0) {
129 if (tempFileName == NULL) {
131 * Try .zip/.jar/.apk, all of which are Zip archives with
132 * "classes.dex" inside. We need to extract the compressed
133 * data to a temp file, the location of which varies.
135 * On the device we must use /sdcard because most other
136 * directories aren't writable (either because of permissions
137 * or because the volume is mounted read-only). On desktop
138 * it's nice to use the designated temp directory.
140 if (access("/tmp", W_OK) == 0) {
141 sprintf(tempNameBuf, "/tmp/dex-temp-%d", getpid());
142 } else if (access("/sdcard", W_OK) == 0) {
143 sprintf(tempNameBuf, "/sdcard/dex-temp-%d", getpid());
146 "NOTE: /tmp and /sdcard unavailable for temp files\n");
147 sprintf(tempNameBuf, "dex-temp-%d", getpid());
150 tempFileName = tempNameBuf;
153 result = dexUnzipToFile(fileName, tempFileName, quiet);
155 if (result == kUTFRSuccess) {
156 //printf("+++ Good unzip to '%s'\n", tempFileName);
157 fileName = tempFileName;
159 } else if (result == kUTFRNotZip) {
161 fprintf(stderr, "Not Zip, retrying as DEX\n");
164 if (!quiet && result == kUTFRNoClassesDex) {
165 fprintf(stderr, "Zip has no classes.dex\n");
171 result = kUTFRGenericFailure;
174 * Pop open the (presumed) DEX file.
176 fd = open(fileName, O_RDONLY | O_BINARY);
179 fprintf(stderr, "ERROR: unable to open '%s': %s\n",
180 fileName, strerror(errno));
185 if (sysMapFileInShmemWritableReadOnly(fd, pMap) != 0) {
186 fprintf(stderr, "ERROR: Unable to map '%s'\n", fileName);
191 * This call will fail if the file exists on a filesystem that
192 * doesn't support mprotect(). If that's the case, then the file
193 * will have already been mapped private-writable by the previous
194 * call, so we don't need to do anything special if this call
197 sysChangeMapAccess(pMap->addr, pMap->length, true, pMap);
199 if (dexSwapAndVerifyIfNecessary(pMap->addr, pMap->length)) {
200 fprintf(stderr, "ERROR: Failed structural verification of '%s'\n",
206 * Similar to above, this call will fail if the file wasn't ever
207 * read-only to begin with. This is innocuous, though it is
208 * undesirable from a memory hygiene perspective.
210 sysChangeMapAccess(pMap->addr, pMap->length, false, pMap);
213 * Success! Close the file and return with the start/length in pMap.
221 /* this will fail if the OS doesn't allow removal of a mapped file */
222 if (unlink(tempFileName) != 0) {
223 fprintf(stderr, "WARNING: unable to remove temp '%s'\n",