2 * Copyright 2014 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <sys/types.h>
28 * A File represents an open file.
33 * Creates an empty File object, for late initialization.
38 * Create a File object from an existing file descriptor.
39 * Takes ownership of the file descriptor if ownsFd is true.
41 /* implicit */ File(int fd,
45 * Open and create a file object. Throws on error.
47 /* implicit */ File(const char* name,
54 * Create and return a temporary, owned file (uses tmpfile()).
56 static File temporary();
59 * Return the file descriptor, or -1 if the file was closed.
61 int fd() const { return fd_; }
64 * Returns 'true' iff the file was successfully opened.
66 explicit operator bool() const {
71 * Duplicate file descriptor and return File that owns it.
76 * If we own the file descriptor, close the file and throw on error.
77 * Otherwise, do nothing.
82 * Closes the file (if owned). Returns true on success, false (and sets
88 * Returns and releases the file descriptor; no longer owned by this File.
89 * Returns -1 if the File object didn't wrap a file.
94 * Swap this File with another.
96 void swap(File& other);
100 File& operator=(File&&);
102 // FLOCK (INTERPROCESS) LOCKS
104 // NOTE THAT THESE LOCKS ARE flock() LOCKS. That is, they may only be used
105 // for inter-process synchronization -- an attempt to acquire a second lock
106 // on the same file descriptor from the same process may succeed. Attempting
107 // to acquire a second lock on a different file descriptor for the same file
108 // should fail, but some systems might implement flock() using fcntl() locks,
109 // in which case it will succeed.
115 bool try_lock_shared();
116 void unlock_shared();
120 bool doTryLock(int op);
123 File(const File&) = delete;
124 File& operator=(const File&) = delete;
130 void swap(File& a, File& b);
135 #endif /* FOLLY_FILE_H_ */