From efad5d54529add4981a954e39c0941f55958d51f Mon Sep 17 00:00:00 2001 From: Jakub Zalas Date: Mon, 9 Apr 2012 15:14:36 +0100 Subject: [PATCH] [Filesystem] Prevented infiite loop on windows while calling mirror on symlink. Added test for mirroring symlinks. --- .../Component/Filesystem/Filesystem.php | 8 ++++---- .../Filesystem/Tests/FilesystemTest.php | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Component/Filesystem/Filesystem.php b/src/Symfony/Component/Filesystem/Filesystem.php index a63c5a313fbe..8d8c0361c84c 100644 --- a/src/Symfony/Component/Filesystem/Filesystem.php +++ b/src/Symfony/Component/Filesystem/Filesystem.php @@ -229,12 +229,12 @@ public function mirror($originDir, $targetDir, \Traversable $iterator = null, $o } foreach ($iterator as $file) { - $target = $targetDir.'/'.str_replace($originDir.DIRECTORY_SEPARATOR, '', $file->getPathname()); + $target = str_replace($originDir, $targetDir, $file->getPathname()); - if (is_link($file)) { - $this->symlink($file, $target); - } elseif (is_dir($file)) { + if (is_dir($file)) { $this->mkdir($target); + } elseif (!$copyOnWindows && is_link($file)) { + $this->symlink($file, $target); } elseif (is_file($file) || ($copyOnWindows && is_link($file))) { $this->copy($file, $target, isset($options['override']) ? $options['override'] : false); } else { diff --git a/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php index 80b5167231da..c2833f917ebe 100644 --- a/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php +++ b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php @@ -483,6 +483,25 @@ public function testMirrorCopiesFilesAndDirectoriesRecursively() $this->assertFileEquals($file2, $targetPath.'file2'); } + public function testMirrorCopiesLinks() + { + $this->markAsSkippeIfSymlinkIsMissing(); + + $sourcePath = $this->workspace.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR; + + mkdir($sourcePath); + file_put_contents($sourcePath.'file1', 'FILE1'); + symlink($sourcePath.'file1', $sourcePath.'link1'); + + $targetPath = $this->workspace.DIRECTORY_SEPARATOR.'target'.DIRECTORY_SEPARATOR; + + $this->filesystem->mirror($sourcePath, $targetPath); + + $this->assertTrue(is_dir($targetPath)); + $this->assertFileEquals($sourcePath.'file1', $targetPath.DIRECTORY_SEPARATOR.'link1'); + $this->assertTrue(is_link($targetPath.DIRECTORY_SEPARATOR.'link1')); + } + /** * @dataProvider providePathsForIsAbsolutePath */