Skip to content

Commit

Permalink
config: add a switch ton control truncate before update
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kofemann committed Mar 31, 2022
1 parent 5e6ac2b commit 2540b59
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
8 changes: 8 additions & 0 deletions config/config.sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -1769,6 +1769,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.
Expand Down
18 changes: 12 additions & 6 deletions lib/private/Files/Storage/Local.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class Local extends \OC\Files\Storage\Common {

protected $realDataDir;

protected $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 @@ -76,6 +78,9 @@ public function __construct($arguments) {
$this->datadir .= '/';
}
$this->dataDirLength = strlen($this->realDataDir);

// support Write-Once-Read-Many file systems
$unlinkOnTruncate = \OC::$server->getConfig()->getSystemValue('localstorage.unlink_on_truncate', false);
}

public function __destruct() {
Expand Down Expand Up @@ -279,8 +284,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 ($unlinkOnTruncate) {
$this->unlink($path);
}
$result = file_put_contents($this->getSourcePath($path), $data);
umask($oldMask);
return $result;
Expand Down Expand Up @@ -354,8 +360,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 ($unlinkOnTruncate) {
$this->unlink($path2);
}
$result = copy($this->getSourcePath($path1), $this->getSourcePath($path2));
umask($oldMask);
return $result;
Expand All @@ -364,8 +371,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' && $unlinkOnTruncate) {
$this->unlink($path);
}
$result = fopen($this->getSourcePath($path), $mode);
Expand Down

0 comments on commit 2540b59

Please sign in to comment.