Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b524c84

Browse filesBrowse files
gitlostfabpot
authored andcommitted
[Filesystem] mirror - fix copying content with same name as source/target.
1 parent 3c9958c commit b524c84
Copy full SHA for b524c84

File tree

2 files changed

+51
-2
lines changed
Filter options

2 files changed

+51
-2
lines changed

‎src/Symfony/Component/Filesystem/Filesystem.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Filesystem/Filesystem.php
+4-2Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ public function mirror($originDir, $targetDir, \Traversable $iterator = null, $o
444444
{
445445
$targetDir = rtrim($targetDir, '/\\');
446446
$originDir = rtrim($originDir, '/\\');
447+
$originDirLen = strlen($originDir);
447448

448449
// Iterate in destination folder to remove obsolete entries
449450
if ($this->exists($targetDir) && isset($options['delete']) && $options['delete']) {
@@ -452,8 +453,9 @@ public function mirror($originDir, $targetDir, \Traversable $iterator = null, $o
452453
$flags = \FilesystemIterator::SKIP_DOTS;
453454
$deleteIterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($targetDir, $flags), \RecursiveIteratorIterator::CHILD_FIRST);
454455
}
456+
$targetDirLen = strlen($targetDir);
455457
foreach ($deleteIterator as $file) {
456-
$origin = str_replace($targetDir, $originDir, $file->getPathname());
458+
$origin = $originDir.substr($file->getPathname(), $targetDirLen);
457459
if (!$this->exists($origin)) {
458460
$this->remove($file);
459461
}
@@ -475,7 +477,7 @@ public function mirror($originDir, $targetDir, \Traversable $iterator = null, $o
475477
}
476478

477479
foreach ($iterator as $file) {
478-
$target = str_replace($originDir, $targetDir, $file->getPathname());
480+
$target = $targetDir.substr($file->getPathname(), $originDirLen);
479481

480482
if ($copyOnWindows) {
481483
if (is_file($file)) {

‎src/Symfony/Component/Filesystem/Tests/FilesystemTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Filesystem/Tests/FilesystemTest.php
+47Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,53 @@ public function testMirrorCopiesRelativeLinkedContents()
10091009
$this->assertEquals('\\' === DIRECTORY_SEPARATOR ? realpath($sourcePath.'\nested') : 'nested', readlink($targetPath.DIRECTORY_SEPARATOR.'link1'));
10101010
}
10111011

1012+
public function testMirrorContentsWithSameNameAsSourceOrTargetWithoutDeleteOption()
1013+
{
1014+
$sourcePath = $this->workspace.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR;
1015+
1016+
mkdir($sourcePath);
1017+
touch($sourcePath.'source');
1018+
touch($sourcePath.'target');
1019+
1020+
$targetPath = $this->workspace.DIRECTORY_SEPARATOR.'target'.DIRECTORY_SEPARATOR;
1021+
1022+
$oldPath = getcwd();
1023+
chdir($this->workspace);
1024+
1025+
$this->filesystem->mirror('source', $targetPath);
1026+
1027+
chdir($oldPath);
1028+
1029+
$this->assertTrue(is_dir($targetPath));
1030+
$this->assertFileExists($targetPath.'source');
1031+
$this->assertFileExists($targetPath.'target');
1032+
}
1033+
1034+
public function testMirrorContentsWithSameNameAsSourceOrTargetWithDeleteOption()
1035+
{
1036+
$sourcePath = $this->workspace.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR;
1037+
1038+
mkdir($sourcePath);
1039+
touch($sourcePath.'source');
1040+
1041+
$targetPath = $this->workspace.DIRECTORY_SEPARATOR.'target'.DIRECTORY_SEPARATOR;
1042+
1043+
mkdir($targetPath);
1044+
touch($targetPath.'source');
1045+
touch($targetPath.'target');
1046+
1047+
$oldPath = getcwd();
1048+
chdir($this->workspace);
1049+
1050+
$this->filesystem->mirror('source', 'target', null, array('delete' => true));
1051+
1052+
chdir($oldPath);
1053+
1054+
$this->assertTrue(is_dir($targetPath));
1055+
$this->assertFileExists($targetPath.'source');
1056+
$this->assertFileNotExists($targetPath.'target');
1057+
}
1058+
10121059
/**
10131060
* @dataProvider providePathsForIsAbsolutePath
10141061
*/

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.