2 * Copyright 2017 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.
17 #include <folly/File.h>
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>
28 #include <system_error>
30 #include <glog/logging.h>
34 File::File() noexcept : fd_(-1), ownsFd_(false) {}
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";
41 File::File(const char* name, int flags, mode_t mode)
42 : fd_(::open(name, flags, mode))
45 throwSystemError(folly::format("open(\"{}\", {:#o}, 0{:#o}) failed",
46 name, flags, mode).fbstr());
51 File::File(const std::string& name, int flags, mode_t mode)
52 : File(name.c_str(), flags, mode) {}
54 File::File(StringPiece name, int flags, mode_t mode)
55 : File(name.str(), flags, mode) {}
57 File::File(File&& other) noexcept
59 , ownsFd_(other.ownsFd_) {
63 File& File::operator=(File&& other) {
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.";
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); };
83 int fd = ::dup(fileno(tmpFile));
84 checkUnixError(fd, "dup() failed");
86 return File(fd, true);
89 int File::release() noexcept {
96 void File::swap(File& other) {
99 swap(ownsFd_, other.ownsFd_);
102 void swap(File& a, File& b) {
106 File File::dup() const {
109 checkUnixError(fd, "dup() failed");
111 return File(fd, true);
118 if (!closeNoThrow()) {
119 throwSystemError("close() failed");
123 bool File::closeNoThrow() {
124 int r = ownsFd_ ? ::close(fd_) : 0;
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); }
134 void File::doLock(int op) {
135 checkUnixError(flockNoInt(fd_, op), "flock() failed (lock)");
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) return false;
142 checkUnixError(r, "flock() failed (try_lock)");
146 void File::unlock() {
147 checkUnixError(flockNoInt(fd_, LOCK_UN), "flock() failed (unlock)");
149 void File::unlock_shared() { unlock(); }