diff options
| author | Elliott Hughes <enh@google.com> | 2015-07-30 22:56:01 +0000 |
|---|---|---|
| committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2015-07-30 22:56:01 +0000 |
| commit | 0cf93dc345cd5e8131009146a3b2767b1c60f77b (patch) | |
| tree | dc103f77af449c2840cd1e31cb00f2a2cf7bd7ea /base/test_utils.cpp | |
| parent | d548e30f04b2eb7aa00deab42f5559d08396c38e (diff) | |
| parent | 47d67c96ec991ef1690b4c07188335cbc4bfa2aa (diff) | |
Merge "Write mkdirs in more idiomatic C++ style."
Diffstat (limited to 'base/test_utils.cpp')
| -rw-r--r-- | base/test_utils.cpp | 43 |
1 files changed, 32 insertions, 11 deletions
diff --git a/base/test_utils.cpp b/base/test_utils.cpp index 0517bc7138..dceb8b7888 100644 --- a/base/test_utils.cpp +++ b/base/test_utils.cpp @@ -14,7 +14,7 @@ * limitations under the License. */ -#include "test_utils.h" +#include "base/test_utils.h" #include <fcntl.h> #include <stdio.h> @@ -26,34 +26,55 @@ #include <windows.h> #endif -TemporaryFile::TemporaryFile() { +#include <string> + +static std::string GetSystemTempDir() { #if defined(__ANDROID__) - init("/data/local/tmp"); + return "/data/local/tmp"; #elif defined(_WIN32) char wd[MAX_PATH] = {}; _getcwd(wd, sizeof(wd)); - init(wd); + return wd; #else - init("/tmp"); + return "/tmp"; #endif } +TemporaryFile::TemporaryFile() { + init(GetSystemTempDir()); +} + TemporaryFile::~TemporaryFile() { close(fd); - unlink(filename); + unlink(path); } -void TemporaryFile::init(const char* tmp_dir) { - snprintf(filename, sizeof(filename), "%s/TemporaryFile-XXXXXX", tmp_dir); +void TemporaryFile::init(const std::string& tmp_dir) { + snprintf(path, sizeof(path), "%s/TemporaryFile-XXXXXX", tmp_dir.c_str()); #if !defined(_WIN32) - fd = mkstemp(filename); + fd = mkstemp(path); #else // Windows doesn't have mkstemp, and tmpfile creates the file in the root // directory, requiring root (?!) permissions. We have to settle for mktemp. - if (mktemp(filename) == nullptr) { + if (mktemp(path) == nullptr) { abort(); } - fd = open(filename, O_RDWR | O_NOINHERIT | O_CREAT, _S_IREAD | _S_IWRITE); + fd = open(path, O_RDWR | O_NOINHERIT | O_CREAT, _S_IREAD | _S_IWRITE); #endif } + +#if !defined(_WIN32) +TemporaryDir::TemporaryDir() { + init(GetSystemTempDir()); +} + +TemporaryDir::~TemporaryDir() { + rmdir(path); +} + +bool TemporaryDir::init(const std::string& tmp_dir) { + snprintf(path, sizeof(path), "%s/TemporaryDir-XXXXXX", tmp_dir.c_str()); + return (mkdtemp(path) != nullptr); +} +#endif |
