Merge pull request #9158 from owncloud/no-recursion-on-rmdirr-master

fix recursion on rmdirr
This commit is contained in:
Thomas Müller 2014-06-23 23:18:10 +02:00
commit f913b1629b
2 changed files with 35 additions and 6 deletions

View File

@ -405,12 +405,17 @@ class OC_Helper {
*/ */
static function rmdirr($dir) { static function rmdirr($dir) {
if (is_dir($dir)) { if (is_dir($dir)) {
$files = scandir($dir); $files = new RecursiveIteratorIterator(
// FIXME: use flat array instead of recursion to avoid new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),
// too many levels RecursiveIteratorIterator::CHILD_FIRST
foreach ($files as $file) { );
if ($file !== '' && $file !== "." && $file !== "..") {
self::rmdirr("$dir/$file"); foreach ($files as $fileInfo) {
/** @var SplFileInfo $fileInfo */
if ($fileInfo->isDir()) {
rmdir($fileInfo->getRealPath());
} else {
unlink($fileInfo->getRealPath());
} }
} }
rmdir($dir); rmdir($dir);

View File

@ -454,4 +454,28 @@ class Test_Helper extends PHPUnit_Framework_TestCase {
$this->assertEquals('http://localhost/owncloud/public.php?service=files', $result); $this->assertEquals('http://localhost/owncloud/public.php?service=files', $result);
} }
/**
* Tests recursive folder deletion with rmdirr()
*/
public function testRecursiveFolderDeletion() {
$baseDir = \OC_Helper::tmpFolder() . '/';
mkdir($baseDir . 'a/b/c/d/e', 0777, true);
mkdir($baseDir . 'a/b/c1/d/e', 0777, true);
mkdir($baseDir . 'a/b/c2/d/e', 0777, true);
mkdir($baseDir . 'a/b1/c1/d/e', 0777, true);
mkdir($baseDir . 'a/b2/c1/d/e', 0777, true);
mkdir($baseDir . 'a/b3/c1/d/e', 0777, true);
mkdir($baseDir . 'a1/b', 0777, true);
mkdir($baseDir . 'a1/c', 0777, true);
file_put_contents($baseDir . 'a/test.txt', 'Hello file!');
file_put_contents($baseDir . 'a/b1/c1/test one.txt', 'Hello file one!');
file_put_contents($baseDir . 'a1/b/test two.txt', 'Hello file two!');
\OC_Helper::rmdirr($baseDir . 'a');
$this->assertFalse(file_exists($baseDir . 'a'));
$this->assertTrue(file_exists($baseDir . 'a1'));
\OC_Helper::rmdirr($baseDir);
$this->assertFalse(file_exists($baseDir));
}
} }