diff --git a/include/fs/archive_system_save_data.hpp b/include/fs/archive_system_save_data.hpp index f4aa3f06..fcba5e73 100644 --- a/include/fs/archive_system_save_data.hpp +++ b/include/fs/archive_system_save_data.hpp @@ -13,10 +13,7 @@ class SystemSaveDataArchive : public ArchiveBase { std::string name() override { return "SystemSaveData"; } //HorizonResult createDirectory(const FSPath& path) override; - HorizonResult createFile(const FSPath& path, u64 size) override { - Helpers::panic("Unimplemented CreateFile for SystemSaveData archive"); - return Result::Success; - }; + HorizonResult createFile(const FSPath& path, u64 size) override; HorizonResult deleteFile(const FSPath& path) override { Helpers::panic("Unimplemented DeleteFile for SystemSaveData archive"); diff --git a/src/core/fs/archive_save_data.cpp b/src/core/fs/archive_save_data.cpp index aa94cdc4..8ac51b31 100644 --- a/src/core/fs/archive_save_data.cpp +++ b/src/core/fs/archive_save_data.cpp @@ -12,8 +12,9 @@ HorizonResult SaveDataArchive::createFile(const FSPath& path, u64 size) { fs::path p = IOFile::getAppData() / "SaveData"; p += fs::path(path.utf16_string).make_preferred(); - if (fs::exists(p)) + if (fs::exists(p)) { return Result::FS::AlreadyExists; + } IOFile file(p.string().c_str(), "wb"); @@ -33,7 +34,7 @@ HorizonResult SaveDataArchive::createFile(const FSPath& path, u64 size) { return Result::FS::FileTooLarge; } - Helpers::panic("SaveDataArchive::OpenFile: Failed"); + Helpers::panic("SaveDataArchive::CreateFile: Failed"); return Result::Success; } diff --git a/src/core/fs/archive_system_save_data.cpp b/src/core/fs/archive_system_save_data.cpp index fea46d2c..4f63dc63 100644 --- a/src/core/fs/archive_system_save_data.cpp +++ b/src/core/fs/archive_system_save_data.cpp @@ -47,4 +47,39 @@ FileDescriptor SystemSaveDataArchive::openFile(const FSPath& path, const FilePer Helpers::panic("SystemSaveData::OpenFile: Failed"); return FileError; +} + +HorizonResult SystemSaveDataArchive::createFile(const FSPath& path, u64 size) { + if (path.type == PathType::UTF16) { + if (!isPathSafe(path)) { + Helpers::panic("Unsafe path in SystemSaveData::CreateFile"); + } + + fs::path p = IOFile::getAppData() / ".." / "SharedFiles" / "SystemSaveData"; + p += fs::path(path.utf16_string).make_preferred(); + + if (fs::exists(p)) { + return Result::FS::AlreadyExists; + } + + IOFile file(p.string().c_str(), "wb"); + + // If the size is 0, leave the file empty and return success + if (size == 0) { + file.close(); + return Result::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) { + file.close(); + return Result::Success; + } + + file.close(); + return Result::FS::FileTooLarge; + } + + Helpers::panic("SystemSaveData::CreateFile: Failed"); + return Result::Success; } \ No newline at end of file