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