diff options
author | Tianjie Xu <xunchang@google.com> | 2017-09-11 12:01:09 -0700 |
---|---|---|
committer | Tianjie Xu <xunchang@google.com> | 2017-09-11 14:08:32 -0700 |
commit | f9bc1b057165bb5ea89acf135f7f3a90eff579ab (patch) | |
tree | 4f3974078306f15a7076d8907eeec8aedacc5186 /base/test_utils.cpp | |
parent | cfadedb1391f82a25bc0209aaa2e069875657776 (diff) |
Add the Release function for TemporaryFiles
Some tests may create a File* by calling fdopen() on the temp file's
fd. We should release the ownership of fd in this case to avoid the
double close.
Bug: 65430057
Test: libbase unit tests pass
Change-Id: I54fcce2029f9a574f53afdbdda737ee58620c73a
Diffstat (limited to 'base/test_utils.cpp')
-rw-r--r-- | base/test_utils.cpp | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/base/test_utils.cpp b/base/test_utils.cpp index 636477d362..1cfa9e66fe 100644 --- a/base/test_utils.cpp +++ b/base/test_utils.cpp @@ -85,10 +85,18 @@ TemporaryFile::TemporaryFile() { } TemporaryFile::~TemporaryFile() { - close(fd); + if (fd != -1) { + close(fd); + } unlink(path); } +int TemporaryFile::release() { + int result = fd; + fd = -1; + return result; +} + void TemporaryFile::init(const std::string& tmp_dir) { snprintf(path, sizeof(path), "%s%cTemporaryFile-XXXXXX", tmp_dir.c_str(), OS_PATH_SEPARATOR); |