From 5fa4e9e1789db9ea2a97d4d3db6ca0e012cd9b55 Mon Sep 17 00:00:00 2001 From: wheremyfoodat <44909372+wheremyfoodat@users.noreply.github.com> Date: Sun, 11 Jun 2023 12:31:42 +0300 Subject: [PATCH] [SaveData] Handle size == 0 in CreateFile --- src/core/fs/archive_save_data.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/core/fs/archive_save_data.cpp b/src/core/fs/archive_save_data.cpp index 5e020fbb..572d22e0 100644 --- a/src/core/fs/archive_save_data.cpp +++ b/src/core/fs/archive_save_data.cpp @@ -5,9 +5,6 @@ namespace fs = std::filesystem; FSResult SaveDataArchive::createFile(const FSPath& path, u64 size) { - if (size == 0) - Helpers::panic("SaveData file does not support size == 0"); - if (path.type == PathType::UTF16) { if (!isPathSafe(path)) Helpers::panic("Unsafe path in SaveData::CreateFile"); @@ -17,10 +14,16 @@ FSResult SaveDataArchive::createFile(const FSPath& path, u64 size) { if (fs::exists(p)) return FSResult::AlreadyExists; - - // Create a file of size "size" by creating an empty one, seeking to size - 1 and just writing a 0 there + IOFile file(p.string().c_str(), "wb"); - if (file.seek(size - 1, SEEK_SET) && file.writeBytes("", 1).second == 1) { + + // If the size is 0, leave the file empty and return success + if (size == 0) { + return FSResult::Success; + } + + // If it is not empty, seek to size - 1 and write a 0 to create a file of size "size" + else if (file.seek(size - 1, SEEK_SET) && file.writeBytes("", 1).second == 1) { return FSResult::Success; } @@ -192,4 +195,4 @@ Rust::Result SaveDataArchive::openArchive(const FSPath& std::optional SaveDataArchive::readFile(FileSession* file, u64 offset, u32 size, u32 dataPointer) { Helpers::panic("Unimplemented SaveData::ReadFile"); return 0; -} \ No newline at end of file +}