From b6065a236fe470e37b28e768d9e09b25e92b470d Mon Sep 17 00:00:00 2001 From: Tigran Mkrtchyan Date: Mon, 7 Dec 2020 18:24:37 +0100 Subject: [PATCH 1/3] files: make OC\Files\Storage\Local WORM friendly Some filesystems run as a Write-Once-Read-Many storages. This makes them impossible to use with NexeCloud, as the file system layers uses `truncate` syscall (through file_put_contents function). As Nextcloud is never updates existing files, removing the old entry and creatint a new one on update will allow NextCoud to update on such file systems. Update Local#fopen and Local#file_put_contents to remote existing file before truncating. Signed-off-by: Tigran Mkrtchyan --- lib/private/Files/Storage/Local.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/private/Files/Storage/Local.php b/lib/private/Files/Storage/Local.php index 4996572a40e..7af512ca3f6 100644 --- a/lib/private/Files/Storage/Local.php +++ b/lib/private/Files/Storage/Local.php @@ -297,6 +297,8 @@ class Local extends \OC\Files\Storage\Common { public function file_put_contents($path, $data) { $oldMask = umask($this->defUMask); + // support Write-Once-Read-Many filesystems + $this->unlink($path); $result = file_put_contents($this->getSourcePath($path), $data); umask($oldMask); return $result; @@ -378,6 +380,10 @@ class Local extends \OC\Files\Storage\Common { public function fopen($path, $mode) { $oldMask = umask($this->defUMask); + if ($mode === 'w') { + // support Write-Once-Read-Many filesystems + $this->unlink($path); + } $result = fopen($this->getSourcePath($path), $mode); umask($oldMask); return $result; From 8fc4cf67f1553d0d37bb061925867789b27579fe Mon Sep 17 00:00:00 2001 From: Tigran Mkrtchyan Date: Mon, 7 Mar 2022 10:15:53 +0100 Subject: [PATCH 2/3] files: remove destination file before copying new content (WORM) Signed-off-by: Tigran Mkrtchyan --- lib/private/Files/Storage/Local.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/private/Files/Storage/Local.php b/lib/private/Files/Storage/Local.php index 7af512ca3f6..b9dcb7d0537 100644 --- a/lib/private/Files/Storage/Local.php +++ b/lib/private/Files/Storage/Local.php @@ -372,6 +372,8 @@ class Local extends \OC\Files\Storage\Common { return parent::copy($path1, $path2); } else { $oldMask = umask($this->defUMask); + // support Write-Once-Read-Many filesystems + $this->unlink($path2); $result = copy($this->getSourcePath($path1), $this->getSourcePath($path2)); umask($oldMask); return $result; From f41209a0614a317857b77d3be80cd7ef4dbb4695 Mon Sep 17 00:00:00 2001 From: Tigran Mkrtchyan Date: Thu, 31 Mar 2022 10:38:25 +0200 Subject: [PATCH 3/3] 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 --- config/config.sample.php | 8 ++++++++ lib/private/Files/Storage/Local.php | 18 ++++++++++++------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/config/config.sample.php b/config/config.sample.php index 05fba5777eb..bcd46381ebd 100644 --- a/config/config.sample.php +++ b/config/config.sample.php @@ -1887,6 +1887,14 @@ $CONFIG = [ */ '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. diff --git a/lib/private/Files/Storage/Local.php b/lib/private/Files/Storage/Local.php index b9dcb7d0537..2a7e30cada6 100644 --- a/lib/private/Files/Storage/Local.php +++ b/lib/private/Files/Storage/Local.php @@ -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'); @@ -88,6 +90,9 @@ class Local extends \OC\Files\Storage\Common { $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() { @@ -297,8 +302,9 @@ class Local extends \OC\Files\Storage\Common { public function file_put_contents($path, $data) { $oldMask = umask($this->defUMask); - // support Write-Once-Read-Many filesystems - $this->unlink($path); + if ($this->unlinkOnTruncate) { + $this->unlink($path); + } $result = file_put_contents($this->getSourcePath($path), $data); umask($oldMask); return $result; @@ -372,8 +378,9 @@ class Local extends \OC\Files\Storage\Common { return parent::copy($path1, $path2); } else { $oldMask = umask($this->defUMask); - // support Write-Once-Read-Many filesystems - $this->unlink($path2); + if ($this->unlinkOnTruncate) { + $this->unlink($path2); + } $result = copy($this->getSourcePath($path1), $this->getSourcePath($path2)); umask($oldMask); return $result; @@ -382,8 +389,7 @@ class Local extends \OC\Files\Storage\Common { public function fopen($path, $mode) { $oldMask = umask($this->defUMask); - if ($mode === 'w') { - // support Write-Once-Read-Many filesystems + if (($mode === 'w' || $mode === 'w+') && $this->unlinkOnTruncate) { $this->unlink($path); } $result = fopen($this->getSourcePath($path), $mode);