From e1bd94a2c353c24a72b7aba766dbbe998ad51be1 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 30 May 2015 14:44:16 +0200 Subject: [PATCH] configurable ownlcloud temp directory location changes as requested --- config/config.sample.php | 10 +++++++ lib/base.php | 15 +--------- lib/private/server.php | 2 +- lib/private/tempmanager.php | 59 +++++++++++++++++++++++++++++++++++-- tests/lib/tempmanager.php | 5 ++-- 5 files changed, 71 insertions(+), 20 deletions(-) diff --git a/config/config.sample.php b/config/config.sample.php index ed86dd941315..247ca662327f 100644 --- a/config/config.sample.php +++ b/config/config.sample.php @@ -85,6 +85,16 @@ */ 'datadirectory' => '/var/www/owncloud/data', +/** + * Where ownCloud stores temporary files. Only needed in conjunction with + * external storage types who do not support piping and high temporary data volume + * is expected. May occur with many concurrent data uploads with big sizes. + * Grant write permissions of the webserver user to the path given. + * If not used, ownCloud takes the system default. + * The path used can be seen in the ownCloud log if loglevel is set to debug. + */ +'tempdirectory' => '/tmp/owncloudtemp', + /** * The current version number of your ownCloud installation. This is set up * during installation and update, so you shouldn't need to change it. diff --git a/lib/base.php b/lib/base.php index b7f19c96406d..e07975bf66f2 100644 --- a/lib/base.php +++ b/lib/base.php @@ -1086,25 +1086,12 @@ protected static function tryFormLogin() { } } -if (!function_exists('get_temp_dir')) { /** * Get the temporary dir to store uploaded data * @return null|string Path to the temporary directory or null */ function get_temp_dir() { - if ($temp = ini_get('upload_tmp_dir')) return $temp; - if ($temp = getenv('TMP')) return $temp; - if ($temp = getenv('TEMP')) return $temp; - if ($temp = getenv('TMPDIR')) return $temp; - $temp = tempnam(__FILE__, ''); - if (file_exists($temp)) { - unlink($temp); - return dirname($temp); - } - if ($temp = sys_get_temp_dir()) return $temp; - - return null; + return \OC::$server->getTempManager()->getTempDir(); } -} OC::init(); diff --git a/lib/private/server.php b/lib/private/server.php index aeea4a6485e9..31f6c373ea0b 100644 --- a/lib/private/server.php +++ b/lib/private/server.php @@ -328,7 +328,7 @@ public function __construct($webRoot) { } }); $this->registerService('TempManager', function (Server $c) { - return new TempManager(get_temp_dir(), $c->getLogger()); + return new TempManager($c->getLogger()); }); $this->registerService('AppManager', function(Server $c) { return new \OC\App\AppManager( diff --git a/lib/private/tempmanager.php b/lib/private/tempmanager.php index eeffc6b339dd..3e7ae412c016 100644 --- a/lib/private/tempmanager.php +++ b/lib/private/tempmanager.php @@ -40,8 +40,8 @@ class TempManager implements ITempManager { * @param string $baseDir * @param \OCP\ILogger $logger */ - public function __construct($baseDir, ILogger $logger) { - $this->tmpBaseDir = $baseDir; + public function __construct(ILogger $logger) { + $this->tmpBaseDir = $this->getTempDir(); $this->log = $logger; } @@ -189,4 +189,59 @@ protected function getOldFiles() { } return $files; } + + /** + * Get the temporary directory to store transfer data + * @return null|string Path to the temporary directory or null + */ + public function getTempDir() { + // Get the temporary directory and log the path if loglevel is set to debug + // Info: based on the temp dir, further directories may be created unique to the instance + $temp = $this->gatherTempDir(); + \OCP\Util::writeLog('Core', 'Temporary directory set to: ' . ($temp ? $temp : 'NULL'), \OCP\Util::DEBUG); + return $temp; + } + + /** + * Get a temporary directory from possible sources + * If a temporary directory is set in config.php, use this one + * @return null|string Path to the temporary directory or null + */ + private function gatherTempDir() { + if ($temp = $this->getConfigTempDir()) return $temp; + if ($temp = ini_get('upload_tmp_dir')) return $temp; + if ($temp = getenv('TMP')) return $temp; + if ($temp = getenv('TEMP')) return $temp; + if ($temp = getenv('TMPDIR')) return $temp; + $temp = tempnam(__FILE__, ''); + if (file_exists($temp)) { + unlink($temp); + return dirname($temp); + } + if ($temp = sys_get_temp_dir()) return $temp; + return null; + } + + /** + * Check if the temporary directory is defined in config.php and is present and writable + * @return bool|string Path to the temporary directory or false + */ + private function getConfigTempDir() { + $temp = \OC::$server->getConfig()->getSystemValue('tempdirectory', false); + // surpress any possible errors caused by is_writable + // checks missing or invalid path or characters, wrong permissions ect + if ($temp) { + try { + if (is_writeable($temp)) { + return $temp; + } else { + \OCP\Util::writeLog('Core', 'Manually set temporary directory in config.php is not present or writable: ' . $temp, \OCP\Util::WARN); + return false; + } + } catch (Exception $e) { + return false; + } + } + } + } diff --git a/tests/lib/tempmanager.php b/tests/lib/tempmanager.php index 72741d0dec69..3766868b585e 100644 --- a/tests/lib/tempmanager.php +++ b/tests/lib/tempmanager.php @@ -22,12 +22,11 @@ public function log($level, $message, array $context = array()) { } class TempManager extends \Test\TestCase { - protected $baseDir; protected function setUp() { parent::setUp(); - $this->baseDir = get_temp_dir() . $this->getUniqueID('/oc_tmp_test'); + $this->baseDir = $this->getManager()->getTempDir() . $this->getUniqueID('/oc_tmp_test'); if (!is_dir($this->baseDir)) { mkdir($this->baseDir); } @@ -46,7 +45,7 @@ protected function getManager($logger = null) { if (!$logger) { $logger = new NullLogger(); } - return new \OC\TempManager($this->baseDir, $logger); + return new \OC\TempManager($logger); } public function testGetFile() {