Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return the fileid from copyFromCache and use it instead of doing an extra query #26013

Merged
merged 4 commits into from
Mar 16, 2021
Merged
Show file tree
Hide file tree
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
45 changes: 44 additions & 1 deletion lib/private/Files/Cache/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ protected function getMoveInfo($path) {
/**
* Move a file or folder in the cache
*
* @param \OCP\Files\Cache\ICache $sourceCache
* @param ICache $sourceCache
* @param string $sourcePath
* @param string $targetPath
* @throws \OC\DatabaseException
Expand Down Expand Up @@ -1076,4 +1076,47 @@ public static function getById($id) {
public function normalize($path) {
return trim(\OC_Util::normalizeUnicode($path), '/');
}

/**
* Copy a file or folder in the cache
*
* @param ICache $sourceCache
* @param ICacheEntry $sourceEntry
* @param string $targetPath
* @return int fileid of copied entry
*/
public function copyFromCache(ICache $sourceCache, ICacheEntry $sourceEntry, string $targetPath): int {
if ($sourceEntry->getId() < 0) {
throw new \RuntimeException("Invalid source cache entry on copyFromCache");
}
$data = $this->cacheEntryToArray($sourceEntry);
icewind1991 marked this conversation as resolved.
Show resolved Hide resolved
$fileId = $this->put($targetPath, $data);
if ($fileId <= 0) {
throw new \RuntimeException("Failed to copy to " . $targetPath . " from cache with source data " . json_encode($data) . " ");
}
if ($sourceEntry->getMimeType() === ICacheEntry::DIRECTORY_MIMETYPE) {
$folderContent = $sourceCache->getFolderContentsById($sourceEntry->getId());
foreach ($folderContent as $subEntry) {
$subTargetPath = $targetPath . '/' . $subEntry->getName();
$this->copyFromCache($sourceCache, $subEntry, $subTargetPath);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we're not afraid of very deep folder structures and stack overflows ?

Edit: just saw it's old copied code, but something to keep in mind as tech debt?

}
}
return $fileId;
}

private function cacheEntryToArray(ICacheEntry $entry): array {
return [
'size' => $entry->getSize(),
'mtime' => $entry->getMTime(),
'storage_mtime' => $entry->getStorageMTime(),
'mimetype' => $entry->getMimeType(),
'mimepart' => $entry->getMimePart(),
'etag' => $entry->getEtag(),
'permissions' => $entry->getPermissions(),
'encrypted' => $entry->isEncrypted(),
'creation_time' => $entry->getCreationTime(),
'upload_time' => $entry->getUploadTime(),
'metadata_etag' => $entry->getMetadataEtag(),
];
}
}
5 changes: 5 additions & 0 deletions lib/private/Files/Cache/FailedCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

use OCP\Constants;
use OCP\Files\Cache\ICache;
use OCP\Files\Cache\ICacheEntry;
use OCP\Files\Search\ISearchQuery;

/**
Expand Down Expand Up @@ -134,4 +135,8 @@ public function getPathById($id) {
public function normalize($path) {
return $path;
}

public function copyFromCache(ICache $sourceCache, ICacheEntry $sourceEntry, string $targetPath): int {
throw new \Exception("Invalid cache");
}
}
36 changes: 2 additions & 34 deletions lib/private/Files/Cache/MoveFromCacheTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ trait MoveFromCacheTrait {
*/
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
*
Expand All @@ -55,38 +57,4 @@ public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) {

$sourceCache->remove($sourcePath);
}

/**
* Copy a file or folder in the cache
*
* @param \OCP\Files\Cache\ICache $sourceCache
* @param ICacheEntry $sourceEntry
* @param string $targetPath
*/
public function copyFromCache(ICache $sourceCache, ICacheEntry $sourceEntry, $targetPath) {
$this->put($targetPath, $this->cacheEntryToArray($sourceEntry));
if ($sourceEntry->getMimeType() === ICacheEntry::DIRECTORY_MIMETYPE) {
$folderContent = $sourceCache->getFolderContentsById($sourceEntry->getId());
foreach ($folderContent as $subEntry) {
$subTargetPath = $targetPath . '/' . $subEntry->getName();
$this->copyFromCache($sourceCache, $subEntry, $subTargetPath);
}
}
}

private function cacheEntryToArray(ICacheEntry $entry) {
return [
'size' => $entry->getSize(),
'mtime' => $entry->getMTime(),
'storage_mtime' => $entry->getStorageMTime(),
'mimetype' => $entry->getMimeType(),
'mimepart' => $entry->getMimePart(),
'etag' => $entry->getEtag(),
'permissions' => $entry->getPermissions(),
'encrypted' => $entry->isEncrypted(),
'creation_time' => $entry->getCreationTime(),
'upload_time' => $entry->getUploadTime(),
'metadata_etag' => $entry->getMetadataEtag(),
];
}
}
12 changes: 6 additions & 6 deletions lib/private/Files/ObjectStore/ObjectStoreStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use Icewind\Streams\CallbackWrapper;
use Icewind\Streams\CountWrapper;
use Icewind\Streams\IteratorDirectory;
use OC\Files\Cache\Cache;
use OC\Files\Cache\CacheEntry;
use OC\Files\Storage\PolyFill\CopyDirectory;
use OCP\Files\Cache\ICacheEntry;
Expand Down Expand Up @@ -581,14 +582,13 @@ private function copyFile(ICacheEntry $sourceEntry, string $to) {

$sourceUrn = $this->getURN($sourceEntry->getId());

$cache->copyFromCache($cache, $sourceEntry, $to);
$targetEntry = $cache->get($to);

if (!$targetEntry) {
throw new \Exception('Target not in cache after copy');
if (!$cache instanceof Cache) {
icewind1991 marked this conversation as resolved.
Show resolved Hide resolved
throw new \Exception("Invalid source cache for object store copy");
}

$targetUrn = $this->getURN($targetEntry->getId());
$targetId = $cache->copyFromCache($cache, $sourceEntry, $to);

$targetUrn = $this->getURN($targetId);

try {
$this->objectStore->copyObject($sourceUrn, $targetUrn);
Expand Down
5 changes: 5 additions & 0 deletions lib/private/Lockdown/Filesystem/NullCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use OC\Files\Cache\CacheEntry;
use OCP\Constants;
use OCP\Files\Cache\ICache;
use OCP\Files\Cache\ICacheEntry;
use OCP\Files\FileInfo;
use OCP\Files\Search\ISearchQuery;

Expand Down Expand Up @@ -122,4 +123,8 @@ public function getPathById($id) {
public function normalize($path) {
return $path;
}

public function copyFromCache(ICache $sourceCache, ICacheEntry $sourceEntry, string $targetPath): int {
throw new \OC\ForbiddenException('This request is not allowed to access the filesystem');
}
}
11 changes: 11 additions & 0 deletions lib/public/Files/Cache/ICache.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,17 @@ public function move($source, $target);
*/
public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath);

/**
* Copy a file or folder in the cache
*
* @param ICache $sourceCache
* @param ICacheEntry $sourceEntry
* @param string $targetPath
* @return int fileid of copied entry
* @since 22.0.0
*/
public function copyFromCache(ICache $sourceCache, ICacheEntry $sourceEntry, string $targetPath): int;

/**
* Get the scan status of a file
*
Expand Down