Add FS::RenameFile

This commit is contained in:
wheremyfoodat 2023-09-26 23:31:17 +03:00
parent fc15a179ad
commit 9e2707e097
3 changed files with 50 additions and 0 deletions

View file

@ -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<u32> readFile(FileSession* file, u64 offset, u32 size, u32 dataPointer) = 0;

View file

@ -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);

View file

@ -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<ArchiveSession>();
const auto destArchive = destArchiveObject->getData<ArchiveSession>();
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<u32>(res));
}