Skip to content

Commit 8bf22d9

Browse files
Merge pull request #50805 from nextcloud/backport/48651/stable29
[stable29] fix(files): Correctly copy the cache information during copy operations
2 parents 7ded302 + 85417b5 commit 8bf22d9

File tree

5 files changed

+57
-11
lines changed

5 files changed

+57
-11
lines changed

lib/private/Files/Cache/Updater.php

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
use Doctrine\DBAL\Exception\DeadlockException;
3131
use OC\Files\FileInfo;
32+
use OC\Files\ObjectStore\ObjectStoreStorage;
33+
use OCP\Files\Cache\ICache;
3234
use OCP\Files\Cache\ICacheEntry;
3335
use OCP\Files\Cache\IUpdater;
3436
use OCP\Files\Storage\IStorage;
@@ -177,13 +179,40 @@ public function remove($path) {
177179
}
178180

179181
/**
180-
* Rename a file or folder in the cache and update the size, etag and mtime of the parent folders
182+
* Rename a file or folder in the cache.
181183
*
182184
* @param IStorage $sourceStorage
183185
* @param string $source
184186
* @param string $target
185187
*/
186188
public function renameFromStorage(IStorage $sourceStorage, $source, $target) {
189+
$this->copyOrRenameFromStorage($sourceStorage, $source, $target, function (ICache $sourceCache) use ($sourceStorage, $source, $target) {
190+
// Remove existing cache entry to no reuse the fileId.
191+
if ($this->cache->inCache($target)) {
192+
$this->cache->remove($target);
193+
}
194+
195+
if ($sourceStorage === $this->storage) {
196+
$this->cache->move($source, $target);
197+
} else {
198+
$this->cache->moveFromCache($sourceCache, $source, $target);
199+
}
200+
});
201+
}
202+
203+
/**
204+
* Copy a file or folder in the cache.
205+
*/
206+
public function copyFromStorage(IStorage $sourceStorage, string $source, string $target): void {
207+
$this->copyOrRenameFromStorage($sourceStorage, $source, $target, function (ICache $sourceCache, ICacheEntry $sourceInfo) use ($target) {
208+
$this->cache->copyFromCache($sourceCache, $sourceInfo, $target);
209+
});
210+
}
211+
212+
/**
213+
* Utility to copy or rename a file or folder in the cache and update the size, etag and mtime of the parent folders
214+
*/
215+
private function copyOrRenameFromStorage(IStorage $sourceStorage, string $source, string $target, callable $operation): void {
187216
if (!$this->enabled or Scanner::isPartialFile($source) or Scanner::isPartialFile($target)) {
188217
return;
189218
}
@@ -201,14 +230,8 @@ public function renameFromStorage(IStorage $sourceStorage, $source, $target) {
201230
$targetIsTrash = preg_match("/^d\d+$/", $targetExtension);
202231

203232
if ($sourceInfo !== false) {
204-
if ($this->cache->inCache($target)) {
205-
$this->cache->remove($target);
206-
}
207-
208-
if ($sourceStorage === $this->storage) {
209-
$this->cache->move($source, $target);
210-
} else {
211-
$this->cache->moveFromCache($sourceCache, $source, $target);
233+
if (!$this->storage->instanceOfStorage(ObjectStoreStorage::class)) {
234+
$operation($sourceCache, $sourceInfo);
212235
}
213236

214237
$isDir = $sourceInfo->getMimeType() === FileInfo::MIMETYPE_FOLDER;

lib/private/Files/Storage/Wrapper/Encryption.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -800,7 +800,6 @@ private function copyBetweenStorage(
800800
$info->getUnencryptedSize()
801801
);
802802
}
803-
$this->updateEncryptedVersion($sourceStorage, $sourceInternalPath, $targetInternalPath, $isRename, true);
804803
}
805804
return $result;
806805
}

lib/private/Files/View.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,12 @@ protected function renameUpdate(Storage $sourceStorage, Storage $targetStorage,
316316
}
317317
}
318318

319+
protected function copyUpdate(Storage $sourceStorage, Storage $targetStorage, string $sourceInternalPath, string $targetInternalPath): void {
320+
if ($this->updaterEnabled) {
321+
$targetStorage->getUpdater()->copyFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
322+
}
323+
}
324+
319325
/**
320326
* @param string $path
321327
* @return bool|mixed
@@ -999,7 +1005,9 @@ public function copy($source, $target, $preserveMtime = false) {
9991005
$result = $storage2->copyFromStorage($storage1, $internalPath1, $internalPath2);
10001006
}
10011007

1002-
$this->writeUpdate($storage2, $internalPath2);
1008+
if ($result) {
1009+
$this->copyUpdate($storage1, $storage2, $internalPath1, $internalPath2);
1010+
}
10031011

10041012
$this->changeLock($target, ILockingProvider::LOCK_SHARED);
10051013
$lockTypePath2 = ILockingProvider::LOCK_SHARED;

lib/public/Files/Cache/IUpdater.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,11 @@ public function remove($path);
7272
* @since 9.0.0
7373
*/
7474
public function renameFromStorage(IStorage $sourceStorage, $source, $target);
75+
76+
/**
77+
* Copy a file or folder in the cache and update the size, etag and mtime of the parent folders
78+
*
79+
* @since 31.0.0
80+
*/
81+
public function copyFromStorage(IStorage $sourceStorage, string $source, string $target): void;
7582
}

tests/lib/Files/ViewTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2371,6 +2371,7 @@ public function testLockFileRenameCrossStorage($viewOperation, $storageOperation
23712371
Filesystem::mount($storage2, [], $this->user . '/files/substorage');
23722372
$storage->mkdir('files');
23732373
$view->file_put_contents($sourcePath, 'meh');
2374+
$storage2->getUpdater()->update('');
23742375

23752376
$storage->expects($this->never())
23762377
->method($storageOperation);
@@ -2815,4 +2816,12 @@ public function testMountpointParentsCreated() {
28152816
$this->assertEquals('foo.png', $folderData[1]['name']);
28162817
$this->assertEquals('foo.txt', $folderData[2]['name']);
28172818
}
2819+
2820+
public function testCopyPreservesContent() {
2821+
$viewUser1 = new View('/' . 'userId' . '/files');
2822+
$viewUser1->mkdir('');
2823+
$viewUser1->file_put_contents('foo.txt', 'foo');
2824+
$viewUser1->copy('foo.txt', 'bar.txt');
2825+
$this->assertEquals('foo', $viewUser1->file_get_contents('bar.txt'));
2826+
}
28182827
}

0 commit comments

Comments
 (0)