From a0179e5ca5bc54491f03ff60d2f971197eb03e88 Mon Sep 17 00:00:00 2001
From: mailwl <mailwl@gmail.com>
Date: Tue, 24 Apr 2018 10:56:05 +0300
Subject: [PATCH] Service/FS: implement IFileSystem::RenameFile

---
 src/core/file_sys/disk_filesystem.cpp       | 12 ++++++++---
 src/core/file_sys/disk_filesystem.h         |  2 +-
 src/core/file_sys/filesystem.h              |  3 ++-
 src/core/file_sys/romfs_filesystem.cpp      |  3 ++-
 src/core/file_sys/romfs_filesystem.h        |  2 +-
 src/core/hle/service/filesystem/fsp_srv.cpp | 22 ++++++++++++++++++++-
 6 files changed, 36 insertions(+), 8 deletions(-)

diff --git a/src/core/file_sys/disk_filesystem.cpp b/src/core/file_sys/disk_filesystem.cpp
index ca13238736..4d00249fa8 100644
--- a/src/core/file_sys/disk_filesystem.cpp
+++ b/src/core/file_sys/disk_filesystem.cpp
@@ -67,10 +67,16 @@ ResultCode Disk_FileSystem::DeleteFile(const std::string& path) const {
     return RESULT_SUCCESS;
 }
 
