Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement RenameFile for ExtSaveData #291

Merged
merged 2 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/fs/archive_ext_save_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<ArchiveBase*, HorizonResult> openArchive(const FSPath& path) override;
Rust::Result<DirectorySession, HorizonResult> openDirectory(const FSPath& path) override;
Expand Down
30 changes: 18 additions & 12 deletions include/result/result_fs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +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
DEFINE_HORIZON_RESULT(RenameNonexistentFileOrDir, 120, NotFound, Status);

// Trying to rename a file but the destination already exists
DEFINE_HORIZON_RESULT(RenameFileDestExists, 190, NothingHappened, Status);
}; // namespace Result::FS
37 changes: 37 additions & 0 deletions src/core/fs/archive_ext_save_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathType::UTF16>(oldPath) || !isPathSafe<PathType::UTF16>(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<PathType::UTF16>(path)) {
Expand Down
Loading