diff options
author | Frank Karlitschek <karlitschek@gmx.de> | 2015-09-12 22:04:41 +0200 |
---|---|---|
committer | Frank Karlitschek <karlitschek@gmx.de> | 2015-09-12 22:04:41 +0200 |
commit | 7562e4959b4726939d78720d26fdbe713c8f5c53 (patch) | |
tree | 999024c93f28183409283d51018a3a79b43efa43 | |
parent | 9860a79441b8e3595450e53b6209f314c7537775 (diff) | |
parent | c3a327dc385c617bc2daf0b5a51aacfaea2abde1 (diff) | |
download | nextcloud-server-7562e4959b4726939d78720d26fdbe713c8f5c53.tar.gz nextcloud-server-7562e4959b4726939d78720d26fdbe713c8f5c53.zip |
Merge pull request #18658 from owncloud/configurable-temp
Configurable temporary directory
-rw-r--r-- | config/config.sample.php | 10 | ||||
-rw-r--r-- | lib/base.php | 17 | ||||
-rw-r--r-- | lib/private/server.php | 5 | ||||
-rw-r--r-- | lib/private/tempmanager.php | 86 | ||||
-rw-r--r-- | lib/public/itempmanager.php | 8 | ||||
-rw-r--r-- | tests/lib/tempmanager.php | 36 |
6 files changed, 140 insertions, 22 deletions
diff --git a/config/config.sample.php b/config/config.sample.php index f5e36e66cc4..a841831f018 100644 --- a/config/config.sample.php +++ b/config/config.sample.php @@ -80,6 +80,16 @@ $CONFIG = array( 'datadirectory' => '/var/www/owncloud/data', /** + * Override where ownCloud stores temporary files. Useful in situations where + * the system temporary directory is on a limited space ramdisk or is otherwise + * restricted, or if external storages which do not support streaming are in + * use. + * + * The web server user must have write access to this directory. + */ +'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 a4b5e9e01bf..5f8a7ed0f19 100644 --- a/lib/base.php +++ b/lib/base.php @@ -1115,27 +1115,16 @@ class OC { } return true; } -} -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()->t_get_temp_dir(); } + } + OC::init(); diff --git a/lib/private/server.php b/lib/private/server.php index 59d03c2df97..9657afbdaec 100644 --- a/lib/private/server.php +++ b/lib/private/server.php @@ -356,7 +356,10 @@ class Server extends SimpleContainer implements IServerContainer { } }); $this->registerService('TempManager', function (Server $c) { - return new TempManager(get_temp_dir(), $c->getLogger()); + return new TempManager( + $c->getLogger(), + $c->getConfig() + ); }); $this->registerService('AppManager', function(Server $c) { return new \OC\App\AppManager( diff --git a/lib/private/tempmanager.php b/lib/private/tempmanager.php index b8cef8e036e..cc7c51d8568 100644 --- a/lib/private/tempmanager.php +++ b/lib/private/tempmanager.php @@ -25,6 +25,7 @@ namespace OC; use OCP\ILogger; +use OCP\IConfig; use OCP\ITempManager; class TempManager implements ITempManager { @@ -34,16 +35,20 @@ class TempManager implements ITempManager { protected $tmpBaseDir; /** @var ILogger */ protected $log; + /** @var IConfig */ + protected $config; + /** Prefix */ const TMP_PREFIX = 'oc_tmp_'; /** - * @param string $baseDir * @param \OCP\ILogger $logger + * @param \OCP\IConfig $config */ - public function __construct($baseDir, ILogger $logger) { - $this->tmpBaseDir = $baseDir; + public function __construct(ILogger $logger, IConfig $config) { $this->log = $logger; + $this->config = $config; + $this->tmpBaseDir = $this->getTempBaseDir(); } /** @@ -190,4 +195,79 @@ class TempManager implements ITempManager { } return $files; } + + /** + * Get the temporary base directory configured on the server + * + * @return string Path to the temporary directory or null + * @throws \UnexpectedValueException + */ + public function getTempBaseDir() { + if ($this->tmpBaseDir) { + return $this->tmpBaseDir; + } + + $directories = []; + if ($temp = $this->config->getSystemValue('tempdirectory', null)) { + $directories[] = $temp; + } + if ($temp = ini_get('upload_tmp_dir')) { + $directories[] = $temp; + } + if ($temp = getenv('TMP')) { + $directories[] = $temp; + } + if ($temp = getenv('TEMP')) { + $directories[] = $temp; + } + if ($temp = getenv('TMPDIR')) { + $directories[] = $temp; + } + $temp = tempnam(__FILE__, ''); + if (file_exists($temp)) { + unlink($temp); + $directories[] = dirname($temp); + } + if ($temp = sys_get_temp_dir()) { + $directories[] = $temp; + } + + foreach ($directories as $dir) { + if ($this->checkTemporaryDirectory($dir)) { + return $dir; + } + } + throw new \UnexpectedValueException('Unable to detect system temporary directory'); + } + + /** + * Check if a temporary directory is ready for use + * + * @param mixed $directory + * @return bool + */ + private function checkTemporaryDirectory($directory) { + // surpress any possible errors caused by is_writable + // checks missing or invalid path or characters, wrong permissions ect + try { + if (is_writeable($directory)) { + return true; + } + } catch (Exception $e) { + } + $this->log->warning('Temporary directory {dir} is not present or writable', + ['dir' => $directory] + ); + return false; + } + + /** + * Override the temporary base directory + * + * @param string $directory + */ + public function overrideTempBaseDir($directory) { + $this->tmpBaseDir = $directory; + } + } diff --git a/lib/public/itempmanager.php b/lib/public/itempmanager.php index 7ba5b1e7bff..6364fa71913 100644 --- a/lib/public/itempmanager.php +++ b/lib/public/itempmanager.php @@ -58,4 +58,12 @@ interface ITempManager { * @since 8.0.0 */ public function cleanOld(); + + /** + * Get the temporary base directory + * + * @return string + * @since 8.2.0 + */ + public function getTempBaseDir(); } diff --git a/tests/lib/tempmanager.php b/tests/lib/tempmanager.php index 0fa025d44c8..8faf804a203 100644 --- a/tests/lib/tempmanager.php +++ b/tests/lib/tempmanager.php @@ -22,12 +22,13 @@ class NullLogger extends Log { } class TempManager extends \Test\TestCase { - protected $baseDir; + + protected $baseDir = null; protected function setUp() { parent::setUp(); - $this->baseDir = get_temp_dir() . $this->getUniqueID('/oc_tmp_test'); + $this->baseDir = $this->getManager()->getTempBaseDir() . $this->getUniqueID('/oc_tmp_test'); if (!is_dir($this->baseDir)) { mkdir($this->baseDir); } @@ -35,18 +36,30 @@ class TempManager extends \Test\TestCase { protected function tearDown() { \OC_Helper::rmdirr($this->baseDir); + $this->baseDir = null; parent::tearDown(); } /** * @param \OCP\ILogger $logger + * @param \OCP\IConfig $config * @return \OC\TempManager */ - protected function getManager($logger = null) { + protected function getManager($logger = null, $config = null) { if (!$logger) { $logger = new NullLogger(); } - return new \OC\TempManager($this->baseDir, $logger); + if (!$config) { + $config = $this->getMock('\OCP\IConfig'); + $config->method('getSystemValue') + ->with('tempdirectory', null) + ->willReturn('/tmp'); + } + $manager = new \OC\TempManager($logger, $config); + if ($this->baseDir) { + $manager->overrideTempBaseDir($this->baseDir); + } + return $manager; } public function testGetFile() { @@ -185,4 +198,19 @@ class TempManager extends \Test\TestCase { $this->assertStringEndsNotWith('./Traversal\\../FileName', $tmpManager); $this->assertStringEndsWith('.Traversal..FileName', $tmpManager); } + + public function testGetTempBaseDirFromConfig() { + $dir = $this->getManager()->getTemporaryFolder(); + + $config = $this->getMock('\OCP\IConfig'); + $config->expects($this->once()) + ->method('getSystemValue') + ->with('tempdirectory', null) + ->willReturn($dir); + + $this->baseDir = null; // prevent override + $tmpManager = $this->getManager(null, $config); + + $this->assertEquals($dir, $tmpManager->getTempBaseDir()); + } } |