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