From d310f3e9b7d42fe389c142184fa77043b9936af3 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 10 Aug 2017 11:52:22 +0200 Subject: [PATCH] system/FileDescriptor: add method IsValid() --- src/system/FileDescriptor.cxx | 10 ++++++++++ src/system/FileDescriptor.hxx | 8 ++++++++ 2 files changed, 18 insertions(+) diff --git a/src/system/FileDescriptor.cxx b/src/system/FileDescriptor.cxx index e570090c4..fc3d238f3 100644 --- a/src/system/FileDescriptor.cxx +++ b/src/system/FileDescriptor.cxx @@ -57,6 +57,16 @@ #define O_CLOEXEC 0 #endif +#ifndef _WIN32 + +bool +FileDescriptor::IsValid() const noexcept +{ + return IsDefined() && fcntl(fd, F_GETFL) >= 0; +} + +#endif + bool FileDescriptor::Open(const char *pathname, int flags, mode_t mode) noexcept { diff --git a/src/system/FileDescriptor.hxx b/src/system/FileDescriptor.hxx index 3f29073ef..f4b5f4548 100644 --- a/src/system/FileDescriptor.hxx +++ b/src/system/FileDescriptor.hxx @@ -64,6 +64,14 @@ public: return fd >= 0; } +#ifndef _WIN32 + /** + * Ask the kernel whether this is a valid file descriptor. + */ + gcc_pure + bool IsValid() const noexcept; +#endif + /** * Returns the file descriptor. This may only be called if * IsDefined() returns true.