From eba0794b4faa29a1147533668979bd903f390cdd Mon Sep 17 00:00:00 2001 From: Stenzek Date: Sat, 6 Jul 2024 17:46:05 +1000 Subject: [PATCH] FileSystem: Add Error to file writing --- src/common/file_system.cpp | 14 ++++++++++---- src/common/file_system.h | 4 ++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/common/file_system.cpp b/src/common/file_system.cpp index 377ad45d6..c94caef15 100644 --- a/src/common/file_system.cpp +++ b/src/common/file_system.cpp @@ -1181,26 +1181,32 @@ std::optional FileSystem::ReadFileToString(std::FILE* fp) return res; } -bool FileSystem::WriteBinaryFile(const char* filename, const void* data, size_t data_length) +bool FileSystem::WriteBinaryFile(const char* filename, const void* data, size_t data_length, Error* error) { - ManagedCFilePtr fp = OpenManagedCFile(filename, "wb"); + ManagedCFilePtr fp = OpenManagedCFile(filename, "wb", error); if (!fp) return false; if (data_length > 0 && std::fwrite(data, 1u, data_length, fp.get()) != data_length) + { + Error::SetErrno(error, "fwrite() failed: ", errno); return false; + } return true; } -bool FileSystem::WriteStringToFile(const char* filename, std::string_view sv) +bool FileSystem::WriteStringToFile(const char* filename, std::string_view sv, Error* error) { - ManagedCFilePtr fp = OpenManagedCFile(filename, "wb"); + ManagedCFilePtr fp = OpenManagedCFile(filename, "wb", error); if (!fp) return false; if (sv.length() > 0 && std::fwrite(sv.data(), 1u, sv.length(), fp.get()) != sv.length()) + { + Error::SetErrno(error, "fwrite() failed: ", errno); return false; + } return true; } diff --git a/src/common/file_system.h b/src/common/file_system.h index 56c76269d..9175d217a 100644 --- a/src/common/file_system.h +++ b/src/common/file_system.h @@ -145,8 +145,8 @@ std::optional> ReadBinaryFile(const char* filename, Error* error std::optional> ReadBinaryFile(std::FILE* fp); std::optional ReadFileToString(const char* filename, Error* error = nullptr); std::optional ReadFileToString(std::FILE* fp); -bool WriteBinaryFile(const char* filename, const void* data, size_t data_length); -bool WriteStringToFile(const char* filename, std::string_view sv); +bool WriteBinaryFile(const char* filename, const void* data, size_t data_length, Error* error = nullptr); +bool WriteStringToFile(const char* filename, std::string_view sv, Error* error = nullptr); /// creates a directory in the local filesystem /// if the directory already exists, the return value will be true.