Bug #69003 [Opn->Nab]: RecursiveIteratorIterator breaks after removing directories on disk
| From: | requinix@php.net | Date: | Sat, 07 Feb 2015 10:19:48 +0000 |
| Subject: | Bug #69003 [Opn->Nab]: RecursiveIteratorIterator breaks after removing directories on disk | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-190505@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=69003&edit=1
ID: 69003
Updated by: requinix@php.net
Reported by: elproducer20 at gmail dot com
Summary: RecursiveIteratorIterator breaks after removing
directories on disk
-Status: Open
+Status: Not a bug
Type: Bug
Package: SPL related
Operating System: Ubuntu14, Debian7
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
You're recursing SELF_FIRST: the parent will be examined before its children are retrieved. If
you delete the parent then PHP won't be able to get the children.
Go CHILD_FIRST.
Previous Comments:
------------------------------------------------------------------------
[2015-02-07 08:00:45] elproducer20 at gmail dot com
Description:
------------
I think this is similar to this bug - https://bugs.php.net/bug.php?id=55468 except
I'm trying to delete directories but not sure why this happens. If i remove the line
"rmdir(...)" then script completes successfully printing all empty directory names to
output. Once the line "rmdir(...)" is added back, it breaks with the below stack trace.
I worked around this by saving all directory names, then removing directories after the foreach loop
completed.
Test script:
---------------
function deleteAllEmptyUserDirectories($path){
$iterator = new RecursiveDirectoryIterator($path);
$iterator->setFlags(RecursiveDirectoryIterator::SKIP_DOTS);
// Get directories only
$directories = new ParentIterator($iterator);
// Loop over directories and remove empty ones
foreach (new RecursiveIteratorIterator($directories, RecursiveIteratorIterator::SELF_FIRST) as
$dir) {
// Count the number of "children" from the main directory iterator
if (iterator_count($iterator->getChildren()) === 0) {
rmdir($dir->getPathname());
echo "[ DELETE ] " . $dir->getPathname() . "\n";
}
}
}
deleteAllEmptyUserDirectories("/home/me/somedir/content");
Expected result:
----------------
[ DELETE ] 0 | /home/me/somedir/content/test
[ DELETE ] 0 | /home/me/somedir/content/test2
Actual result:
--------------
[ DELETE ] 0 | /home/me/somedir/content/test
PHP Fatal error: Uncaught exception 'UnexpectedValueException' with message
'RecursiveDirectoryIterator::__construct(/home/me/somedir/content/test): failed to open dir: No
such file or directory' in /home/me/www/scripts/deleteAllEmptyUserDirectories.php:18
Stack trace:
#0 [internal function]: RecursiveDirectoryIterator->__construct('/home/me...', 4096)
#1 [internal function]: RecursiveDirectoryIterator->getChildren()
#2 /home/me/www/scripts/deleteAllEmptyUserDirectories.php(18):
RecursiveFilterIterator->getChildren()
#3 /home/me/www/scripts/deleteAllEmptyUserDirectories.php(27):
deleteAllEmptyUserDirectories('/home/me...')
#4 {main}
thrown in /home/me/www/scripts/deleteAllEmptyUserDirectories.php on line 18
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=69003&edit=1