Repository navigation
Expand file tree
/
Copy pathDirectoryWalker.php
More file actions
348 lines (295 loc) · 9.31 KB
/
Copy pathDirectoryWalker.php
File metadata and controls
348 lines (295 loc) · 9.31 KB
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
341
342
343
344
345
346
347
348
<?php declare(strict_types = 1);
namespace PHPStan\File;
use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\AutowiredService;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\Glob;
use function array_key_exists;
use function array_keys;
use function closedir;
use function explode;
use function file_get_contents;
use function file_put_contents;
use function getmypid;
use function implode;
use function is_array;
use function is_dir;
use function lstat;
use function mkdir;
use function opendir;
use function preg_match;
use function readdir;
use function rename;
use function rtrim;
use function serialize;
use function sprintf;
use function stat;
use function str_contains;
use function str_starts_with;
use function substr;
use function unlink;
use function unserialize;
use const DIRECTORY_SEPARATOR;
/**
* The raw filesystem walk behind FileFinder.
*
* The analyse and the scan FileFinder differ only in their FileExcluder, so an analysis walks the
* same directories twice: once to build the list of analysed files, and once for the result cache
* metadata, which records the files that are scanned but not analysed. walkCached() shares that
* walk between the two - both happen before the analysis starts, on a file set that is snapshotted
* for the whole run anyway.
*
* HashingFileMonitor detects changes by re-running the finder and must see the filesystem as it is now,
* so it walks uncached and clears the shared walks before each check.
*
* Reading the directories is what a walk costs - on a tree the size of Drupal core (28k directories)
* about a second, every run, in the main process and again in the worker that builds the symbol
* index. What a directory contains changes only when an entry is added, removed or renamed in it,
* so the listings are kept in tmpDir and a directory whose stat signature still matches (see
* FileStatSignatures) is not read again. The same technique is behind git's untracked cache.
*
* The walk yields what Symfony Finder yields for files()->name()->followLinks() with its default
* ignores - dot files and VCS directories left out, symlinks followed, files in readdir order - and
* falls back to Finder itself for anything it does not handle the same way (Windows paths, stream
* wrappers, unreadable directories), so errors surface exactly as they did.
*/
#[AutowiredService]
final class DirectoryWalker
{
private const LISTINGS_FORMAT = 'directoryListings-v2';
/** The directories Finder's ignoreVCS() leaves out; the ones starting with a dot are left out anyway. */
private const VCS_DIRECTORIES = ['_svn' => true, 'CVS' => true, '_darcs' => true];
/** @var array<string, list<string>> */
private array $cachedWalks = [];
/**
* Directory path => [stat signature, entries]. The entries are the directory's names in
* readdir order, each prefixed by d (directory), f (anything else - Finder's files() only leaves
* out directories, so a broken symlink is a file too) or l (symlink - resolved on every walk,
* because its target can change without this directory changing), joined by NUL.
*
* @var array<string, array{string, string}>|null
*/
private ?array $listings = null;
private bool $listingsChanged = false;
/**
* @param string $tmpDir where the listings are kept between runs, nowhere when empty
*/
public function __construct(
private FileStatSignatures $fileStatSignatures,
#[AutowiredParameter]
private string $tmpDir = '',
)
{
}
/**
* @param string[] $fileExtensions
* @return list<string>
*/
public function walk(string $directory, array $fileExtensions): array
{
$files = $this->walkWithListings($directory, $fileExtensions);
if ($files !== null) {
return $files;
}
$finder = new Finder();
$finder->followLinks();
$files = [];
foreach ($finder->files()->name('*.{' . implode(',', $fileExtensions) . '}')->in($directory) as $fileInfo) {
$files[] = $fileInfo->getPathname();
}
return $files;
}
/**
* @param string[] $fileExtensions
* @return list<string>
*/
public function walkCached(string $directory, array $fileExtensions): array
{
$key = $directory . "\n" . implode(',', $fileExtensions);
if (array_key_exists($key, $this->cachedWalks)) {
return $this->cachedWalks[$key];
}
return $this->cachedWalks[$key] = $this->walk($directory, $fileExtensions);
}
public function clearCachedWalks(): void
{
$this->cachedWalks = [];
}
/**
* @param string[] $fileExtensions
* @return list<string>|null null when Finder has to do the walk
*/
private function walkWithListings(string $directory, array $fileExtensions): ?array
{
if (DIRECTORY_SEPARATOR !== '/' || str_contains($directory, '://')) {
return null;
}
// what Finder's normalizeDir() does, the filesystem root aside
$directory = rtrim($directory, '/');
if ($directory === '') {
return null;
}
$this->loadListings();
$files = [];
$visited = [];
if (!$this->walkDirectory($directory, Glob::toRegex('*.{' . implode(',', $fileExtensions) . '}'), $this->fileStatSignatures->begin(), $files, $visited)) {
return null;
}
$this->saveListings($directory, $visited);
return $files;
}
/**
* @param list<string> $files
* @param array<string, true> $visited
*/
private function walkDirectory(string $directory, string $pattern, FileStatSignatureReader $signatures, array &$files, array &$visited): bool
{
$stat = @stat($directory);
if ($stat === false) {
return false;
}
$visited[$directory] = true;
$signature = $signatures->fromStat($stat);
$listing = $this->listings[$directory] ?? null;
if ($signature !== null && $listing !== null && $listing[0] === $signature) {
$entries = $listing[1];
} else {
$entries = $this->readDirectory($directory);
if ($entries === null) {
return false;
}
if ($signature !== null) {
$this->listings[$directory] = [$signature, $entries];
} else {
unset($this->listings[$directory]);
}
$this->listingsChanged = true;
}
if ($entries === '') {
return true;
}
foreach (explode("\0", $entries) as $entry) {
$type = $entry[0];
$name = substr($entry, 1);
$path = $directory . '/' . $name;
if ($type === 'l') {
if (is_dir($path)) {
if (isset(self::VCS_DIRECTORIES[$name])) {
continue;
}
$type = 'd';
} else {
$type = 'f';
}
}
if ($type === 'd') {
if (!$this->walkDirectory($path, $pattern, $signatures, $files, $visited)) {
return false;
}
continue;
}
if (preg_match($pattern, $name) !== 1) {
continue;
}
$files[] = $path;
}
return true;
}
private function readDirectory(string $directory): ?string
{
$handle = @opendir($directory);
if ($handle === false) {
return null;
}
$entries = [];
while (($name = readdir($handle)) !== false) {
// also skips . and .. - Finder leaves out everything starting with a dot
if (str_starts_with($name, '.')) {
continue;
}
$stat = @lstat($directory . '/' . $name);
if ($stat === false) {
continue;
}
$type = $stat['mode'] & 0170000;
if ($type === 0120000) {
$entries[] = 'l' . $name;
} elseif ($type === 0040000) {
if (isset(self::VCS_DIRECTORIES[$name])) {
continue;
}
$entries[] = 'd' . $name;
} else {
$entries[] = 'f' . $name;
}
}
closedir($handle);
return implode("\0", $entries);
}
private function getListingsFile(): ?string
{
if ($this->tmpDir === '') {
return null;
}
return $this->tmpDir . '/cache/directory-listings.bin';
}
private function loadListings(): void
{
if ($this->listings !== null) {
return;
}
$this->listings = [];
$file = $this->getListingsFile();
if ($file === null) {
return;
}
$contents = @file_get_contents($file);
if ($contents === false) {
return;
}
$data = @unserialize($contents, ['allowed_classes' => false]);
if (!is_array($data) || ($data['format'] ?? null) !== self::LISTINGS_FORMAT || !is_array($data['listings'] ?? null)) {
return;
}
/** @var array<string, array{string, string}> $listings */
$listings = $data['listings'];
$this->listings = $listings;
}
/**
* @param array<string, true> $visited
*/
private function saveListings(string $root, array $visited): void
{
if (!$this->listingsChanged || $this->listings === null) {
return;
}
// A directory under the walked root that the walk did not reach is gone, or no longer
// reachable - either way its listing is not going to be read again.
foreach (array_keys($this->listings) as $directory) {
if (isset($visited[$directory]) || !str_starts_with($directory, $root . '/')) {
continue;
}
unset($this->listings[$directory]);
}
$this->listingsChanged = false;
$file = $this->getListingsFile();
if ($file === null) {
return;
}
$cacheDirectory = $this->tmpDir . '/cache';
if (!is_dir($cacheDirectory) && !@mkdir($cacheDirectory, 0777, true) && !is_dir($cacheDirectory)) {
return;
}
// written next to the final path and renamed into place, so that a concurrent run reads
// either the old listings or the new ones
$pid = getmypid();
$temporaryFile = sprintf('%s.%s.tmp', $file, $pid === false ? 'x' : $pid);
if (@file_put_contents($temporaryFile, serialize(['format' => self::LISTINGS_FORMAT, 'listings' => $this->listings])) === false) {
return;
}
if (@rename($temporaryFile, $file)) {
return;
}
@unlink($temporaryFile);
}
}