base: Add sensitivity to TMPDIR environment for TemporaryDir and TemporaryFile

Test: compile
Bug: 119313545
Change-Id: I2917847f0e90df94d74f4bfc0b13fbc6a5f243c3
This commit is contained in:
Mark Salyzyn 2018-11-13 15:34:38 -08:00
parent b9cb149441
commit 6009a2debc
1 changed files with 6 additions and 3 deletions

View File

@ -72,7 +72,8 @@ namespace {
std::string GetSystemTempDir() {
#if defined(__ANDROID__)
const char* tmpdir = "/data/local/tmp";
const auto* tmpdir = getenv("TMPDIR");
if (tmpdir == nullptr) tmpdir = "/data/local/tmp";
if (access(tmpdir, R_OK | W_OK | X_OK) == 0) {
return tmpdir;
}
@ -81,7 +82,7 @@ std::string GetSystemTempDir() {
return ".";
#elif defined(_WIN32)
char tmp_dir[MAX_PATH];
DWORD result = GetTempPathA(sizeof(tmp_dir), tmp_dir);
DWORD result = GetTempPathA(sizeof(tmp_dir), tmp_dir); // checks TMP env
CHECK_NE(result, 0ul) << "GetTempPathA failed, error: " << GetLastError();
CHECK_LT(result, sizeof(tmp_dir)) << "path truncated to: " << result;
@ -91,7 +92,9 @@ std::string GetSystemTempDir() {
tmp_dir[result - 1] = '\0';
return tmp_dir;
#else
return "/tmp";
const auto* tmpdir = getenv("TMPDIR");
if (tmpdir == nullptr) tmpdir = "/tmp";
return tmpdir;
#endif
}