From 616d57e7fc9fd294f29f9d76874bd2afe745728e Mon Sep 17 00:00:00 2001 From: "Admiral H. Curtiss" Date: Wed, 22 Feb 2023 02:17:11 +0100 Subject: Common/FileUtil: Add Copy() function as a wrapper around std::filesystem::copy(). --- Source/Core/Common/FileUtil.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'Source/Core/Common/FileUtil.cpp') diff --git a/Source/Core/Common/FileUtil.cpp b/Source/Core/Common/FileUtil.cpp index 0d4288c609..e7e3e033aa 100644 --- a/Source/Core/Common/FileUtil.cpp +++ b/Source/Core/Common/FileUtil.cpp @@ -516,6 +516,31 @@ bool DeleteDirRecursively(const std::string& directory) return success; } +bool Copy(std::string_view source_path, std::string_view dest_path, bool overwrite_existing) +{ + DEBUG_LOG_FMT(COMMON, "{}: {} --> {} ({})", __func__, source_path, dest_path, + overwrite_existing ? "overwrite" : "preserve"); + + auto src_path = StringToPath(source_path); + auto dst_path = StringToPath(dest_path); + std::error_code error; + auto options = fs::copy_options::recursive; + if (overwrite_existing) + options |= fs::copy_options::overwrite_existing; + fs::copy(src_path, dst_path, options, error); + if (error) + { + std::error_code error_ignored; + if (fs::equivalent(src_path, dst_path, error_ignored)) + return true; + + ERROR_LOG_FMT(COMMON, "{}: failed {} --> {} ({}): {}", __func__, source_path, dest_path, + overwrite_existing ? "overwrite" : "preserve", error.message()); + return false; + } + return true; +} + // Create directory and copy contents (optionally overwrites existing files) bool CopyDir(const std::string& source_path, const std::string& dest_path, const bool destructive) { -- cgit v1.2.3 From e479f9241829130d9a23b12eea2b1142b6e2f282 Mon Sep 17 00:00:00 2001 From: "Admiral H. Curtiss" Date: Wed, 22 Feb 2023 02:31:06 +0100 Subject: Common/FileUtil: Add CreateDirs() function as a wrapper around std::filesystem::create_directories(). --- Source/Core/Common/FileUtil.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'Source/Core/Common/FileUtil.cpp') diff --git a/Source/Core/Common/FileUtil.cpp b/Source/Core/Common/FileUtil.cpp index e7e3e033aa..454277f55a 100644 --- a/Source/Core/Common/FileUtil.cpp +++ b/Source/Core/Common/FileUtil.cpp @@ -193,7 +193,6 @@ bool Delete(const std::string& filename, IfAbsentBehavior behavior) return true; } -// Returns true if successful, or path already exists. bool CreateDir(const std::string& path) { DEBUG_LOG_FMT(COMMON, "{}: directory {}", __func__, path); @@ -209,6 +208,22 @@ bool CreateDir(const std::string& path) return success; } +bool CreateDirs(std::string_view path) +{ + DEBUG_LOG_FMT(COMMON, "{}: directory {}", __func__, path); + + std::error_code error; + auto native_path = StringToPath(path); + bool success = fs::create_directories(native_path, error); + // If the path was not created, check if it was a pre-existing directory + std::error_code error_ignored; + if (!success && fs::is_directory(native_path, error_ignored)) + success = true; + if (!success) + ERROR_LOG_FMT(COMMON, "{}: failed on {}: {}", __func__, path, error.message()); + return success; +} + bool CreateFullPath(std::string_view fullPath) { DEBUG_LOG_FMT(COMMON, "{}: path {}", __func__, fullPath); -- cgit v1.2.3 From 884917a6d534cdce4fe7b2f39e1188e591d64b0f Mon Sep 17 00:00:00 2001 From: "Admiral H. Curtiss" Date: Wed, 22 Feb 2023 21:03:23 +0100 Subject: Common/FileUtil: Use non-throwing overload of is_directory() in CreateDir() and CreateFullPath(). --- Source/Core/Common/FileUtil.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'Source/Core/Common/FileUtil.cpp') diff --git a/Source/Core/Common/FileUtil.cpp b/Source/Core/Common/FileUtil.cpp index 454277f55a..656e74134b 100644 --- a/Source/Core/Common/FileUtil.cpp +++ b/Source/Core/Common/FileUtil.cpp @@ -201,7 +201,8 @@ bool CreateDir(const std::string& path) auto native_path = StringToPath(path); bool success = fs::create_directory(native_path, error); // If the path was not created, check if it was a pre-existing directory - if (!success && fs::is_directory(native_path)) + std::error_code error_ignored; + if (!success && fs::is_directory(native_path, error_ignored)) success = true; if (!success) ERROR_LOG_FMT(COMMON, "{}: failed on {}: {}", __func__, path, error.message()); @@ -232,7 +233,8 @@ bool CreateFullPath(std::string_view fullPath) auto native_path = StringToPath(fullPath).parent_path(); bool success = fs::create_directories(native_path, error); // If the path was not created, check if it was a pre-existing directory - if (!success && fs::is_directory(native_path)) + std::error_code error_ignored; + if (!success && fs::is_directory(native_path, error_ignored)) success = true; if (!success) ERROR_LOG_FMT(COMMON, "{}: failed on {}: {}", __func__, fullPath, error.message()); -- cgit v1.2.3 From 5367bf394c6092bed589bc4cdb06a2f0ab2c0f75 Mon Sep 17 00:00:00 2001 From: "Admiral H. Curtiss" Date: Fri, 24 Feb 2023 21:39:02 +0100 Subject: Common/FileUtil: Add Move() function. --- Source/Core/Common/FileUtil.cpp | 52 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'Source/Core/Common/FileUtil.cpp') diff --git a/Source/Core/Common/FileUtil.cpp b/Source/Core/Common/FileUtil.cpp index 656e74134b..333831d39f 100644 --- a/Source/Core/Common/FileUtil.cpp +++ b/Source/Core/Common/FileUtil.cpp @@ -558,6 +558,58 @@ bool Copy(std::string_view source_path, std::string_view dest_path, bool overwri return true; } +static bool MoveWithOverwrite(const std::filesystem::path& src, const std::filesystem::path& dst, + std::error_code& error) +{ + fs::rename(src, dst, error); + if (!error) + return true; + + // rename failed, try fallbacks + + if (!fs::is_directory(src)) + { + // src is not a directory (ie, probably a file), try to copy file + delete + if (!fs::copy_file(src, dst, fs::copy_options::overwrite_existing, error)) + return false; + if (!fs::remove(src, error)) + return false; + return true; + } + + // src is a directory, recurse into it and try to move all sub-elements one by one + // this usually happens because the target is a non-empty directory + for (fs::directory_iterator it(src, error); it != fs::directory_iterator(); it.increment(error)) + { + if (error) + return false; + if (!MoveWithOverwrite(it->path(), dst / it->path().filename(), error)) + return false; + } + if (error) + return false; + + // all sub-elements moved, remove top directory + if (!fs::remove(src, error)) + return false; + + return true; +} + +bool MoveWithOverwrite(std::string_view source_path, std::string_view dest_path) +{ + DEBUG_LOG_FMT(COMMON, "{}: {} --> {}", __func__, source_path, dest_path); + auto src_path = StringToPath(source_path); + auto dst_path = StringToPath(dest_path); + std::error_code error; + if (!MoveWithOverwrite(src_path, dst_path, error)) + { + ERROR_LOG_FMT(COMMON, "{}: failed {} --> {}: {}", __func__, source_path, dest_path, + error.message()); + } + return true; +} + // Create directory and copy contents (optionally overwrites existing files) bool CopyDir(const std::string& source_path, const std::string& dest_path, const bool destructive) { -- cgit v1.2.3 From a11b9d585f0d4668a8d4f698e0d03d1c79013eac Mon Sep 17 00:00:00 2001 From: "Admiral H. Curtiss" Date: Fri, 24 Feb 2023 21:48:48 +0100 Subject: Common/FileUtil: Remove obsolete CopyDir() function. --- Source/Core/Common/FileUtil.cpp | 23 ----------------------- 1 file changed, 23 deletions(-) (limited to 'Source/Core/Common/FileUtil.cpp') diff --git a/Source/Core/Common/FileUtil.cpp b/Source/Core/Common/FileUtil.cpp index 333831d39f..d456952980 100644 --- a/Source/Core/Common/FileUtil.cpp +++ b/Source/Core/Common/FileUtil.cpp @@ -610,29 +610,6 @@ bool MoveWithOverwrite(std::string_view source_path, std::string_view dest_path) return true; } -// Create directory and copy contents (optionally overwrites existing files) -bool CopyDir(const std::string& source_path, const std::string& dest_path, const bool destructive) -{ - auto src_path = StringToPath(source_path); - auto dst_path = StringToPath(dest_path); - if (fs::equivalent(src_path, dst_path)) - return true; - - DEBUG_LOG_FMT(COMMON, "{}: {} --> {}", __func__, source_path, dest_path); - - auto options = fs::copy_options::recursive; - if (destructive) - options |= fs::copy_options::overwrite_existing; - std::error_code error; - bool copied = fs::copy_file(src_path, dst_path, options, error); - if (!copied) - { - ERROR_LOG_FMT(COMMON, "{}: failed {} --> {}: {}", __func__, source_path, dest_path, - error.message()); - } - return copied; -} - // Returns the current directory std::string GetCurrentDir() { -- cgit v1.2.3