Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Filesystem] mirror - fix copying content with same name as source/target. #23473

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Symfony/Component/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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