|
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
|
2 |
|
public static function canFopen($path, $mode = null) |
|
23
|
|
|
{ |
|
24
|
2 |
|
if (null === $mode) { |
|
25
|
1 |
|
$mode = 'rb'; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
2 |
|
$handle = @fopen($path, $mode); |
|
29
|
2 |
|
if (false === $handle) { |
|
30
|
1 |
|
return false; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** @scrutinizer ignore-unhandled */ |
|
34
|
1 |
|
@fclose($handle); |
|
35
|
|
|
|
|
36
|
1 |
|
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
|
|
|
* see 2.2 Standards permit the exclusion of bad filenames / POSIX.1-2008 |
|
70
|
|
|
* |
|
71
|
|
|
* @link https://dwheeler.com/essays/fixing-unix-linux-filenames.html |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $filename |
|
74
|
|
|
* |
|
75
|
|
|
* @return bool |
|
76
|
|
|
*/ |
|
77
|
11 |
|
public static function isPortableFilename($filename) |
|
78
|
|
|
{ |
|
79
|
|
|
# A-Z, a-z, 0-9, <period>, <underscore>, and <hyphen> |
|
80
|
11 |
|
return 1 === Preg::match('(^(?!-)[A-Za-z0-9._-]+$)', $filename); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param string $path |
|
85
|
|
|
* |
|
86
|
|
|
* @return bool |
|
87
|
|
|
*/ |
|
88
|
11 |
|
public static function isReadableFile($path) |
|
89
|
|
|
{ |
|
90
|
11 |
|
if (!is_file($path)) { |
|
91
|
4 |
|
return false; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
9 |
|
return is_readable($path) ?: self::canFopen($path, 'rb'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* create directory if not yet exists |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $path |
|
101
|
|
|
* @param int $mode [optional] |
|
102
|
|
|
* |
|
103
|
|
|
* @return string |
|
104
|
|
|
*/ |
|
105
|
8 |
|
public static function mkDir($path, $mode = 0777) |
|
106
|
|
|
{ |
|
107
|
8 |
|
if (!is_dir($path)) { |
|
108
|
|
|
/** @noinspection NestedPositiveIfStatementsInspection */ |
|
109
|
7 |
|
if (!mkdir($path, $mode, true) && !is_dir($path)) { |
|
110
|
|
|
// @codeCoverageIgnoreStart |
|
111
|
|
|
throw new \RuntimeException( |
|
112
|
|
|
sprintf('Directory "%s" was not created', $path) |
|
113
|
|
|
); |
|
114
|
|
|
// @codeCoverageIgnoreEnd |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
8 |
|
return $path; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* create symbolic link to a directory with parents |
|
123
|
|
|
* |
|
124
|
|
|
* do not create if link pathname is already a |
|
125
|
|
|
* directory or targeting one (even if not the target). |
|
126
|
|
|
* |
|
127
|
|
|
* create parent directory/ies of link if necessary. |
|
128
|
|
|
* |
|
129
|
|
|
* @param string $target pathname of target directory |
|
130
|
|
|
* @param string $link pathname of link |
|
131
|
|
|
* |
|
132
|
|
|
* @return void |
|
133
|
|
|
*/ |
|
134
|
1 |
|
public static function symlinkWithParents($target, $link) |
|
135
|
|
|
{ |
|
136
|
1 |
|
$linkDir = Libfs::mkDir(dirname($link)); |
|
|
|
|
|
|
137
|
1 |
|
if (!is_dir($link)) { |
|
138
|
1 |
|
LibFs::symlink($target, $link); |
|
139
|
1 |
|
if (!is_link($link)) { |
|
140
|
|
|
// @codeCoverageIgnoreStart |
|
141
|
|
|
throw new \RuntimeException( |
|
142
|
|
|
sprintf('Link "%s" was not created', $link) |
|
143
|
|
|
); |
|
144
|
|
|
// @codeCoverageIgnoreEnd |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
1 |
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* rename a file |
|
151
|
|
|
* |
|
152
|
|
|
* @param string $old |
|
153
|
|
|
* @param string $new |
|
154
|
|
|
* |
|
155
|
|
|
* @return string new file-name |
|
156
|
|
|
*/ |
|
157
|
1 |
|
public static function rename($old, $new) |
|
158
|
|
|
{ |
|
159
|
1 |
|
if (!@rename($old, $new)) { |
|
160
|
1 |
|
throw new \RuntimeException(sprintf('Failed to rename "%s" to "%s"', $old, $new)); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
1 |
|
return $new; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @param string $file |
|
168
|
|
|
* |
|
169
|
|
|
* @return string |
|
170
|
|
|
*/ |
|
171
|
5 |
|
public static function rm($file) |
|
172
|
|
|
{ |
|
173
|
5 |
|
if (self::isReadableFile($file)) { |
|
174
|
5 |
|
unlink($file); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
5 |
|
return $file; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @param string $dir |
|
182
|
|
|
* |
|
183
|
|
|
* @throws UnexpectedValueException |
|
184
|
|
|
* |
|
185
|
|
|
* @return void |
|
186
|
|
|
*/ |
|
187
|
4 |
|
public static function rmDir($dir) |
|
188
|
|
|
{ |
|
189
|
4 |
|
$result = @lstat($dir); |
|
190
|
4 |
|
if (false === $result) { |
|
191
|
1 |
|
return; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
4 |
|
$stack = array(); |
|
195
|
4 |
|
$stack[] = $dir; |
|
196
|
|
|
|
|
197
|
4 |
|
for ($i = 0; isset($stack[$i]); $i++) { |
|
198
|
4 |
|
$current = $stack[$i]; |
|
199
|
4 |
|
$result = @scandir($current); |
|
200
|
4 |
|
if (false === $result) { |
|
201
|
1 |
|
throw new UnexpectedValueException(sprintf('Failed to open directory: %s', $current)); |
|
202
|
|
|
} |
|
203
|
3 |
|
$files = array_diff($result, array('.', '..')); |
|
204
|
3 |
|
foreach ($files as $file) { |
|
205
|
2 |
|
$path = "${current}/${file}"; |
|
206
|
2 |
|
if (is_link($path)) { |
|
207
|
1 |
|
self::unlink($path); |
|
208
|
2 |
|
} elseif (is_dir($path)) { |
|
209
|
2 |
|
$stack[] = $path; |
|
210
|
1 |
|
} elseif (is_file($path)) { |
|
211
|
1 |
|
self::rm($path); |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
3 |
|
for ($i = count($stack); isset($stack[--$i]);) { |
|
217
|
|
|
/* @scrutinizer ignore-unhandled */ |
|
218
|
3 |
|
@rmdir($stack[$i]); |
|
219
|
|
|
} |
|
220
|
3 |
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* create symbolic link, recreate if it exists |
|
224
|
|
|
* |
|
225
|
|
|
* @param string $target |
|
226
|
|
|
* @param string $link |
|
227
|
|
|
* |
|
228
|
|
|
* @return void |
|
229
|
|
|
*/ |
|
230
|
2 |
|
public static function symlink($target, $link) |
|
231
|
|
|
{ |
|
232
|
2 |
|
self::unlink($link); |
|
233
|
2 |
|
symlink($target, $link); |
|
234
|
2 |
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* @param string $link |
|
238
|
|
|
* |
|
239
|
|
|
* @return void |
|
240
|
|
|
*/ |
|
241
|
2 |
|
public static function unlink($link) |
|
242
|
|
|
{ |
|
243
|
2 |
|
if (is_link($link)) { |
|
244
|
2 |
|
unlink($link); |
|
245
|
|
|
} |
|
246
|
2 |
|
} |
|
247
|
|
|
} |
|
248
|
|
|
|