diff options
author | Robin Appelman <icewind@owncloud.com> | 2016-01-27 15:45:19 +0100 |
---|---|---|
committer | Robin Appelman <icewind@owncloud.com> | 2016-01-29 13:06:59 +0100 |
commit | ec3f6549f6223a9fb752c5a930549c5c1fe7bcc6 (patch) | |
tree | cbc54cd66341e1fd159d6d94b2d08a64de8bfc4a /lib | |
parent | 308396b770f3b1b41ba31ff0ee889d485b14d14a (diff) | |
download | nextcloud-server-ec3f6549f6223a9fb752c5a930549c5c1fe7bcc6.tar.gz nextcloud-server-ec3f6549f6223a9fb752c5a930549c5c1fe7bcc6.zip |
Add fallback moveFromCache implementation
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/files/cache/cache.php | 67 | ||||
-rw-r--r-- | lib/private/files/cache/movefromcachetrait.php | 87 | ||||
-rw-r--r-- | lib/public/files/cache/icache.php | 4 | ||||
-rw-r--r-- | lib/public/files/cache/icacheentry.php | 2 |
4 files changed, 129 insertions, 31 deletions
diff --git a/lib/private/files/cache/cache.php b/lib/private/files/cache/cache.php index 527c8b76e52..30e00b6080c 100644 --- a/lib/private/files/cache/cache.php +++ b/lib/private/files/cache/cache.php @@ -49,6 +49,10 @@ use OCP\IDBConnection; * - ChangePropagator: updates the mtime and etags of parent folders whenever a change to the cache is made to the cache by the updater */ class Cache implements ICache { + use MoveFromCacheTrait { + MoveFromCacheTrait::moveFromCache as moveFromCacheFallback; + } + /** * @var array partial data for the cache */ @@ -466,39 +470,42 @@ class Cache implements ICache { * @throws \OC\DatabaseException */ public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) { - // normalize source and target - $sourcePath = $this->normalize($sourcePath); - $targetPath = $this->normalize($targetPath); - - $sourceData = $sourceCache->get($sourcePath); - $sourceId = $sourceData['fileid']; - $newParentId = $this->getParentId($targetPath); - - list($sourceStorageId, $sourcePath) = $sourceCache->getMoveInfo($sourcePath); - list($targetStorageId, $targetPath) = $this->getMoveInfo($targetPath); - - // sql for final update - $moveSql = 'UPDATE `*PREFIX*filecache` SET `storage` = ?, `path` = ?, `path_hash` = ?, `name` = ?, `parent` =? WHERE `fileid` = ?'; - - if ($sourceData['mimetype'] === 'httpd/unix-directory') { - //find all child entries - $sql = 'SELECT `path`, `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path` LIKE ?'; - $result = $this->connection->executeQuery($sql, [$sourceStorageId, $this->connection->escapeLikeParameter($sourcePath) . '/%']); - $childEntries = $result->fetchAll(); - $sourceLength = strlen($sourcePath); - $this->connection->beginTransaction(); - $query = $this->connection->prepare('UPDATE `*PREFIX*filecache` SET `storage` = ?, `path` = ?, `path_hash` = ? WHERE `fileid` = ?'); - - foreach ($childEntries as $child) { - $newTargetPath = $targetPath . substr($child['path'], $sourceLength); - $query->execute([$targetStorageId, $newTargetPath, md5($newTargetPath), $child['fileid']]); + if ($sourceCache instanceof Cache) { + // normalize source and target + $sourcePath = $this->normalize($sourcePath); + $targetPath = $this->normalize($targetPath); + + $sourceData = $sourceCache->get($sourcePath); + $sourceId = $sourceData['fileid']; + $newParentId = $this->getParentId($targetPath); + + list($sourceStorageId, $sourcePath) = $sourceCache->getMoveInfo($sourcePath); + list($targetStorageId, $targetPath) = $this->getMoveInfo($targetPath); + + // sql for final update + $moveSql = 'UPDATE `*PREFIX*filecache` SET `storage` = ?, `path` = ?, `path_hash` = ?, `name` = ?, `parent` =? WHERE `fileid` = ?'; + + if ($sourceData['mimetype'] === 'httpd/unix-directory') { + //find all child entries + $sql = 'SELECT `path`, `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path` LIKE ?'; + $result = $this->connection->executeQuery($sql, [$sourceStorageId, $this->connection->escapeLikeParameter($sourcePath) . '/%']); + $childEntries = $result->fetchAll(); + $sourceLength = strlen($sourcePath); + $this->connection->beginTransaction(); + $query = $this->connection->prepare('UPDATE `*PREFIX*filecache` SET `storage` = ?, `path` = ?, `path_hash` = ? WHERE `fileid` = ?'); + + foreach ($childEntries as $child) { + $newTargetPath = $targetPath . substr($child['path'], $sourceLength); + $query->execute([$targetStorageId, $newTargetPath, md5($newTargetPath), $child['fileid']]); + } + $this->connection->executeQuery($moveSql, [$targetStorageId, $targetPath, md5($targetPath), basename($targetPath), $newParentId, $sourceId]); + $this->connection->commit(); + } else { + $this->connection->executeQuery($moveSql, [$targetStorageId, $targetPath, md5($targetPath), basename($targetPath), $newParentId, $sourceId]); } - $this->connection->executeQuery($moveSql, [$targetStorageId, $targetPath, md5($targetPath), basename($targetPath), $newParentId, $sourceId]); - $this->connection->commit(); } else { - $this->connection->executeQuery($moveSql, [$targetStorageId, $targetPath, md5($targetPath), basename($targetPath), $newParentId, $sourceId]); + $this->moveFromCacheFallback($sourceCache, $sourcePath, $targetPath); } - } /** diff --git a/lib/private/files/cache/movefromcachetrait.php b/lib/private/files/cache/movefromcachetrait.php new file mode 100644 index 00000000000..7d8ed7b5d21 --- /dev/null +++ b/lib/private/files/cache/movefromcachetrait.php @@ -0,0 +1,87 @@ +<?php +/** + * @author Robin Appelman <icewind@owncloud.com> + * + * @copyright Copyright (c) 2016, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + +namespace OC\Files\Cache; + +use OCP\Files\Cache\ICache; +use OCP\Files\Cache\ICacheEntry; + +/** + * Fallback implementation for moveFromCache + */ +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); + + /** + * Move a file or folder in the cache + * + * @param \OCP\Files\Cache\ICache $sourceCache + * @param string $sourcePath + * @param string $targetPath + */ + public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) { + $sourceEntry = $sourceCache->get($sourcePath); + + $this->copyFromCache($sourceCache, $sourceEntry, $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() + ]; + } +} diff --git a/lib/public/files/cache/icache.php b/lib/public/files/cache/icache.php index 07396db4588..125e345205d 100644 --- a/lib/public/files/cache/icache.php +++ b/lib/public/files/cache/icache.php @@ -51,7 +51,7 @@ interface ICache { * get the stored metadata of a file or folder * * @param string | int $file either the path of a file or folder or the file id for a file or folder - * @return ICacheEntry[]|false the cache entry or false if the file is not found in the cache + * @return ICacheEntry|false the cache entry or false if the file is not found in the cache * @since 9.0.0 */ public function get($file); @@ -148,6 +148,8 @@ interface ICache { /** * Move a file or folder in the cache * + * Note that this should make sure the entries are removed from the source cache + * * @param \OCP\Files\Cache\ICache $sourceCache * @param string $sourcePath * @param string $targetPath diff --git a/lib/public/files/cache/icacheentry.php b/lib/public/files/cache/icacheentry.php index 8d14bd2c555..63a232c9618 100644 --- a/lib/public/files/cache/icacheentry.php +++ b/lib/public/files/cache/icacheentry.php @@ -27,6 +27,8 @@ namespace OCP\Files\Cache; * @since 9.0.0 */ interface ICacheEntry { + const DIRECTORY_MIMETYPE = 'httpd/unix-directory'; + /** * Get the numeric id of a file * |