Clean up TempManager to follow code guidelines

tmpBaseDir can be overridden for unit testing purposes
This commit is contained in:
Robin McCorkell 2015-08-29 17:36:21 +01:00
parent acae208f2f
commit a0dfaf9be3
4 changed files with 91 additions and 43 deletions

View File

@ -344,7 +344,10 @@ class Server extends SimpleContainer implements IServerContainer {
} }
}); });
$this->registerService('TempManager', function (Server $c) { $this->registerService('TempManager', function (Server $c) {
return new TempManager($c->getLogger()); return new TempManager(
$c->getLogger(),
$c->getConfig()
);
}); });
$this->registerService('AppManager', function(Server $c) { $this->registerService('AppManager', function(Server $c) {
return new \OC\App\AppManager( return new \OC\App\AppManager(

View File

@ -25,6 +25,7 @@
namespace OC; namespace OC;
use OCP\ILogger; use OCP\ILogger;
use OCP\IConfig;
use OCP\ITempManager; use OCP\ITempManager;
class TempManager implements ITempManager { class TempManager implements ITempManager {
@ -34,15 +35,20 @@ class TempManager implements ITempManager {
protected $tmpBaseDir; protected $tmpBaseDir;
/** @var ILogger */ /** @var ILogger */
protected $log; protected $log;
/** @var IConfig */
protected $config;
/** Prefix */ /** Prefix */
const TMP_PREFIX = 'oc_tmp_'; const TMP_PREFIX = 'oc_tmp_';
/** /**
* @param \OCP\ILogger $logger * @param \OCP\ILogger $logger
* @param \OCP\IConfig $config
*/ */
public function __construct(ILogger $logger) { public function __construct(ILogger $logger, IConfig $config) {
$this->tmpBaseDir = $this->t_get_temp_dir();
$this->log = $logger; $this->log = $logger;
$this->config = $config;
$this->tmpBaseDir = $this->getTempBaseDir();
} }
/** /**
@ -191,57 +197,77 @@ class TempManager implements ITempManager {
} }
/** /**
* Get the temporary directory to store transfer data * Get the temporary base directory configured on the server
* @return null|string Path to the temporary directory or null *
* @return string Path to the temporary directory or null
* @throws \UnexpectedValueException
*/ */
public function t_get_temp_dir() { public function getTempBaseDir() {
// Get the temporary directory and log the path if loglevel is set to debug if ($this->tmpBaseDir) {
// Info: based on the temp dir, further directories may be created unique to the instance return $this->tmpBaseDir;
$temp = self::gather_temp_dir(); }
\OCP\Util::writeLog('Core', 'Temporary directory set to: ' . ($temp ? $temp : 'NULL'), \OCP\Util::DEBUG);
return $temp;
}
/** $directories = [];
* Get a temporary directory from possible sources if ($temp = $this->config->getSystemValue('tempdirectory', null)) {
* If a temporary directory is set in config.php, use this one $directories[] = $temp;
* @return null|string Path to the temporary directory or null }
*/ if ($temp = ini_get('upload_tmp_dir')) {
private function gather_temp_dir() { $directories[] = $temp;
if ($temp = self::get_config_temp_dir()) return $temp; }
if ($temp = ini_get('upload_tmp_dir')) return $temp; if ($temp = getenv('TMP')) {
if ($temp = getenv('TMP')) return $temp; $directories[] = $temp;
if ($temp = getenv('TEMP')) return $temp; }
if ($temp = getenv('TMPDIR')) return $temp; if ($temp = getenv('TEMP')) {
$directories[] = $temp;
}
if ($temp = getenv('TMPDIR')) {
$directories[] = $temp;
}
$temp = tempnam(__FILE__, ''); $temp = tempnam(__FILE__, '');
if (file_exists($temp)) { if (file_exists($temp)) {
unlink($temp); unlink($temp);
return dirname($temp); $directories[] = dirname($temp);
} }
if ($temp = sys_get_temp_dir()) return $temp; if ($temp = sys_get_temp_dir()) {
return null; $directories[] = $temp;
}
foreach ($directories as $dir) {
if ($this->checkTemporaryDirectory($dir)) {
return $dir;
}
}
throw new \UnexpectedValueException('Unable to detect system temporary directory');
} }
/** /**
* Check if the temporary directory is defined in config.php and is present and writable * Check if a temporary directory is ready for use
* @return bool|string Path to the temporary directory or false *
* @param mixed $directory
* @return bool
*/ */
private function get_config_temp_dir() { private function checkTemporaryDirectory($directory) {
$temp = \OC::$server->getConfig()->getSystemValue('tempdirectory', false);
// surpress any possible errors caused by is_writable // surpress any possible errors caused by is_writable
// checks missing or invalid path or characters, wrong permissions ect // checks missing or invalid path or characters, wrong permissions ect
if ($temp) { try {
try { if (is_writeable($directory)) {
if (is_writeable($temp)) { return true;
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;
} }
} 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;
} }
} }

View File

@ -58,4 +58,12 @@ interface ITempManager {
* @since 8.0.0 * @since 8.0.0
*/ */
public function cleanOld(); public function cleanOld();
/**
* Get the temporary base directory
*
* @return string
* @since 8.2.0
*/
public function getTempBaseDir();
} }

View File

@ -23,10 +23,12 @@ class NullLogger extends Log {
class TempManager extends \Test\TestCase { class TempManager extends \Test\TestCase {
protected $baseDir = null;
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
$this->baseDir = $this->getManager()->t_get_temp_dir() . $this->getUniqueID('/oc_tmp_test'); $this->baseDir = $this->getManager()->getTempBaseDir() . $this->getUniqueID('/oc_tmp_test');
if (!is_dir($this->baseDir)) { if (!is_dir($this->baseDir)) {
mkdir($this->baseDir); mkdir($this->baseDir);
} }
@ -34,18 +36,27 @@ class TempManager extends \Test\TestCase {
protected function tearDown() { protected function tearDown() {
\OC_Helper::rmdirr($this->baseDir); \OC_Helper::rmdirr($this->baseDir);
$this->baseDir = null;
parent::tearDown(); parent::tearDown();
} }
/** /**
* @param \OCP\ILogger $logger * @param \OCP\ILogger $logger
* @param \OCP\IConfig $config
* @return \OC\TempManager * @return \OC\TempManager
*/ */
protected function getManager($logger = null) { protected function getManager($logger = null, $config = null) {
if (!$logger) { if (!$logger) {
$logger = new NullLogger(); $logger = new NullLogger();
} }
return new \OC\TempManager($logger); if (!$config) {
$config = \OC::$server->getConfig();
}
$manager = new \OC\TempManager($logger, $config);
if ($this->baseDir) {
$manager->overrideTempBaseDir($this->baseDir);
}
return $manager;
} }
public function testGetFile() { public function testGetFile() {