From eb3c214460b38698d4604405ac26e15c04aa5de2 Mon Sep 17 00:00:00 2001 From: Louis Chemineau Date: Thu, 10 Oct 2024 18:33:33 +0200 Subject: [PATCH] fix(files): Correctly copy the cache information on copy operations Needed to copy the `encrypted` flag of encrypted files when those files are two level down in a moved folder. Signed-off-by: Louis Chemineau --- lib/private/Files/Cache/Updater.php | 31 +++++++++++++++---- .../Files/Storage/Wrapper/Encryption.php | 1 - lib/private/Files/View.php | 8 ++++- lib/public/Files/Cache/IUpdater.php | 7 +++++ 4 files changed, 39 insertions(+), 8 deletions(-) diff --git a/lib/private/Files/Cache/Updater.php b/lib/private/Files/Cache/Updater.php index eab68b4f5458e..597631aa7f814 100644 --- a/lib/private/Files/Cache/Updater.php +++ b/lib/private/Files/Cache/Updater.php @@ -9,6 +9,7 @@ use Doctrine\DBAL\Exception\DeadlockException; use OC\Files\FileInfo; +use OCP\Files\Cache\ICache; use OCP\Files\Cache\ICacheEntry; use OCP\Files\Cache\IUpdater; use OCP\Files\Storage\IStorage; @@ -157,13 +158,35 @@ 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) { + 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; } @@ -181,11 +204,7 @@ public function renameFromStorage(IStorage $sourceStorage, $source, $target) { $this->cache->remove($target); } - if ($sourceStorage === $this->storage) { - $this->cache->move($source, $target); - } else { - $this->cache->moveFromCache($sourceCache, $source, $target); - } + $operation($sourceCache, $sourceInfo); $sourceExtension = pathinfo($source, PATHINFO_EXTENSION); $targetExtension = pathinfo($target, PATHINFO_EXTENSION); diff --git a/lib/private/Files/Storage/Wrapper/Encryption.php b/lib/private/Files/Storage/Wrapper/Encryption.php index a29e985377b54..06a008abd5e24 100644 --- a/lib/private/Files/Storage/Wrapper/Encryption.php +++ b/lib/private/Files/Storage/Wrapper/Encryption.php @@ -628,7 +628,6 @@ private function copyBetweenStorage( $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 80679a9481fa5..558e4ddb78379 100644 --- a/lib/private/Files/View.php +++ b/lib/private/Files/View.php @@ -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 @@ -898,7 +904,7 @@ public function copy($source, $target, $preserveMtime = false) { $result = $storage2->copyFromStorage($storage1, $internalPath1, $internalPath2); } - $this->writeUpdate($storage2, $internalPath2); + $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 3c2bc69715c0c..2bc702114b4c5 100644 --- a/lib/public/Files/Cache/IUpdater.php +++ b/lib/public/Files/Cache/IUpdater.php @@ -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; }