From c2603f19ae2ac57b68eb8d6187cda668c70f2b2b Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Sat, 2 Apr 2022 00:08:26 +0200 Subject: proper error handling when setting file size --- zenstore/basicfile.cpp | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) (limited to 'zenstore/basicfile.cpp') diff --git a/zenstore/basicfile.cpp b/zenstore/basicfile.cpp index f518e0491..731aacc69 100644 --- a/zenstore/basicfile.cpp +++ b/zenstore/basicfile.cpp @@ -86,7 +86,7 @@ BasicFile::Open(const std::filesystem::path& FileName, Mode Mode, std::error_cod if (FileHandle == INVALID_HANDLE_VALUE) { - Ec = zen::MakeErrorCodeFromLastError(); + Ec = MakeErrorCodeFromLastError(); return; } @@ -110,7 +110,7 @@ BasicFile::Open(const std::filesystem::path& FileName, Mode Mode, std::error_cod int Fd = open(FileName.c_str(), OpenFlags, 0666); if (Fd < 0) { - Ec = zen::MakeErrorCodeFromLastError(); + Ec = MakeErrorCodeFromLastError(); return; } if (Mode != Mode::kRead) @@ -302,10 +302,10 @@ BasicFile::FileSize() liFileSize.LowPart = ::GetFileSize(m_FileHandle, &liFileSize.HighPart); if (liFileSize.LowPart == INVALID_FILE_SIZE) { - int Error = GetLastError(); + int Error = zen::GetLastError(); if (Error) { - ThrowSystemError(Error, fmt::format("Failed to get file size from file '{}'", zen::PathFromHandle(m_FileHandle))); + ThrowSystemError(Error, fmt::format("Failed to get file size from file '{}'", PathFromHandle(m_FileHandle))); } } return uint64_t(liFileSize.QuadPart); @@ -327,18 +327,30 @@ BasicFile::SetFileSize(uint64_t FileSize) BOOL OK = ::SetFilePointerEx(m_FileHandle, liFileSize, 0, FILE_BEGIN); if (OK == FALSE) { - ThrowLastError(fmt::format("Failed to set file pointer to {} for file {}", FileSize, PathFromHandle(m_FileHandle))); + int Error = zen::GetLastError(); + if (Error) + { + ThrowSystemError(Error, fmt::format("Failed to set file pointer to {} for file {}", FileSize, PathFromHandle(m_FileHandle))); + } } OK = ::SetEndOfFile(m_FileHandle); if (OK == FALSE) { - ThrowLastError(fmt::format("Failed to set end of file to {} for file {}", FileSize, PathFromHandle(m_FileHandle))); + int Error = zen::GetLastError(); + if (Error) + { + ThrowSystemError(Error, fmt::format("Failed to set end of file to {} for file {}", FileSize, PathFromHandle(m_FileHandle))); + } } #elif ZEN_PLATFORM_MAC int Fd = int(intptr_t(m_FileHandle)); if (ftruncate(Fd, (off_t)FileSize) < 0) { - ThrowLastError(fmt::format("Failed to set truncate file to {} for file {}", FileSize, PathFromHandle(m_FileHandle))); + int Error = zen::GetLastError(); + if (Error) + { + ThrowSystemError(Error, fmt::format("Failed to set truncate file to {} for file {}", FileSize, PathFromHandle(m_FileHandle))); + } } std::error_code Ec = posix_fallocate(Fd, 0, (off_t)FileSize); if (Ec) @@ -349,7 +361,11 @@ BasicFile::SetFileSize(uint64_t FileSize) int Fd = int(intptr_t(m_FileHandle)); if (ftruncate64(Fd, (off64_t)FileSize) < 0) { - ThrowLastError(fmt::format("Failed to set truncate file to {} for file {}", FileSize, PathFromHandle(m_FileHandle))); + int Error = zen::GetLastError(); + if (Error) + { + ThrowSystemError(Error, fmt::format("Failed to set truncate file to {} for file {}", FileSize, PathFromHandle(m_FileHandle))); + } } std::error_code Ec = posix_fallocate64(Fd, 0, (off64_t)FileSize); if (Ec) -- cgit v1.2.3