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