Skip to content

Commit

Permalink
Merge pull request #24596 from kofemann/dcache-worm
Browse files Browse the repository at this point in the history
make NextCloud WORM file system friendly
  • Loading branch information
PVince81 authored Sep 16, 2022
2 parents b3ca186 + f41209a commit 1025d04
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config/config.sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -1910,6 +1910,14 @@
*/
'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
* calculation, defaults to false.
Expand Down
14 changes: 14 additions & 0 deletions lib/private/Files/Storage/Local.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class Local extends \OC\Files\Storage\Common {

private $defUMask;

protected bool $unlinkOnTruncate;

public function __construct($arguments) {
if (!isset($arguments['datadir']) || !is_string($arguments['datadir'])) {
throw new \InvalidArgumentException('No data directory set for local storage');
Expand All @@ -88,6 +90,9 @@ public function __construct($arguments) {
$this->config = \OC::$server->get(IConfig::class);
$this->mimeTypeDetector = \OC::$server->get(IMimeTypeDetector::class);
$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() {
Expand Down Expand Up @@ -297,6 +302,9 @@ public function file_get_contents($path) {

public function file_put_contents($path, $data) {
$oldMask = umask($this->defUMask);
if ($this->unlinkOnTruncate) {
$this->unlink($path);
}
$result = file_put_contents($this->getSourcePath($path), $data);
umask($oldMask);
return $result;
Expand Down Expand Up @@ -370,6 +378,9 @@ public function copy($path1, $path2) {
return parent::copy($path1, $path2);
} else {
$oldMask = umask($this->defUMask);
if ($this->unlinkOnTruncate) {
$this->unlink($path2);
}
$result = copy($this->getSourcePath($path1), $this->getSourcePath($path2));
umask($oldMask);
return $result;
Expand All @@ -378,6 +389,9 @@ public function copy($path1, $path2) {

public function fopen($path, $mode) {
$oldMask = umask($this->defUMask);
if (($mode === 'w' || $mode === 'w+') && $this->unlinkOnTruncate) {
$this->unlink($path);
}
$result = fopen($this->getSourcePath($path), $mode);
umask($oldMask);
return $result;
Expand Down

0 comments on commit 1025d04

Please sign in to comment.