proxygen
folly::File Class Reference

#include <File.h>

Public Member Functions

 File () noexcept
 
 File (int fd, bool ownsFd=false) noexcept
 
 File (const char *name, int flags=O_RDONLY, mode_t mode=0666)
 
 File (const std::string &name, int flags=O_RDONLY, mode_t mode=0666)
 
 File (StringPiece name, int flags=O_RDONLY, mode_t mode=0666)
 
 ~File ()
 
int fd () const
 
 operator bool () const
 
File dup () const
 
void close ()
 
bool closeNoThrow ()
 
int release () noexcept
 
void swap (File &other) noexcept
 
 File (File &&) noexcept
 
Fileoperator= (File &&)
 
void lock ()
 
bool try_lock ()
 
void unlock ()
 
void lock_shared ()
 
bool try_lock_shared ()
 
void unlock_shared ()
 

Static Public Member Functions

template<typename... Args>
static Expected< File, exception_wrappermakeFile (Args &&...args) noexcept
 
static File temporary ()
 

Private Member Functions

void doLock (int op)
 
bool doTryLock (int op)
 
 File (const File &)=delete
 
Fileoperator= (const File &)=delete
 

Private Attributes

int fd_
 
bool ownsFd_
 

Detailed Description

A File represents an open file.

Definition at line 37 of file File.h.

Constructor & Destructor Documentation

folly::File::File ( )
noexcept

Creates an empty File object, for late initialization.

Definition at line 33 of file File.cpp.

Referenced by dup(), makeFile(), operator bool(), and temporary().

33 : fd_(-1), ownsFd_(false) {}
int fd_
Definition: File.h:152
bool ownsFd_
Definition: File.h:153
folly::File::File ( int  fd,
bool  ownsFd = false 
)
explicitnoexcept

Create a File object from an existing file descriptor. Takes ownership of the file descriptor if ownsFd is true.

Definition at line 35 of file File.cpp.

References fd().

35  : fd_(fd), ownsFd_(ownsFd) {
36  CHECK_GE(fd, -1) << "fd must be -1 or non-negative";
37  CHECK(fd != -1 || !ownsFd) << "cannot own -1";
38 }
int fd_
Definition: File.h:152
int fd() const
Definition: File.h:85
bool ownsFd_
Definition: File.h:153
folly::File::File ( const char *  name,
int  flags = O_RDONLY,
mode_t  mode = 0666 
)
explicit

Open and create a file object. Throws on error. Owns the file descriptor implicitly.

Definition at line 40 of file File.cpp.

References fd_, folly::format(), ownsFd_, and folly::throwSystemError().

41  : fd_(::open(name, flags, mode)), ownsFd_(false) {
42  if (fd_ == -1) {
44  folly::format("open(\"{}\", {:#o}, 0{:#o}) failed", name, flags, mode)
45  .fbstr());
46  }
47  ownsFd_ = true;
48 }
flags
Definition: http_parser.h:127
folly::Optional< PskKeyExchangeMode > mode
const char * name
Definition: http_parser.c:437
int fd_
Definition: File.h:152
bool ownsFd_
Definition: File.h:153
Formatter< false, Args... > format(StringPiece fmt, Args &&...args)
Definition: Format.h:271
void throwSystemError(Args &&...args)
Definition: Exception.h:76
folly::File::File ( const std::string name,
int  flags = O_RDONLY,
mode_t  mode = 0666 
)
explicit

Definition at line 50 of file File.cpp.

51  : File(name.c_str(), flags, mode) {}
flags
Definition: http_parser.h:127
folly::Optional< PskKeyExchangeMode > mode
const char * name
Definition: http_parser.c:437
File() noexcept
Definition: File.cpp:33
folly::File::File ( StringPiece  name,
int  flags = O_RDONLY,
mode_t  mode = 0666 
)
explicit

Definition at line 53 of file File.cpp.

54  : File(name.str(), flags, mode) {}
flags
Definition: http_parser.h:127
folly::Optional< PskKeyExchangeMode > mode
const char * name
Definition: http_parser.c:437
File() noexcept
Definition: File.cpp:33
folly::File::~File ( )

Definition at line 66 of file File.cpp.

References closeNoThrow(), fd(), and fd_.

Referenced by makeFile().

66  {
67  auto fd = fd_;
68  if (!closeNoThrow()) { // ignore most errors
69  DCHECK_NE(errno, EBADF)
70  << "closing fd " << fd << ", it may already "
71  << "have been closed. Another time, this might close the wrong FD.";
72  }
73 }
int fd_
Definition: File.h:152
bool closeNoThrow()
Definition: File.cpp:123
int fd() const
Definition: File.h:85
folly::File::File ( File &&  other)
noexcept

