Skip to content

Commit

Permalink
Merge pull request #41505 from nextcloud/backport/40935/stable26
Browse files Browse the repository at this point in the history
[stable26] add some support for rename on case insensitive local filesystems
  • Loading branch information
Altahrim committed Nov 16, 2023
2 parents bba6080 + 0fb0314 commit 9147bc1
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions lib/private/Files/Storage/Local.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class Local extends \OC\Files\Storage\Common {

protected bool $unlinkOnTruncate;

protected bool $caseInsensitive = false;

public function __construct($arguments) {
if (!isset($arguments['datadir']) || !is_string($arguments['datadir'])) {
throw new \InvalidArgumentException('No data directory set for local storage');
Expand All @@ -92,6 +94,7 @@ 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);
$this->caseInsensitive = $this->config->getSystemValueBool('localstorage.case_insensitive', false);

// support Write-Once-Read-Many file systems
$this->unlinkOnTruncate = $this->config->getSystemValue('localstorage.unlink_on_truncate', false);
Expand Down Expand Up @@ -155,13 +158,19 @@ public function opendir($path) {
}

public function is_dir($path) {
if ($this->caseInsensitive && !$this->file_exists($path)) {
return false;
}
if (substr($path, -1) == '/') {
$path = substr($path, 0, -1);
}
return is_dir($this->getSourcePath($path));
}

public function is_file($path) {
if ($this->caseInsensitive && !$this->file_exists($path)) {
return false;
}
return is_file($this->getSourcePath($path));
}

Expand Down Expand Up @@ -264,7 +273,13 @@ public function isUpdatable($path) {
}

public function file_exists($path) {
return file_exists($this->getSourcePath($path));
if ($this->caseInsensitive) {
$fullPath = $this->getSourcePath($path);
$content = scandir(dirname($fullPath), SCANDIR_SORT_NONE);
return is_array($content) && array_search(basename($fullPath), $content) !== false;
} else {
return file_exists($this->getSourcePath($path));
}
}

public function filemtime($path) {
Expand Down Expand Up @@ -375,7 +390,16 @@ public function rename($source, $target) {
$this->checkTreeForForbiddenItems($this->getSourcePath($source));
}

return rename($this->getSourcePath($source), $this->getSourcePath($target));
if (rename($this->getSourcePath($source), $this->getSourcePath($target))) {
if ($this->caseInsensitive) {
if (mb_strtolower($target) === mb_strtolower($source) && !$this->file_exists($target)) {
return false;
}
}
return true;
}

return false;
}

public function copy($source, $target) {
Expand All @@ -388,6 +412,11 @@ public function copy($source, $target) {
}
$result = copy($this->getSourcePath($source), $this->getSourcePath($target));
umask($oldMask);
if ($this->caseInsensitive) {
if (mb_strtolower($target) === mb_strtolower($source) && !$this->file_exists($target)) {
return false;
}
}
return $result;
}
}
Expand Down

0 comments on commit 9147bc1

Please sign in to comment.