From 34182990ef73d295ce1e11cb7a7398cfca03366a Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sat, 7 Oct 2023 12:37:26 +0200 Subject: [PATCH] fs/FileInfo: add fstat() wrapper --- src/fs/FileInfo.cxx | 10 ++++++++++ src/fs/FileInfo.hxx | 6 ++++-- src/io/FileReader.cxx | 7 +------ 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/fs/FileInfo.cxx b/src/fs/FileInfo.cxx index 86b6904a57..dcfb67c153 100644 --- a/src/fs/FileInfo.cxx +++ b/src/fs/FileInfo.cxx @@ -8,6 +8,8 @@ #ifdef _WIN32 #include // for HWND (needed by winbase.h) #include // for FILE_*_INFO +#else +#include "io/FileDescriptor.hxx" #endif FileInfo::FileInfo(Path path, bool follow_symlinks) @@ -55,4 +57,12 @@ GetFileInfoByHandle(HANDLE handle, FileInfo &info) noexcept return true; } +#else + +FileInfo::FileInfo(FileDescriptor fd) +{ + if (fstat(fd.Get(), &st) < 0) + throw MakeErrno("Failed to access file"); +} + #endif // _WIN32 diff --git a/src/fs/FileInfo.hxx b/src/fs/FileInfo.hxx index 25ff45a91a..e40094744c 100644 --- a/src/fs/FileInfo.hxx +++ b/src/fs/FileInfo.hxx @@ -14,6 +14,8 @@ #include #include +class FileDescriptor; + class FileInfo { friend bool GetFileInfo(Path path, FileInfo &info, bool follow_symlinks) noexcept; @@ -21,8 +23,6 @@ class FileInfo { friend bool GetFileInfoByHandle(HANDLE handle, FileInfo &info) noexcept; #endif - friend class FileReader; - #ifdef _WIN32 WIN32_FILE_ATTRIBUTE_DATA data; #else @@ -36,6 +36,8 @@ public: #ifdef _WIN32 explicit FileInfo(HANDLE handle); +#else + explicit FileInfo(FileDescriptor fd); #endif constexpr bool IsRegular() const noexcept { diff --git a/src/io/FileReader.cxx b/src/io/FileReader.cxx index 0d72e57ba3..ed2ba38abb 100644 --- a/src/io/FileReader.cxx +++ b/src/io/FileReader.cxx @@ -73,12 +73,7 @@ FileReader::GetFileInfo() const { assert(IsDefined()); - FileInfo info; - const bool success = fstat(fd.Get(), &info.st) == 0; - if (!success) - throw MakeErrno("Failed to access file"); - - return info; + return FileInfo{fd}; } std::size_t