Skip to content
Snippets Groups Projects

Issue #3231996: Ensure file system is writeable

1 unresolved thread
Compare and Show latest version
2 files
+ 41
14
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -6,6 +6,8 @@ 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;
@@ -24,14 +26,24 @@ class WritableFileSystemValidator implements EventSubscriberInterface {
*/
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) {
public function __construct(PathLocator $path_locator, FileSystemInterface $file_system) {
$this->pathLocator = $path_locator;
$this->fileSystem = $file_system;
}
/**
@@ -41,35 +53,49 @@ class WritableFileSystemValidator implements EventSubscriberInterface {
* The event object.
*/
public function checkPermissions(UpdateEvent $event): void {
$dir = $this->pathLocator->getProjectRoot();
$message = $this->t('The project directory "@dir" is not writable.', [
$dir = $this->pathLocator->getWebRoot();
$message = $this->t('The Drupal directory "@dir" is not writable.', [
'@dir' => $dir,
]);
$this->checkPath($dir, $event, $message);
$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, $event, $message);
$this->checkPath($dir, 'composer/LICENSE', $event, $message);
}
/**
* Checks that a path is writable, and flags an error if it's not.
* Tests that a path is writable by writing and deleting a temporary file.
*
* @param string $path
* The path to check.
* @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 $error_message
* The translated error message if the path is not writeable.
* The error message to flag if the path is not writable.
*/
protected function checkPath(string $path, UpdateEvent $event, TranslatableMarkup $error_message): void {
if (is_writable($path)) {
return;
protected function checkPath(string $path_to_check, string $file_to_copy, UpdateEvent $event, TranslatableMarkup $error_message): 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) {
$result = ValidationResult::createError([
$error_message,
$e->getMessage(),
]);
$event->addValidationResult($result);
}
$result = ValidationResult::createError([$error_message]);
$event->addValidationResult($result);
}
/**
Loading