Skip to content

Commit b77e205

Browse files
committed
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>
1 parent 8fc4cf6 commit b77e205

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

config/config.sample.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1887,6 +1887,14 @@
18871887
*/
18881888
'localstorage.umask' => 0022,
18891889

1890+
/**
1891+
* This options allows storage systems that don't allow to modify existing files
1892+
* to overcome this limitation by removing the files before overwriting.
1893+
*
1894+
* Defaults to ``false``
1895+
*/
1896+
'localstorage.unlink_on_truncate' => false,
1897+
18901898
/**
18911899
* EXPERIMENTAL: option whether to include external storage in quota
18921900
* calculation, defaults to false.

lib/private/Files/Storage/Local.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ class Local extends \OC\Files\Storage\Common {
6969

7070
private $defUMask;
7171

72+
protected $unlinkOnTruncate;
73+
7274
public function __construct($arguments) {
7375
if (!isset($arguments['datadir']) || !is_string($arguments['datadir'])) {
7476
throw new \InvalidArgumentException('No data directory set for local storage');
@@ -88,6 +90,9 @@ public function __construct($arguments) {
8890
$this->config = \OC::$server->get(IConfig::class);
8991
$this->mimeTypeDetector = \OC::$server->get(IMimeTypeDetector::class);
9092
$this->defUMask = $this->config->getSystemValue('localstorage.umask', 0022);
93+
94+
// support Write-Once-Read-Many file systems
95+
$this->unlinkOnTruncate = $this->config->getSystemValue('localstorage.unlink_on_truncate', false);
9196
}
9297

9398
public function __destruct() {
@@ -297,8 +302,9 @@ public function file_get_contents($path) {
297302

298303
public function file_put_contents($path, $data) {
299304
$oldMask = umask($this->defUMask);
300-
// support Write-Once-Read-Many filesystems
301-
$this->unlink($path);
305+
if ($this->unlinkOnTruncate) {
306+
$this->unlink($path);
307+
}
302308
$result = file_put_contents($this->getSourcePath($path), $data);
303309
umask($oldMask);
304310
return $result;
@@ -372,8 +378,9 @@ public function copy($path1, $path2) {
372378
return parent::copy($path1, $path2);
373379
} else {
374380
$oldMask = umask($this->defUMask);
375-
// support Write-Once-Read-Many filesystems
376-
$this->unlink($path2);
381+
if ($this->unlinkOnTruncate) {
382+
$this->unlink($path2);
383+
}
377384
$result = copy($this->getSourcePath($path1), $this->getSourcePath($path2));
378385
umask($oldMask);
379386
return $result;
@@ -382,8 +389,7 @@ public function copy($path1, $path2) {
382389

383390
public function fopen($path, $mode) {
384391
$oldMask = umask($this->defUMask);
385-
if ($mode === 'w') {
386-
// support Write-Once-Read-Many filesystems
392+
if (($mode === 'w' || $mode === 'w+') && $this->unlinkOnTruncate) {
387393
$this->unlink($path);
388394
}
389395
$result = fopen($this->getSourcePath($path), $mode);

0 commit comments

Comments
 (0)