From c7854d9267a26d2847a23334377e77f764437e7d Mon Sep 17 00:00:00 2001 From: Philip Pronin Date: Thu, 26 Sep 2013 01:45:07 -0700 Subject: [PATCH] File::dup Test Plan: fbconfig -r folly && fbmake opt -j32 @override-unit-failures Reviewed By: soren@fb.com FB internal diff: D985595 --- folly/File.cpp | 13 ++++++++++++- folly/File.h | 5 +++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/folly/File.cpp b/folly/File.cpp index 3c961aaa..56d30b0c 100644 --- a/folly/File.cpp +++ b/folly/File.cpp @@ -73,7 +73,7 @@ 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); @@ -94,6 +94,17 @@ void swap(File& a, File& 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() { if (!closeNoThrow()) { throwSystemError("close() failed"); diff --git a/folly/File.h b/folly/File.h index 71fcc033..50618158 100644 --- a/folly/File.h +++ b/folly/File.h @@ -67,6 +67,11 @@ class File { 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. * Otherwise, do nothing. -- 2.34.1