annotate vendor/symfony/process/Pipes/WindowsPipes.php @ 2:92f882872392

Trusted hosts, + remove migration modules
author Chris Cannam
date Tue, 05 Dec 2017 09:26:43 +0000
parents 4c8ae668cc8c
children 1fec387a4317
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\Process\Pipes;
Chris@0 13
Chris@0 14 use Symfony\Component\Process\Process;
Chris@0 15 use Symfony\Component\Process\Exception\RuntimeException;
Chris@0 16
Chris@0 17 /**
Chris@0 18 * WindowsPipes implementation uses temporary files as handles.
Chris@0 19 *
Chris@0 20 * @see https://bugs.php.net/bug.php?id=51800
Chris@0 21 * @see https://bugs.php.net/bug.php?id=65650
Chris@0 22 *
Chris@0 23 * @author Romain Neutron <imprec@gmail.com>
Chris@0 24 *
Chris@0 25 * @internal
Chris@0 26 */
Chris@0 27 class WindowsPipes extends AbstractPipes
Chris@0 28 {
Chris@0 29 /** @var array */
Chris@0 30 private $files = array();
Chris@0 31 /** @var array */
Chris@0 32 private $fileHandles = array();
Chris@0 33 /** @var array */
Chris@0 34 private $readBytes = array(
Chris@0 35 Process::STDOUT => 0,
Chris@0 36 Process::STDERR => 0,
Chris@0 37 );
Chris@0 38 /** @var bool */
Chris@0 39 private $haveReadSupport;
Chris@0 40
Chris@0 41 public function __construct($input, $haveReadSupport)
Chris@0 42 {
Chris@0 43 $this->haveReadSupport = (bool) $haveReadSupport;
Chris@0 44
Chris@0 45 if ($this->haveReadSupport) {
Chris@0 46 // Fix for PHP bug #51800: reading from STDOUT pipe hangs forever on Windows if the output is too big.
Chris@0 47 // Workaround for this problem is to use temporary files instead of pipes on Windows platform.
Chris@0 48 //
Chris@0 49 // @see https://bugs.php.net/bug.php?id=51800
Chris@0 50 $pipes = array(
Chris@0 51 Process::STDOUT => Process::OUT,
Chris@0 52 Process::STDERR => Process::ERR,
Chris@0 53 );
Chris@0 54 $tmpCheck = false;
Chris@0 55 $tmpDir = sys_get_temp_dir();
Chris@0 56 $lastError = 'unknown reason';
Chris@0 57 set_error_handler(function ($type, $msg) use (&$lastError) { $lastError = $msg; });
Chris@0 58 for ($i = 0;; ++$i) {
Chris@0 59 foreach ($pipes as $pipe => $name) {
Chris@0 60 $file = sprintf('%s\\sf_proc_%02X.%s', $tmpDir, $i, $name);
Chris@0 61 if (file_exists($file) && !unlink($file)) {
Chris@0 62 continue 2;
Chris@0 63 }
Chris@0 64 $h = fopen($file, 'xb');
Chris@0 65 if (!$h) {
Chris@0 66 $error = $lastError;
Chris@0 67 if ($tmpCheck || $tmpCheck = unlink(tempnam(false, 'sf_check_'))) {
Chris@0 68 continue;
Chris@0 69 }
Chris@0 70 restore_error_handler();
Chris@0 71 throw new RuntimeException(sprintf('A temporary file could not be opened to write the process output: %s', $error));
Chris@0 72 }
Chris@0 73 if (!$h || !$this->fileHandles[$pipe] = fopen($file, 'rb')) {
Chris@0 74 continue 2;
Chris@0 75 }
Chris@0 76 if (isset($this->files[$pipe])) {
Chris@0 77 unlink($this->files[$pipe]);
Chris@0 78 }
Chris@0 79 $this->files[$pipe] = $file;
Chris@0 80 }
Chris@0 81 break;
Chris@0 82 }
Chris@0 83 restore_error_handler();
Chris@0 84 }
Chris@0 85
Chris@0 86 parent::__construct($input);
Chris@0 87 }
Chris@0 88
Chris@0 89 public function __destruct()
Chris@0 90 {
Chris@0 91 $this->close();
Chris@0 92 $this->removeFiles();
Chris@0 93 }
Chris@0 94
Chris@0 95 /**
Chris@0 96 * {@inheritdoc}
Chris@0 97 */
Chris@0 98 public function getDescriptors()
Chris@0 99 {
Chris@0 100 if (!$this->haveReadSupport) {
Chris@0 101 $nullstream = fopen('NUL', 'c');
Chris@0 102
Chris@0 103 return array(
Chris@0 104 array('pipe', 'r'),
Chris@0 105 $nullstream,
Chris@0 106 $nullstream,
Chris@0 107 );
Chris@0 108 }
Chris@0 109
Chris@0 110 // We're not using pipe on Windows platform as it hangs (https://bugs.php.net/bug.php?id=51800)
Chris@0 111 // We're not using file handles as it can produce corrupted output https://bugs.php.net/bug.php?id=65650
Chris@0 112 // So we redirect output within the commandline and pass the nul device to the process
Chris@0 113 return array(
Chris@0 114 array('pipe', 'r'),
Chris@0 115 array('file', 'NUL', 'w'),
Chris@0 116 array('file', 'NUL', 'w'),
Chris@0 117 );
Chris@0 118 }
Chris@0 119
Chris@0 120 /**
Chris@0 121 * {@inheritdoc}
Chris@0 122 */
Chris@0 123 public function getFiles()
Chris@0 124 {
Chris@0 125 return $this->files;
Chris@0 126 }
Chris@0 127
Chris@0 128 /**
Chris@0 129 * {@inheritdoc}
Chris@0 130 */
Chris@0 131 public function readAndWrite($blocking, $close = false)
Chris@0 132 {
Chris@0 133 $this->unblock();
Chris@0 134 $w = $this->write();
Chris@0 135 $read = $r = $e = array();
Chris@0 136
Chris@0 137 if ($blocking) {
Chris@0 138 if ($w) {
Chris@0 139 @stream_select($r, $w, $e, 0, Process::TIMEOUT_PRECISION * 1E6);
Chris@0 140 } elseif ($this->fileHandles) {
Chris@0 141 usleep(Process::TIMEOUT_PRECISION * 1E6);
Chris@0 142 }
Chris@0 143 }
Chris@0 144 foreach ($this->fileHandles as $type => $fileHandle) {
Chris@0 145 $data = stream_get_contents($fileHandle, -1, $this->readBytes[$type]);
Chris@0 146
Chris@0 147 if (isset($data[0])) {
Chris@0 148 $this->readBytes[$type] += strlen($data);
Chris@0 149 $read[$type] = $data;
Chris@0 150 }
Chris@0 151 if ($close) {
Chris@0 152 fclose($fileHandle);
Chris@0 153 unset($this->fileHandles[$type]);
Chris@0 154 }
Chris@0 155 }
Chris@0 156
Chris@0 157 return $read;
Chris@0 158 }
Chris@0 159
Chris@0 160 /**
Chris@0 161 * {@inheritdoc}
Chris@0 162 */
Chris@0 163 public function haveReadSupport()
Chris@0 164 {
Chris@0 165 return $this->haveReadSupport;
Chris@0 166 }
Chris@0 167
Chris@0 168 /**
Chris@0 169 * {@inheritdoc}
Chris@0 170 */
Chris@0 171 public function areOpen()
Chris@0 172 {
Chris@0 173 return $this->pipes && $this->fileHandles;
Chris@0 174 }
Chris@0 175
Chris@0 176 /**
Chris@0 177 * {@inheritdoc}
Chris@0 178 */
Chris@0 179 public function close()
Chris@0 180 {
Chris@0 181 parent::close();
Chris@0 182 foreach ($this->fileHandles as $handle) {
Chris@0 183 fclose($handle);
Chris@0 184 }
Chris@0 185 $this->fileHandles = array();
Chris@0 186 }
Chris@0 187
Chris@0 188 /**
Chris@0 189 * Removes temporary files.
Chris@0 190 */
Chris@0 191 private function removeFiles()
Chris@0 192 {
Chris@0 193 foreach ($this->files as $filename) {
Chris@0 194 if (file_exists($filename)) {
Chris@0 195 @unlink($filename);
Chris@0 196 }
Chris@0 197 }
Chris@0 198 $this->files = array();
Chris@0 199 }
Chris@0 200 }