function Updater::prepareInstallDirectory

Same name in other branches
  1. 7.x includes/updater.inc \Updater::prepareInstallDirectory()
  2. 9 core/lib/Drupal/Core/Updater/Updater.php \Drupal\Core\Updater\Updater::prepareInstallDirectory()
  3. 8.9.x core/lib/Drupal/Core/Updater/Updater.php \Drupal\Core\Updater\Updater::prepareInstallDirectory()
  4. 11.x core/lib/Drupal/Core/Updater/Updater.php \Drupal\Core\Updater\Updater::prepareInstallDirectory()

Makes sure the installation parent directory exists and is writable.

Parameters

\Drupal\Core\FileTransfer\FileTransfer $filetransfer: Object which is a child of FileTransfer.

string $directory: The installation directory to prepare.

Throws

\Drupal\Core\Updater\UpdaterException

2 calls to Updater::prepareInstallDirectory()
Updater::install in core/lib/Drupal/Core/Updater/Updater.php
Installs a Drupal project, returns a list of next actions.
Updater::update in core/lib/Drupal/Core/Updater/Updater.php
Updates a Drupal project and returns a list of next actions.

File

core/lib/Drupal/Core/Updater/Updater.php, line 327

Class

Updater
Defines the base class for Updaters used in Drupal.

Namespace

Drupal\Core\Updater

Code

public function prepareInstallDirectory(&$filetransfer, $directory) {
    // Make the parent dir writable if need be and create the dir.
    if (!is_dir($directory)) {
        $parent_dir = dirname($directory);
        if (!is_writable($parent_dir)) {
            @chmod($parent_dir, 0755);
            // It is expected that this will fail if the directory is owned by the
            // FTP user. If the FTP user == web server, it will succeed.
            try {
                $filetransfer->createDirectory($directory);
                $this->makeWorldReadable($filetransfer, $directory);
            } catch (FileTransferException $e) {
                // Probably still not writable. Try to chmod and do it again.
                // @todo Make a new exception class so we can catch it differently.
                try {
                    $old_perms = fileperms($parent_dir) & 0777;
                    $filetransfer->chmod($parent_dir, 0755);
                    $filetransfer->createDirectory($directory);
                    $this->makeWorldReadable($filetransfer, $directory);
                    // Put the permissions back.
                    $filetransfer->chmod($parent_dir, $old_perms);
                } catch (FileTransferException $e) {
                    $message = t($e->getMessage(), $e->arguments);
                    $throw_message = t('Unable to create %directory due to the following: %reason', [
                        '%directory' => $directory,
                        '%reason' => $message,
                    ]);
                    throw new UpdaterException($throw_message);
                }
            }
            // Put the parent directory back.
            @chmod($parent_dir, 0555);
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.