From 517d21ade4c08c44b00ba513b6fd4018642b8b9d Mon Sep 17 00:00:00 2001 From: Josua Hunziker Date: Sat, 27 May 2017 08:45:40 +0200 Subject: [PATCH] Refactor recursiveDelete The recursiveDelete method causes problems in certain environments with the RecursiveDirectoryIterator not iterating through all elements of a tree when deleting nodes while iterating. This is solved by first completing the iteration and only then deleting all elements. Apply refactoring of recursiveDelete also to lib/Updater.php Remove newline to make integration test happy Now really make the functions the same in index.php and Updater.php Signed-off-by: Morris Jobke --- index.php | 17 +++++++++++++++-- lib/Updater.php | 17 +++++++++++++++-- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/index.php b/index.php index 552e2625..12e8acdb 100644 --- a/index.php +++ b/index.php @@ -828,10 +828,23 @@ private function recursiveDelete($folder) { \RecursiveIteratorIterator::CHILD_FIRST ); + $directories = array(); + $files = array(); foreach ($iterator as $fileInfo) { - $action = $fileInfo->isDir() ? 'rmdir' : 'unlink'; - $action($fileInfo->getRealPath()); + if ($fileInfo->isDir()) { + $directories[] = $fileInfo->getRealPath(); + } else { + $files[] = $fileInfo->getRealPath(); + } + } + + foreach ($files as $file) { + unlink($file); + } + foreach ($directories as $dir) { + rmdir($dir); } + $state = rmdir($folder); if($state === false) { throw new \Exception('Could not rmdir ' . $folder); diff --git a/lib/Updater.php b/lib/Updater.php index 5d0b4808..da1063ea 100644 --- a/lib/Updater.php +++ b/lib/Updater.php @@ -713,10 +713,23 @@ private function recursiveDelete($folder) { \RecursiveIteratorIterator::CHILD_FIRST ); + $directories = array(); + $files = array(); foreach ($iterator as $fileInfo) { - $action = $fileInfo->isDir() ? 'rmdir' : 'unlink'; - $action($fileInfo->getRealPath()); + if ($fileInfo->isDir()) { + $directories[] = $fileInfo->getRealPath(); + } else { + $files[] = $fileInfo->getRealPath(); + } + } + + foreach ($files as $file) { + unlink($file); + } + foreach ($directories as $dir) { + rmdir($dir); } + $state = rmdir($folder); if($state === false) { throw new \Exception('Could not rmdir ' . $folder);