Skip to content

Commit a813519

Browse files
authored
Merge pull request #59916 from nextcloud/jtr/fix-MoveFromCacheTrait-hardening
fix(Files/Cache): align `MoveFromCacheTrait` fallback validation with `Cache::moveFromCache`
2 parents 5e4abd7 + c1ec62c commit a813519

1 file changed

Lines changed: 19 additions & 12 deletions

File tree

lib/private/Files/Cache/MoveFromCacheTrait.php

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,41 @@
1212
use OCP\Files\Cache\ICacheEntry;
1313

1414
/**
15-
* Fallback implementation for moveFromCache
15+
* Generic fallback implementation for moving cache entries.
16+
*
17+
* This fallback copies the source entry to the target path and then removes
18+
* it from the source cache.
19+
*
20+
* It is intended for cache implementations that do not provide a specialized
21+
* in-place move operation.
1622
*/
1723
trait MoveFromCacheTrait {
18-
/**
19-
* store meta data for a file or folder
20-
*
21-
* @param string $file
22-
* @param array $data
23-
*
24-
* @return int file id
25-
* @throws \RuntimeException
26-
*/
24+
2725
abstract public function put($file, array $data);
2826

2927
abstract public function copyFromCache(ICache $sourceCache, ICacheEntry $sourceEntry, string $targetPath): int;
3028

3129
/**
32-
* Move a file or folder in the cache
30+
* Move a file or folder in the cache.
31+
*
32+
* This fallback performs the move as a copy-then-delete, so it does not
33+
* preserve the original cache entry identity and may result in a new file id
34+
* at the destination.
3335
*
3436
* @param ICache $sourceCache
3537
* @param string $sourcePath
3638
* @param string $targetPath
39+
* @throws \RuntimeException if the source path cannot be found in cache
3740
*/
3841
public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) {
3942
$sourceEntry = $sourceCache->get($sourcePath);
4043

41-
$this->copyFromCache($sourceCache, $sourceEntry, $targetPath);
44+
if (!$sourceEntry) {
45+
throw new \RuntimeException('Source path not found in cache: ' . $sourcePath);
46+
}
4247

48+
$this->copyFromCache($sourceCache, $sourceEntry, $targetPath);
49+
// non-atomic; failed removals can leave duplicates
4350
$sourceCache->remove($sourcePath);
4451
}
4552
}

0 commit comments

Comments
 (0)