config: add a switch to control truncate before update

To avoid extra truncate on non WORM file systems, add a new config
option `localstorage.unlink_on_truncate`, which defaults to false.

The OC\Files\Storage\Local is update to respect that option.

Signed-off-by: Tigran Mkrtchyan <tigran.mkrtchyan@desy.de>
This commit is contained in:
Tigran Mkrtchyan 2022-03-31 10:38:25 +02:00
parent 8fc4cf67f1
commit f41209a061
2 changed files with 20 additions and 6 deletions

View File

@ -1887,6 +1887,14 @@ $CONFIG = [
*/ */
'localstorage.umask' => 0022, 'localstorage.umask' => 0022,
/**
* This options allows storage systems that don't allow to modify existing files
* to overcome this limitation by removing the files before overwriting.
*
* Defaults to ``false``
*/
'localstorage.unlink_on_truncate' => false,
/** /**
* EXPERIMENTAL: option whether to include external storage in quota * EXPERIMENTAL: option whether to include external storage in quota
* calculation, defaults to false. * calculation, defaults to false.

View File

@ -69,6 +69,8 @@ class Local extends \OC\Files\Storage\Common {
private $defUMask; private $defUMask;
protected bool $unlinkOnTruncate;
public function __construct($arguments) { public function __construct($arguments) {
if (!isset($arguments['datadir']) || !is_string($arguments['datadir'])) { if (!isset($arguments['datadir']) || !is_string($arguments['datadir'])) {
throw new \InvalidArgumentException('No data directory set for local storage'); throw new \InvalidArgumentException('No data directory set for local storage');
@ -88,6 +90,9 @@ class Local extends \OC\Files\Storage\Common {
$this->config = \OC::$server->get(IConfig::class); $this->config = \OC::$server->get(IConfig::class);
$this->mimeTypeDetector = \OC::$server->get(IMimeTypeDetector::class); $this->mimeTypeDetector = \OC::$server->get(IMimeTypeDetector::class);
$this->defUMask = $this->config->getSystemValue('localstorage.umask', 0022); $this->defUMask = $this->config->getSystemValue('localstorage.umask', 0022);
// support Write-Once-Read-Many file systems
$this->unlinkOnTruncate = $this->config->getSystemValue('localstorage.unlink_on_truncate', false);
} }
public function __destruct() { public function __destruct() {
@ -297,8 +302,9 @@ class Local extends \OC\Files\Storage\Common {
public function file_put_contents($path, $data) { public function file_put_contents($path, $data) {
$oldMask = umask($this->defUMask); $oldMask = umask($this->defUMask);
// support Write-Once-Read-Many filesystems if ($this->unlinkOnTruncate) {
$this->unlink($path); $this->unlink($path);
}
$result = file_put_contents($this->getSourcePath($path), $data); $result = file_put_contents($this->getSourcePath($path), $data);
umask($oldMask); umask($oldMask);
return $result; return $result;
@ -372,8 +378,9 @@ class Local extends \OC\Files\Storage\Common {
return parent::copy($path1, $path2); return parent::copy($path1, $path2);
} else { } else {
$oldMask = umask($this->defUMask); $oldMask = umask($this->defUMask);
// support Write-Once-Read-Many filesystems if ($this->unlinkOnTruncate) {
$this->unlink($path2); $this->unlink($path2);
}
$result = copy($this->getSourcePath($path1), $this->getSourcePath($path2)); $result = copy($this->getSourcePath($path1), $this->getSourcePath($path2));
umask($oldMask); umask($oldMask);
return $result; return $result;
@ -382,8 +389,7 @@ class Local extends \OC\Files\Storage\Common {
public function fopen($path, $mode) { public function fopen($path, $mode) {
$oldMask = umask($this->defUMask); $oldMask = umask($this->defUMask);
if ($mode === 'w') { if (($mode === 'w' || $mode === 'w+') && $this->unlinkOnTruncate) {
// support Write-Once-Read-Many filesystems
$this->unlink($path); $this->unlink($path);
} }
$result = fopen($this->getSourcePath($path), $mode); $result = fopen($this->getSourcePath($path), $mode);