Index: runtime/bin/directory_android.cc |
diff --git a/runtime/bin/directory_posix.cc b/runtime/bin/directory_android.cc |
similarity index 91% |
copy from runtime/bin/directory_posix.cc |
copy to runtime/bin/directory_android.cc |
index 7adb80f9156ab2d308131aee2181243b35b85a27..dbdfca77744f937bae8e721d07fd4745a72ff988 100644 |
--- a/runtime/bin/directory_posix.cc |
+++ b/runtime/bin/directory_android.cc |
@@ -376,7 +376,15 @@ Directory::ExistsResult Directory::Exists(const char* dir_name) { |
char* Directory::Current() { |
- return getcwd(NULL, 0); |
+ // Android's getcwd adheres closely to the POSIX standard. It won't |
+ // allocate memory. We need to make our own copy. |
+ |
+ char buffer[PATH_MAX]; |
+ if (NULL == getcwd(buffer, PATH_MAX)) { |
+ return NULL; |
+ } |
+ |
+ return strdup(buffer); |
} |
@@ -387,6 +395,19 @@ bool Directory::Create(const char* dir_name) { |
} |
+// Android doesn't currently provide mkdtemp. Once Android provied mkdtemp, |
+// remove this function in favor of calling mkdtemp directly. |
+static char* MakeTempDirectory(char* path_template) { |
+ if (mktemp(path_template) == NULL) { |
+ return NULL; |
+ } |
+ if (mkdir(path_template, 0700) != 0) { |
+ return NULL; |
+ } |
+ return path_template; |
+} |
+ |
+ |
char* Directory::CreateTemp(const char* const_template) { |
// Returns a new, unused directory name, modifying the contents of |
// dir_template. Creates the directory with the permissions specified |
@@ -402,11 +423,22 @@ char* Directory::CreateTemp(const char* const_template) { |
snprintf(path + path_length, PATH_MAX - path_length, "XXXXXX"); |
} |
} else { |
- snprintf(path, PATH_MAX, "/tmp/temp_dir1_XXXXXX"); |
+ // Android does not have a /tmp directory. A partial substitute, |
+ // suitable for bring-up work and tests, is to create a tmp |
+ // directory in /data/local/tmp. |
+ // |
+ // TODO(4413): In the long run, when running in an application we should |
+ // probably use android.content.Context.getCacheDir(). |
+ #define ANDROID_TEMP_DIR "/data/local/tmp" |
+ struct stat st; |
+ if (stat(ANDROID_TEMP_DIR, &st) != 0) { |
+ mkdir(ANDROID_TEMP_DIR, 0777); |
+ } |
+ snprintf(path, PATH_MAX, ANDROID_TEMP_DIR "/temp_dir1_XXXXXX"); |
} |
char* result; |
do { |
- result = mkdtemp(path); |
+ result = MakeTempDirectory(path); |
} while (result == NULL && errno == EINTR); |
if (result == NULL) { |
free(path); |