From: Joel Rosdahl Date: Sat, 23 Mar 2024 15:03:34 +0000 (+0100) Subject: refactor: Let util::read_file stat fd on POSIX as well X-Git-Tag: v4.10~68 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=eef86ffef6816a50686233efaca774b336375008;p=thirdparty%2Fccache.git refactor: Let util::read_file stat fd on POSIX as well --- diff --git a/src/ccache/util/file.cpp b/src/ccache/util/file.cpp index 5ea8cbe9..2b00e82f 100644 --- a/src/ccache/util/file.cpp +++ b/src/ccache/util/file.cpp @@ -234,22 +234,6 @@ template tl::expected read_file(const fs::path& path, size_t size_hint) { -#ifndef _WIN32 - if (size_hint == 0) { - DirEntry de(path); - if (!de) { - return tl::unexpected(strerror(errno)); - } - size_hint = de.size(); - } -#endif - - if (size_hint > std::numeric_limits::max() / 4) { - // Too large value on a 32-bit system won't work well, better bail. - return tl::unexpected( - FMT("too large file: {} ({} bytes)", path, size_hint)); - } - const int open_flags = [] { if constexpr (std::is_same::value) { return O_RDONLY | O_TEXT; @@ -262,14 +246,26 @@ read_file(const fs::path& path, size_t size_hint) return tl::unexpected(strerror(errno)); } -#ifdef _WIN32 if (size_hint == 0) { +#ifdef _WIN32 LARGE_INTEGER file_size; GetFileSizeEx(reinterpret_cast(_get_osfhandle(fd.get())), &file_size); - size_hint = (size_t)file_size.QuadPart; - } + size_hint = static_cast(file_size.QuadPart); +#else + DirEntry de(path, *fd); + if (!de) { + return tl::unexpected(strerror(errno)); + } + size_hint = de.size(); #endif + } + + if (size_hint > std::numeric_limits::max() / 4) { + // Too large value on a 32-bit system won't work well, better bail. + return tl::unexpected( + FMT("too large file: {} ({} bytes)", path, size_hint)); + } int64_t ret = 0; size_t pos = 0;