proxygen
File.h
Go to the documentation of this file.
1 /*
2  * Copyright 2013-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <fcntl.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 
23 #include <string>
24 #include <system_error>
25 
26 #include <folly/ExceptionWrapper.h>
27 #include <folly/Expected.h>
28 #include <folly/Portability.h>
29 #include <folly/Range.h>
31 
32 namespace folly {
33 
37 class File {
38  public:
42  File() noexcept;
43 
48  explicit File(int fd, bool ownsFd = false) noexcept;
49 
54  explicit File(const char* name, int flags = O_RDONLY, mode_t mode = 0666);
55  explicit File(
56  const std::string& name,
57  int flags = O_RDONLY,
58  mode_t mode = 0666);
59  explicit File(StringPiece name, int flags = O_RDONLY, mode_t mode = 0666);
60 
66  template <typename... Args>
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  }
74 
75  ~File();
76 
80  static File temporary();
81 
85  int fd() const {
86  return fd_;
87  }
88 
92  explicit operator bool() const {
93  return fd_ != -1;
94  }
95 
99  File dup() const;
100 
105  void close();
106 
111  bool closeNoThrow();
112 
117  int release() noexcept;
118 
122  void swap(File& other) noexcept;
123 
124  // movable
125  File(File&&) noexcept;
126  File& operator=(File&&);
127 
128  // FLOCK (INTERPROCESS) LOCKS
129  //
130  // NOTE THAT THESE LOCKS ARE flock() LOCKS. That is, they may only be used
131  // for inter-process synchronization -- an attempt to acquire a second lock
132  // on the same file descriptor from the same process may succeed. Attempting
133  // to acquire a second lock on a different file descriptor for the same file
134  // should fail, but some systems might implement flock() using fcntl() locks,
135  // in which case it will succeed.
136  void lock();
137  bool try_lock();
138  void unlock();
139 
140  void lock_shared();
141  bool try_lock_shared();
142  void unlock_shared();
143 
144  private:
145  void doLock(int op);
146  bool doTryLock(int op);
147 
148  // unique
149  File(const File&) = delete;
150  File& operator=(const File&) = delete;
151 
152  int fd_;
153  bool ownsFd_;
154 };
155 
156 void swap(File& a, File& b) noexcept;
157 
158 } // namespace folly
void lock()
Definition: File.cpp:129
flags
Definition: http_parser.h:127
char b
void doLock(int op)
Definition: File.cpp:142
int release() noexcept
Definition: File.cpp:89
internal::ArgsMatcher< InnerMatcher > Args(const InnerMatcher &matcher)
File & operator=(File &&)
Definition: File.cpp:60
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
requires E e noexcept(noexcept(s.error(std::move(e))))
folly::Optional< PskKeyExchangeMode > mode
void swap(File &other) noexcept
Definition: File.cpp:96
const char * name
Definition: http_parser.c:437
int fd_
Definition: File.h:152
bool closeNoThrow()
Definition: File.cpp:123
int fd() const
Definition: File.h:85
static Expected< File, exception_wrapper > makeFile(Args &&...args) noexcept
Definition: File.h:67
constexpr Unexpected< typename std::decay< Error >::type > makeUnexpected(Error &&)
Definition: Expected.h:785
char a
bool ownsFd_
Definition: File.h:153
void close()
Definition: File.cpp:117
const char * string
Definition: Conv.cpp:212
bool try_lock_shared()
Definition: File.cpp:138
bool try_lock()
Definition: File.cpp:132
void unlock()
Definition: File.cpp:156
File dup() const
Definition: File.cpp:106
bool doTryLock(int op)
Definition: File.cpp:146
static File temporary()
Definition: File.cpp:75
File() noexcept
Definition: File.cpp:33
void unlock_shared()
Definition: File.cpp:159
void lock_shared()
Definition: File.cpp:135