Skip to content

Commit

Permalink
configurable ownlcloud temp directory location
Browse files Browse the repository at this point in the history
changes as requested

return false -> return null

updates
  • Loading branch information
mmattel committed May 31, 2015
1 parent a3bc112 commit 652d9d5
Show file tree
Hide file tree
Showing 5 changed files with 69 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
57 changes: 55 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,57 @@ 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 sub-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, try to 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 accessible and writable
* @return null|string Path to the temporary directory or null
*/
private function getConfigTempDir() {
$temp = \OC::$server->getConfig()->getSystemValue('tempdirectory', false);
// check on presence if the config.php parameter is existant and writable
if ($temp) {
if (is_writeable($temp)) {
// the config.php parameter is present and writable
return $temp;
} else {
// the config.php parameter is present, but is either not accesible or writable
\OCP\Util::writeLog('Core', 'Manually set temporary directory in config.php is not present or writable: ' . $temp, \OCP\Util::WARN);
return null;
}
}
return null;
}

}
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 652d9d5

Please sign in to comment.