Definition at line 56 of file File.cpp.

56  : fd_(other.fd_), ownsFd_(other.ownsFd_) {
57  other.release();
58 }
int fd_
Definition: File.h:152
bool ownsFd_
Definition: File.h:153
folly::File::File ( const File )
privatedelete

Member Function Documentation

void folly::File::close ( )

If we own the file descriptor, close the file and throw on error. Otherwise, do nothing.

Definition at line 117 of file File.cpp.

References closeNoThrow(), and folly::throwSystemError().

Referenced by closeNoThrow(), folly::gen::detail::FileWriter::compose(), operator bool(), and TEST().

117  {
118  if (!closeNoThrow()) {
119  throwSystemError("close() failed");
120  }
121 }
bool closeNoThrow()
Definition: File.cpp:123
void throwSystemError(Args &&...args)
Definition: Exception.h:76
bool folly::File::closeNoThrow ( )

Closes the file (if owned). Returns true on success, false (and sets errno) on error.

Definition at line 123 of file File.cpp.

References close(), fd_, ownsFd_, and release().

Referenced by close(), operator bool(), operator=(), and ~File().

123  {
124  int r = ownsFd_ ? ::close(fd_) : 0;
125  release();
126  return r == 0;
127 }
int release() noexcept
Definition: File.cpp:89
int fd_
Definition: File.h:152
bool ownsFd_
Definition: File.h:153
void close()
Definition: File.cpp:117
void folly::File::doLock ( int  op)
private

Definition at line 142 of file File.cpp.

References folly::checkUnixError(), fd_, and folly::flockNoInt().

Referenced by lock(), lock_shared(), and operator bool().

142  {
143  checkUnixError(flockNoInt(fd_, op), "flock() failed (lock)");
144 }
int fd_
Definition: File.h:152
int flockNoInt(int fd, int operation)
Definition: FileUtil.cpp:94
void checkUnixError(ssize_t ret, Args &&...args)
Definition: Exception.h:101
bool folly::File::doTryLock ( int  op)
private

Definition at line 146 of file File.cpp.

References folly::checkUnixError(), fd_, and folly::flockNoInt().

Referenced by operator bool(), try_lock(), and try_lock_shared().

146  {
147  int r = flockNoInt(fd_, op | LOCK_NB);
148  // flock returns EWOULDBLOCK if already locked
149  if (r == -1 && errno == EWOULDBLOCK) {
150  return false;
151  }
152  checkUnixError(r, "flock() failed (try_lock)");
153  return true;
154 }
int fd_
Definition: File.h:152
int flockNoInt(int fd, int operation)
Definition: FileUtil.cpp:94
void checkUnixError(ssize_t ret, Args &&...args)
Definition: Exception.h:101
File folly::File::dup ( ) const

Duplicate file descriptor and return File that owns it.

Definition at line 106 of file File.cpp.

References folly::checkUnixError(), fd(), fd_, and File().

Referenced by operator bool(), and temporary().

106  {
107  if (fd_ != -1) {
108  int fd = ::dup(fd_);
109  checkUnixError(fd, "dup() failed");
110 
111  return File(fd, true);
112  }
113 
114  return File();
115 }
int fd_
Definition: File.h:152
int fd() const
Definition: File.h:85
void checkUnixError(ssize_t ret, Args &&...args)
Definition: Exception.h:101
File dup() const
Definition: File.cpp:106
File() noexcept
Definition: File.cpp:33
void folly::File::lock ( )

Definition at line 129 of file File.cpp.

References doLock().

Referenced by operator bool().

129  {
130  doLock(LOCK_EX);
131 }
void doLock(int op)
Definition: File.cpp:142
void folly::File::lock_shared ( )

Definition at line 135 of file File.cpp.

References doLock().

Referenced by operator bool().

135  {
136  doLock(LOCK_SH);
137 }
void doLock(int op)
Definition: File.cpp:142
template<typename... Args>
static Expected<File, exception_wrapper> folly::File::makeFile ( Args &&...  args)
inlinestaticnoexcept

All the constructors that are not noexcept can throw std::system_error. This is a helper method to use folly::Expected to chain a file open event to something else you want to do with the open fd.

Definition at line 67 of file File.h.

References File(), folly::makeUnexpected(), temporary(), and ~File().

Referenced by TEST().

67  {
68  try {
69  return File(std::forward<Args>(args)...);
70  } catch (const std::system_error& se) {
71  return makeUnexpected(exception_wrapper(std::current_exception(), se));
72  }
73  }
constexpr Unexpected< typename std::decay< Error >::type > makeUnexpected(Error &&)
Definition: Expected.h:785
File() noexcept
Definition: File.cpp:33
folly::File::operator bool ( ) const
inlineexplicit

