From 4c2434788f2473953b833695aa4ab5e029f25159 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 24 Aug 2018 18:52:00 +0200 Subject: [PATCH] system/FileDescriptor: add method IsRegularFile() --- src/system/FileDescriptor.cxx | 7 +++++++ src/system/FileDescriptor.hxx | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/src/system/FileDescriptor.cxx b/src/system/FileDescriptor.cxx index 51a57512b..1f6bf0808 100644 --- a/src/system/FileDescriptor.cxx +++ b/src/system/FileDescriptor.cxx @@ -59,6 +59,13 @@ FileDescriptor::IsValid() const noexcept return IsDefined() && fcntl(fd, F_GETFL) >= 0; } +bool +FileDescriptor::IsRegularFile() const noexcept +{ + struct stat st; + return IsDefined() && fstat(fd, &st) == 0 && S_ISREG(st.st_mode); +} + bool FileDescriptor::IsPipe() const noexcept { diff --git a/src/system/FileDescriptor.hxx b/src/system/FileDescriptor.hxx index 11e32caf3..92cdddce6 100644 --- a/src/system/FileDescriptor.hxx +++ b/src/system/FileDescriptor.hxx @@ -78,6 +78,12 @@ public: gcc_pure bool IsValid() const noexcept; + /** + * Ask the kernel whether this is a regular file. + */ + gcc_pure + bool IsRegularFile() const noexcept; + /** * Ask the kernel whether this is a pipe. */