diff --git a/src/io/FileDescriptor.cxx b/src/io/FileDescriptor.cxx index 51a4e7449..f8dd37b31 100644 --- a/src/io/FileDescriptor.cxx +++ b/src/io/FileDescriptor.cxx @@ -28,6 +28,7 @@ */ #include "FileDescriptor.hxx" +#include "UniqueFileDescriptor.hxx" #include "system/Error.hxx" #include @@ -234,6 +235,12 @@ FileDescriptor::DisableCloseOnExec() noexcept fcntl(fd, F_SETFD, old_flags & ~FD_CLOEXEC); } +UniqueFileDescriptor +FileDescriptor::Duplicate() const noexcept +{ + return UniqueFileDescriptor{::dup(Get())}; +} + bool FileDescriptor::CheckDuplicate(FileDescriptor new_fd) noexcept { diff --git a/src/io/FileDescriptor.hxx b/src/io/FileDescriptor.hxx index c050bfc1c..acc6778a3 100644 --- a/src/io/FileDescriptor.hxx +++ b/src/io/FileDescriptor.hxx @@ -44,6 +44,8 @@ #include #endif +class UniqueFileDescriptor; + /** * An OO wrapper for a UNIX file descriptor. * @@ -176,6 +178,14 @@ public: */ void DisableCloseOnExec() noexcept; + /** + * Duplicate this file descriptor. + * + * @return the new file descriptor or UniqueFileDescriptor{} + * on error + */ + UniqueFileDescriptor Duplicate() const noexcept; + /** * Duplicate the file descriptor onto the given file descriptor. */