Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(files): Correctly copy the cache information during copy operations #48651

Merged
merged 2 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions lib/private/Files/Cache/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

use Doctrine\DBAL\Exception\DeadlockException;
use OC\Files\FileInfo;
use OC\Files\ObjectStore\ObjectStoreStorage;
use OCP\Files\Cache\ICache;
use OCP\Files\Cache\ICacheEntry;
use OCP\Files\Cache\IUpdater;
use OCP\Files\Storage\IStorage;
Expand Down Expand Up @@ -157,13 +159,40 @@ public function remove($path) {
}

/**
* Rename a file or folder in the cache and update the size, etag and mtime of the parent folders
* Rename a file or folder in the cache.
*
* @param IStorage $sourceStorage
* @param string $source
* @param string $target
*/
public function renameFromStorage(IStorage $sourceStorage, $source, $target) {
$this->copyOrRenameFromStorage($sourceStorage, $source, $target, function (ICache $sourceCache) use ($sourceStorage, $source, $target) {
// Remove existing cache entry to no reuse the fileId.
if ($this->cache->inCache($target)) {
$this->cache->remove($target);
}

if ($sourceStorage === $this->storage) {
$this->cache->move($source, $target);
} else {
$this->cache->moveFromCache($sourceCache, $source, $target);
}
});
}

/**
* Copy a file or folder in the cache.
*/
public function copyFromStorage(IStorage $sourceStorage, string $source, string $target): void {
$this->copyOrRenameFromStorage($sourceStorage, $source, $target, function (ICache $sourceCache, ICacheEntry $sourceInfo) use ($target) {
$this->cache->copyFromCache($sourceCache, $sourceInfo, $target);
});
}

/**
* Utility to copy or rename a file or folder in the cache and update the size, etag and mtime of the parent folders
*/
private function copyOrRenameFromStorage(IStorage $sourceStorage, string $source, string $target, callable $operation): void {
if (!$this->enabled or Scanner::isPartialFile($source) or Scanner::isPartialFile($target)) {
return;
}
Expand All @@ -177,14 +206,8 @@ public function renameFromStorage(IStorage $sourceStorage, $source, $target) {
$sourceInfo = $sourceCache->get($source);

if ($sourceInfo !== false) {
if ($this->cache->inCache($target)) {
$this->cache->remove($target);
}

if ($sourceStorage === $this->storage) {
$this->cache->move($source, $target);
} else {
$this->cache->moveFromCache($sourceCache, $source, $target);
if (!$this->storage->instanceOfStorage(ObjectStoreStorage::class)) {
$operation($sourceCache, $sourceInfo);
}

$sourceExtension = pathinfo($source, PATHINFO_EXTENSION);
Expand Down
1 change: 0 additions & 1 deletion lib/private/Files/Storage/Wrapper/Encryption.php
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,6 @@ private function copyBetweenStorage(
$info->getUnencryptedSize()
);
}
$this->updateEncryptedVersion($sourceStorage, $sourceInternalPath, $targetInternalPath, $isRename, true);
}
return $result;
}
Expand Down
10 changes: 9 additions & 1 deletion lib/private/Files/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,12 @@ protected function renameUpdate(Storage $sourceStorage, Storage $targetStorage,
}
}

protected function copyUpdate(Storage $sourceStorage, Storage $targetStorage, string $sourceInternalPath, string $targetInternalPath): void {
if ($this->updaterEnabled) {
$targetStorage->getUpdater()->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
}
}

/**
* @param string $path
* @return bool|mixed
Expand Down Expand Up @@ -898,7 +904,9 @@ public function copy($source, $target, $preserveMtime = false) {
$result = $storage2->copyFromStorage($storage1, $internalPath1, $internalPath2);
}

$this->writeUpdate($storage2, $internalPath2);
if ($result) {
$this->copyUpdate($storage1, $storage2, $internalPath1, $internalPath2);
}

$this->changeLock($target, ILockingProvider::LOCK_SHARED);
$lockTypePath2 = ILockingProvider::LOCK_SHARED;
Expand Down
7 changes: 7 additions & 0 deletions lib/public/Files/Cache/IUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,11 @@ public function remove($path);
* @since 9.0.0
*/
public function renameFromStorage(IStorage $sourceStorage, $source, $target);

/**
* Copy a file or folder in the cache and update the size, etag and mtime of the parent folders
*
* @since 31.0.0
*/
public function copyFromStorage(IStorage $sourceStorage, string $source, string $target): void;
}
9 changes: 9 additions & 0 deletions tests/lib/Files/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2386,6 +2386,7 @@ public function testLockFileRenameCrossStorage($viewOperation, $storageOperation
Filesystem::mount($storage2, [], $this->user . '/files/substorage');
$storage->mkdir('files');
$view->file_put_contents($sourcePath, 'meh');
$storage2->getUpdater()->update('');

$storage->expects($this->never())
->method($storageOperation);
Expand Down Expand Up @@ -2830,4 +2831,12 @@ public function testMountpointParentsCreated(): void {
$this->assertEquals('foo.png', $folderData[1]['name']);
$this->assertEquals('foo.txt', $folderData[2]['name']);
}

public function testCopyPreservesContent() {
$viewUser1 = new View('/' . 'userId' . '/files');
$viewUser1->mkdir('');
$viewUser1->file_put_contents('foo.txt', 'foo');
$viewUser1->copy('foo.txt', 'bar.txt');
$this->assertEquals('foo', $viewUser1->file_get_contents('bar.txt'));
}
}
Loading