Skip to content

Commit

Permalink
Refactor recursiveDelete
Browse files Browse the repository at this point in the history
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 <hey@morrisjobke.de>
  • Loading branch information
stjosh authored and MorrisJobke committed Aug 19, 2017
1 parent 6deedac commit 517d21a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
17 changes: 15 additions & 2 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 15 additions & 2 deletions lib/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 517d21a

Please sign in to comment.