Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move canonical storage of tmp dir to config()->get(env.tmp) #3137

Merged
merged 3 commits into from
Nov 3, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion includes/environment.inc
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ function drush_directory_cache($subdir = '') {
if ($home) {
$cache_locations[$home] = '.drush/cache';
}
$cache_locations[drush_find_tmp()] = 'drush-' . Drush::config()->get('env.user') . '/cache';
$cache_locations[Drush::config()->get('env.tmp')] = 'drush-' . Drush::config()->get('env.user') . '/cache';
foreach ($cache_locations as $base => $dir) {
if (!empty($base) && is_writable($base)) {
$cache_dir = $base . '/' . $dir;
Expand Down
59 changes: 4 additions & 55 deletions includes/filesystem.inc
Original file line number Diff line number Diff line change
Expand Up @@ -397,61 +397,10 @@ function drush_save_data_to_temp_file($data, $suffix = NULL) {
/**
* Returns the path to a temporary directory.
*
* This is a custom version of Drupal's file_directory_path().
* We can't directly rely on sys_get_temp_dir() as this
* path is not valid in some setups for Mac, and we want to honor
* an environment variable (used by tests).
* @deprecated Use $this->getConfig()->get('env.tmp').
*/
function drush_find_tmp() {
static $temporary_directory;

if (!isset($temporary_directory)) {
$directories = array();

// Get user specific and operating system temp folders from system environment variables.
// See http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds_shelloverview.mspx?mfr=true
$tempdir = getenv('TEMP');
if (!empty($tempdir)) {
$directories[] = $tempdir;
}
$tmpdir = getenv('TMP');
if (!empty($tmpdir)) {
$directories[] = $tmpdir;
}
// Operating system specific dirs.
if (drush_is_windows()) {
$windir = getenv('WINDIR');
if (isset($windir)) {
// WINDIR itself is not writable, but it always contains a /Temp dir,
// which is the system-wide temporary directory on older versions. Newer
// versions only allow system processes to use it.
$directories[] = Path::join($windir, 'Temp');
}
}
else {
$directories[] = Path::canonicalize('/tmp');
}
$directories[] = Path::canonicalize(sys_get_temp_dir());

foreach ($directories as $directory) {
if (is_dir($directory) && is_writable($directory)) {
$temporary_directory = $directory;
break;
}
}

if (empty($temporary_directory)) {
// If no directory has been found, create one in cwd.
$temporary_directory = Path::join(Drush::config()->get('env.cwd'), 'tmp');
drush_mkdir($temporary_directory, TRUE);
if (!is_dir($temporary_directory)) {
return drush_set_error('DRUSH_UNABLE_TO_CREATE_TMP_DIR', dt("Unable to create a temporary directory."));
}
drush_register_file_for_deletion($temporary_directory);
}
}

return $temporary_directory;
return Drush::config()->get('env.tmp');
}

/**
Expand All @@ -467,7 +416,7 @@ function drush_find_tmp() {
*/
function drush_tempnam($pattern, $tmp_dir = NULL, $suffix = '') {
if ($tmp_dir == NULL) {
$tmp_dir = drush_find_tmp();
$tmp_dir = Drush::config()->get('env.tmp');
}
$tmp_file = tempnam($tmp_dir, $pattern);
drush_register_file_for_deletion($tmp_file);
Expand All @@ -480,7 +429,7 @@ function drush_tempnam($pattern, $tmp_dir = NULL, $suffix = '') {
* Creates a temporary directory and return its path.
*/
function drush_tempdir() {
$tmp_dir = drush_trim_path(drush_find_tmp());
$tmp_dir = drush_trim_path(Drush::config()->get('env.tmp'));
$tmp_dir .= '/' . 'drush_tmp_' . uniqid(time() . '_');

drush_mkdir($tmp_dir);
Expand Down
49 changes: 49 additions & 0 deletions src/Config/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,54 @@ protected function getUsername()
return $name;
}

protected function getTmp()
{
$directories = array();

// Get user specific and operating system temp folders from system environment variables.
// See http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds_shelloverview.mspx?mfr=true
$tempdir = getenv('TEMP');
if (!empty($tempdir)) {
$directories[] = $tempdir;
}
$tmpdir = getenv('TMP');
if (!empty($tmpdir)) {
$directories[] = $tmpdir;
}
// Operating system specific dirs.
if (self::isWindows()) {
$windir = getenv('WINDIR');
if (isset($windir)) {
// WINDIR itself is not writable, but it always contains a /Temp dir,
// which is the system-wide temporary directory on older versions. Newer
// versions only allow system processes to use it.
$directories[] = Path::join($windir, 'Temp');
}
} else {
$directories[] = Path::canonicalize('/tmp');
}
$directories[] = Path::canonicalize(sys_get_temp_dir());

foreach ($directories as $directory) {
if (is_dir($directory) && is_writable($directory)) {
$temporary_directory = $directory;
break;
}
}

if (empty($temporary_directory)) {
// If no directory has been found, create one in cwd.
$temporary_directory = Path::join(Drush::config()->get('env.cwd'), 'tmp');
drush_mkdir($temporary_directory, true);
if (!is_dir($temporary_directory)) {
throw new \Exception(dt("Unable to create a temporary directory."));
}
// Function not available yet - this is not likely to get reached anyway.
// drush_register_file_for_deletion($temporary_directory);
}
return $temporary_directory;
}

/**
* Convert the environment object into an exported configuration
* array. This will be fed though the EnvironmentConfigLoader to
Expand All @@ -111,6 +159,7 @@ public function exportConfigData()
'home' => $this->homeDir(),
'user' => $this->getUsername(),
'is-windows' => $this->isWindows(),
'tmp' => $this->getTmp(),
],
// These values are available as global options, and
// will be passed in to the FormatterOptions et. al.
Expand Down
7 changes: 5 additions & 2 deletions src/Sql/SqlBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
use Drupal\Core\Database\Database;
use Drush\Drush;
use Drush\Log\LogLevel;
use Robo\Common\ConfigAwareTrait;
use Robo\Contract\ConfigAwareInterface;
use Webmozart\PathUtil\Path;

class SqlBase
class SqlBase implements ConfigAwareInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SqlBase is neither a commandfile, nor is it created from the DI container. It will therefore be necessary to manually inject the config manager in any code that creates an instance of a class derived from SqlBase.

{

use SqlTableSelectionTrait;
use ConfigAwareTrait;

// An Drupal style array containing specs for connecting to database.
public $dbSpec;
Expand Down Expand Up @@ -185,7 +188,7 @@ public function dumpFile($file)
if ($file === true) {
$backup_dir = drush_prepare_backup_dir($database);
if (empty($backup_dir)) {
$backup_dir = drush_find_tmp();
$backup_dir = $this->getConfig()->get('env.tmp');
}
$file = Path::join($backup_dir, '@[email protected]');
}
Expand Down