1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* this file is part of pipelines */ |
4
|
|
|
|
5
|
|
|
namespace Ktomk\Pipelines; |
6
|
|
|
|
7
|
|
|
use UnexpectedValueException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class LibFs - Low level file-system utility functions |
11
|
|
|
* |
12
|
|
|
* @covers \Ktomk\Pipelines\LibFs |
13
|
|
|
*/ |
14
|
|
|
class LibFs |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @param string $path |
18
|
|
|
* @param string $mode [optional] |
19
|
|
|
* |
20
|
|
|
* @return bool |
21
|
|
|
*/ |
22
|
3 |
|
public static function canFopen($path, $mode = null) |
23
|
|
|
{ |
24
|
3 |
|
if (null === $mode) { |
25
|
1 |
|
$mode = 'rb'; |
26
|
|
|
} |
27
|
|
|
|
28
|
3 |
|
$handle = @fopen($path, $mode); |
29
|
3 |
|
if (false === $handle) { |
30
|
2 |
|
return false; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** @scrutinizer ignore-unhandled */ |
34
|
2 |
|
@fclose($handle); |
35
|
|
|
|
36
|
2 |
|
return true; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* locate (readable) file by basename upward all parent directories |
41
|
|
|
* |
42
|
|
|
* @param string $basename |
43
|
|
|
* @param string $directory [optional] directory to operate from, defaults |
44
|
|
|
* to "." (relative path of present working directory) |
45
|
|
|
* |
46
|
|
|
* @return null|string |
47
|
|
|
*/ |
48
|
4 |
|
public static function fileLookUp($basename, $directory = null) |
49
|
|
|
{ |
50
|
4 |
|
if ('' === $directory || null === $directory) { |
51
|
1 |
|
$directory = '.'; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
for ( |
55
|
4 |
|
$dirName = $directory, $old = null; |
56
|
4 |
|
$old !== $dirName; |
57
|
2 |
|
$old = $dirName, $dirName = dirname($dirName) |
58
|
|
|
) { |
59
|
4 |
|
$test = $dirName . '/' . $basename; |
60
|
4 |
|
if (self::isReadableFile($test)) { |
61
|
3 |
|
return $test; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
return null; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* check if path is absolute |
70
|
|
|
* |
71
|
|
|
* @param string $path |
72
|
|
|
* |
73
|
|
|
* @return bool |
74
|
|
|
*/ |
75
|
8 |
|
public static function isAbsolutePath($path) |
76
|
|
|
{ |
77
|
|
|
// TODO: a variant with PHP stream wrapper prefix support |
78
|
|
|
/* @see isStreamUri */ |
79
|
|
|
|
80
|
8 |
|
$count = strspn($path, '/', 0, 3) % 2; |
81
|
|
|
|
82
|
8 |
|
return (bool)$count; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* check if path is basename |
87
|
|
|
* |
88
|
|
|
* @param string $path |
89
|
|
|
* |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
5 |
|
public static function isBasename($path) |
93
|
|
|
{ |
94
|
5 |
|
if (in_array($path, array('', '.', '..'), true)) { |
95
|
1 |
|
return false; |
96
|
|
|
} |
97
|
|
|
|
98
|
4 |
|
if (false !== strpos($path, '/')) { |
99
|
3 |
|
return false; |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
return true; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* see 2.2 Standards permit the exclusion of bad filenames / POSIX.1-2008 |
107
|
|
|
* |
108
|
|
|
* @link https://dwheeler.com/essays/fixing-unix-linux-filenames.html |
109
|
|
|
* |
110
|
|
|
* @param string $filename |
111
|
|
|
* |
112
|
|
|
* @return bool |
113
|
|
|
*/ |
114
|
10 |
|
public static function isPortableFilename($filename) |
115
|
|
|
{ |
116
|
|
|
# A-Z, a-z, 0-9, <period>, <underscore>, and <hyphen>) |
117
|
10 |
|
$result = preg_match('(^(?!-)[A-Za-z0-9._-]+$)', $filename); |
118
|
10 |
|
if (false === $result) { |
119
|
|
|
// @codeCoverageIgnoreStart |
120
|
|
|
throw new UnexpectedValueException('preg_match pattern failed'); |
121
|
|
|
// @codeCoverageIgnoreEnd |
122
|
|
|
} |
123
|
|
|
|
124
|
10 |
|
return 1 === $result; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string $path |
129
|
|
|
* |
130
|
|
|
* @return bool |
131
|
|
|
*/ |
132
|
12 |
|
public static function isReadableFile($path) |
133
|
|
|
{ |
134
|
12 |
|
if (!is_file($path)) { |
135
|
5 |
|
return false; |
136
|
|
|
} |
137
|
|
|
|
138
|
10 |
|
return is_readable($path) ?: self::canFopen($path, 'rb'); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* similar to a readable file, a readable path allows stream-urls |
143
|
|
|
* as well, e.g. php://stdin or php://fd/0 but only for local |
144
|
|
|
* streams. |
145
|
|
|
* |
146
|
|
|
* @param string $path |
147
|
|
|
* |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
1 |
|
public static function isReadableStream($path) |
151
|
|
|
{ |
152
|
1 |
|
if (self::isReadableFile($path)) { |
153
|
1 |
|
return true; |
154
|
|
|
} |
155
|
|
|
|
156
|
1 |
|
if (!is_string($path)) { |
|
|
|
|
157
|
1 |
|
return false; |
158
|
|
|
} |
159
|
|
|
|
160
|
1 |
|
if (!stream_is_local($path)) { |
161
|
1 |
|
return false; |
162
|
|
|
} |
163
|
|
|
|
164
|
1 |
|
return self::canFopen($path, 'rb'); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param string $path |
169
|
|
|
* |
170
|
|
|
* @return bool |
171
|
|
|
*/ |
172
|
2 |
|
public static function isStreamUri($path) |
173
|
|
|
{ |
174
|
2 |
|
$scheme = parse_url($path, PHP_URL_SCHEME); |
175
|
2 |
|
if (null === $scheme) { |
176
|
1 |
|
return false; |
177
|
|
|
} |
178
|
|
|
|
179
|
1 |
|
return in_array($scheme, stream_get_wrappers(), true); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* create directory if not yet exists |
184
|
|
|
* |
185
|
|
|
* @param string $path |
186
|
|
|
* @param int $mode [optional] |
187
|
|
|
* |
188
|
|
|
* @return string |
189
|
|
|
*/ |
190
|
7 |
|
public static function mkDir($path, $mode = 0777) |
191
|
|
|
{ |
192
|
7 |
|
if (!is_dir($path)) { |
193
|
|
|
/** @noinspection NestedPositiveIfStatementsInspection */ |
194
|
6 |
|
if (!mkdir($path, $mode, true) && !is_dir($path)) { |
195
|
|
|
// @codeCoverageIgnoreStart |
196
|
|
|
throw new \RuntimeException( |
197
|
|
|
sprintf('Directory "%s" was not created', $path) |
198
|
|
|
); |
199
|
|
|
// @codeCoverageIgnoreEnd |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
7 |
|
return $path; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Normalize a path as/if common in PHP |
208
|
|
|
* |
209
|
|
|
* E.g. w/ phar:// in front which means w/ stream wrappers in |
210
|
|
|
* mind. |
211
|
|
|
* |
212
|
|
|
* @param string $path |
213
|
|
|
* |
214
|
|
|
* @return string |
215
|
|
|
*/ |
216
|
5 |
|
public static function normalizePath($path) |
217
|
|
|
{ |
218
|
5 |
|
$buffer = $path; |
219
|
|
|
|
220
|
5 |
|
$scheme = ''; |
221
|
|
|
// TODO support for all supported stream wrappers (w/ absolute/relative notation?) |
222
|
|
|
/* @see isStreamUri */ |
223
|
|
|
/* @see isAbsolutePath */ |
224
|
5 |
|
if (0 === strpos($buffer, 'phar://') || 0 === strpos($buffer, 'file://')) { |
225
|
4 |
|
$scheme = substr($buffer, 0, 7); |
226
|
4 |
|
$buffer = substr($buffer, 7); |
227
|
|
|
} |
228
|
|
|
|
229
|
5 |
|
$normalized = self::normalizePathSegments($buffer); |
230
|
|
|
|
231
|
5 |
|
return $scheme . $normalized; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Resolve relative path segments in a path on it's own |
236
|
|
|
* |
237
|
|
|
* This is not realpath, not resolving any links. |
238
|
|
|
* |
239
|
|
|
* @param string $path |
240
|
|
|
* |
241
|
|
|
* @return string |
242
|
|
|
*/ |
243
|
18 |
|
public static function normalizePathSegments($path) |
244
|
|
|
{ |
245
|
18 |
|
if ('' === $path) { |
246
|
1 |
|
return $path; |
247
|
|
|
} |
248
|
|
|
|
249
|
17 |
|
$buffer = $path; |
250
|
|
|
|
251
|
17 |
|
$prefix = ''; |
252
|
17 |
|
$len = strspn($buffer, '/'); |
253
|
17 |
|
if (0 < $len) { |
254
|
9 |
|
$prefix = substr($buffer, 0, $len); |
255
|
9 |
|
$buffer = substr($buffer, $len); |
256
|
|
|
} |
257
|
|
|
|
258
|
17 |
|
$buffer = rtrim($buffer, '/'); |
259
|
|
|
|
260
|
17 |
|
if (in_array($buffer, array('', '.'), true)) { |
261
|
4 |
|
return $prefix; |
262
|
|
|
} |
263
|
|
|
|
264
|
13 |
|
$pos = strpos($buffer, '/'); |
265
|
13 |
|
if (false === $pos) { |
266
|
2 |
|
return $prefix . $buffer; |
267
|
|
|
} |
268
|
|
|
|
269
|
11 |
|
$buffer = preg_replace('~/+~', '/', $buffer); |
270
|
|
|
|
271
|
11 |
|
$segments = explode('/', $buffer); |
272
|
11 |
|
$stack = array(); |
273
|
11 |
|
foreach ($segments as $segment) { |
274
|
11 |
|
$i = count($stack) - 1; |
275
|
11 |
|
if ('.' === $segment) { |
276
|
2 |
|
continue; |
277
|
|
|
} |
278
|
|
|
|
279
|
11 |
|
if ('..' !== $segment) { |
280
|
11 |
|
$stack[] = $segment; |
281
|
|
|
|
282
|
11 |
|
continue; |
283
|
|
|
} |
284
|
|
|
|
285
|
9 |
|
if (($i > -1) && '..' !== $stack[$i]) { |
286
|
9 |
|
array_pop($stack); |
287
|
|
|
|
288
|
9 |
|
continue; |
289
|
|
|
} |
290
|
|
|
|
291
|
1 |
|
$stack[] = $segment; |
292
|
|
|
} |
293
|
|
|
|
294
|
11 |
|
return $prefix . implode('/', $stack); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* rename a file |
299
|
|
|
* |
300
|
|
|
* @param string $old |
301
|
|
|
* @param string $new |
302
|
|
|
* |
303
|
|
|
* @return string new file-name |
304
|
|
|
*/ |
305
|
1 |
|
public static function rename($old, $new) |
306
|
|
|
{ |
307
|
1 |
|
if (!@rename($old, $new)) { |
308
|
1 |
|
throw new \RuntimeException(sprintf('Failed to rename "%s" to "%s"', $old, $new)); |
309
|
|
|
} |
310
|
|
|
|
311
|
1 |
|
return $new; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* @param string $file |
316
|
|
|
* |
317
|
|
|
* @return string |
318
|
|
|
*/ |
319
|
5 |
|
public static function rm($file) |
320
|
|
|
{ |
321
|
5 |
|
if (self::isReadableFile($file)) { |
322
|
5 |
|
unlink($file); |
323
|
|
|
} |
324
|
|
|
|
325
|
5 |
|
return $file; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* @param string $dir |
330
|
|
|
* |
331
|
|
|
* @throws UnexpectedValueException |
332
|
|
|
* |
333
|
|
|
* @return void |
334
|
|
|
*/ |
335
|
3 |
|
public static function rmDir($dir) |
336
|
|
|
{ |
337
|
3 |
|
$result = @lstat($dir); |
338
|
3 |
|
if (false === $result) { |
339
|
1 |
|
return; |
340
|
|
|
} |
341
|
|
|
|
342
|
3 |
|
$dirs = array(); |
343
|
3 |
|
$dirs[] = $dir; |
344
|
3 |
|
for ($i = 0; isset($dirs[$i]); $i++) { |
345
|
3 |
|
$current = $dirs[$i]; |
346
|
3 |
|
$result = @scandir($current); |
347
|
3 |
|
if (false === $result) { |
348
|
1 |
|
throw new UnexpectedValueException(sprintf('Failed to open directory: %s', $current)); |
349
|
|
|
} |
350
|
2 |
|
$files = array_diff($result, array('.', '..')); |
351
|
2 |
|
foreach ($files as $file) { |
352
|
1 |
|
$path = "${current}/${file}"; |
353
|
1 |
|
if (is_dir($path)) { |
354
|
1 |
|
$dirs[] = $path; |
355
|
1 |
|
} elseif (is_file($path)) { |
356
|
1 |
|
self::rm($path); |
357
|
|
|
} |
358
|
|
|
} |
359
|
|
|
} |
360
|
|
|
|
361
|
2 |
|
while (null !== ($pop = array_pop($dirs))) { |
362
|
|
|
/* @scrutinizer ignore-unhandled */ |
363
|
2 |
|
@rmdir($pop); |
364
|
|
|
} |
365
|
2 |
|
} |
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* create symbolic link, recreate if it exists |
369
|
|
|
* |
370
|
|
|
* @param string $target |
371
|
|
|
* @param string $link |
372
|
|
|
* |
373
|
|
|
* @return void |
374
|
|
|
*/ |
375
|
|
|
public static function symlink($target, $link) |
376
|
|
|
{ |
377
|
1 |
|
self::unlink($link); |
378
|
1 |
|
symlink($target, $link); |
379
|
1 |
|
} |
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* @param string $link |
383
|
|
|
* |
384
|
|
|
* @return void |
385
|
|
|
*/ |
386
|
|
|
public static function unlink($link) |
387
|
|
|
{ |
388
|
1 |
|
if (is_link($link)) { |
389
|
1 |
|
unlink($link); |
390
|
|
|
} |
391
|
1 |
|
} |
392
|
|
|
} |
393
|
|
|
|