From b2f133d2ac74e073a76367e18abe051abad5bbc1 Mon Sep 17 00:00:00 2001 From: Scott Mansell Date: Mon, 25 Apr 2016 18:03:39 +1200 Subject: [PATCH] make DeleteDirRecursively clean up correctly after failure. Fixes Metroid prime crashing the second boot after loading a save state (issue 9496) --- Source/Core/Common/FileUtil.cpp | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/Source/Core/Common/FileUtil.cpp b/Source/Core/Common/FileUtil.cpp index 5084bf545b..c4563c397f 100644 --- a/Source/Core/Common/FileUtil.cpp +++ b/Source/Core/Common/FileUtil.cpp @@ -530,6 +530,8 @@ FSTEntry ScanDirectoryTree(const std::string& directory, bool recursive) bool DeleteDirRecursively(const std::string& directory) { INFO_LOG(COMMON, "DeleteDirRecursively: %s", directory.c_str()); + bool success = true; + #ifdef _WIN32 // Find the first file in the directory. WIN32_FIND_DATA ffd; @@ -568,22 +570,16 @@ bool DeleteDirRecursively(const std::string& directory) { if (!DeleteDirRecursively(newPath)) { - #ifndef _WIN32 - closedir(dirp); - #endif - - return false; + success = false; + break; } } else { if (!File::Delete(newPath)) { - #ifndef _WIN32 - closedir(dirp); - #endif - - return false; + success = false; + break; } } @@ -594,9 +590,10 @@ bool DeleteDirRecursively(const std::string& directory) } closedir(dirp); #endif - File::DeleteDir(directory); + if (success) + File::DeleteDir(directory); - return true; + return success; } // Create directory and copy contents (does not overwrite existing files)