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 * Utility functions for managing an invocation of "dexopt". 18 */ 19 #include "vm/DalvikVersion.h" 20 21 #include <stdint.h> 22 #include <stdlib.h> 23 #include <unistd.h> 24 #include <fcntl.h> 25 #include <string.h> 26 #include <sys/stat.h> 27 #include <sys/file.h> 28 #include <errno.h> 29 30 #include "OptInvocation.h" 31 #include "DexFile.h" 32 33 static const char* kClassesDex = "classes.dex"; 34 35 36 /* 37 * Given the filename of a .jar or .dex file, construct the DEX file cache 38 * name. 39 * 40 * For a Jar, "subFileName" is the name of the entry (usually "classes.dex"). 41 * For a DEX, it may be NULL. 42 * 43 * Returns a newly-allocated string, or NULL on failure. 44 */ 45 char* dexOptGenerateCacheFileName(const char* fileName, const char* subFileName) 46 { 47 char nameBuf[512]; 48 static const char kDexCachePath[] = "dalvik-cache"; 49 char absoluteFile[sizeof(nameBuf)]; 50 const size_t kBufLen = sizeof(nameBuf) - 1; 51 const char* dataRoot; 52 char* cp; 53 54 /* 55 * Get the absolute path of the Jar or DEX file. 56 */ 57 absoluteFile[0] = '\0'; 58 if (fileName[0] != '/') { 59 /* 60 * Generate the absolute path. This doesn't do everything it 61 * should, e.g. if filename is "./out/whatever" it doesn't crunch 62 * the leading "./" out, but it'll do. 63 */ 64 if (getcwd(absoluteFile, kBufLen) == NULL) { 65 LOGE("Can't get CWD while opening jar file\n"); 66 return NULL; 67 } 68 strncat(absoluteFile, "/", kBufLen); 69 } 70 strncat(absoluteFile, fileName, kBufLen); 71 72 /* 73 * Append the name of the Jar file entry, if any. This is not currently 74 * required, but will be if we start putting more than one DEX file 75 * in a Jar. 76 */ 77 if (subFileName != NULL) { 78 strncat(absoluteFile, "/", kBufLen); 79 strncat(absoluteFile, subFileName, kBufLen); 80 } 81 82 /* Turn the path into a flat filename by replacing 83 * any slashes after the first one with '@' characters. 84 */ 85 cp = absoluteFile + 1; 86 while (*cp != '\0') { 87 if (*cp == '/') { 88 *cp = '@'; 89 } 90 cp++; 91 } 92 93 /* Build the name of the cache directory. 94 */ 95 dataRoot = getenv("ANDROID_DATA"); 96 if (dataRoot == NULL) 97 dataRoot = "/data"; 98 snprintf(nameBuf, kBufLen, "%s/%s", dataRoot, kDexCachePath); 99 100 /* Tack on the file name for the actual cache file path. 101 */ 102 strncat(nameBuf, absoluteFile, kBufLen); 103 104 LOGV("Cache file for '%s' '%s' is '%s'\n", fileName, subFileName, nameBuf); 105 return strdup(nameBuf); 106 } 107 108 /* 109 * Create a skeletal "opt" header in a new file. Most of the fields are 110 * initialized to garbage, but we fill in "dexOffset" so others can 111 * see how large the header is. 112 * 113 * "fd" must be positioned at the start of the file. On return, it will 114 * be positioned just past the header, and the place where the DEX data 115 * should go. 116 * 117 * Returns 0 on success, errno on failure. 118 */ 119 int dexOptCreateEmptyHeader(int fd) 120 { 121 DexOptHeader optHdr; 122 ssize_t actual; 123 124 assert(lseek(fd, 0, SEEK_CUR) == 0); 125 126 /* 127 * The data is only expected to be readable on the current system, so 128 * we just write the structure. We do need the file offset to be 64-bit 129 * aligned to fulfill a DEX requirement. 130 */ 131 assert((sizeof(optHdr) & 0x07) == 0); 132 memset(&optHdr, 0xff, sizeof(optHdr)); 133 optHdr.dexOffset = sizeof(optHdr); 134 actual = write(fd, &optHdr, sizeof(optHdr)); 135 if (actual != sizeof(optHdr)) { 136 int err = errno ? errno : -1; 137 LOGE("opt header write failed: %s", strerror(errno)); 138 return errno; 139 } 140 141 return 0; 142 } 143