Returns 'true' iff the file was successfully opened.

Definition at line 92 of file File.h.

References close(), closeNoThrow(), doLock(), doTryLock(), dup(), fd_, File(), lock(), lock_shared(), folly::pushmi::__adl::noexcept(), operator=(), release(), swap(), try_lock(), try_lock_shared(), unlock(), and unlock_shared().

92  {
93  return fd_ != -1;
94  }
int fd_
Definition: File.h:152
File & folly::File::operator= ( File &&  other)

Definition at line 60 of file File.cpp.

References closeNoThrow(), and swap().

Referenced by operator bool().

60  {
61  closeNoThrow();
62  swap(other);
63  return *this;
64 }
void swap(File &other) noexcept
Definition: File.cpp:96
bool closeNoThrow()
Definition: File.cpp:123
File& folly::File::operator= ( const File )
privatedelete
int folly::File::release ( )
noexcept

Returns and releases the file descriptor; no longer owned by this File. Returns -1 if the File object didn't wrap a file.

Definition at line 89 of file File.cpp.

References fd_, and ownsFd_.

Referenced by closeNoThrow(), operator bool(), and TEST().

89  {
90  int released = fd_;
91  fd_ = -1;
92  ownsFd_ = false;
93  return released;
94 }
int fd_
Definition: File.h:152
bool ownsFd_
Definition: File.h:153
void folly::File::swap ( File other)
noexcept

Swap this File with another.

Definition at line 96 of file File.cpp.

References fd_, ownsFd_, and folly::swap().

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

96  {
97  using std::swap;
98  swap(fd_, other.fd_);
99  swap(ownsFd_, other.ownsFd_);
100 }
void swap(File &a, File &b) noexcept
Definition: File.cpp:102
void swap(File &other) noexcept
Definition: File.cpp:96
int fd_
Definition: File.h:152
bool ownsFd_
Definition: File.h:153
File folly::File::temporary ( )
static

Create and return a temporary, owned file (uses tmpfile()).

Definition at line 75 of file File.cpp.

References folly::checkFopenError(), folly::checkUnixError(), dup(), fd(), FILE, File(), and SCOPE_EXIT.

Referenced by makeFile(), folly::TEST(), and TEST().

75  {
76  // make a temp file with tmpfile(), dup the fd, then return it in a File.
77  FILE* tmpFile = tmpfile();
78  checkFopenError(tmpFile, "tmpfile() failed");
79  SCOPE_EXIT {
80  fclose(tmpFile);
81  };
82 
83  int fd = ::dup(fileno(tmpFile));
84  checkUnixError(fd, "dup() failed");
85 
86  return File(fd, true);
87 }
#define SCOPE_EXIT
Definition: ScopeGuard.h:274
int fd() const
Definition: File.h:85
void checkFopenError(FILE *fp, Args &&...args)
Definition: Exception.h:118
void checkUnixError(ssize_t ret, Args &&...args)
Definition: Exception.h:101
File dup() const
Definition: File.cpp:106
GMockOutputTest ExpectedCall FILE
File() noexcept
Definition: File.cpp:33
bool folly::File::try_lock ( )

Definition at line 132 of file File.cpp.

References doTryLock().

Referenced by main(), and operator bool().

132  {
133  return doTryLock(LOCK_EX);
134 }
bool doTryLock(int op)
Definition: File.cpp:146
bool folly::File::try_lock_shared ( )

Definition at line 138 of file File.cpp.

References doTryLock().

Referenced by main(), and operator bool().

138  {
139  return doTryLock(LOCK_SH);
140 }
bool doTryLock(int op)
Definition: File.cpp:146
void folly::File::unlock ( )

Definition at line 156 of file File.cpp.

References folly::checkUnixError(), fd_, and folly::flockNoInt().

Referenced by operator bool(), and unlock_shared().

156  {
157  checkUnixError(flockNoInt(fd_, LOCK_UN), "flock() failed (unlock)");
158 }
int fd_
Definition: File.h:152
int flockNoInt(int fd, int operation)
Definition: FileUtil.cpp:94
void checkUnixError(ssize_t ret, Args &&...args)
Definition: Exception.h:101
void folly::File::unlock_shared ( )

Definition at line 159 of file File.cpp.

References unlock().

Referenced by operator bool().

159  {
160  unlock();
161 }
void unlock()
Definition: File.cpp:156

Member Data Documentation

int folly::File::fd_
private

Definition at line 152 of file File.h.

Referenced by closeNoThrow(), doLock(), doTryLock(), dup(), fd(), File(), operator bool(), release(), swap(), unlock(), and ~File().

bool folly::File::ownsFd_
private

Definition at line 153 of file File.h.

Referenced by closeNoThrow(), File(), release(), and swap().


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