Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions lib/private/Files/Cache/MoveFromCacheTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,41 @@
use OCP\Files\Cache\ICacheEntry;

/**
* Fallback implementation for moveFromCache
* Generic fallback implementation for moving cache entries.
*
* This fallback copies the source entry to the target path and then removes
* it from the source cache.
*
* It is intended for cache implementations that do not provide a specialized
* in-place move operation.
*/
trait MoveFromCacheTrait {
/**
* store meta data for a file or folder
*
* @param string $file
* @param array $data
*
* @return int file id
* @throws \RuntimeException
*/

abstract public function put($file, array $data);

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

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

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

$this->copyFromCache($sourceCache, $sourceEntry, $targetPath);
// non-atomic; failed removals can leave duplicates
$sourceCache->remove($sourcePath);
}
}
Loading