From: Philip Pronin Date: Thu, 26 Sep 2013 08:45:07 +0000 (-0700) Subject: File::dup X-Git-Tag: v0.22.0~864 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=c7854d9267a26d2847a23334377e77f764437e7d;p=folly.git File::dup Test Plan: fbconfig -r folly && fbmake opt -j32 @override-unit-failures Reviewed By: soren@fb.com FB internal diff: D985595 --- 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.