diff options
author | Robin Appelman <icewind@owncloud.com> | 2013-07-01 17:57:40 +0200 |
---|---|---|
committer | Robin Appelman <icewind@owncloud.com> | 2014-05-28 18:16:23 +0200 |
commit | 03ba497a8c1e149857d6c3a4d4cab49dea5903c1 (patch) | |
tree | acc2cac257a525bb388d81842ba675358b00efc7 | |
parent | 38c1da09768d034ee788f0c6a4284591e914fe4a (diff) | |
download | nextcloud-server-03ba497a8c1e149857d6c3a4d4cab49dea5903c1.tar.gz nextcloud-server-03ba497a8c1e149857d6c3a4d4cab49dea5903c1.zip |
add recursive copy to local storage backend
-rw-r--r-- | lib/private/files/storage/local.php | 26 | ||||
-rw-r--r-- | tests/lib/files/storage/storage.php | 65 |
2 files changed, 84 insertions, 7 deletions
diff --git a/lib/private/files/storage/local.php b/lib/private/files/storage/local.php index ec28ebac6ee..dc6e7a12c65 100644 --- a/lib/private/files/storage/local.php +++ b/lib/private/files/storage/local.php @@ -179,20 +179,34 @@ if (\OC_Util::runningOnWindows()) { if ($this->is_dir($path2)) { $this->rmdir($path2); + } else if ($this->is_file($path2)) { + $this->unlink($path2); } return rename($this->datadir . $path1, $this->datadir . $path2); } public function copy($path1, $path2) { - if ($this->is_dir($path2)) { - if (!$this->file_exists($path2)) { - $this->mkdir($path2); + if ($this->is_dir($path1)) { + if ($this->is_dir($path2)) { + $this->rmdir($path2); + } else if ($this->is_file($path2)) { + $this->unlink($path2); + } + $dir = $this->opendir($path1); + $this->mkdir($path2); + while ($file = readdir($dir)) { + if (($file != '.') && ($file != '..')) { + if (!$this->copy($path1 . '/' . $file, $path2 . '/' . $file)) { + return false; + } + } } - $source = substr($path1, strrpos($path1, '/') + 1); - $path2 .= $source; + closedir($dir); + return true; + } else { + return copy($this->datadir . $path1, $this->datadir . $path2); } - return copy($this->datadir . $path1, $this->datadir . $path2); } public function fopen($path, $mode) { diff --git a/tests/lib/files/storage/storage.php b/tests/lib/files/storage/storage.php index 4a4626fc5c5..24390f05367 100644 --- a/tests/lib/files/storage/storage.php +++ b/tests/lib/files/storage/storage.php @@ -373,7 +373,7 @@ abstract class Storage extends \PHPUnit_Framework_TestCase { $this->assertFalse($this->instance->file_exists('source/test1.txt')); $this->assertFalse($this->instance->file_exists('source/test2.txt')); $this->assertFalse($this->instance->file_exists('source/subfolder')); - $this->assertFalse($this->instance->file_exists('source/test.txt')); + $this->assertFalse($this->instance->file_exists('source/subfolder/test.txt')); $this->assertTrue($this->instance->file_exists('target')); $this->assertTrue($this->instance->file_exists('target/test1.txt')); @@ -401,4 +401,67 @@ abstract class Storage extends \PHPUnit_Framework_TestCase { $this->assertFalse($this->instance->file_exists('target/test2.txt')); $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt')); } + + public function testRenameOverWriteDirectoryOverFile() { + $this->instance->mkdir('source'); + $this->instance->file_put_contents('source/test1.txt', 'foo'); + + $this->instance->file_put_contents('target', 'bar'); + + $this->instance->rename('source', 'target'); + + $this->assertFalse($this->instance->file_exists('source')); + $this->assertFalse($this->instance->file_exists('source/test1.txt')); + $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt')); + } + + public function testCopyDirectory() { + $this->instance->mkdir('source'); + $this->instance->file_put_contents('source/test1.txt', 'foo'); + $this->instance->file_put_contents('source/test2.txt', 'qwerty'); + $this->instance->mkdir('source/subfolder'); + $this->instance->file_put_contents('source/subfolder/test.txt', 'bar'); + $this->instance->copy('source', 'target'); + + $this->assertTrue($this->instance->file_exists('source')); + $this->assertTrue($this->instance->file_exists('source/test1.txt')); + $this->assertTrue($this->instance->file_exists('source/test2.txt')); + $this->assertTrue($this->instance->file_exists('source/subfolder')); + $this->assertTrue($this->instance->file_exists('source/subfolder/test.txt')); + + $this->assertTrue($this->instance->file_exists('target')); + $this->assertTrue($this->instance->file_exists('target/test1.txt')); + $this->assertTrue($this->instance->file_exists('target/test2.txt')); + $this->assertTrue($this->instance->file_exists('target/subfolder')); + $this->assertTrue($this->instance->file_exists('target/subfolder/test.txt')); + + $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt')); + $this->assertEquals('qwerty', $this->instance->file_get_contents('target/test2.txt')); + $this->assertEquals('bar', $this->instance->file_get_contents('target/subfolder/test.txt')); + } + + public function testCopyOverWriteDirectory() { + $this->instance->mkdir('source'); + $this->instance->file_put_contents('source/test1.txt', 'foo'); + + $this->instance->mkdir('target'); + $this->instance->file_put_contents('target/test1.txt', 'bar'); + $this->instance->file_put_contents('target/test2.txt', 'bar'); + + $this->instance->copy('source', 'target'); + + $this->assertFalse($this->instance->file_exists('target/test2.txt')); + $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt')); + } + + public function testCopyOverWriteDirectoryOverFile() { + $this->instance->mkdir('source'); + $this->instance->file_put_contents('source/test1.txt', 'foo'); + + $this->instance->file_put_contents('target', 'bar'); + + $this->instance->copy('source', 'target'); + + $this->assertEquals('foo', $this->instance->file_get_contents('target/test1.txt')); + } } |