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