From 6e9f6df8e4d51852327d05b254813b7b792f40a8 Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Fri, 1 Apr 2022 23:46:04 +0200 Subject: error handling in BasicFile::SetFileSize --- zenstore/basicfile.cpp | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) (limited to 'zenstore/basicfile.cpp') diff --git a/zenstore/basicfile.cpp b/zenstore/basicfile.cpp index 8b3ee2844..eb93638f2 100644 --- a/zenstore/basicfile.cpp +++ b/zenstore/basicfile.cpp @@ -324,16 +324,38 @@ BasicFile::SetFileSize(uint64_t FileSize) #if ZEN_PLATFORM_WINDOWS LARGE_INTEGER liFileSize; liFileSize.QuadPart = FileSize; - ::SetFilePointerEx(m_FileHandle, liFileSize, 0, FILE_BEGIN); - ::SetEndOfFile(m_FileHandle); + 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))); + } + OK = ::SetEndOfFile(m_FileHandle); + if (OK == FALSE) + { + ThrowLastError(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)); - ftruncate(Fd, (off_t)FileSize); - posix_fallocate(Fd, 0, (off_t)FileSize); + if (ftruncate(Fd, (off_t)FileSize) < 0) + { + ThrowLastError(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) + { + ThrowSystemError(Ec, fmt::format("Failed to allocate space of {} for file {}", FileSize, PathFromHandle(m_FileHandle))) + } #else int Fd = int(intptr_t(m_FileHandle)); - ftruncate64(Fd, (off64_t)FileSize); - posix_fallocate64(Fd, 0, (off64_t)FileSize); + if (ftruncate64(Fd, (off64_t)FileSize) < 0) + { + ThrowLastError(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) + { + ThrowSystemError(Ec, fmt::format("Failed to allocate space of {} for file {}", FileSize, PathFromHandle(m_FileHandle))) + } #endif } -- cgit v1.2.3