Merge pull request #18658 from owncloud/configurable-temp

Configurable temporary directory
This commit is contained in:
Frank Karlitschek 2015-09-12 22:04:41 +02:00
commit 7562e4959b
6 changed files with 140 additions and 22 deletions

View File

@ -79,6 +79,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.

View File

@ -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();

View File

@ -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(

View File

@ -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;
}
}

View File

@ -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();
}

View File

@ -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());
}
}