File::dup
authorPhilip Pronin <philipp@fb.com>
Thu, 26 Sep 2013 08:45:07 +0000 (01:45 -0700)
committerPeter Griess <pgriess@fb.com>
Tue, 15 Oct 2013 01:43:34 +0000 (18:43 -0700)
Test Plan: fbconfig -r folly && fbmake opt -j32

@override-unit-failures

Reviewed By: soren@fb.com

FB internal diff: D985595

folly/File.cpp
folly/File.h

index 3c961aaae45f6dd1c2c6100a4256a6d334f5cf68..56d30b0c8ae7718f40bb4814dc0901e46e6cfe9e 100644 (file)
@@ -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");
index 71fcc03361de7e1da64d7a2d4a947c79e66a461d..506181580844857efde6957b1ac102236b18a1f2 100644 (file)
@@ -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.