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

Skip to content

Commit f80d827

Browse files
author
Anthony MARTIN
committed
Handling relative/absolute path
1 parent c1aab93 commit f80d827

File tree

2 files changed

+19
-65
lines changed

2 files changed

+19
-65
lines changed

src/Symfony/Component/Filesystem/Filesystem.php

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,10 @@ public function mirror($originDir, $targetDir, \Traversable $iterator = null, $o
536536
$originDir = rtrim($originDir, '/\\');
537537
$originDirLen = \strlen($originDir);
538538

539+
if (!$this->exists($originDir)) {
540+
throw new IOException(sprintf('The origin directory specified "%s" was not found.', $originDir), 0, null, $originDir);
541+
}
542+
539543
// Iterate in destination folder to remove obsolete entries
540544
if ($this->exists($targetDir) && isset($options['delete']) && $options['delete']) {
541545
$deleteIterator = $iterator;
@@ -562,35 +566,25 @@ public function mirror($originDir, $targetDir, \Traversable $iterator = null, $o
562566
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($originDir, $flags), \RecursiveIteratorIterator::SELF_FIRST);
563567
}
564568

565-
if ($this->exists($originDir)) {
566-
$this->mkdir($targetDir);
567-
}
569+
$this->mkdir($targetDir);
570+
$targetDirInfo = new \SplFileInfo($targetDir);
571+
$regex = sprintf('/^%s.*/', str_replace('/', '\/', $targetDirInfo->getRealPath() ? $targetDirInfo->getRealPath() : $targetDir));
568572

569573
foreach ($iterator as $file) {
570-
if ($file->getPathName() === $targetDir) {
574+
if ($file->getPathName() === $targetDir || $file->getRealPath() === $targetDirInfo->getRealPath() || preg_match($regex, $file->getRealPath())) {
571575
continue;
572576
}
573577

574578
$target = $targetDir.substr($file->getPathname(), $originDirLen);
575579

576-
if ($copyOnWindows) {
577-
if (is_file($file)) {
578-
$this->copy($file, $target, isset($options['override']) ? $options['override'] : false);
579-
} elseif (is_dir($file)) {
580-
$this->mkdir($target);
581-
} else {
582-
throw new IOException(sprintf('Unable to guess "%s" file type.', $file), 0, null, $file);
583-
}
580+
if (!$copyOnWindows && is_link($file)) {
581+
$this->symlink($file->getLinkTarget(), $target);
582+
} elseif (is_dir($file)) {
583+
$this->mkdir($target);
584+
} elseif (is_file($file)) {
585+
$this->copy($file, $target, isset($options['override']) ? $options['override'] : false);
584586
} else {
585-
if (is_link($file)) {
586-
$this->symlink($file->getLinkTarget(), $target);
587-
} elseif (is_dir($file)) {
588-
$this->mkdir($target);
589-
} elseif (is_file($file)) {
590-
$this->copy($file, $target, isset($options['override']) ? $options['override'] : false);
591-
} else {
592-
throw new IOException(sprintf('Unable to guess "%s" file type.', $file), 0, null, $file);
593-
}
587+
throw new IOException(sprintf('Unable to guess "%s" file type.', $file), 0, null, $file);
594588
}
595589
}
596590
}

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

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,46 +1348,6 @@ public function testMirrorContentsWithSameNameAsSourceOrTargetWithDeleteOption()
13481348
$this->assertFileNotExists($targetPath.'target');
13491349
}
13501350

1351-
public function testMirrorWithCustomIterator()
1352-
{
1353-
$sourcePath = $this->workspace.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR;
1354-
mkdir($sourcePath);
1355-
1356-
$file = $sourcePath.\DIRECTORY_SEPARATOR.'file';
1357-
file_put_contents($file, 'FILE');
1358-
1359-
$targetPath = $this->workspace.\DIRECTORY_SEPARATOR.'target'.\DIRECTORY_SEPARATOR;
1360-
1361-
$splFile = new \SplFileInfo($file);
1362-
$iterator = new \ArrayObject([$splFile]);
1363-
1364-
$this->filesystem->mirror($sourcePath, $targetPath, $iterator);
1365-
1366-
$this->assertTrue(is_dir($targetPath));
1367-
$this->assertFileEquals($file, $targetPath.\DIRECTORY_SEPARATOR.'file');
1368-
}
1369-
1370-
/**
1371-
* @expectedException \Symfony\Component\Filesystem\Exception\IOException
1372-
* @expectedExceptionMessageRegExp /Unable to mirror "(.*)" directory/
1373-
*/
1374-
public function testMirrorWithCustomIteratorWithRelativePath()
1375-
{
1376-
$sourcePath = $this->workspace.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR;
1377-
$realSourcePath = $this->workspace.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR;
1378-
mkdir($realSourcePath);
1379-
1380-
$file = $realSourcePath.'file';
1381-
file_put_contents($file, 'FILE');
1382-
1383-
$targetPath = $this->workspace.\DIRECTORY_SEPARATOR.'target'.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'target'.\DIRECTORY_SEPARATOR;
1384-
1385-
$splFile = new \SplFileInfo($file);
1386-
$iterator = new \ArrayObject([$splFile]);
1387-
1388-
$this->filesystem->mirror($sourcePath, $targetPath, $iterator);
1389-
}
1390-
13911351
public function testMirrorAvoidCopyingTargetDirectoryIfInSourceDirectory()
13921352
{
13931353
$sourcePath = $this->workspace.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR;
@@ -1402,15 +1362,15 @@ public function testMirrorAvoidCopyingTargetDirectoryIfInSourceDirectory()
14021362

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

1405-
$this->filesystem->mirror($sourcePath, $targetPath);
1365+
$this->filesystem->mirror($sourcePath, $targetPath, null, ['delete' => true]);
14061366

1407-
$this->assertTrue(is_dir($targetPath));
1408-
$this->assertTrue(is_dir($targetPath.'directory'));
1367+
$this->assertTrue($this->filesystem->exists($targetPath));
1368+
$this->assertTrue($this->filesystem->exists($targetPath.'directory'));
14091369

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

1413-
$this->assertFileNotExists($targetPath.'target');
1373+
$this->assertFalse($this->filesystem->exists($targetPath.'target'));
14141374
}
14151375

14161376
/**

0 commit comments

Comments
 (0)
0