From 65aadc4200cbd9396879e70b1401b20fcec43ea5 Mon Sep 17 00:00:00 2001 From: gitlost Date: Tue, 11 Jul 2017 04:43:03 +0100 Subject: [PATCH 1/4] [Filesystem] mirror - fix copying content with same name as source/target. --- .../Component/Filesystem/Filesystem.php | 6 ++-- .../Filesystem/Tests/FilesystemTest.php | 30 +++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Filesystem/Filesystem.php b/src/Symfony/Component/Filesystem/Filesystem.php index bc2e3dcc2d897..34d15626d93bb 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 b7bdfac4155e0..dc896f8e2d70c 100644 --- a/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php +++ b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php @@ -1009,6 +1009,36 @@ public function testMirrorCopiesRelativeLinkedContents() $this->assertEquals('\\' === DIRECTORY_SEPARATOR ? realpath($sourcePath.'\nested') : 'nested', readlink($targetPath.DIRECTORY_SEPARATOR.'link1')); } + public function testMirrorContentsWithSameNameAsSourceOrTarget() + { + $sourcePath = $this->workspace.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR; + + $oldPath = getcwd(); + chdir($this->workspace); + + mkdir($sourcePath); + touch($sourcePath.'source'); + touch($sourcePath.'target'); + + $targetPath = $this->workspace.DIRECTORY_SEPARATOR.'target'.DIRECTORY_SEPARATOR; + + $this->filesystem->mirror('source', $targetPath); + + $this->assertTrue(is_dir($targetPath)); + $this->assertFileExists($targetPath.'source'); + $this->assertFileExists($targetPath.'target'); + + unlink($sourcePath.'target'); + + $this->filesystem->mirror('source', 'target', null, array('delete' => true)); + + $this->assertTrue(is_dir($targetPath)); + $this->assertFileExists($targetPath.'source'); + $this->assertFileNotExists($targetPath.'target'); + + chdir($oldPath); + } + /** * @dataProvider providePathsForIsAbsolutePath */ From 444cf2236e6a9074fc130b37f222e100bd4e0848 Mon Sep 17 00:00:00 2001 From: gitlost Date: Tue, 11 Jul 2017 04:50:58 +0100 Subject: [PATCH 2/4] Replace tabs with spaces. --- src/Symfony/Component/Filesystem/Filesystem.php | 6 +++--- src/Symfony/Component/Filesystem/Tests/FilesystemTest.php | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/Filesystem/Filesystem.php b/src/Symfony/Component/Filesystem/Filesystem.php index 34d15626d93bb..b563dcc3751f2 100644 --- a/src/Symfony/Component/Filesystem/Filesystem.php +++ b/src/Symfony/Component/Filesystem/Filesystem.php @@ -444,7 +444,7 @@ public function mirror($originDir, $targetDir, \Traversable $iterator = null, $o { $targetDir = rtrim($targetDir, '/\\'); $originDir = rtrim($originDir, '/\\'); - $originDirLen = strlen($originDir); + $originDirLen = strlen($originDir); // Iterate in destination folder to remove obsolete entries if ($this->exists($targetDir) && isset($options['delete']) && $options['delete']) { @@ -453,9 +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); + $targetDirLen = strlen($targetDir); foreach ($deleteIterator as $file) { - $origin = $originDir.substr($file->getPathname(), $targetDirLen); + $origin = $originDir.substr($file->getPathname(), $targetDirLen); if (!$this->exists($origin)) { $this->remove($file); } diff --git a/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php index dc896f8e2d70c..5550f36420f70 100644 --- a/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php +++ b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php @@ -1018,7 +1018,7 @@ public function testMirrorContentsWithSameNameAsSourceOrTarget() mkdir($sourcePath); touch($sourcePath.'source'); - touch($sourcePath.'target'); + touch($sourcePath.'target'); $targetPath = $this->workspace.DIRECTORY_SEPARATOR.'target'.DIRECTORY_SEPARATOR; @@ -1028,7 +1028,7 @@ public function testMirrorContentsWithSameNameAsSourceOrTarget() $this->assertFileExists($targetPath.'source'); $this->assertFileExists($targetPath.'target'); - unlink($sourcePath.'target'); + unlink($sourcePath.'target'); $this->filesystem->mirror('source', 'target', null, array('delete' => true)); @@ -1037,7 +1037,7 @@ public function testMirrorContentsWithSameNameAsSourceOrTarget() $this->assertFileNotExists($targetPath.'target'); chdir($oldPath); - } + } /** * @dataProvider providePathsForIsAbsolutePath From a12161f7b57c48326b7233ceda843d0926714c90 Mon Sep 17 00:00:00 2001 From: gitlost Date: Sun, 13 Aug 2017 12:29:09 +0100 Subject: [PATCH 3/4] Split test into 2 cases. --- .../Filesystem/Tests/FilesystemTest.php | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php index 5550f36420f70..88ed974137d25 100644 --- a/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php +++ b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php @@ -1009,7 +1009,7 @@ public function testMirrorCopiesRelativeLinkedContents() $this->assertEquals('\\' === DIRECTORY_SEPARATOR ? realpath($sourcePath.'\nested') : 'nested', readlink($targetPath.DIRECTORY_SEPARATOR.'link1')); } - public function testMirrorContentsWithSameNameAsSourceOrTarget() + public function testMirrorContentsWithSameNameAsSourceOrTargetWithoutDeleteOption() { $sourcePath = $this->workspace.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR; @@ -1028,7 +1028,24 @@ public function testMirrorContentsWithSameNameAsSourceOrTarget() $this->assertFileExists($targetPath.'source'); $this->assertFileExists($targetPath.'target'); - unlink($sourcePath.'target'); + chdir($oldPath); + } + + public function testMirrorContentsWithSameNameAsSourceOrTargetWithDeleteOption() + { + $sourcePath = $this->workspace.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR; + + $oldPath = getcwd(); + chdir($this->workspace); + + mkdir($sourcePath); + touch($sourcePath.'source'); + + $targetPath = $this->workspace.DIRECTORY_SEPARATOR.'target'.DIRECTORY_SEPARATOR; + + mkdir($targetPath); + touch($targetPath.'source'); + touch($targetPath.'target'); $this->filesystem->mirror('source', 'target', null, array('delete' => true)); From ff39f615a5674307eae4e458ee8c4c4d34db3158 Mon Sep 17 00:00:00 2001 From: gitlost Date: Mon, 14 Aug 2017 13:04:06 +0100 Subject: [PATCH 4/4] Move getcwd/chdir to before/after mirror call. --- .../Filesystem/Tests/FilesystemTest.php | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php index 88ed974137d25..a4d111c218c6a 100644 --- a/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php +++ b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php @@ -1013,31 +1013,28 @@ public function testMirrorContentsWithSameNameAsSourceOrTargetWithoutDeleteOptio { $sourcePath = $this->workspace.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR; - $oldPath = getcwd(); - chdir($this->workspace); - 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'); - - chdir($oldPath); } public function testMirrorContentsWithSameNameAsSourceOrTargetWithDeleteOption() { $sourcePath = $this->workspace.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR; - $oldPath = getcwd(); - chdir($this->workspace); - mkdir($sourcePath); touch($sourcePath.'source'); @@ -1047,13 +1044,16 @@ public function testMirrorContentsWithSameNameAsSourceOrTargetWithDeleteOption() 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'); - - chdir($oldPath); } /**