Skip to content

Commit

Permalink
fs/FileInfo: add fstat() wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Oct 7, 2023
1 parent 1ca5d6b commit 3418299
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
10 changes: 10 additions & 0 deletions src/fs/FileInfo.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
6 changes: 4 additions & 2 deletions src/fs/FileInfo.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
#include <chrono>
#include <cstdint>

class FileDescriptor;

class FileInfo {
friend bool GetFileInfo(Path path, FileInfo &info,
bool follow_symlinks) noexcept;
#ifdef _WIN32
friend bool GetFileInfoByHandle(HANDLE handle, FileInfo &info) noexcept;
#endif

friend class FileReader;

#ifdef _WIN32
WIN32_FILE_ATTRIBUTE_DATA data;
#else
Expand All @@ -36,6 +36,8 @@ public:

#ifdef _WIN32
explicit FileInfo(HANDLE handle);
#else
explicit FileInfo(FileDescriptor fd);
#endif

constexpr bool IsRegular() const noexcept {
Expand Down
7 changes: 1 addition & 6 deletions src/io/FileReader.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3418299

Please sign in to comment.