From d036e2082679120dfaf4d670106aa29b585da8f6 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 21 Aug 2018 18:15:37 +0200 Subject: [PATCH] system/FileDescriptor: add openat() wrapper --- src/system/FileDescriptor.cxx | 12 ++++++++++++ src/system/FileDescriptor.hxx | 5 +++++ 2 files changed, 17 insertions(+) diff --git a/src/system/FileDescriptor.cxx b/src/system/FileDescriptor.cxx index a6e176166..f2516b872 100644 --- a/src/system/FileDescriptor.cxx +++ b/src/system/FileDescriptor.cxx @@ -76,6 +76,18 @@ FileDescriptor::IsSocket() const noexcept #endif +#ifdef __linux + +bool +FileDescriptor::Open(FileDescriptor dir, const char *pathname, + int flags, mode_t mode) noexcept +{ + fd = ::openat(dir.Get(), pathname, flags | O_NOCTTY | O_CLOEXEC, mode); + return IsDefined(); +} + +#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 61b8a0aa4..e3176df60 100644 --- a/src/system/FileDescriptor.hxx +++ b/src/system/FileDescriptor.hxx @@ -116,6 +116,11 @@ public: return FileDescriptor(-1); } +#ifdef __linux + bool Open(FileDescriptor dir, const char *pathname, + int flags, mode_t mode=0666) noexcept; +#endif + bool Open(const char *pathname, int flags, mode_t mode=0666) noexcept; #ifdef _WIN32