From 63c898c064233d08823e1b18ec7cb20185b1fe05 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Thu, 6 Jun 2013 20:47:20 +0200 Subject: [PATCH] Make rmdir recursive for local storage --- lib/files/storage/local.php | 22 +++++++++++++++++++++- tests/lib/files/storage/storage.php | 14 +++++++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/lib/files/storage/local.php b/lib/files/storage/local.php index d684905bf9a..b08fd73ce19 100644 --- a/lib/files/storage/local.php +++ b/lib/files/storage/local.php @@ -39,7 +39,27 @@ if (\OC_Util::runningOnWindows()) { } public function rmdir($path) { - return @rmdir($this->datadir . $path); + try { + $it = new \RecursiveIteratorIterator( + new \RecursiveDirectoryIterator($this->datadir . $path), + \RecursiveIteratorIterator::CHILD_FIRST + ); + foreach ($it as $file) { + /** + * @var \SplFileInfo $file + */ + if (in_array($file->getBasename(), array('.', '..'))) { + continue; + } elseif ($file->isDir()) { + rmdir($file->getPathname()); + } elseif ($file->isFile() || $file->isLink()) { + unlink($file->getPathname()); + } + } + return rmdir($this->datadir . $path); + } catch (\UnexpectedValueException $e) { + return false; + } } public function opendir($path) { diff --git a/tests/lib/files/storage/storage.php b/tests/lib/files/storage/storage.php index 0e22f26ae83..fb3e05e66b3 100644 --- a/tests/lib/files/storage/storage.php +++ b/tests/lib/files/storage/storage.php @@ -258,9 +258,21 @@ abstract class Storage extends \PHPUnit_Framework_TestCase { $this->assertEquals(file_get_contents($textFile), $content); } - public function testTouchCreateFile(){ + public function testTouchCreateFile() { $this->assertFalse($this->instance->file_exists('foo')); $this->instance->touch('foo'); $this->assertTrue($this->instance->file_exists('foo')); } + + public function testRecursiveRmdir() { + $this->instance->mkdir('folder'); + $this->instance->mkdir('folder/bar'); + $this->instance->file_put_contents('folder/asd.txt', 'foobar'); + $this->instance->file_put_contents('folder/bar/foo.txt', 'asd'); + $this->instance->rmdir('folder'); + $this->assertFalse($this->instance->file_exists('folder/asd.txt')); + $this->assertFalse($this->instance->file_exists('folder/bar/foo.txt')); + $this->assertFalse($this->instance->file_exists('folder/bar')); + $this->assertFalse($this->instance->file_exists('folder')); + } } -- 2.39.5