Merge "base: Add sensitivity to TMPDIR environment for TemporaryDir and TemporaryFile"

This commit is contained in:
Treehugger Robot 2018-11-14 23:03:32 +00:00 committed by Gerrit Code Review
commit 01acb70e80

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
}