[Filesystem] Fix usages of error_get_last()
This commit is contained in:
parent
07d25709a9
commit
9d015c7c50
@ -205,12 +205,11 @@ class Command
|
|||||||
|
|
||||||
if (null !== $this->processTitle) {
|
if (null !== $this->processTitle) {
|
||||||
if (function_exists('cli_set_process_title')) {
|
if (function_exists('cli_set_process_title')) {
|
||||||
if (false === @cli_set_process_title($this->processTitle)) {
|
if (!@cli_set_process_title($this->processTitle)) {
|
||||||
if ('Darwin' === PHP_OS) {
|
if ('Darwin' === PHP_OS) {
|
||||||
$output->writeln('<comment>Running "cli_get_process_title" as an unprivileged user is not supported on MacOS.</comment>');
|
$output->writeln('<comment>Running "cli_get_process_title" as an unprivileged user is not supported on MacOS.</comment>');
|
||||||
} else {
|
} else {
|
||||||
$error = error_get_last();
|
cli_set_process_title($this->processTitle);
|
||||||
trigger_error($error['message'], E_USER_WARNING);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} elseif (function_exists('setproctitle')) {
|
} elseif (function_exists('setproctitle')) {
|
||||||
|
@ -21,6 +21,8 @@ use Symfony\Component\Filesystem\Exception\FileNotFoundException;
|
|||||||
*/
|
*/
|
||||||
class Filesystem
|
class Filesystem
|
||||||
{
|
{
|
||||||
|
private static $lastError;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Copies a file.
|
* Copies a file.
|
||||||
*
|
*
|
||||||
@ -95,12 +97,11 @@ class Filesystem
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (true !== @mkdir($dir, $mode, true)) {
|
if (!self::box('mkdir', $dir, $mode, true)) {
|
||||||
$error = error_get_last();
|
|
||||||
if (!is_dir($dir)) {
|
if (!is_dir($dir)) {
|
||||||
// The directory was not created by a concurrent process. Let's throw an exception with a developer friendly error message if we have one
|
// The directory was not created by a concurrent process. Let's throw an exception with a developer friendly error message if we have one
|
||||||
if ($error) {
|
if (self::$lastError) {
|
||||||
throw new IOException(sprintf('Failed to create "%s": %s.', $dir, $error['message']), 0, null, $dir);
|
throw new IOException(sprintf('Failed to create "%s": %s.', $dir, self::$lastError), 0, null, $dir);
|
||||||
}
|
}
|
||||||
throw new IOException(sprintf('Failed to create "%s"', $dir), 0, null, $dir);
|
throw new IOException(sprintf('Failed to create "%s"', $dir), 0, null, $dir);
|
||||||
}
|
}
|
||||||
@ -169,20 +170,17 @@ class Filesystem
|
|||||||
foreach ($files as $file) {
|
foreach ($files as $file) {
|
||||||
if (is_link($file)) {
|
if (is_link($file)) {
|
||||||
// See https://bugs.php.net/52176
|
// See https://bugs.php.net/52176
|
||||||
if (!@(unlink($file) || '\\' !== DIRECTORY_SEPARATOR || rmdir($file)) && file_exists($file)) {
|
if (!(self::box('unlink', $file) || '\\' !== DIRECTORY_SEPARATOR || self::box('rmdir', $file)) && file_exists($file)) {
|
||||||
$error = error_get_last();
|
throw new IOException(sprintf('Failed to remove symlink "%s": %s.', $file, self::$lastError));
|
||||||
throw new IOException(sprintf('Failed to remove symlink "%s": %s.', $file, $error['message']));
|
|
||||||
}
|
}
|
||||||
} elseif (is_dir($file)) {
|
} elseif (is_dir($file)) {
|
||||||
$this->remove(new \FilesystemIterator($file, \FilesystemIterator::CURRENT_AS_PATHNAME | \FilesystemIterator::SKIP_DOTS));
|
$this->remove(new \FilesystemIterator($file, \FilesystemIterator::CURRENT_AS_PATHNAME | \FilesystemIterator::SKIP_DOTS));
|
||||||
|
|
||||||
if (!@rmdir($file) && file_exists($file)) {
|
if (!self::box('rmdir', $file) && file_exists($file)) {
|
||||||
$error = error_get_last();
|
throw new IOException(sprintf('Failed to remove directory "%s": %s.', $file, self::$lastError));
|
||||||
throw new IOException(sprintf('Failed to remove directory "%s": %s.', $file, $error['message']));
|
|
||||||
}
|
}
|
||||||
} elseif (!@unlink($file) && file_exists($file)) {
|
} elseif (!self::box('unlink', $file) && file_exists($file)) {
|
||||||
$error = error_get_last();
|
throw new IOException(sprintf('Failed to remove file "%s": %s.', $file, self::$lastError));
|
||||||
throw new IOException(sprintf('Failed to remove file "%s": %s.', $file, $error['message']));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -336,19 +334,16 @@ class Filesystem
|
|||||||
|
|
||||||
$this->mkdir(dirname($targetDir));
|
$this->mkdir(dirname($targetDir));
|
||||||
|
|
||||||
$ok = false;
|
|
||||||
if (is_link($targetDir)) {
|
if (is_link($targetDir)) {
|
||||||
if (readlink($targetDir) != $originDir) {
|
if (readlink($targetDir) === $originDir) {
|
||||||
$this->remove($targetDir);
|
return;
|
||||||
} else {
|
|
||||||
$ok = true;
|
|
||||||
}
|
}
|
||||||
|
$this->remove($targetDir);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$ok && true !== @symlink($originDir, $targetDir)) {
|
if (!self::box('symlink', $originDir, $targetDir)) {
|
||||||
$report = error_get_last();
|
if (null !== self::$lastError) {
|
||||||
if (is_array($report)) {
|
if ('\\' === DIRECTORY_SEPARATOR && false !== strpos(self::$lastError, 'error code(1314)')) {
|
||||||
if ('\\' === DIRECTORY_SEPARATOR && false !== strpos($report['message'], 'error code(1314)')) {
|
|
||||||
throw new IOException('Unable to create symlink due to error code 1314: \'A required privilege is not held by the client\'. Do you have the required Administrator-rights?', 0, null, $targetDir);
|
throw new IOException('Unable to create symlink due to error code 1314: \'A required privilege is not held by the client\'. Do you have the required Administrator-rights?', 0, null, $targetDir);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -580,4 +575,29 @@ class Filesystem
|
|||||||
|
|
||||||
return $files;
|
return $files;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static function box($func)
|
||||||
|
{
|
||||||
|
self::$lastError = null;
|
||||||
|
\set_error_handler(__CLASS__.'::handleError');
|
||||||
|
try {
|
||||||
|
$result = \call_user_func_array($func, \array_slice(\func_get_args(), 1));
|
||||||
|
\restore_error_handler();
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
}
|
||||||
|
\restore_error_handler();
|
||||||
|
|
||||||
|
throw $e;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
public static function handleError($type, $msg)
|
||||||
|
{
|
||||||
|
self::$lastError = $msg;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -66,12 +66,11 @@ class SplFileInfo extends \SplFileInfo
|
|||||||
*/
|
*/
|
||||||
public function getContents()
|
public function getContents()
|
||||||
{
|
{
|
||||||
$level = error_reporting(0);
|
set_error_handler(function ($type, $msg) use (&$error) { $error = $msg; });
|
||||||
$content = file_get_contents($this->getPathname());
|
$content = file_get_contents($this->getPathname());
|
||||||
error_reporting($level);
|
restore_error_handler();
|
||||||
if (false === $content) {
|
if (false === $content) {
|
||||||
$error = error_get_last();
|
throw new \RuntimeException($error);
|
||||||
throw new \RuntimeException($error['message']);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $content;
|
return $content;
|
||||||
|
@ -93,9 +93,11 @@ class File extends \SplFileInfo
|
|||||||
{
|
{
|
||||||
$target = $this->getTargetFile($directory, $name);
|
$target = $this->getTargetFile($directory, $name);
|
||||||
|
|
||||||
if (!@rename($this->getPathname(), $target)) {
|
set_error_handler(function ($type, $msg) use (&$error) { $error = $msg; });
|
||||||
$error = error_get_last();
|
$renamed = rename($this->getPathname(), $target);
|
||||||
throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error['message'])));
|
restore_error_handler();
|
||||||
|
if (!$renamed) {
|
||||||
|
throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@chmod($target, 0666 & ~umask());
|
@chmod($target, 0666 & ~umask());
|
||||||
|
@ -192,9 +192,11 @@ class UploadedFile extends File
|
|||||||
|
|
||||||
$target = $this->getTargetFile($directory, $name);
|
$target = $this->getTargetFile($directory, $name);
|
||||||
|
|
||||||
if (!@move_uploaded_file($this->getPathname(), $target)) {
|
set_error_handler(function ($type, $msg) use (&$error) { $error = $msg; });
|
||||||
$error = error_get_last();
|
$moved = move_uploaded_file($this->getPathname(), $target);
|
||||||
throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error['message'])));
|
restore_error_handler();
|
||||||
|
if (!$moved) {
|
||||||
|
throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@chmod($target, 0666 & ~umask());
|
@chmod($target, 0666 & ~umask());
|
||||||
|
@ -23,6 +23,7 @@ abstract class AbstractPipes implements PipesInterface
|
|||||||
private $inputBuffer = '';
|
private $inputBuffer = '';
|
||||||
private $input;
|
private $input;
|
||||||
private $blocked = true;
|
private $blocked = true;
|
||||||
|
private $lastError;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param resource|null $input
|
* @param resource|null $input
|
||||||
@ -56,10 +57,11 @@ abstract class AbstractPipes implements PipesInterface
|
|||||||
*/
|
*/
|
||||||
protected function hasSystemCallBeenInterrupted()
|
protected function hasSystemCallBeenInterrupted()
|
||||||
{
|
{
|
||||||
$lastError = error_get_last();
|
$lastError = $this->lastError;
|
||||||
|
$this->lastError = null;
|
||||||
|
|
||||||
// stream_select returns false when the `select` system call is interrupted by an incoming signal
|
// stream_select returns false when the `select` system call is interrupted by an incoming signal
|
||||||
return isset($lastError['message']) && false !== stripos($lastError['message'], 'interrupted system call');
|
return null !== $lastError && false !== stripos($lastError, 'interrupted system call');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -137,4 +139,12 @@ abstract class AbstractPipes implements PipesInterface
|
|||||||
return array($this->pipes[0]);
|
return array($this->pipes[0]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
public function handleError($type, $msg)
|
||||||
|
{
|
||||||
|
$this->lastError = $msg;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -99,7 +99,9 @@ class UnixPipes extends AbstractPipes
|
|||||||
unset($r[0]);
|
unset($r[0]);
|
||||||
|
|
||||||
// let's have a look if something changed in streams
|
// let's have a look if something changed in streams
|
||||||
if (($r || $w) && false === @stream_select($r, $w, $e, 0, $blocking ? Process::TIMEOUT_PRECISION * 1E6 : 0)) {
|
set_error_handler(array($this, 'handleError'));
|
||||||
|
if (($r || $w) && false === stream_select($r, $w, $e, 0, $blocking ? Process::TIMEOUT_PRECISION * 1E6 : 0)) {
|
||||||
|
restore_error_handler();
|
||||||
// if a system call has been interrupted, forget about it, let's try again
|
// if a system call has been interrupted, forget about it, let's try again
|
||||||
// otherwise, an error occurred, let's reset pipes
|
// otherwise, an error occurred, let's reset pipes
|
||||||
if (!$this->hasSystemCallBeenInterrupted()) {
|
if (!$this->hasSystemCallBeenInterrupted()) {
|
||||||
@ -108,6 +110,7 @@ class UnixPipes extends AbstractPipes
|
|||||||
|
|
||||||
return $read;
|
return $read;
|
||||||
}
|
}
|
||||||
|
restore_error_handler();
|
||||||
|
|
||||||
foreach ($r as $pipe) {
|
foreach ($r as $pipe) {
|
||||||
// prior PHP 5.4 the array passed to stream_select is modified and
|
// prior PHP 5.4 the array passed to stream_select is modified and
|
||||||
|
Reference in New Issue
Block a user