From 899eaa330712c9113086341d5d60c719bfc283af Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 24 Jun 2022 14:21:59 +0200 Subject: [PATCH] io/FileDescriptor: add Duplicate() returning UniqueFileDescriptor --- src/io/FileDescriptor.cxx | 7 +++++++ src/io/FileDescriptor.hxx | 10 ++++++++++ 2 files changed, 17 insertions(+) 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. */