Bug #69003 [NEW]: RecursiveIteratorIterator breaks after removing directories on disk

From: Date: Sat, 07 Feb 2015 08:00:45 +0000
Subject: Bug #69003 [NEW]: RecursiveIteratorIterator breaks after removing directories on disk
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-190501@lists.php.net to get a copy of this message
From:             elproducer20 at gmail dot com
Operating system: Ubuntu14, Debian7
PHP version:      Irrelevant
Package:          SPL related
Bug Type:         Bug
Bug description:RecursiveIteratorIterator breaks after removing directories on disk

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 bug report at https://bugs.php.net/bug.php?id=69003&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=69003&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=69003&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=69003&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=69003&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=69003&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=69003&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=69003&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=69003&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=69003&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=69003&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=69003&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=69003&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=69003&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=69003&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=69003&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=69003&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=69003&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=69003&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=69003&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=69003&r=mysqlcfg



Thread (2 messages)

« previous php.bugs (#190501) next »