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 <[email protected]>
  • Loading branch information
icewind1991 authored and Altahrim committed Oct 2, 2024
1 parent dfeca3e commit 4b1759c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
6 changes: 1 addition & 5 deletions apps/files_trashbin/tests/StorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,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 @@ -697,10 +696,7 @@ public function testTrashbinCollision() {
$this->assertEquals('bar', $this->rootView->file_get_contents($this->user . '/files_trashbin/files/test.txt.d1001'));
}

public function testMoveFromStoragePreserveFileId() {
if (!$this->userView->getMount('')->getStorage()->instanceOfStorage(Local::class)) {
$this->markTestSkipped("Skipping on non-local users storage");
}
public function testMoveFromStoragePreserveFileId(): void {
$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 @@ -69,6 +69,7 @@ class ObjectStoreStorage extends \OC\Files\Storage\Common implements IChunkedFil
private $logger;

private bool $handleCopiesAsOwned;
private bool $preserveCacheItemsOnDelete = false;

/** @var bool */
protected $validateWrites = true;
Expand Down Expand Up @@ -204,7 +205,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 @@ -236,7 +239,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 @@ -770,4 +775,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 @@ -48,6 +48,7 @@
use OC\Files\Cache\Updater;
use OC\Files\Cache\Watcher;
use OC\Files\Filesystem;
use OC\Files\ObjectStore\ObjectStoreStorage;
use OC\Files\Storage\Wrapper\Jail;
use OC\Files\Storage\Wrapper\Wrapper;
use OCP\Files\EmptyFileNameException;
Expand Down Expand Up @@ -698,10 +699,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 4b1759c

Please sign in to comment.