-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstallManifestManager.php
More file actions
309 lines (263 loc) · 8.48 KB
/
Copy pathInstallManifestManager.php
File metadata and controls
309 lines (263 loc) · 8.48 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
<?php
declare(strict_types=1);
namespace Oak\Engine\Installer;
/**
* Tracks every file (with its SHA1 hash) that an installation wrote to disk
* so that subsequent installs can remove files which are no longer part of
* the package.
*
* One manifest is kept per install target directory (runner, plugin, data).
*/
final class InstallManifestManager
{
public const MANIFEST_FILENAME = '.oak-install-manifest.json';
/**
* @return array{
* package_type: string,
* package_id: string,
* version: string,
* files: array<string, string>
* }|null
*/
public function loadManifest(string $packageTargetDir): ?array
{
$path = $this->manifestPath($packageTargetDir);
if (!is_file($path)) {
return null;
}
$content = file_get_contents($path);
if (!is_string($content)) {
return null;
}
$decoded = json_decode($content, true);
if (!is_array($decoded)) {
return null;
}
$files = $decoded['files'] ?? null;
if (!is_array($files)) {
return null;
}
$normalizedFiles = [];
foreach ($files as $relativePath => $hash) {
if (!is_string($relativePath) || !is_scalar($hash)) {
continue;
}
$normalizedFiles[$relativePath] = (string) $hash;
}
return [
'package_type' => $this->normalizeString($decoded['package_type'] ?? null),
'package_id' => $this->normalizeString($decoded['package_id'] ?? null),
'version' => $this->normalizeString($decoded['version'] ?? null),
'files' => $normalizedFiles,
];
}
public function manifestExists(string $packageTargetDir): bool
{
return is_file($this->manifestPath($packageTargetDir));
}
/**
* @param array{
* package_type: string,
* package_id: string,
* version: string,
* files: array<string, string>
* } $manifest
*/
public function saveManifest(string $packageTargetDir, array $manifest): bool
{
$path = $this->manifestPath($packageTargetDir);
$directory = dirname($path);
if (!is_dir($directory)) {
if (!createDirectoryTree($directory, 0o755)) {
return false;
}
}
$content = json_encode($manifest, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
if (!is_string($content)) {
return false;
}
return false !== file_put_contents($path, $content);
}
/**
* Builds a manifest from the given extracted relative paths by computing
* the SHA1 hash of every file that still exists on disk.
*
* @param list<string> $extractedFiles
*
* @return array{
* package_type: string,
* package_id: string,
* version: string,
* files: array<string, string>
* }
*/
public function buildManifest(
string $packageTargetDir,
string $packageType,
string $packageId,
string $version,
array $extractedFiles,
): array {
$files = [];
$basePath = rtrim($packageTargetDir, '/');
foreach ($extractedFiles as $relativePath) {
if (!is_string($relativePath) || '' === $relativePath) {
continue;
}
$normalizedPath = str_replace('\\', '/', $relativePath);
if (self::MANIFEST_FILENAME === $normalizedPath) {
continue;
}
$absolutePath = $basePath.'/'.ltrim($normalizedPath, '/');
if (!is_file($absolutePath)) {
continue;
}
$hash = sha1_file($absolutePath);
if (!is_string($hash)) {
continue;
}
$files[$normalizedPath] = $hash;
}
return [
'package_type' => $packageType,
'package_id' => $packageId,
'version' => $version,
'files' => $files,
];
}
/**
* Returns the relative paths of files that are tracked by the old manifest
* but are no longer present in the new manifest.
*
* @param array{
* package_type: string,
* package_id: string,
* version: string,
* files: array<string, string>
* }|null $oldManifest
* @param array{
* package_type: string,
* package_id: string,
* version: string,
* files: array<string, string>
* } $newManifest
*
* @return list<string>
*/
public function diffStaleFiles(?array $oldManifest, array $newManifest): array
{
if (null === $oldManifest) {
return [];
}
$oldFiles = $oldManifest['files'];
$newFiles = $newManifest['files'];
$stale = [];
foreach ($oldFiles as $relativePath => $hash) {
if (!is_string($relativePath)) {
continue;
}
if (!array_key_exists($relativePath, $newFiles)) {
$stale[] = $relativePath;
}
}
sort($stale);
return $stale;
}
/**
* Removes the given stale files (relative to the package target directory)
* and afterwards every directory that became empty as a result.
*
* @param list<string> $staleFiles
*
* @return array{
* deleted_files: list<string>,
* deleted_dirs: list<string>,
* errors: list<string>
* }
*/
public function deleteStaleFilesAndEmptyDirs(string $packageTargetDir, array $staleFiles): array
{
$basePath = rtrim($packageTargetDir, '/');
$deletedFiles = [];
$errors = [];
foreach ($staleFiles as $relativePath) {
if (!is_string($relativePath) || '' === $relativePath) {
continue;
}
$normalizedPath = str_replace('\\', '/', $relativePath);
if ($this->containsTraversal($normalizedPath)) {
continue;
}
$absolutePath = $basePath.'/'.ltrim($normalizedPath, '/');
if (!is_file($absolutePath)) {
continue;
}
if (@unlink($absolutePath)) {
$deletedFiles[] = $normalizedPath;
} else {
$errors[] = $absolutePath;
}
}
$deletedDirs = $this->removeEmptyDirectories($basePath);
return [
'deleted_files' => $deletedFiles,
'deleted_dirs' => $deletedDirs,
'errors' => $errors,
];
}
public function manifestPath(string $packageTargetDir): string
{
return rtrim($packageTargetDir, '/').'/'.self::MANIFEST_FILENAME;
}
/**
* @return list<string>
*/
private function removeEmptyDirectories(string $basePath): array
{
$deleted = [];
if (!is_dir($basePath)) {
return $deleted;
}
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($basePath, \FilesystemIterator::SKIP_DOTS),
\RecursiveIteratorIterator::CHILD_FIRST,
\RecursiveIteratorIterator::CATCH_GET_CHILD,
);
foreach ($iterator as $item) {
\assert($item instanceof \SplFileInfo);
if (!$item->isDir()) {
continue;
}
$path = $item->getPathname();
if ($this->isDirectoryEmpty($path) && @rmdir($path)) {
$deleted[] = $this->relativePathFromBase($path, $basePath);
}
}
return $deleted;
}
private function isDirectoryEmpty(string $directory): bool
{
$entries = @scandir($directory);
return is_array($entries) && 2 === count($entries);
}
/**
* Returns true when the relative path contains a `..` segment which would
* escape the package target directory.
*/
private function containsTraversal(string $relativePath): bool
{
return in_array('..', explode('/', $relativePath), true);
}
private function relativePathFromBase(string $absolutePath, string $basePath): string
{
$relative = substr($absolutePath, strlen(rtrim($basePath, '/')) + 1);
return str_replace('\\', '/', (string) $relative);
}
private function normalizeString(mixed $value): string
{
if (!is_scalar($value)) {
return '';
}
return trim((string) $value);
}
}