-ResultCode Disk_FileSystem::RenameFile(const Path& src_path, const Path& dest_path) const {
-    LOG_WARNING(Service_FS, "(STUBBED) called");
+ResultCode Disk_FileSystem::RenameFile(const std::string& src_path,
+                                       const std::string& dest_path) const {
+    const std::string full_src_path = base_directory + src_path;
+    const std::string full_dest_path = base_directory + dest_path;
+
+    if (!FileUtil::Exists(full_src_path)) {
+        return ERROR_PATH_NOT_FOUND;
+    }
     // TODO(wwylele): Use correct error code
-    return ResultCode(-1);
+    return FileUtil::Rename(full_src_path, full_dest_path) ? RESULT_SUCCESS : ResultCode(-1);
 }
 
 ResultCode Disk_FileSystem::DeleteDirectory(const Path& path) const {
diff --git a/src/core/file_sys/disk_filesystem.h b/src/core/file_sys/disk_filesystem.h
index 8f9e1145a9..591e39fdac 100644
--- a/src/core/file_sys/disk_filesystem.h
+++ b/src/core/file_sys/disk_filesystem.h
@@ -26,7 +26,7 @@ public:
     ResultVal<std::unique_ptr<StorageBackend>> OpenFile(const std::string& path,
                                                         Mode mode) const override;
     ResultCode DeleteFile(const std::string& path) const override;
-    ResultCode RenameFile(const Path& src_path, const Path& dest_path) const override;
+    ResultCode RenameFile(const std::string& src_path, const std::string& dest_path) const override;
     ResultCode DeleteDirectory(const Path& path) const override;
     ResultCode DeleteDirectoryRecursively(const Path& path) const override;
     ResultCode CreateFile(const std::string& path, u64 size) const override;
diff --git a/src/core/file_sys/filesystem.h b/src/core/file_sys/filesystem.h
index beefcfdb2c..295a3133e7 100644
--- a/src/core/file_sys/filesystem.h
+++ b/src/core/file_sys/filesystem.h
@@ -126,7 +126,8 @@ public:
      * @param dest_path Destination path relative to the archive
      * @return Result of the operation
      */
-    virtual ResultCode RenameFile(const Path& src_path, const Path& dest_path) const = 0;
+    virtual ResultCode RenameFile(const std::string& src_path,
+                                  const std::string& dest_path) const = 0;
 
     /**
      * Rename a Directory specified by its path
diff --git a/src/core/file_sys/romfs_filesystem.cpp b/src/core/file_sys/romfs_filesystem.cpp
index 3d77e2d5fb..b9982e6fae 100644
--- a/src/core/file_sys/romfs_filesystem.cpp
+++ b/src/core/file_sys/romfs_filesystem.cpp
@@ -27,7 +27,8 @@ ResultCode RomFS_FileSystem::DeleteFile(const std::string& path) const {
     return ResultCode(-1);
 }
 
-ResultCode RomFS_FileSystem::RenameFile(const Path& src_path, const Path& dest_path) const {
+ResultCode RomFS_FileSystem::RenameFile(const std::string& src_path,
+                                        const std::string& dest_path) const {
     LOG_CRITICAL(Service_FS, "Attempted to rename a file within an ROMFS archive (%s).",
                  GetName().c_str());
     // TODO(wwylele): Use correct error code
diff --git a/src/core/file_sys/romfs_filesystem.h b/src/core/file_sys/romfs_filesystem.h
index 1b5cac4098..ba9d85823b 100644
--- a/src/core/file_sys/romfs_filesystem.h
+++ b/src/core/file_sys/romfs_filesystem.h
@@ -32,7 +32,7 @@ public:
     ResultVal<std::unique_ptr<StorageBackend>> OpenFile(const std::string& path,
                                                         Mode mode) const override;
     ResultCode DeleteFile(const std::string& path) const override;
-    ResultCode RenameFile(const Path& src_path, const Path& dest_path) const override;
+    ResultCode RenameFile(const std::string& src_path, const std::string& dest_path) const override;
     ResultCode DeleteDirectory(const Path& path) const override;
     ResultCode DeleteDirectoryRecursively(const Path& path) const override;
     ResultCode CreateFile(const std::string& path, u64 size) const override;
diff --git a/src/core/hle/service/filesystem/fsp_srv.cpp b/src/core/hle/service/filesystem/fsp_srv.cpp
index 2f476c8697..02e270f266 100644
--- a/src/core/hle/service/filesystem/fsp_srv.cpp
+++ b/src/core/hle/service/filesystem/fsp_srv.cpp
@@ -239,7 +239,7 @@ public:
             {2, &IFileSystem::CreateDirectory, "CreateDirectory"},
             {3, nullptr, "DeleteDirectory"},
             {4, nullptr, "DeleteDirectoryRecursively"},
-            {5, nullptr, "RenameFile"},
+            {5, &IFileSystem::RenameFile, "RenameFile"},
             {6, nullptr, "RenameDirectory"},
             {7, &IFileSystem::GetEntryType, "GetEntryType"},
             {8, &IFileSystem::OpenFile, "OpenFile"},
@@ -300,6 +300,26 @@ public:
         rb.Push(backend->CreateDirectory(name));
     }
 
+    void RenameFile(Kernel::HLERequestContext& ctx) {
+        IPC::RequestParser rp{ctx};
+
+        std::vector<u8> buffer;
+        buffer.resize(ctx.BufferDescriptorX()[0].Size());
+        Memory::ReadBlock(ctx.BufferDescriptorX()[0].Address(), buffer.data(), buffer.size());
+        auto end = std::find(buffer.begin(), buffer.end(), '\0');
+        std::string src_name(buffer.begin(), end);
+
+        buffer.resize(ctx.BufferDescriptorX()[1].Size());
+        Memory::ReadBlock(ctx.BufferDescriptorX()[1].Address(), buffer.data(), buffer.size());
+        end = std::find(buffer.begin(), buffer.end(), '\0');
+        std::string dst_name(buffer.begin(), end);
+
+        LOG_DEBUG(Service_FS, "called file '%s' to file '%s'", src_name.c_str(), dst_name.c_str());
+
+        IPC::ResponseBuilder rb{ctx, 2};
+        rb.Push(backend->RenameFile(src_name, dst_name));
+    }
+
     void OpenFile(Kernel::HLERequestContext& ctx) {
         IPC::RequestParser rp{ctx};
 
-- 
GitLab