From 039df94e876679d177a6db7f8bf3d2bf94fb0370 Mon Sep 17 00:00:00 2001 From: Vignesh Venkatasubramanian Date: Thu, 7 Apr 2016 10:09:47 -0700 Subject: [PATCH] Add TEST_TMPDIR environment variable Honor TEST_TMPDIR environment variable for creating temporary test files in non-windows platforms. This helps in environments where we cannot create a temporary file in the current directory. Change-Id: I39e0a19580635d752171e0573dd21ec838cb81a4 --- common/file_util.cc | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/common/file_util.cc b/common/file_util.cc index 4f91318..6dab146 100644 --- a/common/file_util.cc +++ b/common/file_util.cc @@ -14,6 +14,7 @@ #include #include +#include #include #include @@ -21,13 +22,23 @@ namespace libwebm { std::string GetTempFileName() { #if !defined _MSC_VER && !defined __MINGW32__ - char temp_file_name_template[] = "libwebm_temp.XXXXXX"; + std::string temp_file_name_template_str = + std::string(std::getenv("TEST_TMPDIR") ? std::getenv("TEST_TMPDIR") : + ".") + + "/libwebm_temp.XXXXXX"; + char* temp_file_name_template = + new char[temp_file_name_template_str.length() + 1]; + memset(temp_file_name_template, 0, temp_file_name_template_str.length() + 1); + temp_file_name_template_str.copy(temp_file_name_template, + temp_file_name_template_str.length(), 0); int fd = mkstemp(temp_file_name_template); + std::string temp_file_name = + (fd != -1) ? std::string(temp_file_name_template) : std::string(); + delete[] temp_file_name_template; if (fd != -1) { close(fd); - return std::string(temp_file_name_template); } - return std::string(); + return temp_file_name; #else char tmp_file_name[_MAX_PATH]; errno_t err = tmpnam_s(tmp_file_name);