Add missing include for flock()
[folly.git] / folly / File.cpp
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 #include <folly/File.h>
18
19 #include <fcntl.h>
20 #include <unistd.h>
21 #include <sys/file.h>
22
23 #include <folly/Exception.h>
24 #include <folly/FileUtil.h>
25 #include <folly/Format.h>
26 #include <folly/ScopeGuard.h>
27
28 #include <system_error>
29
30 #include <glog/logging.h>
31
32 namespace folly {
33
34 File::File()
35   : fd_(-1)
36   , ownsFd_(false)
37 {}
38
39 File::File(int fd, bool ownsFd)
40   : fd_(fd)
41   , ownsFd_(ownsFd) {
42   CHECK_GE(fd, -1) << "fd must be -1 or non-negative";
43   CHECK(fd != -1 || !ownsFd) << "cannot own -1";
44 }
45
46 File::File(const char* name, int flags, mode_t mode)
47   : fd_(::open(name, flags, mode))
48   , ownsFd_(false) {
49   if (fd_ == -1) {
50     throwSystemError(folly::format("open(\"{}\", {:#o}, 0{:#o}) failed",
51                                    name, flags, mode).fbstr());
52   }
53   ownsFd_ = true;
54 }
55
56 File::File(File&& other) noexcept
57   : fd_(other.fd_)
58   , ownsFd_(other.ownsFd_) {
59   other.release();
60 }
61
62 File& File::operator=(File&& other) {
63   closeNoThrow();
64   swap(other);
65   return *this;
66 }
67
68 File::~File() {
69   auto fd = fd_;
70   if (!closeNoThrow()) {  // ignore most errors
71     DCHECK_NE(errno, EBADF) << "closing fd " << fd << ", it may already "
72       << "have been closed. Another time, this might close the wrong FD.";
73   }
74 }
75
76 /* static */ File File::temporary() {
77   // make a temp file with tmpfile(), dup the fd, then return it in a File.
78   FILE* tmpFile = tmpfile();
79   checkFopenError(tmpFile, "tmpfile() failed");
80   SCOPE_EXIT { fclose(tmpFile); };
81
82   int fd = ::dup(fileno(tmpFile));
83   checkUnixError(fd, "dup() failed");
84
85   return File(fd, true);
86 }
87
88 int File::release() noexcept {
89   int released = fd_;
90   fd_ = -1;
91   ownsFd_ = false;
92   return released;
93 }
94
95 void File::swap(File& other) {
96   using std::swap;
97   swap(fd_, other.fd_);
98   swap(ownsFd_, other.ownsFd_);
99 }
100
101 void swap(File& a, File& b) {
102   a.swap(b);
103 }
104
105 File File::dup() const {
106   if (fd_ != -1) {
107     int fd = ::dup(fd_);
108     checkUnixError(fd, "dup() failed");
109
110     return File(fd, true);
111   }
112
113   return File();
114 }
115
116 void File::close() {
117   if (!closeNoThrow()) {
118     throwSystemError("close() failed");
119   }
120 }
121
122 bool File::closeNoThrow() {
123   int r = ownsFd_ ? ::close(fd_) : 0;
124   release();
125   return r == 0;
126 }
127
128 void File::lock() { doLock(LOCK_EX); }
129 bool File::try_lock() { return doTryLock(LOCK_EX); }
130 void File::lock_shared() { doLock(LOCK_SH); }
131 bool File::try_lock_shared() { return doTryLock(LOCK_SH); }
132
133 void File::doLock(int op) {
134   checkUnixError(flockNoInt(fd_, op), "flock() failed (lock)");
135 }
136
137 bool File::doTryLock(int op) {
138   int r = flockNoInt(fd_, op | LOCK_NB);
139   // flock returns EWOULDBLOCK if already locked
140   if (r == -1 && errno == EWOULDBLOCK) return false;
141   checkUnixError(r, "flock() failed (try_lock)");
142   return true;
143 }
144
145 void File::unlock() {
146   checkUnixError(flockNoInt(fd_, LOCK_UN), "flock() failed (unlock)");
147 }
148 void File::unlock_shared() { unlock(); }
149
150 }  // namespace folly