From 53cabbb1343ce005dd259641432af2cc06b2bd3e Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Sat, 3 Jul 2021 16:48:22 +1000 Subject: [PATCH] FileSystem: Add RenamePath() --- src/common/file_system.cpp | 28 ++++++++++++++++++++++++++++ src/common/file_system.h | 3 +++ 2 files changed, 31 insertions(+) diff --git a/src/common/file_system.cpp b/src/common/file_system.cpp index 8efe8e93a..2602fef97 100644 --- a/src/common/file_system.cpp +++ b/src/common/file_system.cpp @@ -1571,6 +1571,20 @@ bool FileSystem::DeleteFile(const char* Path) return false; } +bool FileSystem::RenamePath(const char* OldPath, const char* NewPath) +{ + const std::wstring old_wpath(StringUtil::UTF8StringToWideString(OldPath)); + const std::wstring new_wpath(StringUtil::UTF8StringToWideString(NewPath)); + + if (!MoveFileExW(old_wpath.c_str(), new_wpath.c_str(), MOVEFILE_REPLACE_EXISTING)) + { + Log_ErrorPrintf("MoveFileEx('%s', '%s') failed: %08X", OldPath, NewPath, GetLastError()); + return false; + } + + return true; +} + static bool RecursiveDeleteDirectory(const std::wstring& wpath, bool Recursive) { // ensure it exists @@ -2046,6 +2060,20 @@ bool DeleteFile(const char* Path) return (unlink(Path) == 0); } +bool RenamePath(const char* OldPath, const char* NewPath) +{ + if (OldPath[0] == '\0' || NewPath[0] == '\0') + return false; + + if (rename(OldPath, NewPath) != 0) + { + Log_ErrorPrintf("rename('%s', '%s') failed: %d", OldPath, NewPath, errno); + return false; + } + + return true; +} + bool DeleteDirectory(const char* Path, bool Recursive) { Log_ErrorPrintf("FileSystem::DeleteDirectory(%s) not implemented", Path); diff --git a/src/common/file_system.h b/src/common/file_system.h index 9547a74f5..3fa33ab82 100644 --- a/src/common/file_system.h +++ b/src/common/file_system.h @@ -182,6 +182,9 @@ bool DirectoryExists(const char* Path); // delete file bool DeleteFile(const char* Path); +// rename file +bool RenamePath(const char* OldPath, const char* NewPath); + // open files std::unique_ptr OpenFile(const char* FileName, u32 Flags);