Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable temporary directory #18658

Merged
merged 4 commits into from
Sep 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions config/config.sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@
*/
'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.
Expand Down
17 changes: 3 additions & 14 deletions lib/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -1108,27 +1108,16 @@ protected static function tryFormLogin() {
}
return true;
}
}

if (!function_exists('get_temp_dir')) {
/**
* Get the temporary dir to store uploaded data
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? I guess this can cause problems or wasn't there a reason to inclide this check?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah ... you added it as an \OC method :)

* @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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

t_get_temp_dir ?????? not know

bildschirmfoto von 2015-09-14 18-07-42

}

}


OC::init();
5 changes: 4 additions & 1 deletion lib/private/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,10 @@ public function __construct($webRoot) {
}
});
$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(
Expand Down
86 changes: 83 additions & 3 deletions lib/private/tempmanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
namespace OC;

use OCP\ILogger;
use OCP\IConfig;
use OCP\ITempManager;

class TempManager implements ITempManager {
Expand All @@ -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();
}

/**
Expand Down Expand Up @@ -190,4 +195,79 @@ protected function getOldFiles() {
}
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;
}

}
8 changes: 8 additions & 0 deletions lib/public/itempmanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,12 @@ public function clean();
* @since 8.0.0
*/
public function cleanOld();

/**
* Get the temporary base directory
*
* @return string
* @since 8.2.0
*/
public function getTempBaseDir();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@icewind1991 Isn't there already a public method to get a temp directory? What is the difference here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FFrom what I can tell this is the base folder we create the other folders in, I dont know if we need to expose this as public

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the public API since the original get_temp_dir() was accessible.

}
36 changes: 32 additions & 4 deletions tests/lib/tempmanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,44 @@ public function log($level, $message, array $context = array()) {
}

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

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() {
Expand Down Expand Up @@ -185,4 +198,19 @@ public function testBuildFileNameWithSuffixPathTraversal() {
$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());
}
}