aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Engelbrecht <[email protected]>2022-04-02 00:08:26 +0200
committerDan Engelbrecht <[email protected]>2022-04-02 00:08:26 +0200
commitc2603f19ae2ac57b68eb8d6187cda668c70f2b2b (patch)
treedfadb017062226a0df0e5e24f4b8c2e92be547dc
parentrename EMode to Mode (diff)
downloadzen-c2603f19ae2ac57b68eb8d6187cda668c70f2b2b.tar.xz
zen-c2603f19ae2ac57b68eb8d6187cda668c70f2b2b.zip
proper error handling when setting file size
-rw-r--r--zenstore/basicfile.cpp32
-rw-r--r--zenstore/compactcas.cpp2
-rw-r--r--zenstore/gc.cpp14
3 files changed, 32 insertions, 16 deletions
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)
diff --git a/zenstore/compactcas.cpp b/zenstore/compactcas.cpp
index 36c9df26f..c266d4994 100644
--- a/zenstore/compactcas.cpp
+++ b/zenstore/compactcas.cpp
@@ -346,7 +346,7 @@ namespace {
}
BasicFile BlockFile;
- BlockFile.Open(LegacySobsPath, BasicFile::Mode::kRead);
+ BlockFile.Open(LegacySobsPath, CleanSource ? BasicFile::Mode::kWrite : BasicFile::Mode::kRead);
std::unordered_map<IoHash, LegacyCasDiskIndexEntry, IoHash::Hasher> LegacyDiskIndex;
diff --git a/zenstore/gc.cpp b/zenstore/gc.cpp
index 117fe3b49..311f7ced4 100644
--- a/zenstore/gc.cpp
+++ b/zenstore/gc.cpp
@@ -73,7 +73,7 @@ namespace {
if (FileHandle == INVALID_HANDLE_VALUE)
{
- return zen::MakeErrorCodeFromLastError();
+ return MakeErrorCodeFromLastError();
}
bool Keep = true;
auto _ = MakeGuard([FileHandle, &Keep, Path]() {
@@ -88,12 +88,12 @@ namespace {
BOOL OK = ::SetFilePointerEx(FileHandle, liFileSize, 0, FILE_BEGIN);
if (!OK)
{
- return zen::MakeErrorCodeFromLastError();
+ return MakeErrorCodeFromLastError();
}
OK = ::SetEndOfFile(FileHandle);
if (!OK)
{
- return zen::MakeErrorCodeFromLastError();
+ return MakeErrorCodeFromLastError();
}
Keep = true;
#else
@@ -101,7 +101,7 @@ namespace {
int Fd = open(Path.c_str(), OpenFlags, 0666);
if (Fd < 0)
{
- return zen::MakeErrorCodeFromLastError();
+ return MakeErrorCodeFromLastError();
}
bool Keep = true;
@@ -115,13 +115,13 @@ namespace {
if (fchmod(Fd, 0666) < 0)
{
- return zen::MakeErrorCodeFromLastError();
+ return MakeErrorCodeFromLastError();
}
# if ZEN_PLATFORM_MAC
if (ftruncate(Fd, (off_t)Size) < 0)
{
- return zen::MakeErrorCodeFromLastError();
+ return MakeErrorCodeFromLastError();
}
std::error_code Ec = posix_fallocate(Fd, 0, (off_t)Size);
if (Ec)
@@ -131,7 +131,7 @@ namespace {
# else
if (ftruncate64(Fd, (off64_t)Size) < 0)
{
- return zen::MakeErrorCodeFromLastError();
+ return MakeErrorCodeFromLastError();
}
std::error_code Ec = posix_fallocate64(Fd, 0, (off64_t)Size);
if (Ec)