Skip to content

Commit

Permalink
configurable ownlcloud temp directory location
Browse files Browse the repository at this point in the history
changes as requested
  • Loading branch information
mmattel committed May 30, 2015
1 parent a3bc112 commit e1bd94a
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 20 deletions.
10 changes: 10 additions & 0 deletions config/config.sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 1 addition & 14 deletions lib/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
2 changes: 1 addition & 1 deletion lib/private/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
59 changes: 57 additions & 2 deletions lib/private/tempmanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -189,4 +189,59 @@ 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 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, 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 present and writable
* @return bool|string Path to the temporary directory or false
*/
private function getConfigTempDir() {
$temp = \OC::$server->getConfig()->getSystemValue('tempdirectory', false);
// surpress any possible errors caused by is_writable
// checks missing or invalid path or characters, wrong permissions ect
if ($temp) {
try {
if (is_writeable($temp)) {
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;
}
}
}

}
5 changes: 2 additions & 3 deletions tests/lib/tempmanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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() {
Expand Down

0 comments on commit e1bd94a

Please sign in to comment.