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..dc2f6fc1f15b 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,57 @@ 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 sub-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, try to 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 accessible and writable + * @return null|string Path to the temporary directory or null + */ + private function getConfigTempDir() { + $temp = \OC::$server->getConfig()->getSystemValue('tempdirectory', false); + // check on presence if the config.php parameter is existant and writable + if ($temp) { + if (is_writeable($temp)) { + // the config.php parameter is present and writable + return $temp; + } else { + // the config.php parameter is present, but is either not accesible or writable + \OCP\Util::writeLog('Core', 'Manually set temporary directory in config.php is not present or writable: ' . $temp, \OCP\Util::WARN); + return null; + } + } + return null; + } + } 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() {