ExtSaveData work

This commit is contained in:
wheremyfoodat 2023-01-22 14:23:28 +02:00
parent 902b877c40
commit 995dfb81df
12 changed files with 68 additions and 28 deletions

View file

@ -3,18 +3,27 @@
namespace fs = std::filesystem;
bool ExtSaveDataArchive::openFile(const FSPath& path) {
FileDescriptor ExtSaveDataArchive::openFile(const FSPath& path, const FilePerms& perms) {
if (path.type == PathType::UTF16) {
if (!isPathSafe<PathType::UTF16>(path))
Helpers::panic("Unsafe path in ExtSaveData::OpenFile");
if (perms.create())
Helpers::panic("[ExtSaveData] CAn't open file with create flag");
fs::path p = IOFile::getAppData() / "NAND";
p += fs::path(path.utf16_string).make_preferred();
return false;
if (fs::exists(p)) { // Return file descriptor if the file exists
IOFile file(p.string().c_str(), "r+b"); // According to Citra, this ignores the OpenFile flags and always opens as r+b? TODO: Check
return file.isOpen() ? file.getHandle() : FileError;
} else {
return FileError;
}
}
Helpers::panic("ExtSaveDataArchive::OpenFile: Failed");
return false;
return FileError;
}
ArchiveBase* ExtSaveDataArchive::openArchive(const FSPath& path) {
@ -22,6 +31,8 @@ ArchiveBase* ExtSaveDataArchive::openArchive(const FSPath& path) {
Helpers::panic("ExtSaveData accessed with an invalid path in OpenArchive");
}
if (path.binary[0] != 0) Helpers::panic("ExtSaveData: Tried to access something other than NAND");
return this;
}

View file

@ -1,15 +1,15 @@
#include "fs/archive_ncch.hpp"
#include <memory>
bool SelfNCCHArchive::openFile(const FSPath& path) {
FileDescriptor SelfNCCHArchive::openFile(const FSPath& path, const FilePerms& perms) {
if (!hasRomFS()) {
printf("Tried to open a SelfNCCH file without a RomFS\n");
return false;
return FileError;
}
if (path.type != PathType::Binary || path.binary.size() != 12) {
printf("Invalid SelfNCCH path type\n");
return false;
return FileError;
}
// Where to read the file from. (https://www.3dbrew.org/wiki/Filesystem_services#SelfNCCH_File_Path_Data_Format)
@ -19,7 +19,7 @@ bool SelfNCCHArchive::openFile(const FSPath& path) {
Helpers::panic("Read from NCCH's non-RomFS section!");
}
return true;
return NoFile; // No file descriptor needed for RomFS
}
ArchiveBase* SelfNCCHArchive::openArchive(const FSPath& path) {

View file

@ -2,23 +2,23 @@
#include <algorithm>
#include <memory>
bool SaveDataArchive::openFile(const FSPath& path) {
FileDescriptor SaveDataArchive::openFile(const FSPath& path, const FilePerms& perms) {
if (!cartHasSaveData()) {
printf("Tried to read SaveData FS without save data\n");
return false;
return FileError;
}
if (path.type == PathType::UTF16 /* && path.utf16_string == u"/game_header" */) {
printf("Opened file from the SaveData archive \n");
return true;
return NoFile;
}
if (path.type != PathType::Binary) {
printf("Unimplemented SaveData path type: %d\n", path.type);
return false;
return FileError;
}
return true;
return NoFile;
}
ArchiveBase* SaveDataArchive::openArchive(const FSPath& path) {

View file

@ -1,9 +1,9 @@
#include "fs/archive_sdmc.hpp"
#include <memory>
bool SDMCArchive::openFile(const FSPath& path) {
FileDescriptor SDMCArchive::openFile(const FSPath& path, const FilePerms& perms) {
printf("SDMCArchive::OpenFile: Failed");
return false;
return FileError;
}
ArchiveBase* SDMCArchive::openArchive(const FSPath& path) {

View file

@ -39,12 +39,13 @@ ArchiveBase* FSService::getArchiveFromID(u32 id) {
}
}
std::optional<Handle> FSService::openFileHandle(ArchiveBase* archive, const FSPath& path) {
bool opened = archive->openFile(path);
if (opened) {
std::optional<Handle> FSService::openFileHandle(ArchiveBase* archive, const FSPath& path, const FilePerms& perms) {
FileDescriptor opened = archive->openFile(path, perms);
if (opened.has_value()) { // If opened doesn't have a value, we failed to open the file
auto handle = kernel.makeObject(KernelObjectType::File);
auto& file = kernel.getObjects()[handle];
file.data = new FileSession(archive, path);
file.data = new FileSession(archive, path, opened.value());
return handle;
} else {
@ -164,8 +165,9 @@ void FSService::openFile(u32 messagePointer) {
ArchiveBase* archive = archiveObject->getData<ArchiveSession>()->archive;
auto filePath = readPath(filePathType, filePathPointer, filePathSize);
const FilePerms perms(openFlags);
std::optional<Handle> handle = openFileHandle(archive, filePath);
std::optional<Handle> handle = openFileHandle(archive, filePath, perms);
if (!handle.has_value()) {
printf("OpenFile failed\n");
mem.write32(messagePointer + 4, Result::FileNotFound);
@ -196,13 +198,14 @@ void FSService::openFileDirectly(u32 messagePointer) {
auto archivePath = readPath(archivePathType, archivePathPointer, archivePathSize);
auto filePath = readPath(filePathType, filePathPointer, filePathSize);
const FilePerms perms(openFlags);
archive = archive->openArchive(archivePath);
if (archive == nullptr) [[unlikely]] {
Helpers::panic("OpenFileDirectly: Failed to open archive with given path");
}
std::optional<Handle> handle = openFileHandle(archive, filePath);
std::optional<Handle> handle = openFileHandle(archive, filePath, perms);
if (!handle.has_value()) {
Helpers::panic("OpenFileDirectly: Failed to open file with given path");
} else {