From 2f47f15315a1103ed5578b6a9c6c6bdc86cef872 Mon Sep 17 00:00:00 2001 From: wheremyfoodat <44909372+wheremyfoodat@users.noreply.github.com> Date: Wed, 27 Sep 2023 14:16:27 +0300 Subject: [PATCH 1/2] Implement RenameFile for ExtSaveData --- include/fs/archive_ext_save_data.hpp | 1 + include/result/result_fs.hpp | 6 +++++ src/core/fs/archive_ext_save_data.cpp | 37 +++++++++++++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/include/fs/archive_ext_save_data.hpp b/include/fs/archive_ext_save_data.hpp index adba568f3..7c8c7503e 100644 --- a/include/fs/archive_ext_save_data.hpp +++ b/include/fs/archive_ext_save_data.hpp @@ -12,6 +12,7 @@ class ExtSaveDataArchive : public ArchiveBase { HorizonResult createDirectory(const FSPath& path) override; HorizonResult createFile(const FSPath& path, u64 size) override; HorizonResult deleteFile(const FSPath& path) override; + HorizonResult renameFile(const FSPath& oldPath, const FSPath& newPath) override; Rust::Result openArchive(const FSPath& path) override; Rust::Result openDirectory(const FSPath& path) override; diff --git a/include/result/result_fs.hpp b/include/result/result_fs.hpp index 96df09952..146d0d2d8 100644 --- a/include/result/result_fs.hpp +++ b/include/result/result_fs.hpp @@ -15,4 +15,10 @@ namespace Result::FS { // Trying to access an archive that needs formatting and has not been formatted DEFINE_HORIZON_RESULT(NotFormatted, 340, InvalidState, Status); DEFINE_HORIZON_RESULT(UnexpectedFileOrDir, 770, NotSupported, Usage); + + // Trying to rename a file that doesn't exist or is a directory + DEFINE_HORIZON_RESULT(RenameNonexistentFileOrDir, 120, NotFound, Status); + + // Trying to rename a file but the destination already exists + DEFINE_HORIZON_RESULT(RenameFileDestExists, 190, NothingHappened, Status); }; diff --git a/src/core/fs/archive_ext_save_data.cpp b/src/core/fs/archive_ext_save_data.cpp index 87b3662b3..4b57f2457 100644 --- a/src/core/fs/archive_ext_save_data.cpp +++ b/src/core/fs/archive_ext_save_data.cpp @@ -87,6 +87,43 @@ FileDescriptor ExtSaveDataArchive::openFile(const FSPath& path, const FilePerms& return FileError; } +HorizonResult ExtSaveDataArchive::renameFile(const FSPath& oldPath, const FSPath& newPath) { + if (oldPath.type != PathType::UTF16 || newPath.type != PathType::UTF16) { + Helpers::panic("Invalid path type for ExtSaveData::RenameFile"); + } + + if (!isPathSafe(oldPath) || !isPathSafe(newPath)) { + Helpers::panic("Unsafe path in ExtSaveData::RenameFile"); + } + + // Construct host filesystem paths + fs::path sourcePath = IOFile::getAppData() / backingFolder; + fs::path destPath = sourcePath; + + sourcePath += fs::path(oldPath.utf16_string).make_preferred(); + destPath += fs::path(newPath.utf16_string).make_preferred(); + + if (!fs::is_regular_file(sourcePath) || fs::is_directory(sourcePath)) { + Helpers::warn("ExtSaveData::RenameFile: Source path is not a file or is directory"); + return Result::FS::RenameNonexistentFileOrDir; + } + + if (fs::is_regular_file(destPath) || fs::is_directory(destPath)) { + Helpers::warn("ExtSaveData::RenameFile: Dest path already exists"); + return Result::FS::RenameFileDestExists; + } + + std::error_code ec; + fs::rename(sourcePath, destPath, ec); + + if (ec) { + Helpers::warn("Error in ExtSaveData::RenameFile"); + return Result::FS::RenameNonexistentFileOrDir; + } + + return Result::Success; +} + HorizonResult ExtSaveDataArchive::createDirectory(const FSPath& path) { if (path.type == PathType::UTF16) { if (!isPathSafe(path)) { From aacb7c58e7e618f896610556ccc899fefebd76d7 Mon Sep 17 00:00:00 2001 From: wheremyfoodat <44909372+wheremyfoodat@users.noreply.github.com> Date: Wed, 27 Sep 2023 14:18:57 +0300 Subject: [PATCH 2/2] clang-format --- include/result/result_fs.hpp | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/include/result/result_fs.hpp b/include/result/result_fs.hpp index 146d0d2d8..b7e798f97 100644 --- a/include/result/result_fs.hpp +++ b/include/result/result_fs.hpp @@ -4,21 +4,21 @@ DEFINE_HORIZON_RESULT_MODULE(Result::FS, FS); namespace Result::FS { - // TODO: Verify this - DEFINE_HORIZON_RESULT(FileNotFound, 100, NotFound, Status); - // TODO: Verify this - DEFINE_HORIZON_RESULT(FileNotFoundAlt, 112, NotFound, Status); - // Also a not found error code used here and there in the FS module. - DEFINE_HORIZON_RESULT(NotFoundInvalid, 120, InvalidState, Status); - DEFINE_HORIZON_RESULT(AlreadyExists, 190, NothingHappened, Info); - DEFINE_HORIZON_RESULT(FileTooLarge, 210, OutOfResource, Info); - // Trying to access an archive that needs formatting and has not been formatted - DEFINE_HORIZON_RESULT(NotFormatted, 340, InvalidState, Status); - DEFINE_HORIZON_RESULT(UnexpectedFileOrDir, 770, NotSupported, Usage); + // TODO: Verify this + DEFINE_HORIZON_RESULT(FileNotFound, 100, NotFound, Status); + // TODO: Verify this + DEFINE_HORIZON_RESULT(FileNotFoundAlt, 112, NotFound, Status); + // Also a not found error code used here and there in the FS module. + DEFINE_HORIZON_RESULT(NotFoundInvalid, 120, InvalidState, Status); + DEFINE_HORIZON_RESULT(AlreadyExists, 190, NothingHappened, Info); + DEFINE_HORIZON_RESULT(FileTooLarge, 210, OutOfResource, Info); + // Trying to access an archive that needs formatting and has not been formatted + DEFINE_HORIZON_RESULT(NotFormatted, 340, InvalidState, Status); + DEFINE_HORIZON_RESULT(UnexpectedFileOrDir, 770, NotSupported, Usage); - // Trying to rename a file that doesn't exist or is a directory + // Trying to rename a file that doesn't exist or is a directory DEFINE_HORIZON_RESULT(RenameNonexistentFileOrDir, 120, NotFound, Status); - // Trying to rename a file but the destination already exists + // Trying to rename a file but the destination already exists DEFINE_HORIZON_RESULT(RenameFileDestExists, 190, NothingHappened, Status); -}; +}; // namespace Result::FS