[FS] Done with CreateFile

This commit is contained in:
wheremyfoodat 2023-01-23 02:35:10 +02:00
parent 93d2d300b9
commit 49dcc25795
15 changed files with 85 additions and 3 deletions

View file

@ -3,6 +3,33 @@
namespace fs = std::filesystem;
CreateFileResult ExtSaveDataArchive::createFile(const FSPath& path, u64 size) {
if (size == 0)
Helpers::panic("ExtSaveData file does not support size == 0");
if (path.type == PathType::UTF16) {
if (!isPathSafe<PathType::UTF16>(path))
Helpers::panic("Unsafe path in ExtSaveData::CreateFile");
fs::path p = IOFile::getAppData() / "NAND";
p += fs::path(path.utf16_string).make_preferred();
if (fs::exists(p))
return CreateFileResult::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) {
return CreateFileResult::Success;
}
return CreateFileResult::FileTooLarge;
}
Helpers::panic("ExtSaveDataArchive::OpenFile: Failed");
return CreateFileResult::Success;
}
FileDescriptor ExtSaveDataArchive::openFile(const FSPath& path, const FilePerms& perms) {
if (path.type == PathType::UTF16) {
if (!isPathSafe<PathType::UTF16>(path))

View file

@ -1,6 +1,11 @@
#include "fs/archive_ncch.hpp"
#include <memory>
CreateFileResult SelfNCCHArchive::createFile(const FSPath& path, u64 size) {
Helpers::panic("[SelfNCCH] CreateFile not yet supported");
return CreateFileResult::Success;
}
FileDescriptor SelfNCCHArchive::openFile(const FSPath& path, const FilePerms& perms) {
if (!hasRomFS()) {
printf("Tried to open a SelfNCCH file without a RomFS\n");

View file

@ -2,6 +2,11 @@
#include <algorithm>
#include <memory>
CreateFileResult SaveDataArchive::createFile(const FSPath& path, u64 size) {
Helpers::panic("[SaveData] CreateFile not yet supported");
return CreateFileResult::Success;
}
FileDescriptor SaveDataArchive::openFile(const FSPath& path, const FilePerms& perms) {
if (!cartHasSaveData()) {
printf("Tried to read SaveData FS without save data\n");

View file

@ -1,6 +1,11 @@
#include "fs/archive_sdmc.hpp"
#include <memory>
CreateFileResult SDMCArchive::createFile(const FSPath& path, u64 size) {
Helpers::panic("[SDMC] CreateFile not yet supported");
return CreateFileResult::Success;
}
FileDescriptor SDMCArchive::openFile(const FSPath& path, const FilePerms& perms) {
printf("SDMCArchive::OpenFile: Failed");
return FileError;

View file

@ -1,5 +1,6 @@
#include "services/fs.hpp"
#include "kernel/kernel.hpp"
#include "io_file.hpp"
#ifdef CreateFile // windows.h defines this because of course it does.
#undef CreateFile
@ -32,6 +33,16 @@ void FSService::reset() {
priority = 0;
}
// Creates directories for NAND, ExtSaveData, etc if they don't already exist. Should be executed after loading a new ROM.
void FSService::initializeFilesystem() {
const auto nandPath = IOFile::getAppData() / "NAND"; // Create NAND
// TODO: Savedata, SDMC, etc
if (!std::filesystem::is_directory(nandPath)) {
std::filesystem::create_directories(nandPath);
}
}
ArchiveBase* FSService::getArchiveFromID(u32 id) {
switch (id) {
case ArchiveID::SelfNCCH: return &selfNcch;
@ -225,7 +236,7 @@ void FSService::createFile(u32 messagePointer) {
const u32 filePathType = mem.read32(messagePointer + 16);
const u32 filePathSize = mem.read32(messagePointer + 20);
const u32 attributes = mem.read32(messagePointer + 24);
const u64 bytesToZero = mem.read64(messagePointer + 28); // Bytes to fill with zeroes in the file
const u64 size = mem.read64(messagePointer + 28);
const u32 filePathPointer = mem.read32(messagePointer + 40);
log("FS::CreateFile\n");
@ -240,8 +251,8 @@ void FSService::createFile(u32 messagePointer) {
ArchiveBase* archive = archiveObject->getData<ArchiveSession>()->archive;
auto filePath = readPath(filePathType, filePathPointer, filePathSize);
for (auto c : filePath.utf16_string) std::cout << (char)c;
Helpers::panic("Tried to create file");
CreateFileResult res = archive->createFile(filePath, size);
mem.write32(messagePointer + 4, static_cast<u32>(res));
}
void FSService::getPriority(u32 messagePointer) {

View file

@ -49,6 +49,7 @@ void Emulator::runFrame() {
}
bool Emulator::loadROM(const std::filesystem::path& path) {
kernel.initializeFS();
auto extension = path.extension();
if (extension == ".elf" || extension == ".axf")