From 3f6f84461a74fb2d6d37b31e737711b5f83c2f66 Mon Sep 17 00:00:00 2001 From: EmmaZhu-MSFT Date: Fri, 15 Mar 2019 09:18:36 +0800 Subject: [PATCH] Returns int64 value in function of seeking to file end on x64 Windows. (#1057) --- Release/include/cpprest/filestream.h | 9 ++++++++- Release/src/streams/fileio_win32.cpp | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/Release/include/cpprest/filestream.h b/Release/include/cpprest/filestream.h index 39020468fc..1e4a0f278e 100644 --- a/Release/include/cpprest/filestream.h +++ b/Release/include/cpprest/filestream.h @@ -649,12 +649,19 @@ class basic_file_buffer : public details::streambuf_state_manager<_CharType> if (mode == std::ios_base::in) { m_readOps.wait(); + size_t current_pos = static_cast(-1); switch (way) { case std::ios_base::beg: return (pos_type)_seekrdpos_fsb(m_info, size_t(offset), sizeof(_CharType)); case std::ios_base::cur: return (pos_type)_seekrdpos_fsb(m_info, size_t(m_info->m_rdpos + offset), sizeof(_CharType)); - case std::ios_base::end: return (pos_type)_seekrdtoend_fsb(m_info, int64_t(offset), sizeof(_CharType)); + case std::ios_base::end: + current_pos = _seekrdtoend_fsb(m_info, int64_t(offset), sizeof(_CharType)); + if (current_pos == static_cast(-1)) + { + return -1; + } + return (pos_type)current_pos; default: // Fail on invalid input (_S_ios_seekdir_end) assert(false); diff --git a/Release/src/streams/fileio_win32.cpp b/Release/src/streams/fileio_win32.cpp index 3972a707cc..86b5bd1ddc 100644 --- a/Release/src/streams/fileio_win32.cpp +++ b/Release/src/streams/fileio_win32.cpp @@ -905,11 +905,26 @@ size_t __cdecl _seekrdtoend_fsb(_In_ streams::details::_file_info* info, int64_t fInfo->m_bufoff = fInfo->m_buffill = fInfo->m_bufsize = 0; } +#ifdef _WIN64 + LARGE_INTEGER filesize; + filesize.QuadPart = 0; + + BOOL result = GetFileSizeEx(fInfo->m_handle, &filesize); + if (FALSE == result) + { + return static_cast(-1); + } + else + { + fInfo->m_rdpos = static_cast(filesize.QuadPart) / char_size; + } +#else auto newpos = SetFilePointer(fInfo->m_handle, (LONG)(offset * char_size), nullptr, FILE_END); if (newpos == INVALID_SET_FILE_POINTER) return static_cast(-1); fInfo->m_rdpos = static_cast(newpos) / char_size; +#endif return fInfo->m_rdpos; }