-
Notifications
You must be signed in to change notification settings - Fork 1
/
CcProcess.php
340 lines (314 loc) · 7.34 KB
/
CcProcess.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<?php
/**
* @copyright Andreas Dirmeier (C) 2018
*
* This file is part of CcGitServer.
*
* CcGitServer is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CcGitServer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with CcGitServer. If not, see <http://www.gnu.org/licenses/>.
**/
/**
* @file CcProcess.php
* @author Andreas Dirmeier
* @par Language: PHP
*
* Description for class CcProcess
*/
namespace NGitServer;
require_once 'CcStringUtil.php';
require_once 'CcFilesystemUtil.php';
/**
* Execute an local application.
* This class can read/write to pipes and setup working direcotry.
*/
class CcProcess
{
/**
* Path to executable
* @var string $sExecutable
*/
private $sExecutable;
/**
* Process handle, wich get set on exec
* @var resource $pProcess
*/
private $pProcess = null;
/**
* Pipe descriptor list for reading and writing support
* @var string[][] $aDescriptors
*/
private $aDescriptors;
/**
* Generated pipes from exec to read/write from/to.
* @var resource[] $aPipes
*/
private $aPipes;
/**
* Working directory to exectute in. If empty, current dir will be used
* @var string $sWorkingDir
*/
private $sWorkingDir;
/**
* Last state received from proc_get_status
* @var string[] $aStatus
*/
private $aStatus;
/**
* Create a process object with target executable.
* @param string $sExecutable
*/
public function __construct($sExecutable)
{
$this->aDescriptors = array(
0 => array("pipe", "r"), // stdin
1 => array("pipe", "w"), // stdout
2 => array("pipe", "w") // stderr
);
$this->aPipes = array(
0 => null, // stdin
1 => null, // stdout
2 => null // stderr
);
$this->sExecutable = $sExecutable;
}
/**
* close all handles if required
*/
public function __destruct()
{
$this->close();
}
/**
* Execute current setup
*/
public function start($sArgumentLine = null)
{
$sCurrentDir = getcwd();
$sRun = $this->sExecutable;
if($sArgumentLine)
{
$sRun .= " ".$sArgumentLine;
}
$bAllOk = function_exists("proc_open");
if( $this->getWorkingDir() &&
is_dir($this->getWorkingDir()))
{
$bAllOk = chdir($this->getWorkingDir());
}
else
{
$bAllOk = false;
}
if($bAllOk)
{
$this->pProcess = proc_open($sRun,
$this->aDescriptors,
$this->aPipes);
}
if($this->getWorkingDir())
{
chdir($sCurrentDir);
}
return $bAllOk;
}
/**
* close process if not already done
*/
public function close()
{
$iResult = -1;
for($i=0; $i < count($this->aPipes); $i++)
{
if($this->aPipes[$i] != null) fclose($this->aPipes[$i]);
$this->aPipes[$i] = null;
}
if($this->pProcess != null)
{
$iResult = $this->getExitCode();
$i = proc_close($this->pProcess);
$this->pProcess = null;
}
return $iResult;
}
/**
* Close the stdin pipe of executable
*/
public function closeWrite()
{
if($this->pProcess != null)
{
if($this->aPipes[0])
{
fclose($this->aPipes[0]);
$this->aPipes[0] = null;
}
}
}
/**
* Working directory to exectute in.
* If empty, current dir will be used.
* @param string $sWorkingDir: New working directory
*/
public function setWorkingDir($sWorkingDir)
{
$this->sWorkingDir = $sWorkingDir;
}
/**
* Read form executable stdout pipe
* @param number $iMaxLength: Maximum length of string to read
* @return string|false Read string or false on error
*/
public function read($iMaxLength = 10240)
{
return fread($this->aPipes[1], $iMaxLength);
}
/**
* Read form executable stderror pipe
* @param number $iMaxLength: Maximum length of string to read
* @return string|false Read string or false on error
*/
public function readError($iMaxLength = 10240)
{
return fread($this->aPipes[2], $iMaxLength);
}
/**
* Read all data from stdout pipe
* @return string|false Read string or false on error
*/
public function readAll()
{
$sOutput = "";
do
{
$sRead = $this->read(10240);
if($sRead)
{
$sOutput .= $sRead;
}
} while($sRead);
return $sOutput;
}
/**
* Read all data from stderror pipe
* @return string|false Read string or false on error
*/
public function readAllError()
{
$sOutput = "";
do
{
$sRead = $this->readError(10240);
if($sRead)
{
$sOutput .= $sRead;
}
} while($sRead);
return $sOutput;
}
/**
*
* @param number $iMaxLength: Maximum length of line to read
* @return string
*/
public function readLine($iMaxLength = 10240)
{
return fgets($this->aPipes[1], $iMaxLength);
}
/**
* Write data to stdin of executable
* @param string $sData: Data to write to process
* @return number Number of bytes written
*/
public function write($sData)
{
$iWritten = fwrite($this->aPipes[0], $sData);
return $iWritten;
}
public function getStatus()
{
$bSuccess = true;
if($this->aStatus == null ||
$this->aStatus["running"] == true)
{
if($this->pProcess != null)
{
$this->aStatus = proc_get_status($this->pProcess);
}
}
if($this->aStatus == null) $bSuccess = false;
return $bSuccess;
}
/**
* @brief Check if current process is in a running state.
* @return bool true if running
*/
public function isRunning()
{
$bSuccess = false;
if($this->getStatus())
{
$bSuccess = $this->aStatus["running"];
}
return $bSuccess;
}
/**
* @brief Wait for process until isRunning is false
*/
public function waitFinished()
{
while($this->isRunning());
}
/**
* @brief Get Exitcode of closed process.
* @return number ExitCode as number
*/
public function getExitCode()
{
$iResult = -1;
if($this->getStatus())
{
$iResult = $this->aStatus["exitcode"];
}
return $iResult;
}
/**
* @brief Get setted working dir of this prosses.
* @return string Current Working dir or empty if not yet set.
*/
public function getWorkingDir()
{
return $this->sWorkingDir;
}
/**
* Directly exeute a program if possible
* @param string $sProgram: Name of Program to execute
* @param string $sWorkingDir: Working directory if required
* @param string &$sData: Output data if required
*/
public static function exec($sProgram, $sWorkingDir=null, &$sData=null)
{
$oProc = new CcProcess($sProgram);
if($sWorkingDir) $oProc->setWorkingDir($sWorkingDir);
$oProc->run();
if($sData != null)
{
$sData = "";
while($oProc->isRunning()) $sData .= $oProc->readAll();
}
else
{
$oProc->waitFinished();
}
return $oProc->getExitCode();
}
}