fs/FileInfo: add GetFileInformationByHandleEx() wrapper

This commit is contained in:
Max Kellermann 2023-10-07 12:11:38 +02:00
parent e9ec03f769
commit 7b5f107341
2 changed files with 56 additions and 0 deletions

View File

@ -5,6 +5,11 @@
#include "lib/fmt/PathFormatter.hxx"
#include "lib/fmt/SystemError.hxx"
#ifdef _WIN32
#include <windef.h> // for HWND (needed by winbase.h)
#include <winbase.h> // for FILE_*_INFO
#endif
FileInfo::FileInfo(Path path, bool follow_symlinks)
{
if (!GetFileInfo(path, *this, follow_symlinks)) {
@ -15,3 +20,39 @@ FileInfo::FileInfo(Path path, bool follow_symlinks)
#endif
}
}
#ifdef _WIN32
FileInfo::FileInfo(HANDLE handle)
{
if (!GetFileInfoByHandle(handle, *this))
throw MakeLastError("Failed to access file");
}
bool
GetFileInfoByHandle(HANDLE handle, FileInfo &info) noexcept
{
FILE_BASIC_INFO basic;
FILE_STANDARD_INFO standard;
if (!GetFileInformationByHandleEx(handle, FileBasicInfo,
&basic, sizeof(basic)) ||
!GetFileInformationByHandleEx(handle, FileStandardInfo,
&standard, sizeof(standard)))
return false;
info.data.dwFileAttributes = basic.FileAttributes;
info.data.ftCreationTime.dwLowDateTime = basic.CreationTime.LowPart;
info.data.ftCreationTime.dwHighDateTime = basic.CreationTime.HighPart;
info.data.ftLastAccessTime.dwLowDateTime = basic.LastAccessTime.LowPart;
info.data.ftLastAccessTime.dwHighDateTime = basic.LastAccessTime.HighPart;
info.data.ftLastWriteTime.dwLowDateTime = basic.LastWriteTime.LowPart;
info.data.ftLastWriteTime.dwHighDateTime = basic.LastWriteTime.HighPart;
info.data.nFileSizeLow = standard.EndOfFile.LowPart;
info.data.nFileSizeHigh = standard.EndOfFile.HighPart;
return true;
}
#endif // _WIN32

View File

@ -17,6 +17,10 @@
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
@ -30,6 +34,10 @@ public:
explicit FileInfo(Path path, bool follow_symlinks=true);
#ifdef _WIN32
explicit FileInfo(HANDLE handle);
#endif
constexpr bool IsRegular() const noexcept {
#ifdef _WIN32
return (data.dwFileAttributes &
@ -97,3 +105,10 @@ GetFileInfo(Path path, FileInfo &info, bool follow_symlinks=true) noexcept
return ret == 0;
#endif
}
#ifdef _WIN32
bool
GetFileInfoByHandle(HANDLE handle, FileInfo &info) noexcept;
#endif