8000 Throw an exception if the target can't be reliably defined · symfony/symfony@a92a8ab · GitHub
[go: up one dir, main page]

Skip to content

Commit a92a8ab

Browse files
Frederic GodfrinFrederic Godfrin
Frederic Godfrin
authored and
Frederic Godfrin
committed
Throw an exception if the target can't be reliably defined
1 parent 6f540dc commit a92a8ab

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

src/Symfony/Component/Filesystem/Filesystem.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,12 +445,11 @@ public function makePathRelative($endPath, $startPath)
445445
public function mirror($originDir, $targetDir, \Traversable $iterator = null, $options = array())
446446
{
447447
$targetDir = rtrim($targetDir, '/\\');
448-
$originDir = realpath(rtrim($originDir, '/\\'));
448+
$originDir = rtrim($originDir, '/\\');
449449
$originDirLen = \strlen($originDir);
450450

451451
// Iterate in destination folder to remove obsolete entries
452452
if ($this->exists($targetDir) && isset($options['delete']) && $options['delete']) {
453-
$targetDir = realpath($targetDir);
454453
$deleteIterator = $iterator;
455454
if (null === $deleteIterator) {
456455
$flags = \FilesystemIterator::SKIP_DOTS;
@@ -480,6 +479,10 @@ public function mirror($originDir, $targetDir, \Traversable $iterator = null, $o
480479
}
481480

482481
foreach ($iterator as $file) {
482+
if (false === strpos($file->getPath(), $originDir)) {
483+
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);
484+
}
485+
483486
$target = $targetDir.substr($file->getPathname(), $originDirLen);
484487

485488
if ($copyOnWindows) {

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,10 @@ public function testMirrorWithCustomIterator()
10841084
$this->assertFileEquals($file, $targetPath.DIRECTORY_SEPARATOR.'file');
10851085
}
10861086

1087+
/**
1088+
* @expectedException \Symfony\Component\Filesystem\Exception\IOException
1089+
* @expectedExceptionMessageRegExp /Unable to mirror "(.*)" directory/
1090+
*/
10871091
public function testMirrorWithCustomIteratorWithRelativePath()
10881092
{
10891093
$sourcePath = $this->workspace.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'source'.DIRECTORY_SEPARATOR;
@@ -1094,17 +1098,11 @@ public function testMirrorWithCustomIteratorWithRelativePath()
10941098
file_put_contents($file, 'FILE');
10951099

10961100
$targetPath = $this->workspace.DIRECTORY_SEPARATOR.'target'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'target'.DIRECTORY_SEPARATOR;
1097-
$realTargetPath = $this->workspace.DIRECTORY_SEPARATOR.'target'.DIRECTORY_SEPARATOR;
10981101

10991102
$splFile = new \SplFileInfo($file);
11001103
$iterator = new \ArrayObject(array($splFile));
11011104

11021105
$this->filesystem->mirror($sourcePath, $targetPath, $iterator);
1103-
1104-
$this->assertTrue(is_dir($targetPath));
1105-
$this->assertTrue(is_dir($realTargetPath));
1106-
$this->assertFileEquals($file, $targetPath.DIRECTORY_SEPARATOR.'file');
1107-
$this->assertFileEquals($file, $realTargetPath.DIRECTORY_SEPARATOR.'file');
11081106
}
11091107

11101108
/**

0 commit comments

Comments
 (0)
0