projects
/
folly.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
logging: improve the AsyncFileWriter flush test()
[folly.git]
/
folly
/
File.cpp
diff --git
a/folly/File.cpp
b/folly/File.cpp
index 3c961aaae45f6dd1c2c6100a4256a6d334f5cf68..be44ce572925cb614a26d54c2b86fe86f5566e4a 100644
(file)
--- a/
folly/File.cpp
+++ b/
folly/File.cpp
@@
-1,5
+1,5
@@
/*
/*
- * Copyright 201
3
Facebook, Inc.
+ * Copyright 201
7
Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* 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.
*/
* limitations under the License.
*/
-#include
"folly/File.h"
+#include
<folly/File.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include "folly/Exception.h"
-#include "folly/FileUtil.h"
-#include "folly/Format.h"
-#include "folly/ScopeGuard.h"
+#include <folly/Exception.h>
+#include <folly/FileUtil.h>
+#include <folly/Format.h>
+#include <folly/ScopeGuard.h>
+#include <folly/portability/Fcntl.h>
+#include <folly/portability/SysFile.h>
+#include <folly/portability/Unistd.h>
#include <system_error>
#include <system_error>
@@
-30,15
+31,12
@@
namespace folly {
namespace folly {
-File::File()
- : fd_(-1)
- , ownsFd_(false)
-{}
+File::File() noexcept : fd_(-1), ownsFd_(false) {}
-File::File(int fd, bool ownsFd)
- : fd_(fd)
- , ownsFd_(ownsFd)
-
{
}
+File::File(int fd, bool ownsFd)
noexcept : fd_(fd), 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))
File::File(const char* name, int flags, mode_t mode)
: fd_(::open(name, flags, mode))
@@
-50,10
+48,15
@@
File::File(const char* name, int flags, mode_t mode)
ownsFd_ = true;
}
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_) {
: fd_(other.fd_)
, ownsFd_(other.ownsFd_) {
-
other.release();
}
other.release();
}
@@
-64,7
+67,11
@@
File& File::operator=(File&& other) {
}
File::~File() {
}
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() {
}
/* static */ File File::temporary() {
@@
-73,15
+80,17
@@
File::~File() {
checkFopenError(tmpFile, "tmpfile() failed");
SCOPE_EXIT { fclose(tmpFile); };
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);
}
checkUnixError(fd, "dup() failed");
return File(fd, true);
}
-void File::release() {
+int File::release() noexcept {
+ int released = fd_;
fd_ = -1;
ownsFd_ = false;
fd_ = -1;
ownsFd_ = false;
+ return released;
}
void File::swap(File& other) {
}
void File::swap(File& other) {
@@
-94,6
+103,17
@@
void swap(File& a, File& b) {
a.swap(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");
void File::close() {
if (!closeNoThrow()) {
throwSystemError("close() failed");