Skip to content

Commit

Permalink
fix: preserve fileid when moving from objectstore to non-objectstore
Browse files Browse the repository at this point in the history
Signed-off-by: Robin Appelman <robin@icewind.nl>
  • Loading branch information
icewind1991 committed Sep 25, 2024
1 parent 26f50f7 commit ade3133
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
4 changes: 0 additions & 4 deletions apps/files_trashbin/tests/StorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

use OC\Files\Filesystem;
use OC\Files\Storage\Common;
use OC\Files\Storage\Local;
use OC\Files\Storage\Temporary;
use OCA\Files_Trashbin\AppInfo\Application;
use OCA\Files_Trashbin\Events\MoveToTrashEvent;
Expand Down Expand Up @@ -673,9 +672,6 @@ public function testTrashbinCollision(): void {
}

public function testMoveFromStoragePreserveFileId(): void {
if (!$this->userView->getMount('')->getStorage()->instanceOfStorage(Local::class)) {
$this->markTestSkipped('Skipping on non-local users storage');
}
$this->userView->file_put_contents('test.txt', 'foo');
$fileId = $this->userView->getFileInfo('test.txt')->getId();

Expand Down
13 changes: 11 additions & 2 deletions lib/private/Files/ObjectStore/ObjectStoreStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class ObjectStoreStorage extends \OC\Files\Storage\Common implements IChunkedFil

private bool $handleCopiesAsOwned;
protected bool $validateWrites = true;
private bool $preserveCacheItemsOnDelete = false;

/**
* @param array $params
Expand Down Expand Up @@ -170,7 +171,9 @@ private function rmObjects(ICacheEntry $entry): bool {
}
}

$this->getCache()->remove($entry->getPath());
if (!$this->preserveCacheItemsOnDelete) {
$this->getCache()->remove($entry->getPath());
}

return true;
}
Expand Down Expand Up @@ -205,7 +208,9 @@ public function rmObject(ICacheEntry $entry): bool {
}
//removing from cache is ok as it does not exist in the objectstore anyway
}
$this->getCache()->remove($entry->getPath());
if (!$this->preserveCacheItemsOnDelete) {
$this->getCache()->remove($entry->getPath());
}
return true;
}

Expand Down Expand Up @@ -762,4 +767,8 @@ public function cancelChunkedWrite(string $targetPath, string $writeToken): void
$urn = $this->getURN($cacheEntry->getId());
$this->objectStore->abortMultipartUpload($urn, $writeToken);
}

public function setPreserveCacheOnDelete(bool $preserve) {
$this->preserveCacheItemsOnDelete = $preserve;
}
}
20 changes: 16 additions & 4 deletions lib/private/Files/Storage/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use OC\Files\Cache\Watcher;
use OC\Files\FilenameValidator;
use OC\Files\Filesystem;
use OC\Files\ObjectStore\ObjectStoreStorage;
use OC\Files\Storage\Wrapper\Jail;
use OC\Files\Storage\Wrapper\Wrapper;
use OCP\Files\ForbiddenException;
Expand Down Expand Up @@ -636,10 +637,21 @@ public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $t

$result = $this->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath, true);
if ($result) {
if ($sourceStorage->is_dir($sourceInternalPath)) {
$result = $sourceStorage->rmdir($sourceInternalPath);
} else {
$result = $sourceStorage->unlink($sourceInternalPath);
if ($sourceStorage->instanceOfStorage(ObjectStoreStorage::class)) {
/** @var ObjectStoreStorage $sourceStorage */
$sourceStorage->setPreserveCacheOnDelete(true);
}
try {
if ($sourceStorage->is_dir($sourceInternalPath)) {
$result = $sourceStorage->rmdir($sourceInternalPath);
} else {
$result = $sourceStorage->unlink($sourceInternalPath);
}
} finally {
if ($sourceStorage->instanceOfStorage(ObjectStoreStorage::class)) {
/** @var ObjectStoreStorage $sourceStorage */
$sourceStorage->setPreserveCacheOnDelete(false);
}
}
}
return $result;
Expand Down

0 comments on commit ade3133

Please sign in to comment.