8000 Handling relative/absolute path · symfony/symfony@dd2d409 · GitHub
[go: up one dir, main page]

Skip to content

Commit dd2d409

Browse files
author
Anthony MARTIN
committed
Handling relative/absolute path
1 parent 8b7a267 commit dd2d409

File tree

2 files changed

+23
-69
lines changed

2 files changed

+23
-69
lines changed

src/Symfony/Component/Filesystem/Filesystem.php

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,10 @@ public function mirror($originDir, $targetDir, \Traversable $iterator = null, $o
541541
$originDir = rtrim($originDir, '/\\');
542542
$originDirLen = \strlen($originDir);
543543

544+
if (!$this->exists($originDir)) {
545+
throw new IOException(sprintf('The origin directory specified "%s" was not found.', $originDir), 0, null, $originDir);
546+
}
547+
544548
// Iterate in destination folder to remove obsolete entries
545549
if ($this->exists($targetDir) && isset($options['delete']) && $options['delete']) {
546550
$deleteIterator = $iterator;
@@ -564,39 +568,24 @@ public function mirror($originDir, $targetDir, \Traversable $iterator = null, $o
564568
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($originDir, $flags), \RecursiveIteratorIterator::SELF_FIRST);
565569
}
566570

567-
if ($this->exists($originDir)) {
568-
$this->mkdir($targetDir);
569-
}
571+
$this->mkdir($targetDir);
572+
$targetDirInfo = new \SplFileInfo($targetDir);
570573

571574
foreach ($iterator as $file) {
572-
if ($file->getPathName() === $targetDir) {
575+
if ($file->getPathName() === $targetDir || $file->getRealPath() === $targetDir || 0 === strpos($file->getRealPath(), $targetDirInfo->getRealPath())) {
573576
continue;
574577
}
575578

576-
if (false === strpos($file->getPath(), $originDir)) {
577-
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);
578-
}
579-
580579
$target = $targetDir.substr($file->getPathname(), $originDirLen);
581580

582-
if ($copyOnWindows) {
583-
if (is_file($file)) {
584-
$this->copy($file, $target, isset($options['override']) ? $options['override'] : false);
585-
} elseif (is_dir($file)) {
586-
$this->mkdir($target);
587-
} else {
588-
throw new IOException(sprintf('Unable to guess "%s" file type.', $file), 0, null, $file);
589-
}
581+
if (!$copyOnWindows && is_link($file)) {
582+
$this->symlink($file->getLinkTarget(), $target);
583+
} elseif (is_dir($file)) {
584+
$this->mkdir($target);
585+
} elseif (is_file($file)) {
586+
$this->copy($file, $target, isset($options['override']) ? $options['override'] : false);
590587
} else {
591-
if (is_link($file)) {
592-
$this->symlink($file->getLinkTarget(), $target);
593-
} elseif (is_dir($file)) {
594-
$this->mkdir($target);
595-
} elseif (is_file($file)) {
596-
$this->copy($file, $target, isset($options['override']) ? $options['override'] : false);
597-
} else {
598-
throw new IOException(sprintf('Unable to guess "%s" file type.', $file), 0, null, $file);
599-
}
588+
throw new IOException(sprintf('Unable to guess "%s" file type.', $file), 0, null, $file);
600589
}
601590
}
602591
}

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

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,46 +1332,6 @@ 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([$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([$splFile]);
1371-
1372-
$this->filesystem->mirror($sourcePath, $targetPath, $iterator);
1373-
}
1374-
13751335
public function testMirrorAvoidCopyingTargetDirectoryIfInSourceDirectory()
13761336
{
13771337
$sourcePath = $this->workspace.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR;
@@ -1386,15 +1346,20 @@ public function testMirrorAvoidCopyingTargetDirectoryIfInSourceDirectory()
13861346

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

1389-
$this->filesystem->mirror($sourcePath, $targetPath);
1349+
if ('\\' !== \DIRECTORY_SEPARATOR) {
1350+
$this->filesystem->symlink($targetPath, $sourcePath . 'target_simlink');
1351+
}
13901352

1391-
$this->assertTrue(is_dir($targetPath));
1392-
$this->assertTrue(is_dir($targetPath.'directory'));
1353+
$this->filesystem->mirror($sourcePath, $targetPath, null, ['delete' => true]);
1354+
1355+
$this->assertTrue($this->filesystem->exists($targetPath));
1356+
$this->assertTrue($this->filesystem->exists($targetPath.'directory'));
13931357

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

1397-
$this->assertFileNotExists($targetPath.'target');
1361+
$this->assertFalse($this->filesystem->exists($targetPath.'target_simlink'));
1362+
$this->assertFalse($this->filesystem->exists($targetPath.'target'));
13981363
}
13991364

14001365
/**

0 commit comments

Comments
 (0)
0