Skip to content

Commit 09e89c7

Browse files
fix(Files/Cache): harden MoveFromCacheTrait::moveFromCache to validate source entry
Signed-off-by: Josh <josh.t.richards@gmail.com>
1 parent 5c0fe4c commit 09e89c7

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
@@ -11,34 +11,41 @@
1111
use OCP\Files\Cache\ICacheEntry;
1212

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

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

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

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

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

0 commit comments

Comments
 (0)