Skip to content

Commit

Permalink
[Filesystem] mirror - fix copying content with same name as source/ta…
Browse files Browse the repository at this point in the history
…rget.
  • Loading branch information
gitlost authored and fabpot committed Sep 13, 2017
1 parent 3c9958c commit b524c84
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Symfony/Component/Filesystem/Filesystem.php
Expand Up @@ -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']) {
Expand All @@ -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);
}
Expand All @@ -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)) {
Expand Down
47 changes: 47 additions & 0 deletions src/Symfony/Component/Filesystem/Tests/FilesystemTest.php
Expand Up @@ -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
*/
Expand Down

0 comments on commit b524c84

Please sign in to comment.