From 15f25783a8b9e2ca1aa6821beb842a4e22764387 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lam?= Date: Wed, 16 Aug 2017 23:33:34 +0200 Subject: [PATCH] WFS: Implement RENAME. --- Source/Core/Core/IOS/WFS/WFSSRV.cpp | 32 +++++++++++++++++++++++++++++ Source/Core/Core/IOS/WFS/WFSSRV.h | 12 ++++++++--- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/Source/Core/Core/IOS/WFS/WFSSRV.cpp b/Source/Core/Core/IOS/WFS/WFSSRV.cpp index 1ba9166eba..4b6cc1e03d 100644 --- a/Source/Core/Core/IOS/WFS/WFSSRV.cpp +++ b/Source/Core/Core/IOS/WFS/WFSSRV.cpp @@ -4,6 +4,7 @@ #include "Core/IOS/WFS/WFSSRV.h" +#include #include #include #include @@ -184,6 +185,17 @@ IPCCommandResult WFSSRV::IOCtl(const IOCtlRequest& request) break; } + case IOCTL_WFS_RENAME: + case IOCTL_WFS_RENAME_2: + { + const std::string source_path = + Memory::GetString(request.buffer_in + 2, Memory::Read_U16(request.buffer_in)); + const std::string dest_path = + Memory::GetString(request.buffer_in + 512 + 2, Memory::Read_U16(request.buffer_in + 512)); + return_error_code = Rename(source_path, dest_path); + break; + } + case IOCTL_WFS_CREATE_OPEN: case IOCTL_WFS_OPEN: { @@ -353,6 +365,26 @@ IPCCommandResult WFSSRV::IOCtl(const IOCtlRequest& request) return GetDefaultReply(return_error_code); } +s32 WFSSRV::Rename(std::string source, std::string dest) const +{ + source = NormalizePath(source); + dest = NormalizePath(dest); + + INFO_LOG(IOS_WFS, "IOCTL_WFS_RENAME: %s to %s", source.c_str(), dest.c_str()); + + const bool opened = std::any_of(m_fds.begin(), m_fds.end(), + [&](const auto& fd) { return fd.in_use && fd.path == source; }); + + if (opened) + return WFS_FILE_IS_OPENED; + + // TODO(wfs): Handle other rename failures + if (!File::Rename(WFS::NativePath(source), WFS::NativePath(dest))) + return WFS_ENOENT; + + return IPC_SUCCESS; +} + std::string WFSSRV::NormalizePath(const std::string& path) const { std::string expanded; diff --git a/Source/Core/Core/IOS/WFS/WFSSRV.h b/Source/Core/Core/IOS/WFS/WFSSRV.h index a5697f28fc..a970ba1da0 100644 --- a/Source/Core/Core/IOS/WFS/WFSSRV.h +++ b/Source/Core/Core/IOS/WFS/WFSSRV.h @@ -24,9 +24,11 @@ std::string NativePath(const std::string& wfs_path); enum { - WFS_EBADFD = -10026, // Invalid file descriptor. - WFS_EEXIST = -10027, // File already exists. - WFS_ENOENT = -10028, // No such file or directory. + WFS_EINVAL = -10003, // Invalid argument. + WFS_EBADFD = -10026, // Invalid file descriptor. + WFS_EEXIST = -10027, // File already exists. + WFS_ENOENT = -10028, // No such file or directory. + WFS_FILE_IS_OPENED = -10032, // Cannot perform operation on an opened file. }; namespace Device @@ -38,6 +40,8 @@ public: IPCCommandResult IOCtl(const IOCtlRequest& request) override; + s32 Rename(std::string source, std::string dest) const; + private: // WFS device name, e.g. msc01/msc02. std::string m_device_name; @@ -66,6 +70,7 @@ private: IOCTL_WFS_GET_HOMEDIR = 0x12, IOCTL_WFS_GETCWD = 0x13, IOCTL_WFS_DELETE = 0x15, + IOCTL_WFS_RENAME = 0x16, IOCTL_WFS_GET_ATTRIBUTES = 0x17, IOCTL_WFS_CREATE_OPEN = 0x19, IOCTL_WFS_OPEN = 0x1A, @@ -75,6 +80,7 @@ private: IOCTL_WFS_WRITE = 0x22, IOCTL_WFS_ATTACH_DETACH = 0x2d, IOCTL_WFS_ATTACH_DETACH_2 = 0x2e, + IOCTL_WFS_RENAME_2 = 0x41, IOCTL_WFS_CLOSE_2 = 0x47, IOCTL_WFS_READ_ABSOLUTE = 0x48, IOCTL_WFS_WRITE_ABSOLUTE = 0x49,