diff options
author | Andy Scherzinger <info@andy-scherzinger.de> | 2025-02-14 07:41:02 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-02-14 07:41:02 +0100 |
commit | 8bf22d939975d17b7258a570ffd933086e2a7caa (patch) | |
tree | 6595c26284a3621d810a9cec03e507ac0beaf0c4 | |
parent | 7ded302f5f9d5cb3d5baf0fec83437bd2699da3c (diff) | |
parent | 85417b56bf2bff3c892be6e227f8b07cf49a65a6 (diff) | |
download | nextcloud-server-8bf22d939975d17b7258a570ffd933086e2a7caa.tar.gz nextcloud-server-8bf22d939975d17b7258a570ffd933086e2a7caa.zip |
Merge pull request #50805 from nextcloud/backport/48651/stable29
[stable29] fix(files): Correctly copy the cache information during copy operations
-rw-r--r-- | lib/private/Files/Cache/Updater.php | 41 | ||||
-rw-r--r-- | lib/private/Files/Storage/Wrapper/Encryption.php | 1 | ||||
-rw-r--r-- | lib/private/Files/View.php | 10 | ||||
-rw-r--r-- | lib/public/Files/Cache/IUpdater.php | 7 | ||||
-rw-r--r-- | tests/lib/Files/ViewTest.php | 9 |
5 files changed, 57 insertions, 11 deletions
diff --git a/lib/private/Files/Cache/Updater.php b/lib/private/Files/Cache/Updater.php index d84ce0967a0..a07741e1fd4 100644 --- a/lib/private/Files/Cache/Updater.php +++ b/lib/private/Files/Cache/Updater.php @@ -29,6 +29,8 @@ namespace OC\Files\Cache; 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; @@ -177,13 +179,40 @@ class Updater implements IUpdater { } /** - * 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; } @@ -201,14 +230,8 @@ class Updater implements IUpdater { $targetIsTrash = preg_match("/^d\d+$/", $targetExtension); 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); } $isDir = $sourceInfo->getMimeType() === FileInfo::MIMETYPE_FOLDER; diff --git a/lib/private/Files/Storage/Wrapper/Encryption.php b/lib/private/Files/Storage/Wrapper/Encryption.php index bae2362cea2..895a369d428 100644 --- a/lib/private/Files/Storage/Wrapper/Encryption.php +++ b/lib/private/Files/Storage/Wrapper/Encryption.php @@ -800,7 +800,6 @@ class Encryption extends Wrapper { $info->getUnencryptedSize() ); } - $this->updateEncryptedVersion($sourceStorage, $sourceInternalPath, $targetInternalPath, $isRename, true); } return $result; } diff --git a/lib/private/Files/View.php b/lib/private/Files/View.php index 2a6bc507d00..8a2c8f530bd 100644 --- a/lib/private/Files/View.php +++ b/lib/private/Files/View.php @@ -316,6 +316,12 @@ class View { } } + 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 @@ -999,7 +1005,9 @@ class View { $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; diff --git a/lib/public/Files/Cache/IUpdater.php b/lib/public/Files/Cache/IUpdater.php index 625bc91c5a7..7a42587b809 100644 --- a/lib/public/Files/Cache/IUpdater.php +++ b/lib/public/Files/Cache/IUpdater.php @@ -72,4 +72,11 @@ interface IUpdater { * @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; } diff --git a/tests/lib/Files/ViewTest.php b/tests/lib/Files/ViewTest.php index b3d7fcb3f03..d5c1404451d 100644 --- a/tests/lib/Files/ViewTest.php +++ b/tests/lib/Files/ViewTest.php @@ -2371,6 +2371,7 @@ class ViewTest extends \Test\TestCase { 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); @@ -2815,4 +2816,12 @@ class ViewTest extends \Test\TestCase { $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')); + } } |