2 * Copyright 2013 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"
23 #include "folly/Format.h"
24 #include "folly/Exception.h"
25 #include "folly/ScopeGuard.h"
27 #include <system_error>
29 #include <glog/logging.h>
38 File::File(int fd, bool ownsFd)
43 File::File(const char* name, int flags, mode_t mode)
44 : fd_(::open(name, flags, mode))
47 throwSystemError(folly::format("open(\"{}\", {:#o}, 0{:#o}) failed",
48 name, flags, mode).fbstr());
53 File::File(File&& other)
55 , ownsFd_(other.ownsFd_) {
60 File& File::operator=(File&& other) {
67 closeNoThrow(); // ignore error
70 /* static */ File File::temporary() {
71 // make a temp file with tmpfile(), dup the fd, then return it in a File.
72 FILE* tmpFile = tmpfile();
73 checkFopenError(tmpFile, "tmpfile() failed");
74 SCOPE_EXIT { fclose(tmpFile); };
76 int fd = dup(fileno(tmpFile));
77 checkUnixError(fd, "dup() failed");
79 return File(fd, true);
82 void File::release() {
87 void File::swap(File& other) {
90 swap(ownsFd_, other.ownsFd_);
93 void swap(File& a, File& b) {
98 if (!closeNoThrow()) {
99 throwSystemError("close() failed");
103 bool File::closeNoThrow() {
104 int r = ownsFd_ ? ::close(fd_) : 0;
109 void File::lock() { doLock(LOCK_EX); }
110 bool File::try_lock() { return doTryLock(LOCK_EX); }
111 void File::lock_shared() { doLock(LOCK_SH); }
112 bool File::try_lock_shared() { return doTryLock(LOCK_SH); }
114 void File::doLock(int op) {
115 checkUnixError(flock(fd_, op), "flock() failed (lock)");
118 bool File::doTryLock(int op) {
119 int r = flock(fd_, op | LOCK_NB);
120 // flock returns EWOULDBLOCK if already locked
121 if (r == -1 && errno == EWOULDBLOCK) return false;
122 checkUnixError(r, "flock() failed (try_lock)");
126 void File::unlock() {
127 checkUnixError(flock(fd_, LOCK_UN), "flock() failed (unlock)");
129 void File::unlock_shared() { unlock(); }