Fix copy() sometimes failing on virtualbox shared folders, fixes #12057
parent
bbb603490b
commit
a17096f5ba
|
@ -198,7 +198,7 @@ class Cache
|
|||
$this->io->writeError('Writing '.$this->root . $file.' into cache from '.$source);
|
||||
}
|
||||
|
||||
return copy($source, $this->root . $file);
|
||||
return $this->filesystem->copy($source, $this->root . $file);
|
||||
}
|
||||
|
||||
return false;
|
||||
|
@ -224,7 +224,7 @@ class Cache
|
|||
|
||||
$this->io->writeError('Reading '.$this->root . $file.' from cache', true, IOInterface::DEBUG);
|
||||
|
||||
return copy($this->root . $file, $target);
|
||||
return $this->filesystem->copy($this->root . $file, $target);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
namespace Composer\Util;
|
||||
|
||||
use Composer\Pcre\Preg;
|
||||
use ErrorException;
|
||||
use React\Promise\PromiseInterface;
|
||||
use RecursiveDirectoryIterator;
|
||||
use RecursiveIteratorIterator;
|
||||
|
@ -367,7 +368,29 @@ class Filesystem
|
|||
$target = $this->normalizePath($target);
|
||||
|
||||
if (!is_dir($source)) {
|
||||
try {
|
||||
return copy($source, $target);
|
||||
} catch (ErrorException $e) {
|
||||
// if copy fails we attempt to copy it manually as this can help bypass issues with VirtualBox shared folders
|
||||
// see https://github.com/composer/composer/issues/12057
|
||||
if (str_contains($e->getMessage(), 'Bad address')) {
|
||||
$sourceHandle = fopen($source, 'r');
|
||||
$targetHandle = fopen($target, 'w');
|
||||
if (false === $sourceHandle || false === $targetHandle) {
|
||||
throw $e;
|
||||
}
|
||||
while (!feof($sourceHandle)) {
|
||||
if (false === fwrite($targetHandle, (string) fread($sourceHandle, 1024 * 1024))) {
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
fclose($sourceHandle);
|
||||
fclose($targetHandle);
|
||||
|
||||
return true;
|
||||
}
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
$it = new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS);
|
||||
|
|
Loading…
Reference in New Issue