Skip to content
Snippets Groups Projects

Issue #3231996: Ensure file system is writeable

Compare and
3 files
+ 135
0
Compare changes
  • Side-by-side
  • Inline
Files
3
+ 111
0
 
<?php
 
 
namespace Drupal\automatic_updates\Validator;
 
 
use Drupal\automatic_updates\AutomaticUpdatesEvents;
 
use Drupal\automatic_updates\Event\UpdateEvent;
 
use Drupal\automatic_updates\PathLocator;
 
use Drupal\automatic_updates\Validation\ValidationResult;
 
use Drupal\Core\File\Exception\FileException;
 
use Drupal\Core\File\FileSystemInterface;
 
use Drupal\Core\StringTranslation\StringTranslationTrait;
 
use Drupal\Core\StringTranslation\TranslatableMarkup;
 
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
 
 
/**
 
* Validates that important file system paths are writable.
 
*/
 
class WritableFileSystemValidator implements EventSubscriberInterface {
 
 
use StringTranslationTrait;
 
 
/**
 
* The path locator service.
 
*
 
* @var \Drupal\automatic_updates\PathLocator
 
*/
 
protected $pathLocator;
 
 
/**
 
* The file system service.
 
*
 
* @var \Drupal\Core\File\FileSystemInterface
 
*/
 
protected $fileSystem;
 
 
/**
 
* Constructs a WriteableFileSystemValidator object.
 
*
 
* @param \Drupal\automatic_updates\PathLocator $path_locator
 
* The path locator service.
 
* @param \Drupal\Core\File\FileSystemInterface $file_system
 
* The file system service.
 
*/
 
public function __construct(PathLocator $path_locator, FileSystemInterface $file_system) {
 
$this->pathLocator = $path_locator;
 
$this->fileSystem = $file_system;
 
}
 
 
/**
 
* Validates that the file system is writable.
 
*
 
* @param \Drupal\automatic_updates\Event\UpdateEvent $event
 
* The event object.
 
*/
 
public function checkPermissions(UpdateEvent $event): void {
 
$dir = implode(DIRECTORY_SEPARATOR, [
 
$this->pathLocator->getProjectRoot(),
 
$this->pathLocator->getWebRoot(),
 
]);
 
$message = $this->t('The Drupal directory "@dir" is not writable.', [
 
'@dir' => $dir,
 
]);
 
$this->checkPath($dir, 'core/core.api.php', $event, $message);
 
 
$dir = $this->pathLocator->getVendorDirectory();
 
$message = $this->t('The vendor directory "@dir" is not writable.', [
 
'@dir' => $dir,
 
]);
 
$this->checkPath($dir, 'composer/LICENSE', $event, $message);
 
}
 
 
/**
 
* Tests that a path is writable by writing and deleting a temporary file.
 
*
 
* @param string $path_to_check
 
* The path (directory) to test.
 
* @param string $file_to_copy
 
* The path of the file, relative to $path_to_check, which should be copied
 
* temporarily.
 
* @param \Drupal\automatic_updates\Event\UpdateEvent $event
 
* The event object.
 
* @param \Drupal\Core\StringTranslation\TranslatableMarkup $summary
 
* The error summary if the path is not writable.
 
*/
 
protected function checkPath(string $path_to_check, string $file_to_copy, UpdateEvent $event, TranslatableMarkup $summary): void {
 
$source = $path_to_check . DIRECTORY_SEPARATOR . $file_to_copy;
 
$destination = uniqid($source);
 
 
try {
 
// If we can copy the file and then delete the copy, the file system is
 
// writable.
 
$this->fileSystem->copy($source, $destination, FileSystemInterface::EXISTS_REPLACE);
 
$this->fileSystem->delete($destination);
 
}
 
catch (FileException $e) {
 
$message = $e->getMessage();
 
$result = ValidationResult::createError([$message], $summary);
 
$event->addValidationResult($result);
 
}
 
}
 
 
/**
 
* {@inheritdoc}
 
*/
 
public static function getSubscribedEvents() {
 
return [
 
AutomaticUpdatesEvents::READINESS_CHECK => 'checkPermissions',
 
];
 
}
 
 
}
Loading