From ef58cef98250305e6d629f3a307a74e45875b8dd Mon Sep 17 00:00:00 2001 From: Yabin Cui Date: Thu, 8 Mar 2018 17:03:04 -0800 Subject: [PATCH] base: Add TemporaryFile::DoNotRemove(). Bug: http://b/73127105 Test: none. Change-Id: I563c12bfb629ddd630568dda4817fb10cc9940a8 --- base/include/android-base/test_utils.h | 4 ++++ base/test_utils.cpp | 4 +++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/base/include/android-base/test_utils.h b/base/include/android-base/test_utils.h index 2edafe344..b95fa07ce 100644 --- a/base/include/android-base/test_utils.h +++ b/base/include/android-base/test_utils.h @@ -31,6 +31,8 @@ class TemporaryFile { // Release the ownership of fd, caller is reponsible for closing the // fd or stream properly. int release(); + // Don't remove the temporary file in the destructor. + void DoNotRemove() { remove_file_ = false; } int fd; char path[1024]; @@ -38,6 +40,8 @@ class TemporaryFile { private: void init(const std::string& tmp_dir); + bool remove_file_ = true; + DISALLOW_COPY_AND_ASSIGN(TemporaryFile); }; diff --git a/base/test_utils.cpp b/base/test_utils.cpp index 9d8dfb2fd..1619c2134 100644 --- a/base/test_utils.cpp +++ b/base/test_utils.cpp @@ -92,7 +92,9 @@ TemporaryFile::~TemporaryFile() { if (fd != -1) { close(fd); } - unlink(path); + if (remove_file_) { + unlink(path); + } } int TemporaryFile::release() {