Newer
Older

Lucas Hedding
committed
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
<?php
namespace Drupal\automatic_updates\Services;
use Drupal\Core\Archiver\ArchiverInterface;
use Drupal\Core\Archiver\ArchiverManager;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\File\Exception\FileException;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Url;
use DrupalFinder\DrupalFinder;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\RequestException;
use Psr\Log\LoggerInterface;
/**
* Class to apply in-place updates.
*/
class InPlaceUpdate implements UpdateInterface {
/**
* The manifest file that lists all file deletions.
*/
const DELETION_MANIFEST = 'DELETION_MANIFEST.txt';
/**
* The checksum file with hashes of archive file contents.
*/
const CHECKSUM_LIST = 'checksumlist.csig';
/**
* The directory inside the archive for file additions and modifications.
*/
const ARCHIVE_DIRECTORY = 'files/';
/**
* The logger.
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* The archive manager.
*
* @var \Drupal\Core\Archiver\ArchiverManager
*/
protected $archiveManager;
/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The file system service.
*
* @var \Drupal\Core\File\FileSystemInterface
*/
protected $fileSystem;
/**
* The HTTP client service.
*
* @var \GuzzleHttp\Client
*/
protected $httpClient;
/**
* The root file path.
*
* @var string
*/
protected $rootPath;
/**
* The vendor file path.
*
* @var string
*/
protected $vendorPath;
/**
* The folder where files are backed up.
*
* @var string
*/
protected $backup;
/**
* The temporary extract directory.
*
* @var string
*/
protected $tempDirectory;
/**
* Constructs an InPlaceUpdate.
*
* @param \Psr\Log\LoggerInterface $logger
* The logger.
* @param \Drupal\Core\Archiver\ArchiverManager $archive_manager
* The archive manager.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory.
* @param \Drupal\Core\File\FileSystemInterface $file_system
* The filesystem service.
* @param \GuzzleHttp\ClientInterface $http_client
* The HTTP client service.
* @param \DrupalFinder\DrupalFinder $drupal_finder
* The Drupal finder service.
*/
public function __construct(LoggerInterface $logger, ArchiverManager $archive_manager, ConfigFactoryInterface $config_factory, FileSystemInterface $file_system, ClientInterface $http_client, DrupalFinder $drupal_finder) {
$this->logger = $logger;
$this->archiveManager = $archive_manager;
$this->configFactory = $config_factory;
$this->fileSystem = $file_system;
$this->httpClient = $http_client;
$drupal_finder->locateRoot(getcwd());
$this->rootPath = $drupal_finder->getDrupalRoot();
$this->vendorPath = rtrim($drupal_finder->getVendorDir(), '/\\') . DIRECTORY_SEPARATOR;
}
/**
* {@inheritdoc}
*/
public function update($project_name, $project_type, $from_version, $to_version) {
$success = FALSE;
if ($project_name === 'drupal') {
$project_root = $this->rootPath;
}
else {
$project_root = drupal_get_path($project_type, $project_name);
}
if ($archive = $this->getArchive($project_name, $from_version, $to_version)) {
if ($this->backup($archive, $project_root)) {
$success = $this->processUpdate($archive, $project_root);
}
if (!$success) {
$this->rollback($project_root);
}
}
return $success;
}
/**
* Get an archive with the quasi-patch contents.
*
* @param string $project_name
* The project name.
* @param string $from_version
* The current project version.
* @param string $to_version
* The desired next project version.
*
* @return \Drupal\Core\Archiver\ArchiverInterface|null
* The archive or NULL if download fails.
*/
protected function getArchive($project_name, $from_version, $to_version) {
$url = $this->buildUrl($project_name, $this->getQuasiPatchFileName($project_name, $from_version, $to_version));
$destination = $this->fileSystem->realpath($this->fileSystem->getDestinationFilename("temporary://$project_name.zip", FileSystemInterface::EXISTS_RENAME));
try {
$this->httpClient->get($url, ['sink' => $destination]);
/** @var \Drupal\Core\Archiver\ArchiverInterface $archive */
return $this->archiveManager->getInstance(['filepath' => $destination]);
}
catch (RequestException $exception) {
$this->logger->error('Update for @project to version @version failed for reason @message', [
'@project' => $project_name,
'@version' => $to_version,
'@message' => $exception->getMessage(),
]);
}
}
/**
* Process update.
*
* @param \Drupal\Core\Archiver\ArchiverInterface $archive
* The archive.
* @param string $project_root
* The project root directory.
*
* @return bool
* Return TRUE if update succeeds, FALSE otherwise.
*/
protected function processUpdate(ArchiverInterface $archive, $project_root) {
$archive->extract($this->getTempDirectory());
foreach ($this->getFilesList($this->getTempDirectory()) as $file) {
$file_real_path = $this->getFileRealPath($file);
$file_path = substr($file_real_path, strlen($this->getTempDirectory() . self::ARCHIVE_DIRECTORY));
$project_real_path = $this->getProjectRealPath($file_path, $project_root);
try {
$directory = dirname($project_real_path);
$this->fileSystem->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY);
$this->fileSystem->copy($file_real_path, $project_real_path, FileSystemInterface::EXISTS_REPLACE);
}
catch (FileException $exception) {
return FALSE;
}
}
foreach ($this->getDeletions() as $deletion) {
try {
$this->fileSystem->delete($this->getProjectRealPath($deletion, $project_root));
}
catch (FileException $exception) {
return FALSE;
}
}
return TRUE;
}
/**
* Backup before an update.
*
* @param \Drupal\Core\Archiver\ArchiverInterface $archive
* The archive.
* @param string $project_root
* The project root directory.
*
* @return bool
* Return TRUE if backup succeeds, FALSE otherwise.
*/
protected function backup(ArchiverInterface $archive, $project_root) {
$backup = $this->fileSystem->createFilename('automatic_updates-backup', 'temporary://');
$this->fileSystem->prepareDirectory($backup);
$this->backup = $this->fileSystem->realpath($backup) . DIRECTORY_SEPARATOR;
if (!$this->backup) {
return FALSE;
}
foreach ($archive->listContents() as $file) {
// Ignore files that aren't in the files directory.
if (!$this->stripFileDirectoryPath($file)) {
continue;
}
$success = $this->doBackup($file, $project_root);
if (!$success) {
return FALSE;
}
}
$archive->extract($this->getTempDirectory(), [self::DELETION_MANIFEST]);
foreach ($this->getDeletions() as $deletion) {
$success = $this->doBackup($deletion, $project_root);
if (!$success) {
return FALSE;
}
}
return TRUE;
}
/**
* Remove the files directory path from files from the archive.
*
* @param string $file
* The file path.
*
* @return bool
* TRUE if path was removed, else FALSE.
*/
protected function stripFileDirectoryPath(&$file) {
if (substr($file, 0, 6) === self::ARCHIVE_DIRECTORY) {
$file = substr($file, 6);
return TRUE;
}
return FALSE;
}
/**
* Execute file backup.
*
* @param string $file
* The file to backup.
* @param string $project_root
* The project root directory.
*
* @return bool
* Return TRUE if backup succeeds, FALSE otherwise.
*/
protected function doBackup($file, $project_root) {
$directory = $this->backup . dirname($file);
if (!file_exists($directory) && !$this->fileSystem->mkdir($directory, NULL, TRUE)) {
return FALSE;
}
$project_real_path = $this->getProjectRealPath($file, $project_root);
if (file_exists($project_real_path) && !is_dir($project_real_path)) {
try {
$this->fileSystem->copy($project_real_path, $this->backup . $file, FileSystemInterface::EXISTS_REPLACE);
}
catch (FileException $exception) {
return FALSE;
}
}
return TRUE;
}
/**
* Rollback after a failed update.
*
* @param string $project_root
* The project root directory.
*/
protected function rollback($project_root) {
if (!$this->backup) {
return;
}
foreach ($this->getFilesList($this->backup) as $file) {
$file_real_path = $this->getFileRealPath($file);
$file_path = substr($file_real_path, strlen($this->backup));
try {
$this->fileSystem->copy($file_real_path, $this->getProjectRealPath($file_path, $project_root), FileSystemInterface::EXISTS_REPLACE);
}
catch (FileException $exception) {
$this->logger->error('@file was not rolled back successfully.', ['@file' => $file_real_path]);
}
}
}
/**
* Provide a recursive list of files, excluding directories.
*
* @param string $directory
* The directory to recurse for files.
*
* @return \RecursiveIteratorIterator|\SplFileInfo[]
* The iterator of SplFileInfos.
*/
protected function getFilesList($directory) {
$filter = function ($file, $file_name, $iterator) {
/** @var \SplFileInfo $file */
/** @var string $file_name */
/** @var \RecursiveDirectoryIterator $iterator */
if ($iterator->hasChildren() && !in_array($file->getFilename(), ['.git'], TRUE)) {
return TRUE;
}
return $file->isFile() && !in_array($file->getFilename(), [self::DELETION_MANIFEST], TRUE);
};
$innerIterator = new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::SKIP_DOTS);
return new \RecursiveIteratorIterator(new \RecursiveCallbackFilterIterator($innerIterator, $filter));
}
/**
* Build a project quasi-patch download URL.
*
* @param string $project_name
* The project name.
* @param string $file_name
* The file name.
*
* @return string
* The URL endpoint with for an extension.
*/
protected function buildUrl($project_name, $file_name) {
$uri = $this->configFactory->get('automatic_updates.settings')->get('download_uri');
return Url::fromUri($uri . "/$project_name/" . $file_name)->toString();
}
/**
* Get the quasi-patch file name.
*
* @param string $project_name
* The project name.
* @param string $from_version
* The current project version.
* @param string $to_version
* The desired next project version.
*
* @return string
* The quasi-patch file name.
*/
protected function getQuasiPatchFileName($project_name, $from_version, $to_version) {
return "$project_name-$from_version-to-$to_version.zip";
}
/**
* Get file real path.
*
* @param \SplFileInfo $file
* The file to retrieve the real path.
*
* @return string
* The file real path.
*/
protected function getFileRealPath(\SplFileInfo $file) {
$real_path = $file->getRealPath();
if (!$real_path) {
throw new FileException(sprintf('Could not get real path for "%s"', $file->getFilename()));
}
return $real_path;
}
/**
* Get the real path of a file.
*
* @param string $file_path
* The file path.
* @param string $project_root
* The project root directory.
*
* @return string
* The real path of a file.
*/
protected function getProjectRealPath($file_path, $project_root) {
if (substr($file_path, 0, 6) === 'vendor/') {
return $this->vendorPath . substr($file_path, 7);
}
return rtrim($project_root, '/\\') . DIRECTORY_SEPARATOR . $file_path;
}
/**
* Provides the temporary extraction directory.
*
* @return string
* The temporary directory.
*/
protected function getTempDirectory() {
if (!$this->tempDirectory) {
$this->tempDirectory = $this->fileSystem->createFilename('automatic_updates-update', 'temporary://');
$this->fileSystem->prepareDirectory($this->tempDirectory);
$this->tempDirectory = $this->fileSystem->realpath($this->tempDirectory) . DIRECTORY_SEPARATOR;
}
return $this->tempDirectory;
}
/**
* Get an iterator of files to delete.
*
* @return \ArrayIterator
* Iterator of files to delete.
*/
protected function getDeletions() {
$deletions = [];
if (!file_exists($this->getTempDirectory() . self::DELETION_MANIFEST)) {
return new \ArrayIterator($deletions);
}
$handle = fopen($this->getTempDirectory() . self::DELETION_MANIFEST, 'r');
if ($handle) {
while (($deletion = fgets($handle)) !== FALSE) {
if ($result = trim($deletion)) {
$deletions[] = $result;
}
}
fclose($handle);
}
return new \ArrayIterator($deletions);
}
}