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