diff options
author | Thomas Müller <thomas.mueller@tmit.eu> | 2016-01-28 11:20:42 +0100 |
---|---|---|
committer | Thomas Müller <thomas.mueller@tmit.eu> | 2016-01-28 11:20:42 +0100 |
commit | b7710ab6d7db3aa54b1491d2968bc1b0e3a742c4 (patch) | |
tree | a4f06c10ded24170a70372895ad2b4aa5e02bcc4 /lib | |
parent | dda9f31455e55088d7c5148765f2cab06234dd48 (diff) | |
parent | 2563c3ffafb225742c8bf559bbcf567e878d93df (diff) | |
download | nextcloud-server-b7710ab6d7db3aa54b1491d2968bc1b0e3a742c4.tar.gz nextcloud-server-b7710ab6d7db3aa54b1491d2968bc1b0e3a742c4.zip |
Merge pull request #21858 from owncloud/getMountsForFileId
add IUserMountCache->getMountsForFileId
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/files/config/usermountcache.php | 71 | ||||
-rw-r--r-- | lib/public/files/config/iusermountcache.php | 15 |
2 files changed, 80 insertions, 6 deletions
diff --git a/lib/private/files/config/usermountcache.php b/lib/private/files/config/usermountcache.php index e3a494e93a1..7d7b03fbc06 100644 --- a/lib/private/files/config/usermountcache.php +++ b/lib/private/files/config/usermountcache.php @@ -22,9 +22,12 @@ namespace OC\Files\Config; use Doctrine\DBAL\Exception\UniqueConstraintViolationException; +use OC\Files\Filesystem; +use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\Files\Config\ICachedMountInfo; use OCP\Files\Config\IUserMountCache; use OCP\Files\Mount\IMountPoint; +use OCP\Files\NotFoundException; use OCP\ICache; use OCP\IDBConnection; use OCP\ILogger; @@ -53,6 +56,8 @@ class UserMountCache implements IUserMountCache { */ private $logger; + private $cacheInfoCache = []; + /** * UserMountCache constructor. * @@ -133,7 +138,7 @@ class UserMountCache implements IUserMountCache { $query = $builder->update('mounts') ->set('mount_point', $builder->createNamedParameter($mount->getMountPoint())) ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($mount->getUser()->getUID()))) - ->andWhere($builder->expr()->eq('root_id', $builder->createNamedParameter($mount->getRootId(), \PDO::PARAM_INT))); + ->andWhere($builder->expr()->eq('root_id', $builder->createNamedParameter($mount->getRootId(), IQueryBuilder::PARAM_INT))); $query->execute(); } @@ -143,7 +148,7 @@ class UserMountCache implements IUserMountCache { $query = $builder->delete('mounts') ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($mount->getUser()->getUID()))) - ->andWhere($builder->expr()->eq('root_id', $builder->createNamedParameter($mount->getRootId(), \PDO::PARAM_INT))); + ->andWhere($builder->expr()->eq('root_id', $builder->createNamedParameter($mount->getRootId(), IQueryBuilder::PARAM_INT))); $query->execute(); } @@ -178,7 +183,7 @@ class UserMountCache implements IUserMountCache { $builder = $this->connection->getQueryBuilder(); $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point') ->from('mounts') - ->where($builder->expr()->eq('storage_id', $builder->createPositionalParameter($numericStorageId, \PDO::PARAM_INT))); + ->where($builder->expr()->eq('storage_id', $builder->createPositionalParameter($numericStorageId, IQueryBuilder::PARAM_INT))); $rows = $query->execute()->fetchAll(); @@ -193,7 +198,7 @@ class UserMountCache implements IUserMountCache { $builder = $this->connection->getQueryBuilder(); $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point') ->from('mounts') - ->where($builder->expr()->eq('root_id', $builder->createPositionalParameter($rootFileId, \PDO::PARAM_INT))); + ->where($builder->expr()->eq('root_id', $builder->createPositionalParameter($rootFileId, IQueryBuilder::PARAM_INT))); $rows = $query->execute()->fetchAll(); @@ -201,6 +206,60 @@ class UserMountCache implements IUserMountCache { } /** + * @param $fileId + * @return array + * @throws \OCP\Files\NotFoundException + */ + private function getCacheInfoFromFileId($fileId) { + if (!isset($this->cacheInfoCache[$fileId])) { + $builder = $this->connection->getQueryBuilder(); + $query = $builder->select('storage', 'path') + ->from('filecache') + ->where($builder->expr()->eq('fileid', $builder->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))); + + $row = $query->execute()->fetch(); + if (is_array($row)) { + $this->cacheInfoCache[$fileId] = [ + (int)$row['storage'], + $row['path'] + ]; + } else { + throw new NotFoundException('File with id "' . $fileId . '" not found'); + } + } + return $this->cacheInfoCache[$fileId]; + } + + /** + * @param int $fileId + * @return ICachedMountInfo[] + * @since 9.0.0 + */ + public function getMountsForFileId($fileId) { + try { + list($storageId, $internalPath) = $this->getCacheInfoFromFileId($fileId); + } catch (NotFoundException $e) { + return []; + } + $mountsForStorage = $this->getMountsForStorageId($storageId); + + // filter mounts that are from the same storage but a different directory + return array_filter($mountsForStorage, function (ICachedMountInfo $mount) use ($internalPath, $fileId) { + if ($fileId === $mount->getRootId()) { + return true; + } + try { + list(, $internalMountPath) = $this->getCacheInfoFromFileId($mount->getRootId()); + } catch (NotFoundException $e) { + return false; + } + + return $internalMountPath === '' || substr($internalPath, 0, strlen($internalMountPath) + 1) === $internalMountPath . '/'; + }); + + } + + /** * Remove all cached mounts for a user * * @param IUser $user @@ -218,7 +277,7 @@ class UserMountCache implements IUserMountCache { $query = $builder->delete('mounts') ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($userId))) - ->andWhere($builder->expr()->eq('storage_id', $builder->createNamedParameter($storageId, \PDO::PARAM_INT))); + ->andWhere($builder->expr()->eq('storage_id', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT))); $query->execute(); } @@ -226,7 +285,7 @@ class UserMountCache implements IUserMountCache { $builder = $this->connection->getQueryBuilder(); $query = $builder->delete('mounts') - ->where($builder->expr()->eq('storage_id', $builder->createNamedParameter($storageId, \PDO::PARAM_INT))); + ->where($builder->expr()->eq('storage_id', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT))); $query->execute(); } } diff --git a/lib/public/files/config/iusermountcache.php b/lib/public/files/config/iusermountcache.php index f722ad16310..77f58cd8670 100644 --- a/lib/public/files/config/iusermountcache.php +++ b/lib/public/files/config/iusermountcache.php @@ -40,6 +40,8 @@ interface IUserMountCache { public function registerMounts(IUser $user, array $mounts); /** + * Get all cached mounts for a user + * * @param IUser $user * @return ICachedMountInfo[] * @since 9.0.0 @@ -47,6 +49,8 @@ interface IUserMountCache { public function getMountsForUser(IUser $user); /** + * Get all cached mounts by storage + * * @param int $numericStorageId * @return ICachedMountInfo[] * @since 9.0.0 @@ -54,6 +58,8 @@ interface IUserMountCache { public function getMountsForStorageId($numericStorageId); /** + * Get all cached mounts by root + * * @param int $rootFileId * @return ICachedMountInfo[] * @since 9.0.0 @@ -61,6 +67,15 @@ interface IUserMountCache { public function getMountsForRootId($rootFileId); /** + * Get all cached mounts that contain a file + * + * @param int $fileId + * @return ICachedMountInfo[] + * @since 9.0.0 + */ + public function getMountsForFileId($fileId); + + /** * Remove all cached mounts for a user * * @param IUser $user |