Skip to content

Commit

Permalink
Merge pull request #291 from wheremyfoodat/qt
Browse files Browse the repository at this point in the history
Implement RenameFile for ExtSaveData
  • Loading branch information
wheremyfoodat authored Sep 27, 2023
2 parents 572e8ea + aacb7c5 commit b77ed3b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 12 deletions.
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

0 comments on commit b77ed3b

Please sign in to comment.