Handling relative/absolute path

This commit is contained in:
Anthony MARTIN 2019-02-08 15:34:28 +01:00
parent 59437a4af9
commit 8011f494d4
2 changed files with 23 additions and 69 deletions

View File

@ -541,6 +541,10 @@ class Filesystem
$originDir = rtrim($originDir, '/\\');
$originDirLen = \strlen($originDir);
if (!$this->exists($originDir)) {
throw new IOException(sprintf('The origin directory specified "%s" was not found.', $originDir), 0, null, $originDir);
}
// Iterate in destination folder to remove obsolete entries
if ($this->exists($targetDir) && isset($options['delete']) && $options['delete']) {
$deleteIterator = $iterator;
@ -564,39 +568,24 @@ class Filesystem
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($originDir, $flags), \RecursiveIteratorIterator::SELF_FIRST);
}
if ($this->exists($originDir)) {
$this->mkdir($targetDir);
}
$this->mkdir($targetDir);
$targetDirInfo = new \SplFileInfo($targetDir);
foreach ($iterator as $file) {
if ($file->getPathName() === $targetDir) {
if ($file->getPathName() === $targetDir || $file->getRealPath() === $targetDir || 0 === strpos($file->getRealPath(), $targetDirInfo->getRealPath())) {
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);
}
$target = $targetDir.substr($file->getPathname(), $originDirLen);
if ($copyOnWindows) {
if (is_file($file)) {
$this->copy($file, $target, isset($options['override']) ? $options['override'] : false);
} elseif (is_dir($file)) {
$this->mkdir($target);
} else {
throw new IOException(sprintf('Unable to guess "%s" file type.', $file), 0, null, $file);
}
if (!$copyOnWindows && is_link($file)) {
$this->symlink($file->getLinkTarget(), $target);
} elseif (is_dir($file)) {
$this->mkdir($target);
} elseif (is_file($file)) {
$this->copy($file, $target, isset($options['override']) ? $options['override'] : false);
} else {
if (is_link($file)) {
$this->symlink($file->getLinkTarget(), $target);
} elseif (is_dir($file)) {
$this->mkdir($target);
} elseif (is_file($file)) {
$this->copy($file, $target, isset($options['override']) ? $options['override'] : false);
} else {
throw new IOException(sprintf('Unable to guess "%s" file type.', $file), 0, null, $file);
}
throw new IOException(sprintf('Unable to guess "%s" file type.', $file), 0, null, $file);
}
}
}

View File

@ -1332,46 +1332,6 @@ class FilesystemTest extends FilesystemTestCase
$this->assertFileNotExists($targetPath.'target');
}
public function testMirrorWithCustomIterator()
{
$sourcePath = $this->workspace.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR;
mkdir($sourcePath);
$file = $sourcePath.\DIRECTORY_SEPARATOR.'file';
file_put_contents($file, 'FILE');
$targetPath = $this->workspace.\DIRECTORY_SEPARATOR.'target'.\DIRECTORY_SEPARATOR;
$splFile = new \SplFileInfo($file);
$iterator = new \ArrayObject([$splFile]);
$this->filesystem->mirror($sourcePath, $targetPath, $iterator);
$this->assertTrue(is_dir($targetPath));
$this->assertFileEquals($file, $targetPath.\DIRECTORY_SEPARATOR.'file');
}
/**
* @expectedException \Symfony\Component\Filesystem\Exception\IOException
* @expectedExceptionMessageRegExp /Unable to mirror "(.*)" directory/
*/
public function testMirrorWithCustomIteratorWithRelativePath()
{
$sourcePath = $this->workspace.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR;
$realSourcePath = $this->workspace.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR;
mkdir($realSourcePath);
$file = $realSourcePath.'file';
file_put_contents($file, 'FILE');
$targetPath = $this->workspace.\DIRECTORY_SEPARATOR.'target'.\DIRECTORY_SEPARATOR.'..'.\DIRECTORY_SEPARATOR.'target'.\DIRECTORY_SEPARATOR;
$splFile = new \SplFileInfo($file);
$iterator = new \ArrayObject([$splFile]);
$this->filesystem->mirror($sourcePath, $targetPath, $iterator);
}
public function testMirrorAvoidCopyingTargetDirectoryIfInSourceDirectory()
{
$sourcePath = $this->workspace.\DIRECTORY_SEPARATOR.'source'.\DIRECTORY_SEPARATOR;
@ -1386,15 +1346,20 @@ class FilesystemTest extends FilesystemTestCase
$targetPath = $sourcePath.'target'.\DIRECTORY_SEPARATOR;
$this->filesystem->mirror($sourcePath, $targetPath);
if ('\\' !== \DIRECTORY_SEPARATOR) {
$this->filesystem->symlink($targetPath, $sourcePath.'target_simlink');
}
$this->assertTrue(is_dir($targetPath));
$this->assertTrue(is_dir($targetPath.'directory'));
$this->filesystem->mirror($sourcePath, $targetPath, null, ['delete' => true]);
$this->assertTrue($this->filesystem->exists($targetPath));
$this->assertTrue($this->filesystem->exists($targetPath.'directory'));
$this->assertFileEquals($file1, $targetPath.'directory'.\DIRECTORY_SEPARATOR.'file1');
$this->assertFileEquals($file2, $targetPath.'file2');
$this->assertFileNotExists($targetPath.'target');
$this->assertFalse($this->filesystem->exists($targetPath.'target_simlink'));
$this->assertFalse($this->filesystem->exists($targetPath.'target'));
}
/**