base: Add sensitivity to TMPDIR environment for TemporaryDir and TemporaryFile
Test: compile
Bug: 119313545
Change-Id: I2917847f0e90df94d74f4bfc0b13fbc6a5f243c3
diff --git a/base/file.cpp b/base/file.cpp
index e4407d0..f362f94 100644
--- a/base/file.cpp
+++ b/base/file.cpp
@@ -72,7 +72,8 @@
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 @@
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 @@
tmp_dir[result - 1] = '\0';
return tmp_dir;
#else
- return "/tmp";
+ const auto* tmpdir = getenv("TMPDIR");
+ if (tmpdir == nullptr) tmpdir = "/tmp";
+ return tmpdir;
#endif
}