diff --git a/src/Symfony/Component/Filesystem/Filesystem.php b/src/Symfony/Component/Filesystem/Filesystem.php index 046ca4551f250..9240a215c656b 100644 --- a/src/Symfony/Component/Filesystem/Filesystem.php +++ b/src/Symfony/Component/Filesystem/Filesystem.php @@ -541,6 +541,10 @@ public function mirror($originDir, $targetDir, \Traversable $iterator = null, $o $originDir = rtrim($originDir, '/\\'); $originDirLen = \strlen($originDir); + if (!$this->exists($originDir)) { + throw new IOException(sprintf('The origin directory specified "%s" was not found.', $originDir), 0, null, $originDir); + } + // Iterate in destination folder to remove obsolete entries if ($this->exists($targetDir) && isset($options['delete']) && $options['delete']) { $deleteIterator = $iterator; @@ -564,35 +568,24 @@ public function mirror($originDir, $targetDir, \Traversable $iterator = null, $o $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($originDir, $flags), \RecursiveIteratorIterator::SELF_FIRST); } - if ($this->exists($originDir)) { - $this->mkdir($targetDir); - } + $this->mkdir($targetDir); + $targetDirInfo = new \SplFileInfo($targetDir); foreach ($iterator as $file) { - if (false === strpos($file->getPath(), $originDir)) { - throw new IOException(sprintf('Unable to mirror "%s" directory. If the origin directory is relative, try using "realpath" before calling the mirror method.', $originDir), 0, null, $originDir); + if ($file->getPathName() === $targetDir || $file->getRealPath() === $targetDir || 0 === strpos($file->getRealPath(), $targetDirInfo->getRealPath())) { + continue; } $target = $targetDir.substr($file->getPathname(), $originDirLen); - if ($copyOnWindows) { - if (is_file($file)) { - $this->copy($file, $target, isset($options['override']) ? $options['override'] : false); - } elseif (is_dir($file)) { - $this->mkdir($target); - } else { - throw new IOException(sprintf('Unable to guess "%s" file type.', $file), 0, null, $file); - } + if (!$copyOnWindows && is_link($file)) { + $this->symlink($file->getLinkTarget(), $target); + } elseif (is_dir($file)) { + $this->mkdir($target); + } elseif (is_file($file)) { + $this->copy($file, $target, isset($options['override']) ? $options['override'] : false); } else { - if (is_link($file)) { - $this->symlink($file->getLinkTarget(), $target); - } elseif (is_dir($file)) { - $this->mkdir($target); - } elseif (is_file($file)) { - $this->copy($file, $target, isset($options['override']) ? $options['override'] : false); - } else { - throw new IOException(sprintf('Unable to guess "%s" file type.', $file), 0, null, $file); - } + throw new IOException(sprintf('Unable to guess "%s" file type.', $file), 0, null, $file); } } } diff --git a/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php index 24df7a765c914..a1a3ce0e6e444 100644 --- a/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php +++ b/src/Symfony/Component/Filesystem/Tests/FilesystemTest.php @@ -1332,44 +1332,34 @@ public function testMirrorContentsWithSameNameAsSourceOrTargetWithDeleteOption() $this->assertFileNotExists($targetPath.'target'); } - public function testMirrorWithCustomIterator() + public function testMirrorAvoidCopyingTargetDirectoryIfInSourceDirectory() { $sourcePath = $this->workspace.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR; - mkdir($sourcePath); - - $file = $sourcePath.\DIRECTORY_SEPARATOR.'file'; - file_put_contents($file, 'FILE'); - - $targetPath = $this->workspace.\DIRECTORY_SEPARATOR.'target'.\DIRECTORY_SEPARATOR; + $directory = $sourcePath.'directory'.\DIRECTORY_SEPARATOR; + $file1 = $directory.'file1'; + $file2 = $sourcePath.'file2'; - $splFile = new \SplFileInfo($file); - $iterator = new \ArrayObject([$splFile]); + mkdir($sourcePath); + mkdir($directory); + file_put_contents($file1, 'FILE1'); + file_put_contents($file2, 'FILE2'); - $this->filesystem->mirror($sourcePath, $targetPath, $iterator); + $targetPath = $sourcePath.'target'.\DIRECTORY_SEPARATOR; - $this->assertTrue(is_dir($targetPath)); - $this->assertFileEquals($file, $targetPath.\DIRECTORY_SEPARATOR.'file'); - } - - /** - * @expectedException \Symfony\Component\Filesystem\Exception\IOException - * @expectedExceptionMessageRegExp /Unable to mirror "(.*)" directory/ - */ - public function testMirrorWithCustomIteratorWithRelativePath() - { - $sourcePath = $this->workspace.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR; - $realSourcePath = $this->workspace.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR; - mkdir($realSourcePath); + if ('\\' !== \DIRECTORY_SEPARATOR) { + $this->filesystem->symlink($targetPath, $sourcePath.'target_simlink'); + } - $file = $realSourcePath.'file'; - file_put_contents($file, 'FILE'); + $this->filesystem->mirror($sourcePath, $targetPath, null, ['delete' => true]); - $targetPath = $this->workspace.\DIRECTORY_SEPARATOR.'target'.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'target'.\DIRECTORY_SEPARATOR; + $this->assertTrue($this->filesystem->exists($targetPath)); + $this->assertTrue($this->filesystem->exists($targetPath.'directory')); - $splFile = new \SplFileInfo($file); - $iterator = new \ArrayObject([$splFile]); + $this->assertFileEquals($file1, $targetPath.'directory'.\DIRECTORY_SEPARATOR.'file1'); + $this->assertFileEquals($file2, $targetPath.'file2'); - $this->filesystem->mirror($sourcePath, $targetPath, $iterator); + $this->assertFalse($this->filesystem->exists($targetPath.'target_simlink')); + $this->assertFalse($this->filesystem->exists($targetPath.'target')); } /** pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy