8000 [Filesystem] Fix mirroring a directory with a relative path and a cus… · symfony/symfony@27b673c · GitHub
[go: up one dir, main page]

Skip to content

Commit 27b673c

Browse files
Frederic Godfrinfabpot
authored andcommitted
[Filesystem] Fix mirroring a directory with a relative path and a custom iterator
1 parent c01359e commit 27b673c

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/Symfony/Component/Filesystem/Filesystem.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,10 @@ public function mirror($originDir, $targetDir, \Traversable $iterator = null, $o
572572
}
573573

574574
foreach ($iterator as $file) {
575+
if (false === strpos($file->getPath(), $originDir)) {
576+
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);
577+
}
578+
575579
$target = $targetDir.substr($file->getPathname(), $originDirLen);
576580

577581
if ($copyOnWindows) {

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,6 +1332,46 @@ public function testMirrorContentsWithSameNameAsSourceOrTargetWithDeleteOption()
13321332
$this->assertFileNotExists($targetPath.'target');
13331333
}
13341334

1335+
public function testMirrorWithCustomIterator()
1336+
{
1337+
$sourcePath = $this->workspace.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR;
1338+
mkdir($sourcePath);
1339+
1340+
$file = $sourcePath.DIRECTORY_SEPARATOR.'file';
1341+
file_put_contents($file, 'FILE');
1342+
1343+
$targetPath = $this->workspace.DIRECTORY_SEPARATOR.'target'.DIRECTORY_SEPARATOR;
1344+
1345+
$splFile = new \SplFileInfo($file);
1346+
$iterator = new \ArrayObject(array($splFile));
1347+
1348+
$this->filesystem->mirror($sourcePath, $targetPath, $iterator);
1349+
1350+
$this->assertTrue(is_dir($targetPath));
1351+
$this->assertFileEquals($file, $targetPath.DIRECTORY_SEPARATOR.'file');
1352+
}
1353+
1354+
/**
1355+
* @expectedException \Symfony\Component\Filesystem\Exception\IOException
1356+
* @expectedExceptionMessageRegExp /Unable to mirror "(.*)" directory/
1357+
*/
1358+
public function testMirrorWithCustomIteratorWithRelativePath()
1359+
{
1360+
$sourcePath = $this->workspace.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR;
1361+
$realSourcePath = $this->workspace.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR;
1362+
mkdir($realSourcePath);
1363+
1364+
$file = $realSourcePath.'file';
1365+
file_put_contents($file, 'FILE');
1366+
1367+
$targetPath = $this->workspace.DIRECTORY_SEPARATOR.'target'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'target'.DIRECTORY_SEPARATOR;
1368+
1369+
$splFile = new \SplFileInfo($file);
1370+
$iterator = new \ArrayObject(array($splFile));
1371+
1372+
$this->filesystem->mirror($sourcePath, $targetPath, $iterator);
1373+
}
1374+
13351375
/**
13361376
* @dataProvider providePathsForIsAbsolutePath
13371377
*/

0 commit comments

Comments
 (0)
0