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 * Utility functions for managing an invocation of "dexopt".
19 #include "vm/DalvikVersion.h"
30 #include "OptInvocation.h"
33 static const char* kClassesDex = "classes.dex";
37 * Given the filename of a .jar or .dex file, construct the DEX file cache
40 * For a Jar, "subFileName" is the name of the entry (usually "classes.dex").
41 * For a DEX, it may be NULL.
43 * Returns a newly-allocated string, or NULL on failure.
45 char* dexOptGenerateCacheFileName(const char* fileName, const char* subFileName)
48 static const char kDexCachePath[] = "dalvik-cache";
49 char absoluteFile[sizeof(nameBuf)];
50 const size_t kBufLen = sizeof(nameBuf) - 1;
55 * Get the absolute path of the Jar or DEX file.
57 absoluteFile[0] = '\0';
58 if (fileName[0] != '/') {
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.
64 if (getcwd(absoluteFile, kBufLen) == NULL) {
65 LOGE("Can't get CWD while opening jar file\n");
68 strncat(absoluteFile, "/", kBufLen);
70 strncat(absoluteFile, fileName, kBufLen);
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
77 if (subFileName != NULL) {
78 strncat(absoluteFile, "/", kBufLen);
79 strncat(absoluteFile, subFileName, kBufLen);
82 /* Turn the path into a flat filename by replacing
83 * any slashes after the first one with '@' characters.
85 cp = absoluteFile + 1;
93 /* Build the name of the cache directory.
95 dataRoot = getenv("ANDROID_DATA");
98 snprintf(nameBuf, kBufLen, "%s/%s", dataRoot, kDexCachePath);
100 /* Tack on the file name for the actual cache file path.
102 strncat(nameBuf, absoluteFile, kBufLen);
104 LOGV("Cache file for '%s' '%s' is '%s'\n", fileName, subFileName, nameBuf);
105 return strdup(nameBuf);
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.
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
117 * Returns 0 on success, errno on failure.
119 int dexOptCreateEmptyHeader(int fd)
124 assert(lseek(fd, 0, SEEK_CUR) == 0);
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.
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));