libbase: fix the way to find temp dir.

Tests running in app context can't access /data/local/tmp,
so try current directory if /data/local/tmp is not accessible.

Bug: http://b/18790309
Test: run unit test in app context and shell context.
Change-Id: If66fe8f0ac3edb3a32a2a2a50a524364f818a58b
This commit is contained in:
Yabin Cui 2016-12-14 17:45:49 -08:00
parent 5b202c4376
commit 57e9cea099
1 changed files with 7 additions and 1 deletions

View File

@ -57,7 +57,13 @@ char* mkdtemp(char* template_name) {
static std::string GetSystemTempDir() {
#if defined(__ANDROID__)
return "/data/local/tmp";
const char* tmpdir = "/data/local/tmp";
if (access(tmpdir, R_OK | W_OK | X_OK) == 0) {
return tmpdir;
}
// Tests running in app context can't access /data/local/tmp,
// so try current directory if /data/local/tmp is not accessible.
return ".";
#elif defined(_WIN32)
char tmp_dir[MAX_PATH];
DWORD result = GetTempPathA(sizeof(tmp_dir), tmp_dir);