12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175 |
- <?php
- namespace OC\Files\Cache;
- use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
- use OC\Files\Search\SearchComparison;
- use OC\Files\Search\SearchQuery;
- use OC\Files\Storage\Wrapper\Encryption;
- use OCP\DB\QueryBuilder\IQueryBuilder;
- use OCP\EventDispatcher\IEventDispatcher;
- use OCP\Files\Cache\CacheEntryInsertedEvent;
- use OCP\Files\Cache\CacheEntryUpdatedEvent;
- use OCP\Files\Cache\CacheInsertEvent;
- use OCP\Files\Cache\CacheEntryRemovedEvent;
- use OCP\Files\Cache\CacheUpdateEvent;
- use OCP\Files\Cache\ICache;
- use OCP\Files\Cache\ICacheEntry;
- use OCP\Files\FileInfo;
- use OCP\Files\IMimeTypeLoader;
- use OCP\Files\Search\ISearchComparison;
- use OCP\Files\Search\ISearchOperator;
- use OCP\Files\Search\ISearchQuery;
- use OCP\Files\Storage\IStorage;
- use OCP\IDBConnection;
- use OCP\Util;
- use Psr\Log\LoggerInterface;
- class Cache implements ICache {
- use MoveFromCacheTrait {
- MoveFromCacheTrait::moveFromCache as moveFromCacheFallback;
- }
-
- protected $partial = [];
-
- protected $storageId;
- private $storage;
-
- protected $storageCache;
-
- protected $mimetypeLoader;
-
- protected $connection;
-
- protected $eventDispatcher;
-
- protected $querySearchHelper;
-
- public function __construct(IStorage $storage) {
- $this->storageId = $storage->getId();
- $this->storage = $storage;
- if (strlen($this->storageId) > 64) {
- $this->storageId = md5($this->storageId);
- }
- $this->storageCache = new Storage($storage);
- $this->mimetypeLoader = \OC::$server->getMimeTypeLoader();
- $this->connection = \OC::$server->getDatabaseConnection();
- $this->eventDispatcher = \OC::$server->get(IEventDispatcher::class);
- $this->querySearchHelper = \OC::$server->query(QuerySearchHelper::class);
- }
- protected function getQueryBuilder() {
- return new CacheQueryBuilder(
- $this->connection,
- \OC::$server->getSystemConfig(),
- \OC::$server->get(LoggerInterface::class)
- );
- }
-
- public function getNumericStorageId() {
- return $this->storageCache->getNumericId();
- }
-
- public function get($file) {
- $query = $this->getQueryBuilder();
- $query->selectFileCache();
- if (is_string($file) || $file == '') {
-
- $file = $this->normalize($file);
- $query->whereStorageId($this->getNumericStorageId())
- ->wherePath($file);
- } else {
- $query->whereFileId($file);
- }
- $result = $query->execute();
- $data = $result->fetch();
- $result->closeCursor();
-
- if (!$data && is_string($file) && isset($this->partial[$file])) {
- return $this->partial[$file];
- } elseif (!$data) {
- return $data;
- } else {
- return self::cacheEntryFromData($data, $this->mimetypeLoader);
- }
- }
-
- public static function cacheEntryFromData($data, IMimeTypeLoader $mimetypeLoader) {
-
- $data['name'] = (string)$data['name'];
- $data['path'] = (string)$data['path'];
- $data['fileid'] = (int)$data['fileid'];
- $data['parent'] = (int)$data['parent'];
- $data['size'] = Util::numericToNumber($data['size']);
- $data['unencrypted_size'] = Util::numericToNumber($data['unencrypted_size'] ?? 0);
- $data['mtime'] = (int)$data['mtime'];
- $data['storage_mtime'] = (int)$data['storage_mtime'];
- $data['encryptedVersion'] = (int)$data['encrypted'];
- $data['encrypted'] = (bool)$data['encrypted'];
- $data['storage_id'] = $data['storage'];
- $data['storage'] = (int)$data['storage'];
- $data['mimetype'] = $mimetypeLoader->getMimetypeById($data['mimetype']);
- $data['mimepart'] = $mimetypeLoader->getMimetypeById($data['mimepart']);
- if ($data['storage_mtime'] == 0) {
- $data['storage_mtime'] = $data['mtime'];
- }
- $data['permissions'] = (int)$data['permissions'];
- if (isset($data['creation_time'])) {
- $data['creation_time'] = (int)$data['creation_time'];
- }
- if (isset($data['upload_time'])) {
- $data['upload_time'] = (int)$data['upload_time'];
- }
- return new CacheEntry($data);
- }
-
- public function getFolderContents($folder) {
- $fileId = $this->getId($folder);
- return $this->getFolderContentsById($fileId);
- }
-
- public function getFolderContentsById($fileId) {
- if ($fileId > -1) {
- $query = $this->getQueryBuilder();
- $query->selectFileCache()
- ->whereParent($fileId)
- ->orderBy('name', 'ASC');
- $result = $query->execute();
- $files = $result->fetchAll();
- $result->closeCursor();
- return array_map(function (array $data) {
- return self::cacheEntryFromData($data, $this->mimetypeLoader);
- }, $files);
- }
- return [];
- }
-
- public function put($file, array $data) {
- if (($id = $this->getId($file)) > -1) {
- $this->update($id, $data);
- return $id;
- } else {
- return $this->insert($file, $data);
- }
- }
-
- public function insert($file, array $data) {
-
- $file = $this->normalize($file);
- if (isset($this->partial[$file])) {
- $data = array_merge($this->partial[$file], $data);
- unset($this->partial[$file]);
- }
- $requiredFields = ['size', 'mtime', 'mimetype'];
- foreach ($requiredFields as $field) {
- if (!isset($data[$field])) {
- $this->partial[$file] = $data;
- return -1;
- }
- }
- $data['path'] = $file;
- if (!isset($data['parent'])) {
- $data['parent'] = $this->getParentId($file);
- }
- $data['name'] = basename($file);
- [$values, $extensionValues] = $this->normalizeData($data);
- $storageId = $this->getNumericStorageId();
- $values['storage'] = $storageId;
- try {
- $builder = $this->connection->getQueryBuilder();
- $builder->insert('filecache');
- foreach ($values as $column => $value) {
- $builder->setValue($column, $builder->createNamedParameter($value));
- }
- if ($builder->execute()) {
- $fileId = $builder->getLastInsertId();
- if (count($extensionValues)) {
- $query = $this->getQueryBuilder();
- $query->insert('filecache_extended');
- $query->setValue('fileid', $query->createNamedParameter($fileId, IQueryBuilder::PARAM_INT));
- foreach ($extensionValues as $column => $value) {
- $query->setValue($column, $query->createNamedParameter($value));
- }
- $query->execute();
- }
- $event = new CacheEntryInsertedEvent($this->storage, $file, $fileId, $storageId);
- $this->eventDispatcher->dispatch(CacheInsertEvent::class, $event);
- $this->eventDispatcher->dispatchTyped($event);
- return $fileId;
- }
- } catch (UniqueConstraintViolationException $e) {
-
- if ($this->connection->inTransaction()) {
- $this->connection->commit();
- $this->connection->beginTransaction();
- }
- }
-
- if (($id = $this->getId($file)) > -1) {
- $this->update($id, $data);
- return $id;
- } else {
- throw new \RuntimeException('File entry could not be inserted but could also not be selected with getId() in order to perform an update. Please try again.');
- }
- }
-
- public function update($id, array $data) {
- if (isset($data['path'])) {
-
- $data['path'] = $this->normalize($data['path']);
- }
- if (isset($data['name'])) {
-
- $data['name'] = $this->normalize($data['name']);
- }
- [$values, $extensionValues] = $this->normalizeData($data);
- if (count($values)) {
- $query = $this->getQueryBuilder();
- $query->update('filecache')
- ->whereFileId($id)
- ->andWhere($query->expr()->orX(...array_map(function ($key, $value) use ($query) {
- return $query->expr()->orX(
- $query->expr()->neq($key, $query->createNamedParameter($value)),
- $query->expr()->isNull($key)
- );
- }, array_keys($values), array_values($values))));
- foreach ($values as $key => $value) {
- $query->set($key, $query->createNamedParameter($value));
- }
- $query->execute();
- }
- if (count($extensionValues)) {
- try {
- $query = $this->getQueryBuilder();
- $query->insert('filecache_extended');
- $query->setValue('fileid', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT));
- foreach ($extensionValues as $column => $value) {
- $query->setValue($column, $query->createNamedParameter($value));
- }
- $query->execute();
- } catch (UniqueConstraintViolationException $e) {
- $query = $this->getQueryBuilder();
- $query->update('filecache_extended')
- ->whereFileId($id)
- ->andWhere($query->expr()->orX(...array_map(function ($key, $value) use ($query) {
- return $query->expr()->orX(
- $query->expr()->neq($key, $query->createNamedParameter($value)),
- $query->expr()->isNull($key)
- );
- }, array_keys($extensionValues), array_values($extensionValues))));
- foreach ($extensionValues as $key => $value) {
- $query->set($key, $query->createNamedParameter($value));
- }
- $query->execute();
- }
- }
- $path = $this->getPathById($id);
-
- if ($path !== null) {
- $event = new CacheEntryUpdatedEvent($this->storage, $path, $id, $this->getNumericStorageId());
- $this->eventDispatcher->dispatch(CacheUpdateEvent::class, $event);
- $this->eventDispatcher->dispatchTyped($event);
- }
- }
-
- protected function normalizeData(array $data): array {
- $fields = [
- 'path', 'parent', 'name', 'mimetype', 'size', 'mtime', 'storage_mtime', 'encrypted',
- 'etag', 'permissions', 'checksum', 'storage', 'unencrypted_size'];
- $extensionFields = ['metadata_etag', 'creation_time', 'upload_time'];
- $doNotCopyStorageMTime = false;
- if (array_key_exists('mtime', $data) && $data['mtime'] === null) {
-
- unset($data['mtime']);
- $doNotCopyStorageMTime = true;
- }
- $params = [];
- $extensionParams = [];
- foreach ($data as $name => $value) {
- if (array_search($name, $fields) !== false) {
- if ($name === 'path') {
- $params['path_hash'] = md5($value);
- } elseif ($name === 'mimetype') {
- $params['mimepart'] = $this->mimetypeLoader->getId(substr($value, 0, strpos($value, '/')));
- $value = $this->mimetypeLoader->getId($value);
- } elseif ($name === 'storage_mtime') {
- if (!$doNotCopyStorageMTime && !isset($data['mtime'])) {
- $params['mtime'] = $value;
- }
- } elseif ($name === 'encrypted') {
- if (isset($data['encryptedVersion'])) {
- $value = $data['encryptedVersion'];
- } else {
-
- $value = $value ? 1 : 0;
- }
- }
- $params[$name] = $value;
- }
- if (array_search($name, $extensionFields) !== false) {
- $extensionParams[$name] = $value;
- }
- }
- return [$params, array_filter($extensionParams)];
- }
-
- public function getId($file) {
-
- $file = $this->normalize($file);
- $query = $this->getQueryBuilder();
- $query->select('fileid')
- ->from('filecache')
- ->whereStorageId($this->getNumericStorageId())
- ->wherePath($file);
- $result = $query->execute();
- $id = $result->fetchOne();
- $result->closeCursor();
- return $id === false ? -1 : (int)$id;
- }
-
- public function getParentId($file) {
- if ($file === '') {
- return -1;
- } else {
- $parent = $this->getParentPath($file);
- return (int)$this->getId($parent);
- }
- }
- private function getParentPath($path) {
- $parent = dirname($path);
- if ($parent === '.') {
- $parent = '';
- }
- return $parent;
- }
-
- public function inCache($file) {
- return $this->getId($file) != -1;
- }
-
- public function remove($file) {
- $entry = $this->get($file);
- if ($entry instanceof ICacheEntry) {
- $query = $this->getQueryBuilder();
- $query->delete('filecache')
- ->whereFileId($entry->getId());
- $query->execute();
- $query = $this->getQueryBuilder();
- $query->delete('filecache_extended')
- ->whereFileId($entry->getId());
- $query->execute();
- if ($entry->getMimeType() == FileInfo::MIMETYPE_FOLDER) {
- $this->removeChildren($entry);
- }
- $this->eventDispatcher->dispatchTyped(new CacheEntryRemovedEvent($this->storage, $entry->getPath(), $entry->getId(), $this->getNumericStorageId()));
- }
- }
-
- private function getSubFolders(ICacheEntry $entry) {
- $children = $this->getFolderContentsById($entry->getId());
- return array_filter($children, function ($child) {
- return $child->getMimeType() == FileInfo::MIMETYPE_FOLDER;
- });
- }
-
- private function removeChildren(ICacheEntry $entry) {
- $parentIds = [$entry->getId()];
- $queue = [$entry->getId()];
- $deletedIds = [];
- $deletedPaths = [];
-
-
- while ($entryId = array_pop($queue)) {
- $children = $this->getFolderContentsById($entryId);
- $childIds = array_map(function (ICacheEntry $cacheEntry) {
- return $cacheEntry->getId();
- }, $children);
- $childPaths = array_map(function (ICacheEntry $cacheEntry) {
- return $cacheEntry->getPath();
- }, $children);
- $deletedIds = array_merge($deletedIds, $childIds);
- $deletedPaths = array_merge($deletedPaths, $childPaths);
- $query = $this->getQueryBuilder();
- $query->delete('filecache_extended')
- ->where($query->expr()->in('fileid', $query->createParameter('childIds')));
- foreach (array_chunk($childIds, 1000) as $childIdChunk) {
- $query->setParameter('childIds', $childIdChunk, IQueryBuilder::PARAM_INT_ARRAY);
- $query->execute();
- }
-
- $childFolders = array_filter($children, function ($child) {
- return $child->getMimeType() == FileInfo::MIMETYPE_FOLDER;
- });
- foreach ($childFolders as $folder) {
- $parentIds[] = $folder->getId();
- $queue[] = $folder->getId();
- }
- }
- $query = $this->getQueryBuilder();
- $query->delete('filecache')
- ->whereParentInParameter('parentIds');
- foreach (array_chunk($parentIds, 1000) as $parentIdChunk) {
- $query->setParameter('parentIds', $parentIdChunk, IQueryBuilder::PARAM_INT_ARRAY);
- $query->execute();
- }
- foreach (array_combine($deletedIds, $deletedPaths) as $fileId => $filePath) {
- $cacheEntryRemovedEvent = new CacheEntryRemovedEvent(
- $this->storage,
- $filePath,
- $fileId,
- $this->getNumericStorageId()
- );
- $this->eventDispatcher->dispatchTyped($cacheEntryRemovedEvent);
- }
- }
-
- public function move($source, $target) {
- $this->moveFromCache($this, $source, $target);
- }
-
- protected function getMoveInfo($path) {
- return [$this->getNumericStorageId(), $path];
- }
- protected function hasEncryptionWrapper(): bool {
- return $this->storage->instanceOfStorage(Encryption::class);
- }
-
- public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) {
- if ($sourceCache instanceof Cache) {
-
- $sourcePath = $this->normalize($sourcePath);
- $targetPath = $this->normalize($targetPath);
- $sourceData = $sourceCache->get($sourcePath);
- if ($sourceData === false) {
- throw new \Exception('Invalid source storage path: ' . $sourcePath);
- }
- $sourceId = $sourceData['fileid'];
- $newParentId = $this->getParentId($targetPath);
- [$sourceStorageId, $sourcePath] = $sourceCache->getMoveInfo($sourcePath);
- [$targetStorageId, $targetPath] = $this->getMoveInfo($targetPath);
- if (is_null($sourceStorageId) || $sourceStorageId === false) {
- throw new \Exception('Invalid source storage id: ' . $sourceStorageId);
- }
- if (is_null($targetStorageId) || $targetStorageId === false) {
- throw new \Exception('Invalid target storage id: ' . $targetStorageId);
- }
- $this->connection->beginTransaction();
- if ($sourceData['mimetype'] === 'httpd/unix-directory') {
-
- $sourceLength = mb_strlen($sourcePath);
- $query = $this->connection->getQueryBuilder();
- $fun = $query->func();
- $newPathFunction = $fun->concat(
- $query->createNamedParameter($targetPath),
- $fun->substring('path', $query->createNamedParameter($sourceLength + 1, IQueryBuilder::PARAM_INT))
- );
- $query->update('filecache')
- ->set('storage', $query->createNamedParameter($targetStorageId, IQueryBuilder::PARAM_INT))
- ->set('path_hash', $fun->md5($newPathFunction))
- ->set('path', $newPathFunction)
- ->where($query->expr()->eq('storage', $query->createNamedParameter($sourceStorageId, IQueryBuilder::PARAM_INT)))
- ->andWhere($query->expr()->like('path', $query->createNamedParameter($this->connection->escapeLikeParameter($sourcePath) . '/%')));
-
- if ($sourceCache->hasEncryptionWrapper() && !$this->hasEncryptionWrapper()) {
- $query->set('encrypted', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT));
- }
- try {
- $query->execute();
- } catch (\OC\DatabaseException $e) {
- $this->connection->rollBack();
- throw $e;
- }
- }
- $query = $this->getQueryBuilder();
- $query->update('filecache')
- ->set('storage', $query->createNamedParameter($targetStorageId))
- ->set('path', $query->createNamedParameter($targetPath))
- ->set('path_hash', $query->createNamedParameter(md5($targetPath)))
- ->set('name', $query->createNamedParameter(basename($targetPath)))
- ->set('parent', $query->createNamedParameter($newParentId, IQueryBuilder::PARAM_INT))
- ->whereFileId($sourceId);
-
- if ($sourceCache->hasEncryptionWrapper() && !$this->hasEncryptionWrapper()) {
- $query->set('encrypted', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT));
- }
- $query->execute();
- $this->connection->commit();
- if ($sourceCache->getNumericStorageId() !== $this->getNumericStorageId()) {
- $this->eventDispatcher->dispatchTyped(new CacheEntryRemovedEvent($this->storage, $sourcePath, $sourceId, $sourceCache->getNumericStorageId()));
- $event = new CacheEntryInsertedEvent($this->storage, $targetPath, $sourceId, $this->getNumericStorageId());
- $this->eventDispatcher->dispatch(CacheInsertEvent::class, $event);
- $this->eventDispatcher->dispatchTyped($event);
- } else {
- $event = new CacheEntryUpdatedEvent($this->storage, $targetPath, $sourceId, $this->getNumericStorageId());
- $this->eventDispatcher->dispatch(CacheUpdateEvent::class, $event);
- $this->eventDispatcher->dispatchTyped($event);
- }
- } else {
- $this->moveFromCacheFallback($sourceCache, $sourcePath, $targetPath);
- }
- }
-
- public function clear() {
- $query = $this->getQueryBuilder();
- $query->delete('filecache')
- ->whereStorageId($this->getNumericStorageId());
- $query->execute();
- $query = $this->connection->getQueryBuilder();
- $query->delete('storages')
- ->where($query->expr()->eq('id', $query->createNamedParameter($this->storageId)));
- $query->execute();
- }
-
- public function getStatus($file) {
-
- $file = $this->normalize($file);
- $query = $this->getQueryBuilder();
- $query->select('size')
- ->from('filecache')
- ->whereStorageId($this->getNumericStorageId())
- ->wherePath($file);
- $result = $query->execute();
- $size = $result->fetchOne();
- $result->closeCursor();
- if ($size !== false) {
- if ((int)$size === -1) {
- return self::SHALLOW;
- } else {
- return self::COMPLETE;
- }
- } else {
- if (isset($this->partial[$file])) {
- return self::PARTIAL;
- } else {
- return self::NOT_FOUND;
- }
- }
- }
-
- public function search($pattern) {
- $operator = new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', $pattern);
- return $this->searchQuery(new SearchQuery($operator, 0, 0, [], null));
- }
-
- public function searchByMime($mimetype) {
- if (strpos($mimetype, '/') === false) {
- $operator = new SearchComparison(ISearchComparison::COMPARE_LIKE, 'mimetype', $mimetype . '/%');
- } else {
- $operator = new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mimetype', $mimetype);
- }
- return $this->searchQuery(new SearchQuery($operator, 0, 0, [], null));
- }
- public function searchQuery(ISearchQuery $searchQuery) {
- return current($this->querySearchHelper->searchInCaches($searchQuery, [$this]));
- }
-
- public function correctFolderSize($path, $data = null, $isBackgroundScan = false) {
- $this->calculateFolderSize($path, $data);
- if ($path !== '') {
- $parent = dirname($path);
- if ($parent === '.' || $parent === '/') {
- $parent = '';
- }
- if ($isBackgroundScan) {
- $parentData = $this->get($parent);
- if ($parentData['size'] !== -1 && $this->getIncompleteChildrenCount($parentData['fileid']) === 0) {
- $this->correctFolderSize($parent, $parentData, $isBackgroundScan);
- }
- } else {
- $this->correctFolderSize($parent);
- }
- }
- }
-
- public function getIncompleteChildrenCount($fileId) {
- if ($fileId > -1) {
- $query = $this->getQueryBuilder();
- $query->select($query->func()->count())
- ->from('filecache')
- ->whereParent($fileId)
- ->andWhere($query->expr()->lt('size', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT)));
- $result = $query->execute();
- $size = (int)$result->fetchOne();
- $result->closeCursor();
- return $size;
- }
- return -1;
- }
-
- public function calculateFolderSize($path, $entry = null) {
- return $this->calculateFolderSizeInner($path, $entry);
- }
-
- protected function calculateFolderSizeInner(string $path, $entry = null, bool $ignoreUnknown = false) {
- $totalSize = 0;
- if (is_null($entry) || !isset($entry['fileid'])) {
- $entry = $this->get($path);
- }
- if (isset($entry['mimetype']) && $entry['mimetype'] === FileInfo::MIMETYPE_FOLDER) {
- $id = $entry['fileid'];
- $query = $this->getQueryBuilder();
- $query->select('size', 'unencrypted_size')
- ->from('filecache')
- ->whereParent($id);
- if ($ignoreUnknown) {
- $query->andWhere($query->expr()->gte('size', $query->createNamedParameter(0)));
- }
- $result = $query->execute();
- $rows = $result->fetchAll();
- $result->closeCursor();
- if ($rows) {
- $sizes = array_map(function (array $row) {
- return Util::numericToNumber($row['size']);
- }, $rows);
- $unencryptedOnlySizes = array_map(function (array $row) {
- return Util::numericToNumber($row['unencrypted_size']);
- }, $rows);
- $unencryptedSizes = array_map(function (array $row) {
- return Util::numericToNumber(($row['unencrypted_size'] > 0) ? $row['unencrypted_size'] : $row['size']);
- }, $rows);
- $sum = array_sum($sizes);
- $min = min($sizes);
- $unencryptedSum = array_sum($unencryptedSizes);
- $unencryptedMin = min($unencryptedSizes);
- $unencryptedMax = max($unencryptedOnlySizes);
- $sum = 0 + $sum;
- $min = 0 + $min;
- if ($min === -1) {
- $totalSize = $min;
- } else {
- $totalSize = $sum;
- }
- if ($unencryptedMin === -1 || $min === -1) {
- $unencryptedTotal = $unencryptedMin;
- } else {
- $unencryptedTotal = $unencryptedSum;
- }
- } else {
- $totalSize = 0;
- $unencryptedTotal = 0;
- $unencryptedMax = 0;
- }
-
- $shouldWriteUnEncryptedSize = $unencryptedMax > 0 || $totalSize === 0 || $entry['unencrypted_size'] > 0;
- if ($entry['size'] !== $totalSize || ($entry['unencrypted_size'] !== $unencryptedTotal && $shouldWriteUnEncryptedSize)) {
- if ($shouldWriteUnEncryptedSize) {
-
- if ($unencryptedMax === 0) {
- $unencryptedTotal = 0;
- }
- $this->update($id, [
- 'size' => $totalSize,
- 'unencrypted_size' => $unencryptedTotal,
- ]);
- } else {
- $this->update($id, [
- 'size' => $totalSize,
- ]);
- }
- }
- }
- return $totalSize;
- }
-
- public function getAll() {
- $query = $this->getQueryBuilder();
- $query->select('fileid')
- ->from('filecache')
- ->whereStorageId($this->getNumericStorageId());
- $result = $query->execute();
- $files = $result->fetchAll(\PDO::FETCH_COLUMN);
- $result->closeCursor();
- return array_map(function ($id) {
- return (int)$id;
- }, $files);
- }
-
- public function getIncomplete() {
- $query = $this->getQueryBuilder();
- $query->select('path')
- ->from('filecache')
- ->whereStorageId($this->getNumericStorageId())
- ->andWhere($query->expr()->lt('size', $query->createNamedParameter(0, IQueryBuilder::PARAM_INT)))
- ->orderBy('fileid', 'DESC')
- ->setMaxResults(1);
- $result = $query->execute();
- $path = $result->fetchOne();
- $result->closeCursor();
- if ($path === false) {
- return false;
- }
-
- return (string)$path;
- }
-
- public function getPathById($id) {
- $query = $this->getQueryBuilder();
- $query->select('path')
- ->from('filecache')
- ->whereStorageId($this->getNumericStorageId())
- ->whereFileId($id);
- $result = $query->execute();
- $path = $result->fetchOne();
- $result->closeCursor();
- if ($path === false) {
- return null;
- }
- return (string)$path;
- }
-
- public static function getById($id) {
- $query = \OC::$server->getDatabaseConnection()->getQueryBuilder();
- $query->select('path', 'storage')
- ->from('filecache')
- ->where($query->expr()->eq('fileid', $query->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
- $result = $query->execute();
- $row = $result->fetch();
- $result->closeCursor();
- if ($row) {
- $numericId = $row['storage'];
- $path = $row['path'];
- } else {
- return null;
- }
- if ($id = Storage::getStorageId($numericId)) {
- return [$id, $path];
- } else {
- return null;
- }
- }
-
- public function normalize($path) {
- return trim(\OC_Util::normalizeUnicode($path), '/');
- }
-
- 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);
-
- if ($sourceCache instanceof Cache && $sourceCache->hasEncryptionWrapper() && !$this->hasEncryptionWrapper()) {
- $data['encrypted'] = 0;
- }
- $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);
- }
- }
- 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(),
- ];
- }
- public function getQueryFilterForStorage(): ISearchOperator {
- return new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'storage', $this->getNumericStorageId());
- }
- public function getCacheEntryFromSearchResult(ICacheEntry $rawEntry): ?ICacheEntry {
- if ($rawEntry->getStorageId() === $this->getNumericStorageId()) {
- return $rawEntry;
- } else {
- return null;
- }
- }
- }
|