proxygen
folly::test::TemporaryFile Class Reference

#include <TestUtil.h>

Public Types

enum  Scope { Scope::PERMANENT, Scope::UNLINK_IMMEDIATELY, Scope::UNLINK_ON_DESTRUCTION }
 

Public Member Functions

 TemporaryFile (StringPiece namePrefix=StringPiece(), fs::path dir=fs::path(), Scope scope=Scope::UNLINK_ON_DESTRUCTION, bool closeOnDestruction=true)
 
 ~TemporaryFile ()
 
 TemporaryFile (TemporaryFile &&other) noexcept
 
TemporaryFileoperator= (TemporaryFile &&other)
 
void close ()
 
int fd () const
 
const fs::path & path () const
 
void reset ()
 

Private Member Functions

void assign (TemporaryFile &other)
 

Private Attributes

Scope scope_
 
bool closeOnDestruction_
 
int fd_
 
fs::path path_
 

Detailed Description

Temporary file.

By default, the file is created in a system-specific location (the value of the TMPDIR environment variable, or /tmp), but you can override that with a different (non-empty) directory passed to the constructor.

By default, the file is closed and deleted when the TemporaryFile object is destroyed, but both these behaviors can be overridden with arguments to the constructor.

Definition at line 40 of file TestUtil.h.

Member Enumeration Documentation

Enumerator
PERMANENT 
UNLINK_IMMEDIATELY 
UNLINK_ON_DESTRUCTION 

Definition at line 42 of file TestUtil.h.

42  {
43  PERMANENT,
44  UNLINK_IMMEDIATELY,
45  UNLINK_ON_DESTRUCTION,
46  };

Constructor & Destructor Documentation

folly::test::TemporaryFile::TemporaryFile ( StringPiece  namePrefix = StringPiece(),
fs::path  dir = fs::path(),
Scope  scope = Scope::UNLINK_ON_DESTRUCTION,
bool  closeOnDestruction = true 
)
explicit

Definition at line 55 of file TestUtil.cpp.

References folly::checkUnixError(), fd_, path_, scope_, UNLINK_IMMEDIATELY, and folly::WARNING.

60  : scope_(scope),
61  closeOnDestruction_(closeOnDestruction),
62  fd_(-1),
63  path_(generateUniquePath(std::move(dir), namePrefix)) {
64  fd_ = open(path_.string().c_str(), O_RDWR | O_CREAT | O_EXCL, 0666);
65  checkUnixError(fd_, "open failed");
66 
68  boost::system::error_code ec;
69  fs::remove(path_, ec);
70  if (ec) {
71  LOG(WARNING) << "unlink on construction failed: " << ec;
72  } else {
73  path_.clear();
74  }
75  }
76 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void checkUnixError(ssize_t ret, Args &&...args)
Definition: Exception.h:101
folly::test::TemporaryFile::~TemporaryFile ( )

Definition at line 109 of file TestUtil.cpp.

References reset().

109  {
110  reset();
111 }
folly::test::TemporaryFile::TemporaryFile ( TemporaryFile &&  other)
inlinenoexcept

Definition at line 55 of file TestUtil.h.

References assign().

55  {
56  assign(other);
57  }
void assign(TemporaryFile &other)
Definition: TestUtil.h:80

Member Function Documentation

void folly::test::TemporaryFile::assign ( TemporaryFile other)
inlineprivate

Definition at line 80 of file TestUtil.h.

References closeOnDestruction_, folly::exchange(), fd_, path_, and scope_.

Referenced by operator=(), and TemporaryFile().

80  {
81  scope_ = other.scope_;
82  closeOnDestruction_ = other.closeOnDestruction_;
83  fd_ = std::exchange(other.fd_, -1);
84  path_ = other.path_;
85  }
T exchange(T &obj, U &&new_value)
Definition: Utility.h:120
void folly::test::TemporaryFile::close ( )

Definition at line 78 of file TestUtil.cpp.

References fd_.

Referenced by operator=(), reset(), and TEST().

78  {
79  if (::close(fd_) == -1) {
80  PLOG(ERROR) << "close failed";
81  }
82  fd_ = -1;
83 }
int folly::test::TemporaryFile::fd ( ) const
inline

Definition at line 68 of file TestUtil.h.

References fd_, path(), and reset().

Referenced by folly::symbolizer::test::TEST(), folly::test::TEST(), TEST(), TEST_F(), and TEST_P().

68  {
69  return fd_;
70  }
TemporaryFile& folly::test::TemporaryFile::operator= ( TemporaryFile &&  other)
inline

Definition at line 59 of file TestUtil.h.

References assign(), close(), and reset().

59  {
60  if (this != &other) {
61  reset();
62  assign(other);
63  }
64  return *this;
65  }
void assign(TemporaryFile &other)
Definition: TestUtil.h:80
void folly::test::TemporaryFile::reset ( )

Definition at line 91 of file TestUtil.cpp.

References close(), closeOnDestruction_, fd_, path_, PERMANENT, scope_, and folly::WARNING.

Referenced by fd(), operator=(), and ~TemporaryFile().

91  {
92  if (fd_ != -1 && closeOnDestruction_) {
93  if (::close(fd_) == -1) {
94  PLOG(ERROR) << "close failed (fd = " << fd_ << "): ";
95  }
96  }
97 
98  // If we previously failed to unlink() (UNLINK_IMMEDIATELY), we'll
99  // try again here.
100  if (scope_ != Scope::PERMANENT && !path_.empty()) {
101  boost::system::error_code ec;
102  fs::remove(path_, ec);
103  if (ec) {
104  LOG(WARNING) << "unlink on destruction failed: " << ec;
105  }
106  }
107 }

Member Data Documentation

bool folly::test::TemporaryFile::closeOnDestruction_
private

Definition at line 76 of file TestUtil.h.

Referenced by assign(), and reset().

int folly::test::TemporaryFile::fd_
private

Definition at line 77 of file TestUtil.h.

Referenced by assign(), close(), fd(), reset(), and TemporaryFile().

fs::path folly::test::TemporaryFile::path_
private

Definition at line 78 of file TestUtil.h.

Referenced by assign(), path(), folly::test::TemporaryDirectory::path(), reset(), and TemporaryFile().

Scope folly::test::TemporaryFile::scope_
private

Definition at line 75 of file TestUtil.h.

Referenced by assign(), path(), reset(), and TemporaryFile().


The documentation for this class was generated from the following files: