Commit c7854d92 authored by Philip Pronin's avatar Philip Pronin Committed by Peter Griess

File::dup

Test Plan: fbconfig -r folly && fbmake opt -j32

@override-unit-failures

Reviewed By: soren@fb.com

FB internal diff: D985595
parent 2c3828bc
...@@ -73,7 +73,7 @@ File::~File() { ...@@ -73,7 +73,7 @@ File::~File() {
checkFopenError(tmpFile, "tmpfile() failed"); checkFopenError(tmpFile, "tmpfile() failed");
SCOPE_EXIT { fclose(tmpFile); }; SCOPE_EXIT { fclose(tmpFile); };
int fd = dup(fileno(tmpFile)); int fd = ::dup(fileno(tmpFile));
checkUnixError(fd, "dup() failed"); checkUnixError(fd, "dup() failed");
return File(fd, true); return File(fd, true);
...@@ -94,6 +94,17 @@ void swap(File& a, File& b) { ...@@ -94,6 +94,17 @@ void swap(File& a, File& b) {
a.swap(b); a.swap(b);
} }
File File::dup() const {
if (fd_ >= 0) {
int fd = ::dup(fd_);
checkUnixError(fd, "dup() failed");
return File(fd, true);
}
return File();
}
void File::close() { void File::close() {
if (!closeNoThrow()) { if (!closeNoThrow()) {
throwSystemError("close() failed"); throwSystemError("close() failed");
......
...@@ -67,6 +67,11 @@ class File { ...@@ -67,6 +67,11 @@ class File {
return fd_ >= 0; return fd_ >= 0;
} }
/**
* Duplicate file descriptor and return File that owns it.
*/
File dup() const;
/** /**
* If we own the file descriptor, close the file and throw on error. * If we own the file descriptor, close the file and throw on error.
* Otherwise, do nothing. * Otherwise, do nothing.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment