OSDN Git Service

Merge "Shift register index tests to static pass." into dalvik-dev
[android-x86/dalvik.git] / libdex / CmdUtils.c
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /*
17  * Some utility functions for use with command-line utilities.
18  */
19 #include "DexFile.h"
20 #include "ZipArchive.h"
21 #include "CmdUtils.h"
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <fcntl.h>
26 #include <errno.h>
27
28
29 /*
30  * Extract "classes.dex" from archive file.
31  *
32  * If "quiet" is set, don't report common errors.
33  */
34 UnzipToFileResult dexUnzipToFile(const char* zipFileName,
35     const char* outFileName, bool quiet)
36 {
37     UnzipToFileResult result = kUTFRSuccess;
38     static const char* kFileToExtract = "classes.dex";
39     ZipArchive archive;
40     ZipEntry entry;
41     bool unlinkOnFailure = false;
42     int fd = -1;
43
44     if (dexZipOpenArchive(zipFileName, &archive) != 0) {
45         if (!quiet) {
46             fprintf(stderr, "Unable to open '%s' as zip archive\n",
47                 zipFileName);
48         }
49         result = kUTFRNotZip;
50         goto bail;
51     }
52
53     fd = open(outFileName, O_WRONLY | O_CREAT | O_EXCL, 0600);
54     if (fd < 0) {
55         fprintf(stderr, "Unable to create output file '%s': %s\n",
56             outFileName, strerror(errno));
57         result = kUTFROutputFileProblem;
58         goto bail;
59     }
60
61     unlinkOnFailure = true;
62
63     entry = dexZipFindEntry(&archive, kFileToExtract);
64     if (entry == NULL) {
65         if (!quiet) {
66             fprintf(stderr, "Unable to find '%s' in '%s'\n",
67                 kFileToExtract, zipFileName);
68         }
69         result = kUTFRNoClassesDex;
70         goto bail;
71     }
72
73     if (dexZipExtractEntryToFile(&archive, entry, fd) != 0) {
74         fprintf(stderr, "Extract of '%s' from '%s' failed\n",
75             kFileToExtract, zipFileName);
76         result = kUTFRBadZip;
77         goto bail;
78     }
79
80 bail:
81     if (fd >= 0)
82         close(fd);
83     if (unlinkOnFailure && result != kUTFRSuccess)
84         unlink(outFileName);
85     dexZipCloseArchive(&archive);
86     return result;
87 }
88
89 /*
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.
92  * If the file is an unoptimized DEX file, then byte-swapping and structural
93  * verification are performed on it before the memory is made read-only.
94  *
95  * The temp file is deleted after the map succeeds.
96  *
97  * This is intended for use by tools (e.g. dexdump) that need to get a
98  * read-only copy of a DEX file that could be in a number of different states.
99  *
100  * If "tempFileName" is NULL, a default value is used.  The temp file is
101  * deleted after the map succeeds.
102  *
103  * If "quiet" is set, don't report common errors.
104  *
105  * Returns 0 (kUTFRSuccess) on success.
106  */
107 UnzipToFileResult dexOpenAndMap(const char* fileName, const char* tempFileName,
108     MemMapping* pMap, bool quiet)
109 {
110     UnzipToFileResult result = kUTFRGenericFailure;
111     int len = strlen(fileName);
112     char tempNameBuf[32];
113     bool removeTemp = false;
114     int fd = -1;
115
116     if (len < 5) {
117         if (!quiet) {
118             fprintf(stderr,
119                 "ERROR: filename must end in .dex, .zip, .jar, or .apk\n");
120         }
121         result = kUTFRBadArgs;
122         goto bail;
123     }
124
125     if (strcasecmp(fileName + len -3, "dex") != 0) {
126         if (tempFileName == NULL) {
127             /*
128              * Try .zip/.jar/.apk, all of which are Zip archives with
129              * "classes.dex" inside.  We need to extract the compressed
130              * data to a temp file, the location of which varies.
131              *
132              * On the device we must use /sdcard because most other
133              * directories aren't writable (either because of permissions
134              * or because the volume is mounted read-only).  On desktop
135              * it's nice to use the designated temp directory.
136              */
137             if (access("/tmp", W_OK) == 0) {
138                 sprintf(tempNameBuf, "/tmp/dex-temp-%d", getpid());
139             } else if (access("/sdcard", W_OK) == 0) {
140                 sprintf(tempNameBuf, "/sdcard/dex-temp-%d", getpid());
141             } else {
142                 fprintf(stderr,
143                     "NOTE: /tmp and /sdcard unavailable for temp files\n");
144                 sprintf(tempNameBuf, "dex-temp-%d", getpid());
145             }
146
147             tempFileName = tempNameBuf;
148         }
149
150         result = dexUnzipToFile(fileName, tempFileName, quiet);
151
152         if (result == kUTFRSuccess) {
153             //printf("+++ Good unzip to '%s'\n", tempFileName);
154             fileName = tempFileName;
155             removeTemp = true;
156         } else if (result == kUTFRNotZip) {
157             if (!quiet) {
158                 fprintf(stderr, "Not Zip, retrying as DEX\n");
159             }
160         } else {
161             if (!quiet && result == kUTFRNoClassesDex) {
162                 fprintf(stderr, "Zip has no classes.dex\n");
163             }
164             goto bail;
165         }
166     }
167
168     result = kUTFRGenericFailure;
169
170     /*
171      * Pop open the (presumed) DEX file.
172      */
173     fd = open(fileName, O_RDONLY);
174     if (fd < 0) {
175         if (!quiet) {
176             fprintf(stderr, "ERROR: unable to open '%s': %s\n",
177                 fileName, strerror(errno));
178         }
179         goto bail;
180     }
181
182     if (sysMapFileInShmemWritableReadOnly(fd, pMap) != 0) {
183         fprintf(stderr, "ERROR: Unable to map '%s'\n", fileName);
184         goto bail;
185     }
186
187     /*
188      * This call will fail if the file exists on a filesystem that
189      * doesn't support mprotect(). If that's the case, then the file
190      * will have already been mapped private-writable by the previous
191      * call, so we don't need to do anything special if this call
192      * returns non-zero.
193      */
194     sysChangeMapAccess(pMap->addr, pMap->length, true, pMap);
195
196     if (dexSwapAndVerifyIfNecessary(pMap->addr, pMap->length)) {
197         fprintf(stderr, "ERROR: Failed structural verification of '%s'\n",
198             fileName);
199         goto bail;
200     }
201
202     /*
203      * Similar to above, this call will fail if the file wasn't ever
204      * read-only to begin with. This is innocuous, though it is
205      * undesirable from a memory hygiene perspective.
206      */
207     sysChangeMapAccess(pMap->addr, pMap->length, false, pMap);
208
209     /*
210      * Success!  Close the file and return with the start/length in pMap.
211      */
212     result = 0;
213
214 bail:
215     if (fd >= 0)
216         close(fd);
217     if (removeTemp) {
218         /* this will fail if the OS doesn't allow removal of a mapped file */
219         if (unlink(tempFileName) != 0) {
220             fprintf(stderr, "WARNING: unable to remove temp '%s'\n",
221                 tempFileName);
222         }
223     }
224     return result;
225 }