Skip to content
Snippets Groups Projects

Issue #3231996: Ensure file system is writeable

Compare and Show latest version
3 files
+ 22
76
Compare changes
  • Side-by-side
  • Inline
Files
3
@@ -6,15 +6,9 @@ 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;
@@ -27,74 +21,44 @@ class WritableFileSystemValidator implements EventSubscriberInterface {
protected $pathLocator;
/**
* The file system service.
*
* @var \Drupal\Core\File\FileSystemInterface
*/
protected $fileSystem;
/**
* Constructs a WriteableFileSystemValidator object.
* Constructs a WritableFileSystemValidator 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) {
public function __construct(PathLocator $path_locator) {
$this->pathLocator = $path_locator;
$this->fileSystem = $file_system;
}
/**
* Validates that the file system is writable.
* Checks 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);
$messages = [];
$dir = $this->pathLocator->getVendorDirectory();
$message = $this->t('The vendor directory "@dir" is not writable.', [
'@dir' => $dir,
]);
$this->checkPath($dir, 'composer/LICENSE', $event, $message);
}
$dir = $this->pathLocator->getProjectRoot();
if (!is_writable($dir)) {
$messages[] = $this->t('The project directory "@dir" is not writable.', ['@dir' => $dir]);
}
/**
* 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);
$web_root = $this->pathLocator->getWebRoot();
if ($web_root) {
$dir .= DIRECTORY_SEPARATOR . $web_root;
if (!is_writable($dir)) {
$messages[] = $this->t('The Drupal directory "@dir" is not writable.', ['@dir' => $dir]);
}
}
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);
$dir = $this->pathLocator->getVendorDirectory();
if (!is_writable($dir)) {
$messages[] = $this->t('The vendor directory "@dir" is not writable.', ['@dir' => $dir]);
}
catch (FileException $e) {
$message = $e->getMessage();
$result = ValidationResult::createError([$message], $summary);
if ($messages) {
$result = ValidationResult::createError($messages, $this->t('The file system is not writable.'));
$event->addValidationResult($result);
}
}
Loading