8000 bug #23473 [Filesystem] mirror - fix copying content with same name a… · symfony/symfony@5bf241a · GitHub
[go: up one dir, main page]

Skip to content

Commit 5bf241a

Browse files
committed
bug #23473 [Filesystem] mirror - fix copying content with same name as source/target. (gitlost)
This PR was squashed before being merged into the 2.7 branch (closes #23473). Discussion ---------- [Filesystem] mirror - fix copying content with same name as source/target. | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #23472 | License | MIT | Doc PR | Uses `substr()` and lengths in `Filesystem::mirror()` rather than `str_replace()` to avoid multiple replacements. Commits ------- b524c84 [Filesystem] mirror - fix copying content with same name as source/target.
2 parents cd83745 + b524c84 commit 5bf241a

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

src/Symfony/Component/Filesystem/Filesystem.php

Lines 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

Lines 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