Update SSLContext to use folly::Random and discrete_distribution
[folly.git] / folly / File.h
1 /*
2  * Copyright 2015 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 #ifndef FOLLY_FILE_H_
18 #define FOLLY_FILE_H_
19
20 #include <fcntl.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <unistd.h>
24
25 #include <string>
26
27 #include <folly/Portability.h>
28 #include <folly/Range.h>
29
30 namespace folly {
31
32 /**
33  * A File represents an open file.
34  */
35 class File {
36  public:
37   /**
38    * Creates an empty File object, for late initialization.
39    */
40   File();
41
42   /**
43    * Create a File object from an existing file descriptor.
44    * Takes ownership of the file descriptor if ownsFd is true.
45    */
46   explicit File(int fd, bool ownsFd = false);
47
48   /**
49    * Open and create a file object.  Throws on error.
50    */
51   explicit File(const char* name, int flags = O_RDONLY, mode_t mode = 0666);
52   explicit File(
53       const std::string& name, int flags = O_RDONLY, mode_t mode = 0666);
54   explicit File(StringPiece name, int flags = O_RDONLY, mode_t mode = 0666);
55
56   ~File();
57
58   /**
59    * Create and return a temporary, owned file (uses tmpfile()).
60    */
61   static File temporary();
62
63   /**
64    * Return the file descriptor, or -1 if the file was closed.
65    */
66   int fd() const { return fd_; }
67
68   /**
69    * Returns 'true' iff the file was successfully opened.
70    */
71   explicit operator bool() const {
72     return fd_ != -1;
73   }
74
75   /**
76    * Duplicate file descriptor and return File that owns it.
77    */
78   File dup() const;
79
80   /**
81    * If we own the file descriptor, close the file and throw on error.
82    * Otherwise, do nothing.
83    */
84   void close();
85
86   /**
87    * Closes the file (if owned).  Returns true on success, false (and sets
88    * errno) on error.
89    */
90   bool closeNoThrow();
91
92   /**
93    * Returns and releases the file descriptor; no longer owned by this File.
94    * Returns -1 if the File object didn't wrap a file.
95    */
96   int release() noexcept;
97
98   /**
99    * Swap this File with another.
100    */
101   void swap(File& other);
102
103   // movable
104   File(File&&) noexcept;
105   File& operator=(File&&);
106
107   // FLOCK (INTERPROCESS) LOCKS
108   //
109   // NOTE THAT THESE LOCKS ARE flock() LOCKS.  That is, they may only be used
110   // for inter-process synchronization -- an attempt to acquire a second lock
111   // on the same file descriptor from the same process may succeed.  Attempting
112   // to acquire a second lock on a different file descriptor for the same file
113   // should fail, but some systems might implement flock() using fcntl() locks,
114   // in which case it will succeed.
115   void lock();
116   bool try_lock();
117   void unlock();
118
119   void lock_shared();
120   bool try_lock_shared();
121   void unlock_shared();
122
123  private:
124   void doLock(int op);
125   bool doTryLock(int op);
126
127   // unique
128   File(const File&) = delete;
129   File& operator=(const File&) = delete;
130
131   int fd_;
132   bool ownsFd_;
133 };
134
135 void swap(File& a, File& b);
136
137
138 }  // namespace folly
139
140 #endif /* FOLLY_FILE_H_ */