Skip to content
Snippets Groups Projects

Issue #3231996: Ensure file system is writeable

1 unresolved thread
Compare and Show latest version
8 files
+ 230
17
Compare changes
  • Side-by-side
  • Inline
Files
8
@@ -7,13 +7,12 @@ use Drupal\automatic_updates\Event\UpdateEvent;
use Drupal\automatic_updates\PathLocator;
use Drupal\automatic_updates\Validation\ValidationResult;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Validates that important file system paths are writable.
* Checks that the file system is writable.
*/
class WriteableFileSystemValidator implements EventSubscriberInterface {
class WritableFileSystemValidator implements EventSubscriberInterface {
use StringTranslationTrait;
@@ -25,51 +24,54 @@ class WriteableFileSystemValidator implements EventSubscriberInterface {
protected $pathLocator;
/**
* Constructs a WriteableFileSystemValidator object.
* The Drupal root.
*
* @var string
*/
protected $appRoot;
/**
* Constructs a WritableFileSystemValidator object.
*
* @param \Drupal\automatic_updates\PathLocator $path_locator
* The path locator service.
* @param string $app_root
* The Drupal root.
*/
public function __construct(PathLocator $path_locator) {
public function __construct(PathLocator $path_locator, string $app_root) {
$this->pathLocator = $path_locator;
$this->appRoot = $app_root;
}
/**
* Validates that the file system is writable.
* Checks that the file system is writable.
*
* @param \Drupal\automatic_updates\Event\UpdateEvent $event
* The event object.
*
* @todo It might make sense to use a more sophisticated method of testing
* writability than is_writable(), since it's not clear if that can return
* false negatives/positives due to things like SELinux, exotic file
* systems, and so forth.
*/
public function checkPermissions(UpdateEvent $event): void {
$dir = $this->pathLocator->getProjectRoot();
$message = $this->t('The project directory "@dir" is not writable.', [
'@dir' => $dir,
]);
$this->checkPath($dir, $event, $message);
$messages = [];
if (!is_writable($this->appRoot)) {
$messages[] = $this->t('The Drupal directory "@dir" is not writable.', [
'@dir' => $this->appRoot,
]);
}
$dir = $this->pathLocator->getVendorDirectory();
$message = $this->t('The vendor directory "@dir" is not writable.', [
'@dir' => $dir,
]);
$this->checkPath($dir, $event, $message);
}
if (!is_writable($dir)) {
$messages[] = $this->t('The vendor directory "@dir" is not writable.', ['@dir' => $dir]);
}
/**
* Checks that a path is writable, and flags an error if it's not.
*
* @param string $path
* The path to check.
* @param \Drupal\automatic_updates\Event\UpdateEvent $event
* The event object.
* @param \Drupal\Core\StringTranslation\TranslatableMarkup $error_message
* The translated error message if the path is not writeable.
*/
protected function checkPath(string $path, UpdateEvent $event, TranslatableMarkup $error_message): void {
if (is_writable($path)) {
return;
if ($messages) {
$result = ValidationResult::createError($messages, $this->t('The file system is not writable.'));
$event->addValidationResult($result);
}
$result = ValidationResult::createError([$error_message]);
$event->addValidationResult($result);
}
/**
@@ -78,6 +80,7 @@ class WriteableFileSystemValidator implements EventSubscriberInterface {
public static function getSubscribedEvents() {
return [
AutomaticUpdatesEvents::READINESS_CHECK => 'checkPermissions',
AutomaticUpdatesEvents::PRE_START => 'checkPermissions',
];
}
Loading