From ba487f5ee415a39453de2800e1bc17aef962b7dc Mon Sep 17 00:00:00 2001 From: Stefan Boberg Date: Thu, 3 Oct 2024 12:27:32 +0200 Subject: simplified CleanDirectory implementation (#182) we use the std implementation for all normal cases now. The Windows-only path still exists for VFS cleanup --- src/zencore/filesystem.cpp | 34 ++++++++-------------------------- 1 file changed, 8 insertions(+), 26 deletions(-) (limited to 'src/zencore/filesystem.cpp') diff --git a/src/zencore/filesystem.cpp b/src/zencore/filesystem.cpp index d79a39880..7ca076daa 100644 --- a/src/zencore/filesystem.cpp +++ b/src/zencore/filesystem.cpp @@ -93,6 +93,8 @@ CreateDirectories(const wchar_t* Dir) // Erase all files and directories in a given directory, leaving an empty directory // behind +bool DeleteDirectoriesInternal(const wchar_t* DirPath); + static bool WipeDirectory(const wchar_t* DirPath, bool KeepDotFiles) { @@ -159,7 +161,7 @@ WipeDirectory(const wchar_t* DirPath, bool KeepDotFiles) } } - bool Succeeded = DeleteDirectories(Path.c_str()); + bool Succeeded = DeleteDirectoriesInternal(Path.c_str()); if (!Succeeded) { @@ -193,25 +195,12 @@ WipeDirectory(const wchar_t* DirPath, bool KeepDotFiles) } bool -DeleteDirectories(const wchar_t* DirPath) +DeleteDirectoriesInternal(const wchar_t* DirPath) { const bool KeepDotFiles = false; return WipeDirectory(DirPath, KeepDotFiles) && RemoveDirectoryW(DirPath) == TRUE; } -bool -CleanDirectory(const wchar_t* DirPath) -{ - if (std::filesystem::exists(DirPath)) - { - const bool KeepDotFiles = false; - - return WipeDirectory(DirPath, KeepDotFiles); - } - - return CreateDirectories(DirPath); -} - bool CleanDirectory(const wchar_t* DirPath, bool KeepDotFiles) { @@ -222,7 +211,6 @@ CleanDirectory(const wchar_t* DirPath, bool KeepDotFiles) return CreateDirectories(DirPath); } - #endif // ZEN_PLATFORM_WINDOWS bool @@ -252,35 +240,29 @@ CreateDirectories(const std::filesystem::path& Dir) bool DeleteDirectories(const std::filesystem::path& Dir) { -#if ZEN_PLATFORM_WINDOWS - return DeleteDirectories(Dir.c_str()); -#else std::error_code ErrorCode; return std::filesystem::remove_all(Dir, ErrorCode); -#endif } bool CleanDirectory(const std::filesystem::path& Dir) { -#if ZEN_PLATFORM_WINDOWS - return CleanDirectory(Dir.c_str()); -#else if (std::filesystem::exists(Dir)) { bool Success = true; - std::error_code ErrorCode; for (const auto& Item : std::filesystem::directory_iterator(Dir)) { - Success &= std::filesystem::remove_all(Item, ErrorCode); + std::error_code ErrorCode; + const uintmax_t RemovedCount = std::filesystem::remove_all(Item, ErrorCode); + + Success = Success && !ErrorCode && RemovedCount; } return Success; } return CreateDirectories(Dir); -#endif } bool -- cgit v1.2.3