From aea5059e17c52fcd13c2081ad1d275f1ab8da113 Mon Sep 17 00:00:00 2001 From: Tigran Mkrtchyan Date: Thu, 31 Mar 2022 10:38:25 +0200 Subject: [PATCH] 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. --- 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 357321f6d29c2..cc1e0ea36fdbf 100644 --- a/config/config.sample.php +++ b/config/config.sample.php @@ -1796,6 +1796,14 @@ */ 'localstorage.allowsymlinks' => false, +/** + * 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 a5767d5b9a028..badef74c99022 100644 --- a/lib/private/Files/Storage/Local.php +++ b/lib/private/Files/Storage/Local.php @@ -66,6 +66,8 @@ class Local extends \OC\Files\Storage\Common { private IMimeTypeDetector $mimeTypeDetector; + protected $unlinkOnTruncate; + public function __construct($arguments) { if (!isset($arguments['datadir']) || !is_string($arguments['datadir'])) { throw new \InvalidArgumentException('No data directory set for local storage'); @@ -84,6 +86,9 @@ public function __construct($arguments) { $this->dataDirLength = strlen($this->realDataDir); $this->config = \OC::$server->get(IConfig::class); $this->mimeTypeDetector = \OC::$server->get(IMimeTypeDetector::class); + + // support Write-Once-Read-Many file systems + $this->$unlinkOnTruncate = $this->config->getSystemValue('localstorage.unlink_on_truncate', false); } public function __destruct() { @@ -289,8 +294,9 @@ public function file_get_contents($path) { public function file_put_contents($path, $data) { $oldMask = umask(022); - // 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; @@ -364,8 +370,9 @@ public function copy($path1, $path2) { return parent::copy($path1, $path2); } else { $oldMask = umask(022); - // 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; @@ -374,8 +381,7 @@ public function copy($path1, $path2) { public function fopen($path, $mode) { $oldMask = umask(022); - if ($mode === 'w') { - // support Write-Once-Read-Many filesystems + if ($mode === 'w' && $this->$unlinkOnTruncate) { $this->unlink($path); } $result = fopen($this->getSourcePath($path), $mode);