From 9e2707e097f1597fe9d4b05dc6165bedc5b6e441 Mon Sep 17 00:00:00 2001 From: wheremyfoodat <44909372+wheremyfoodat@users.noreply.github.com> Date: Tue, 26 Sep 2023 23:31:17 +0300 Subject: [PATCH] Add FS::RenameFile --- include/fs/archive_base.hpp | 5 +++++ include/services/fs.hpp | 1 + src/core/services/fs.cpp | 44 +++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+) diff --git a/include/fs/archive_base.hpp b/include/fs/archive_base.hpp index c782f6a6..2843be68 100644 --- a/include/fs/archive_base.hpp +++ b/include/fs/archive_base.hpp @@ -246,6 +246,11 @@ public: Helpers::panic("Unimplemented Format for %s archive", name().c_str()); } + virtual HorizonResult renameFile(const FSPath& oldPath, const FSPath& newPath) { + Helpers::panic("Unimplemented RenameFile for %s archive", name().c_str()); + return Result::Success; + } + // Read size bytes from a file starting at offset "offset" into a certain buffer in memory // Returns the number of bytes read, or nullopt if the read failed virtual std::optional readFile(FileSession* file, u64 offset, u32 size, u32 dataPointer) = 0; diff --git a/include/services/fs.hpp b/include/services/fs.hpp index 435dc5a6..2fe3ba5d 100644 --- a/include/services/fs.hpp +++ b/include/services/fs.hpp @@ -71,6 +71,7 @@ class FSService { void openDirectory(u32 messagePointer); void openFile(u32 messagePointer); void openFileDirectly(u32 messagePointer); + void renameFile(u32 messagePointer); void setArchivePriority(u32 messagePointer); void setPriority(u32 messagePointer); void setThisSaveDataSecureValue(u32 messagePointer); diff --git a/src/core/services/fs.cpp b/src/core/services/fs.cpp index 1c243a57..0bcbab37 100644 --- a/src/core/services/fs.cpp +++ b/src/core/services/fs.cpp @@ -16,6 +16,7 @@ namespace FSCommands { OpenFile = 0x080201C2, OpenFileDirectly = 0x08030204, DeleteFile = 0x08040142, + RenameFile = 0x08050244, DeleteDirectory = 0x08060142, DeleteDirectoryRecursively = 0x08070142, CreateFile = 0x08080202, @@ -187,6 +188,7 @@ void FSService::handleSyncRequest(u32 messagePointer) { case FSCommands::OpenDirectory: openDirectory(messagePointer); break; case FSCommands::OpenFile: [[likely]] openFile(messagePointer); break; case FSCommands::OpenFileDirectly: [[likely]] openFileDirectly(messagePointer); break; + case FSCommands::RenameFile: renameFile(messagePointer); break; case FSCommands::SetArchivePriority: setArchivePriority(messagePointer); break; case FSCommands::SetPriority: setPriority(messagePointer); break; case FSCommands::SetThisSaveDataSecureValue: setThisSaveDataSecureValue(messagePointer); break; @@ -716,4 +718,46 @@ void FSService::cardSlotIsInserted(u32 messagePointer) { mem.write32(messagePointer, IPC::responseHeader(0x821, 2, 0)); mem.write32(messagePointer + 4, Result::Success); mem.write8(messagePointer + 8, cardInserted ? 1 : 0); +} + +void FSService::renameFile(u32 messagePointer) { + log("FS::RenameFile\n"); + + mem.write32(messagePointer, IPC::responseHeader(0x805, 1, 0)); + + const Handle sourceArchiveHandle = mem.read64(messagePointer + 8); + const Handle destArchiveHandle = mem.read64(messagePointer + 24); + + // Read path info + const u32 sourcePathType = mem.read32(messagePointer + 16); + const u32 sourcePathSize = mem.read32(messagePointer + 20); + const u32 sourcePathPointer = mem.read32(messagePointer + 44); + const FSPath sourcePath = readPath(sourcePathType, sourcePathPointer, sourcePathSize); + + const u32 destPathType = mem.read32(messagePointer + 32); + const u32 destPathSize = mem.read32(messagePointer + 36); + const u32 destPathPointer = mem.read32(messagePointer + 52); + const FSPath destPath = readPath(destPathType, destPathPointer, destPathSize); + + const auto sourceArchiveObject = kernel.getObject(sourceArchiveHandle, KernelObjectType::Archive); + const auto destArchiveObject = kernel.getObject(destArchiveHandle, KernelObjectType::Archive); + + if (sourceArchiveObject == nullptr || destArchiveObject == nullptr) { + Helpers::panic("FS::RenameFile: One of the archive handles is invalid"); + } + + const auto sourceArchive = sourceArchiveObject->getData(); + const auto destArchive = destArchiveObject->getData(); + if (!sourceArchive->isOpen || !destArchive->isOpen) { + Helpers::warn("FS::RenameFile: Not both archive sessions are open"); + } + + // This returns error 0xE0C046F8 according to 3DBrew + if (sourceArchive->archive->name() != destArchive->archive->name()) { + Helpers::panic("FS::RenameFile: Both archive handles should belong to the same archive"); + } + + // Everything is OK, let's do the rename. Both archives should match so we don't need the dest anymore + const HorizonResult res = sourceArchive->archive->renameFile(sourcePath, destPath); + mem.write32(messagePointer + 4, static_cast(res)); } \ No newline at end of file