Merge pull request #20 from Wunkolo/savedata-create

Implement `SaveDataArchive::createFile`
This commit is contained in:
wheremyfoodat 2023-06-11 12:51:42 +03:00 committed by GitHub
commit f2b67a2757
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -5,7 +5,32 @@
namespace fs = std::filesystem;
FSResult SaveDataArchive::createFile(const FSPath& path, u64 size) {
Helpers::panic("[SaveData] CreateFile not yet supported");
if (path.type == PathType::UTF16) {
if (!isPathSafe<PathType::UTF16>(path))
Helpers::panic("Unsafe path in SaveData::CreateFile");
fs::path p = IOFile::getAppData() / "SaveData";
p += fs::path(path.utf16_string).make_preferred();
if (fs::exists(p))
return FSResult::AlreadyExists;
IOFile file(p.string().c_str(), "wb");
// 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;
}
return FSResult::FileTooLarge;
}
Helpers::panic("SaveDataArchive::OpenFile: Failed");
return FSResult::Success;
}
@ -170,4 +195,4 @@ Rust::Result<ArchiveBase*, FSResult> SaveDataArchive::openArchive(const FSPath&
std::optional<u32> SaveDataArchive::readFile(FileSession* file, u64 offset, u32 size, u32 dataPointer) {
Helpers::panic("Unimplemented SaveData::ReadFile");
return 0;
}
}