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"
30 * Extract "classes.dex" from archive file.
32 * If "quiet" is set, don't report common errors.
34 UnzipToFileResult dexUnzipToFile(const char* zipFileName,
35 const char* outFileName, bool quiet)
37 UnzipToFileResult result = kUTFRSuccess;
38 static const char* kFileToExtract = "classes.dex";
41 bool unlinkOnFailure = false;
44 if (dexZipOpenArchive(zipFileName, &archive) != 0) {
46 fprintf(stderr, "Unable to open '%s' as zip archive\n",
53 fd = open(outFileName, O_WRONLY | O_CREAT | O_EXCL, 0600);
55 fprintf(stderr, "Unable to create output file '%s': %s\n",
56 outFileName, strerror(errno));
57 result = kUTFROutputFileProblem;
61 unlinkOnFailure = true;
63 entry = dexZipFindEntry(&archive, kFileToExtract);
66 fprintf(stderr, "Unable to find '%s' in '%s'\n",
67 kFileToExtract, zipFileName);
69 result = kUTFRNoClassesDex;
73 if (!dexZipExtractEntryToFile(&archive, entry, fd)) {
74 fprintf(stderr, "Extract of '%s' from '%s' failed\n",
75 kFileToExtract, zipFileName);
83 if (unlinkOnFailure && result != kUTFRSuccess)
85 dexZipCloseArchive(&archive);
90 * Map the specified DEX file read-only (possibly after expanding it into a
91 * temp file from a Jar). Pass in a MemMapping struct to hold the info.
93 * The temp file is deleted after the map succeeds.
95 * This is intended for use by tools (e.g. dexdump) that need to get a
96 * read-only copy of a DEX file that could be in a number of different states.
98 * If "quiet" is set, don't report common errors.
100 * Returns 0 (kUTFRSuccess) on success.
102 UnzipToFileResult dexOpenAndMap(const char* fileName, const char* tempFileName,
103 MemMapping* pMap, bool quiet)
105 UnzipToFileResult result = kUTFRGenericFailure;
106 int len = strlen(fileName);
107 char tempNameBuf[32];
108 bool removeTemp = false;
114 "ERROR: filename must end in .dex, .zip, .jar, or .apk\n");
116 result = kUTFRBadArgs;
120 if (strcasecmp(fileName + len -3, "dex") != 0) {
121 if (tempFileName == NULL) {
123 * Try .zip/.jar/.apk, all of which are Zip archives with
124 * "classes.dex" inside. We need to extract the compressed
125 * data to a temp file, the location of which varies.
127 if (access("/tmp", W_OK) == 0)
128 sprintf(tempNameBuf, "/tmp/dex-temp-%d", getpid());
130 sprintf(tempNameBuf, "/sdcard/dex-temp-%d", getpid());
132 tempFileName = tempNameBuf;
135 result = dexUnzipToFile(fileName, tempFileName, quiet);
137 if (result == kUTFRSuccess) {
138 //printf("+++ Good unzip to '%s'\n", tempFileName);
139 fileName = tempFileName;
141 } else if (result == kUTFRNotZip) {
143 fprintf(stderr, "Not Zip, retrying as DEX\n");
146 if (!quiet && result == kUTFRNoClassesDex) {
147 fprintf(stderr, "Zip has no classes.dex\n");
154 * Pop open the (presumed) DEX file.
156 fd = open(fileName, O_RDONLY);
159 fprintf(stderr, "ERROR: unable to open '%s': %s\n",
160 fileName, strerror(errno));
165 if (sysMapFileInShmem(fd, pMap) != 0) {
166 fprintf(stderr, "ERROR: Unable to map %s\n", fileName);
172 * Success! Close the file and return with the start/length in pMap.
180 if (unlink(tempFileName) != 0) {
181 fprintf(stderr, "Warning: unable to remove temp '%s'\n",