summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorTianjie Xu2017-09-11 14:01:09 -0500
committerTianjie Xu2017-09-11 16:08:32 -0500
commitf9bc1b057165bb5ea89acf135f7f3a90eff579ab (patch)
tree4f3974078306f15a7076d8907eeec8aedacc5186 /base
parentcfadedb1391f82a25bc0209aaa2e069875657776 (diff)
downloadplatform-system-core-f9bc1b057165bb5ea89acf135f7f3a90eff579ab.tar.gz
platform-system-core-f9bc1b057165bb5ea89acf135f7f3a90eff579ab.tar.xz
platform-system-core-f9bc1b057165bb5ea89acf135f7f3a90eff579ab.zip
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')
-rw-r--r--base/include/android-base/test_utils.h4
-rw-r--r--base/test_utils.cpp10
2 files changed, 13 insertions, 1 deletions
diff --git a/base/include/android-base/test_utils.h b/base/include/android-base/test_utils.h
index c0bf0c1e1..07a5edda0 100644
--- a/base/include/android-base/test_utils.h
+++ b/base/include/android-base/test_utils.h
@@ -26,6 +26,10 @@ class TemporaryFile {
26 TemporaryFile(); 26 TemporaryFile();
27 ~TemporaryFile(); 27 ~TemporaryFile();
28 28
29 // Release the ownership of fd, caller is reponsible for closing the
30 // fd or stream properly.
31 int release();
32
29 int fd; 33 int fd;
30 char path[1024]; 34 char path[1024];
31 35
diff --git a/base/test_utils.cpp b/base/test_utils.cpp
index 636477d36..1cfa9e66f 100644
--- a/base/test_utils.cpp
+++ b/base/test_utils.cpp
@@ -85,10 +85,18 @@ TemporaryFile::TemporaryFile() {
85} 85}
86 86
87TemporaryFile::~TemporaryFile() { 87TemporaryFile::~TemporaryFile() {
88 close(fd); 88 if (fd != -1) {
89 close(fd);
90 }
89 unlink(path); 91 unlink(path);
90} 92}
91 93
94int TemporaryFile::release() {
95 int result = fd;
96 fd = -1;
97 return result;
98}
99
92void TemporaryFile::init(const std::string& tmp_dir) { 100void TemporaryFile::init(const std::string& tmp_dir) {
93 snprintf(path, sizeof(path), "%s%cTemporaryFile-XXXXXX", tmp_dir.c_str(), 101 snprintf(path, sizeof(path), "%s%cTemporaryFile-XXXXXX", tmp_dir.c_str(),
94 OS_PATH_SEPARATOR); 102 OS_PATH_SEPARATOR);