diff options
| author | Dan Engelbrecht <[email protected]> | 2022-04-01 23:46:04 +0200 |
|---|---|---|
| committer | Dan Engelbrecht <[email protected]> | 2022-04-01 23:46:04 +0200 |
| commit | 6e9f6df8e4d51852327d05b254813b7b792f40a8 (patch) | |
| tree | 743f941b39711bc5a4a3e85d6c11051460d99d81 /zenstore/basicfile.cpp | |
| parent | use std::unsigned_integral for ToHexNumber and ParseHexNumber (diff) | |
| download | zen-6e9f6df8e4d51852327d05b254813b7b792f40a8.tar.xz zen-6e9f6df8e4d51852327d05b254813b7b792f40a8.zip | |
error handling in BasicFile::SetFileSize
Diffstat (limited to 'zenstore/basicfile.cpp')
| -rw-r--r-- | zenstore/basicfile.cpp | 34 |
1 files changed, 28 insertions, 6 deletions
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 } |