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 /lib | |
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
Diffstat (limited to 'lib')
-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 |
4 files changed, 98 insertions, 18 deletions
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(); } |