8000 [Filesystem] Fix mirroring a directory into itself or in his child with realpath checks by XuruDragon · Pull Request #30116 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Filesystem] Fix mirroring a directory into itself or in his child with realpath checks #30116

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

Merged
merged 2 commits into from
Mar 31, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Skipping iterations in the mirror() method where the iterated file's …
…path name is equal to the target path

Added a new test what is called testMirrorAvoidCopyingTargetDirectoryIfInSourceDirectory

Adjustments to comply with the Symfony Code Standards
  • Loading branch information
Fleuv authored and Anthony MARTIN committed Mar 27, 2019
commit 59437a4af9790edbc729dc783f3d8f0f6aeefd99
4 changes: 4 additions & 0 deletions src/Symfony/Component/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,10 @@ public function mirror($originDir, $targetDir, \Traversable $iterator = null, $o
}

foreach ($iterator as $file) {
if ($file->getPathName() === $targetDir) {
continue;
}

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);
}
Expand Down
25 changes: 25 additions & 0 deletions src/Symfony/Component/Filesystem/Tests/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,31 @@ public function testMirrorWithCustomIteratorWithRelativePath()
$this->filesystem->mirror($sourcePath, $targetPath, $iterator);
}

public function testMirrorAvoidCopyingTargetDirectoryIfInSourceDirectory()
{
$sourcePath = $this->workspace.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR;
$directory = $sourcePath.'directory'.\DIRECTORY_SEPARATOR;
$file1 = $directory.'file1';
$file2 = $sourcePath.'file2';

mkdir($sourcePath);
mkdir($directory);
file_put_contents($file1, 'FILE1');
file_put_contents($file2, 'FILE2');

$targetPath = $sourcePath.'target'.\DIRECTORY_SEPARATOR;

5EBA $this->filesystem->mirror($sourcePath, $targetPath);

$this->assertTrue(is_dir($targetPath));
$this->assertTrue(is_dir($targetPath.'directory'));

$this->assertFileEquals($file1, $targetPath.'directory'.\DIRECTORY_SEPARATOR.'file1');
$this->assertFileEquals($file2, $targetPath.'file2');

$this->assertFileNotExists($targetPath.'target');
}

/**
* @dataProvider providePathsForIsAbsolutePath
*/
Expand Down
0