[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

@ -105,6 +105,12 @@ struct ArchiveSession {
// Otherwise the fd of the opened file is returned (or nullptr if the opened file doesn't require one)
using FileDescriptor = std::optional<FILE*>;
enum class CreateFileResult : u32 {
Success = 0,
AlreadyExists = 0x82044BE,
FileTooLarge = 0x86044D2
};
class ArchiveBase {
protected:
using Handle = u32;
@ -161,6 +167,7 @@ protected:
public:
virtual std::string name() = 0;
virtual u64 getFreeBytes() = 0;
virtual CreateFileResult createFile(const FSPath& path, u64 size) = 0;
// Returns nullopt if opening the file failed, otherwise returns a file descriptor to it (nullptr if none is needed)
virtual FileDescriptor openFile(const FSPath& path, const FilePerms& perms) = 0;

View file

@ -9,6 +9,7 @@ public:
std::string name() override { return "ExtSaveData"; }
ArchiveBase* openArchive(const FSPath& path) override;
CreateFileResult createFile(const FSPath& path, u64 size) override;
FileDescriptor openFile(const FSPath& path, const FilePerms& perms) override;
std::optional<u32> readFile(FileSession* file, u64 offset, u32 size, u32 dataPointer) override;

View file

@ -9,6 +9,7 @@ public:
std::string name() override { return "SelfNCCH"; }
ArchiveBase* openArchive(const FSPath& path) override;
CreateFileResult createFile(const FSPath& path, u64 size) override;
FileDescriptor openFile(const FSPath& path, const FilePerms& perms) override;
std::optional<u32> readFile(FileSession* file, u64 offset, u32 size, u32 dataPointer) override;

View file

@ -9,6 +9,7 @@ public:
std::string name() override { return "SaveData"; }
ArchiveBase* openArchive(const FSPath& path) override;
CreateFileResult createFile(const FSPath& path, u64 size) override;
FileDescriptor openFile(const FSPath& path, const FilePerms& perms) override;
std::optional<u32> readFile(FileSession* file, u64 offset, u32 size, u32 dataPointer) override;

View file

@ -9,6 +9,7 @@ public:
std::string name() override { return "SDMC"; }
ArchiveBase* openArchive(const FSPath& path) override;
CreateFileResult createFile(const FSPath& path, u64 size) override;
FileDescriptor openFile(const FSPath& path, const FilePerms& perms) override;
std::optional<u32> readFile(FileSession* file, u64 offset, u32 size, u32 dataPointer) override;
};

View file

@ -62,6 +62,19 @@ public:
return read(data, count, sizeof(std::uint8_t));
}
std::pair<bool, std::size_t> write(const void* data, std::size_t length, std::size_t dataSize) {
if (!isOpen()) {
return { false, std::numeric_limits<std::size_t>::max() };
}
if (length == 0) return { true, 0 };
return { true, std::fwrite(data, dataSize, length, handle) };
}
auto writeBytes(const void* data, std::size_t count) {
return write(data, count, sizeof(std::uint8_t));
}
std::uint64_t size() {
if (!isOpen()) return 0;

View file

@ -124,6 +124,7 @@ private:
public:
Kernel(CPU& cpu, Memory& mem, GPU& gpu);
void initializeFS() { return serviceManager.initializeFS(); }
void setVersion(u8 major, u8 minor);
void serviceSVC(u32 svc);
void reset();

View file

@ -53,4 +53,6 @@ public:
void reset();
void handleSyncRequest(u32 messagePointer);
// Creates directories for NAND, ExtSaveData, etc if they don't already exist. Should be executed after loading a new ROM.
void initializeFilesystem();
};

View file

@ -60,6 +60,7 @@ class ServiceManager {
public:
ServiceManager(std::array<u32, 16>& regs, Memory& mem, GPU& gpu, u32& currentPID, Kernel& kernel);
void reset();
void initializeFS() { fs.initializeFilesystem(); }
void handleSyncRequest(u32 messagePointer);
// Forward a SendSyncRequest IPC message to the service with the respective handle