fs/FileInfo: add fstat() wrapper

This commit is contained in:
Max Kellermann 2023-10-07 12:37:26 +02:00
parent 1ca5d6baa6
commit 34182990ef
3 changed files with 15 additions and 8 deletions

View File

@ -8,6 +8,8 @@
#ifdef _WIN32
#include <windef.h> // for HWND (needed by winbase.h)
#include <winbase.h> // 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

View File

@ -14,6 +14,8 @@
#include <chrono>
#include <cstdint>
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 {

View File

@ -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