From b524c84ce104b285a0c490c712582b3f28fe12eb Mon Sep 17 00:00:00 2001 From: gitlost Date: Tue, 11 Jul 2017 04:43:03 +0100 Subject: [PATCH] [Filesystem] mirror - fix copying content with same name as source/target. --- .../Component/Filesystem/Filesystem.php | 6 ++- .../Filesystem/Tests/FilesystemTest.php | 47 +++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Filesystem/Filesystem.php b/src/Symfony/Component/Filesystem/Filesystem.php index bc2e3dcc2d89..b563dcc3751f 100644 --- a/src/Symfony/Component/Filesystem/Filesystem.php +++ b/src/Symfony/Component/Filesystem/Filesystem.php @@ -444,6 +444,7 @@ public function mirror($originDir, $targetDir, \Traversable $iterator = null, $o { $targetDir = rtrim($targetDir, '/\\'); $originDir = rtrim($originDir, '/\\'); + $originDirLen = strlen($originDir); // Iterate in destination folder to remove obsolete entries if ($this->exists($targetDir) && isset($options['delete']) && $options['delete']) { @@ -452,8 +453,9 @@ public function mirror($originDir, $targetDir, \Traversable $iterator = null, $o $flags = \FilesystemIterator::SKIP_DOTS; $deleteIterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($targetDir, $flags), \RecursiveIteratorIterator::CHILD_FIRST); } + $targetDirLen = strlen($targetDir); foreach ($deleteIterator as $file) { - $origin = str_replace($targetDir, $originDir, $file->getPathname()); + $origin = $originDir.substr($file->getPathname(), $targetDirLen); if (!$this->exists($origin)) { $this->remove($file); } @@ -475,7 +477,7 @@ public function mirror($originDir, $targetDir, \Traversable $iterator = null, $o } foreach ($iterator as $file) { - $target = str_replace($originDir, $targetDir, $file->getPathname()); + $target = $targetDir.substr($file->getPathname(), $originDirLen); if ($copyOnWindows) { if (is_file($file)) { diff --git a/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php index b7bdfac4155e..a4d111c218c6 100644 --- a/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php +++ b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php @@ -1009,6 +1009,53 @@ public function testMirrorCopiesRelativeLinkedContents() $this->assertEquals('\\' === DIRECTORY_SEPARATOR ? realpath($sourcePath.'\nested') : 'nested', readlink($targetPath.DIRECTORY_SEPARATOR.'link1')); } + public function testMirrorContentsWithSameNameAsSourceOrTargetWithoutDeleteOption() + { + $sourcePath = $this->workspace.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR; + + mkdir($sourcePath); + touch($sourcePath.'source'); + touch($sourcePath.'target'); + + $targetPath = $this->workspace.DIRECTORY_SEPARATOR.'target'.DIRECTORY_SEPARATOR; + + $oldPath = getcwd(); + chdir($this->workspace); + + $this->filesystem->mirror('source', $targetPath); + + chdir($oldPath); + + $this->assertTrue(is_dir($targetPath)); + $this->assertFileExists($targetPath.'source'); + $this->assertFileExists($targetPath.'target'); + } + + public function testMirrorContentsWithSameNameAsSourceOrTargetWithDeleteOption() + { + $sourcePath = $this->workspace.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR; + + mkdir($sourcePath); + touch($sourcePath.'source'); + + $targetPath = $this->workspace.DIRECTORY_SEPARATOR.'target'.DIRECTORY_SEPARATOR; + + mkdir($targetPath); + touch($targetPath.'source'); + touch($targetPath.'target'); + + $oldPath = getcwd(); + chdir($this->workspace); + + $this->filesystem->mirror('source', 'target', null, array('delete' => true)); + + chdir($oldPath); + + $this->assertTrue(is_dir($targetPath)); + $this->assertFileExists($targetPath.'source'); + $this->assertFileNotExists($targetPath.'target'); + } + /** * @dataProvider providePathsForIsAbsolutePath */