From 2808db7f2fb48d0a1e1cb17aac6d39fc2a521560 Mon Sep 17 00:00:00 2001 From: Dentomologist Date: Tue, 23 Aug 2022 17:10:07 -0700 Subject: [PATCH] FileUtil: Return success bool from CopyDir --- Source/Core/Common/FileUtil.cpp | 29 +++++++++++++++++------------ Source/Core/Common/FileUtil.h | 2 +- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/Source/Core/Common/FileUtil.cpp b/Source/Core/Common/FileUtil.cpp index cbd92fc265..1dbadafe86 100644 --- a/Source/Core/Common/FileUtil.cpp +++ b/Source/Core/Common/FileUtil.cpp @@ -635,15 +635,19 @@ bool DeleteDirRecursively(const std::string& directory) return success; } -// Create directory and copy contents (does not overwrite existing files) -void CopyDir(const std::string& source_path, const std::string& dest_path, bool destructive) +// Create directory and copy contents (optionally overwrites existing files) +bool CopyDir(const std::string& source_path, const std::string& dest_path, const bool destructive) { if (source_path == dest_path) - return; + return true; if (!Exists(source_path)) - return; + return false; + + // Shouldn't be used to short circuit operations after an earlier failure + bool everything_copied = true; + if (!Exists(dest_path)) - File::CreateFullPath(dest_path); + everything_copied = File::CreateFullPath(dest_path) && everything_copied; #ifdef _WIN32 WIN32_FIND_DATA ffd; @@ -652,7 +656,7 @@ void CopyDir(const std::string& source_path, const std::string& dest_path, bool if (hFind == INVALID_HANDLE_VALUE) { FindClose(hFind); - return; + return false; } do @@ -661,7 +665,7 @@ void CopyDir(const std::string& source_path, const std::string& dest_path, bool #else DIR* dirp = opendir(source_path.c_str()); if (!dirp) - return; + return false; while (dirent* result = readdir(dirp)) { @@ -671,21 +675,21 @@ void CopyDir(const std::string& source_path, const std::string& dest_path, bool if (virtualName == "." || virtualName == "..") continue; - std::string source = source_path + DIR_SEP + virtualName; - std::string dest = dest_path + DIR_SEP + virtualName; + const std::string source = source_path + DIR_SEP + virtualName; + const std::string dest = dest_path + DIR_SEP + virtualName; if (IsDirectory(source)) { if (!Exists(dest)) File::CreateFullPath(dest + DIR_SEP); - CopyDir(source, dest, destructive); + everything_copied = CopyDir(source, dest, destructive) && everything_copied; } else if (!destructive && !Exists(dest)) { - Copy(source, dest); + everything_copied = Copy(source, dest) && everything_copied; } else if (destructive) { - Rename(source, dest); + everything_copied = Rename(source, dest) && everything_copied; } #ifdef _WIN32 } while (FindNextFile(hFind, &ffd) != 0); @@ -694,6 +698,7 @@ void CopyDir(const std::string& source_path, const std::string& dest_path, bool } closedir(dirp); #endif + return everything_copied; } // Returns the current directory diff --git a/Source/Core/Common/FileUtil.h b/Source/Core/Common/FileUtil.h index ea29d05e40..b87bde0295 100644 --- a/Source/Core/Common/FileUtil.h +++ b/Source/Core/Common/FileUtil.h @@ -193,7 +193,7 @@ bool DeleteDirRecursively(const std::string& directory); std::string GetCurrentDir(); // Create directory and copy contents (optionally overwrites existing files) -void CopyDir(const std::string& source_path, const std::string& dest_path, +bool CopyDir(const std::string& source_path, const std::string& dest_path, bool destructive = false); // Set the current directory to given directory