X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FFile.cpp;h=4df3274eb86c3f77fa3280abb49303bbee9da9f0;hb=2642bd3dcf9658be7da3e5b5bc622fe051200e97;hp=aaf109f07aad557b439392f285818b8652d17a06;hpb=d1028d196481191a6e3adf1b081f736b89c52cfa;p=folly.git diff --git a/folly/File.cpp b/folly/File.cpp index aaf109f0..4df3274e 100644 --- a/folly/File.cpp +++ b/folly/File.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2013 Facebook, Inc. + * Copyright 2017 Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,15 +14,16 @@ * limitations under the License. */ -#include "folly/File.h" +#include -#include -#include -#include -#include "folly/Format.h" -#include "folly/Exception.h" -#include "folly/ScopeGuard.h" +#include +#include +#include +#include +#include +#include +#include #include @@ -37,8 +38,10 @@ File::File() File::File(int fd, bool ownsFd) : fd_(fd) - , ownsFd_(ownsFd) -{} + , ownsFd_(ownsFd) { + CHECK_GE(fd, -1) << "fd must be -1 or non-negative"; + CHECK(fd != -1 || !ownsFd) << "cannot own -1"; +} File::File(const char* name, int flags, mode_t mode) : fd_(::open(name, flags, mode)) @@ -50,10 +53,15 @@ File::File(const char* name, int flags, mode_t mode) ownsFd_ = true; } -File::File(File&& other) +File::File(const std::string& name, int flags, mode_t mode) + : File(name.c_str(), flags, mode) {} + +File::File(StringPiece name, int flags, mode_t mode) + : File(name.str(), flags, mode) {} + +File::File(File&& other) noexcept : fd_(other.fd_) , ownsFd_(other.ownsFd_) { - other.release(); } @@ -64,7 +72,11 @@ File& File::operator=(File&& other) { } File::~File() { - closeNoThrow(); // ignore error + auto fd = fd_; + if (!closeNoThrow()) { // ignore most errors + DCHECK_NE(errno, EBADF) << "closing fd " << fd << ", it may already " + << "have been closed. Another time, this might close the wrong FD."; + } } /* static */ File File::temporary() { @@ -73,15 +85,17 @@ File::~File() { checkFopenError(tmpFile, "tmpfile() failed"); SCOPE_EXIT { fclose(tmpFile); }; - int fd = dup(fileno(tmpFile)); + int fd = ::dup(fileno(tmpFile)); checkUnixError(fd, "dup() failed"); return File(fd, true); } -void File::release() { +int File::release() noexcept { + int released = fd_; fd_ = -1; ownsFd_ = false; + return released; } void File::swap(File& other) { @@ -94,6 +108,17 @@ void swap(File& a, File& b) { a.swap(b); } +File File::dup() const { + if (fd_ != -1) { + int fd = ::dup(fd_); + checkUnixError(fd, "dup() failed"); + + return File(fd, true); + } + + return File(); +} + void File::close() { if (!closeNoThrow()) { throwSystemError("close() failed"); @@ -112,11 +137,11 @@ void File::lock_shared() { doLock(LOCK_SH); } bool File::try_lock_shared() { return doTryLock(LOCK_SH); } void File::doLock(int op) { - checkUnixError(flock(fd_, op), "flock() failed (lock)"); + checkUnixError(flockNoInt(fd_, op), "flock() failed (lock)"); } bool File::doTryLock(int op) { - int r = flock(fd_, op | LOCK_NB); + int r = flockNoInt(fd_, op | LOCK_NB); // flock returns EWOULDBLOCK if already locked if (r == -1 && errno == EWOULDBLOCK) return false; checkUnixError(r, "flock() failed (try_lock)"); @@ -124,7 +149,7 @@ bool File::doTryLock(int op) { } void File::unlock() { - checkUnixError(flock(fd_, LOCK_UN), "flock() failed (unlock)"); + checkUnixError(flockNoInt(fd_, LOCK_UN), "flock() failed (unlock)"); } void File::unlock_shared() { unlock(); }