nextcloud/lib/private/Files/Config/UserMountCache.php

323 lines
10 KiB
PHP
Raw Normal View History

2015-11-26 19:47:53 +03:00
<?php
/**
* @author Joas Schilling <nickvergessen@owncloud.com>
2015-11-26 19:47:53 +03:00
* @author Robin Appelman <icewind@owncloud.com>
2016-05-26 20:56:05 +03:00
* @author Vincent Petry <pvince81@owncloud.com>
2015-11-26 19:47:53 +03:00
*
* @copyright Copyright (c) 2016, ownCloud, Inc.
2015-11-26 19:47:53 +03:00
* @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\Config;
2015-12-01 14:41:48 +03:00
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use OC\Files\Filesystem;
2016-03-08 13:23:20 +03:00
use OCA\Files_Sharing\SharedMount;
2016-01-26 16:50:57 +03:00
use OCP\DB\QueryBuilder\IQueryBuilder;
2015-11-26 19:47:53 +03:00
use OCP\Files\Config\ICachedMountInfo;
use OCP\Files\Config\IUserMountCache;
use OCP\Files\Mount\IMountPoint;
use OCP\Files\NotFoundException;
2015-11-26 19:47:53 +03:00
use OCP\ICache;
use OCP\IDBConnection;
2015-12-01 14:41:48 +03:00
use OCP\ILogger;
2015-11-26 19:47:53 +03:00
use OCP\IUser;
use OCP\IUserManager;
use OC\Cache\CappedMemoryCache;
2015-11-26 19:47:53 +03:00
2015-12-02 15:36:33 +03:00
/**
* Cache mounts points per user in the cache so we can easilly look them up
*/
2015-11-26 19:47:53 +03:00
class UserMountCache implements IUserMountCache {
/**
* @var IDBConnection
*/
private $connection;
/**
* @var IUserManager
*/
private $userManager;
/**
* Cached mount info.
* Map of $userId to ICachedMountInfo.
*
* @var ICache
**/
private $mountsForUsers;
2015-11-26 19:47:53 +03:00
2015-12-01 14:41:48 +03:00
/**
* @var ILogger
*/
private $logger;
/**
* @var ICache
*/
private $cacheInfoCache;
2015-11-26 19:47:53 +03:00
/**
* UserMountCache constructor.
*
* @param IDBConnection $connection
* @param IUserManager $userManager
2015-12-01 14:41:48 +03:00
* @param ILogger $logger
2015-11-26 19:47:53 +03:00
*/
2015-12-01 14:41:48 +03:00
public function __construct(IDBConnection $connection, IUserManager $userManager, ILogger $logger) {
2015-11-26 19:47:53 +03:00
$this->connection = $connection;
$this->userManager = $userManager;
2015-12-01 14:41:48 +03:00
$this->logger = $logger;
$this->cacheInfoCache = new CappedMemoryCache();
$this->mountsForUsers = new CappedMemoryCache();
2015-11-26 19:47:53 +03:00
}
public function registerMounts(IUser $user, array $mounts) {
2015-12-02 15:36:33 +03:00
// filter out non-proper storages coming from unit tests
2015-11-30 19:29:42 +03:00
$mounts = array_filter($mounts, function (IMountPoint $mount) {
2016-03-08 13:23:20 +03:00
return $mount instanceof SharedMount || $mount->getStorage() && $mount->getStorage()->getCache();
2015-11-30 19:29:42 +03:00
});
2015-11-26 19:47:53 +03:00
/** @var ICachedMountInfo[] $newMounts */
$newMounts = array_map(function (IMountPoint $mount) use ($user) {
2015-12-02 15:36:33 +03:00
// filter out any storages which aren't scanned yet since we aren't interested in files from those storages (yet)
if ($mount->getStorageRootId() === -1) {
2015-11-30 19:29:42 +03:00
return null;
} else {
return new LazyStorageMountInfo($user, $mount);
2015-11-30 19:29:42 +03:00
}
2015-11-26 19:47:53 +03:00
}, $mounts);
2015-11-30 19:29:42 +03:00
$newMounts = array_values(array_filter($newMounts));
2015-11-26 19:47:53 +03:00
2015-12-02 15:36:33 +03:00
$cachedMounts = $this->getMountsForUser($user);
2015-11-26 19:47:53 +03:00
$mountDiff = function (ICachedMountInfo $mount1, ICachedMountInfo $mount2) {
2015-12-02 15:36:33 +03:00
// since we are only looking for mounts for a specific user comparing on root id is enough
2015-11-26 19:47:53 +03:00
return $mount1->getRootId() - $mount2->getRootId();
};
/** @var ICachedMountInfo[] $addedMounts */
$addedMounts = array_udiff($newMounts, $cachedMounts, $mountDiff);
/** @var ICachedMountInfo[] $removedMounts */
$removedMounts = array_udiff($cachedMounts, $newMounts, $mountDiff);
$changedMounts = $this->findChangedMounts($newMounts, $cachedMounts);
2016-01-11 15:06:10 +03:00
2015-11-26 19:47:53 +03:00
foreach ($addedMounts as $mount) {
$this->addToCache($mount);
$this->mountsForUsers[$user->getUID()][] = $mount;
}
foreach ($removedMounts as $mount) {
$this->removeFromCache($mount);
2015-12-02 15:36:33 +03:00
$index = array_search($mount, $this->mountsForUsers[$user->getUID()]);
unset($this->mountsForUsers[$user->getUID()][$index]);
2015-11-26 19:47:53 +03:00
}
2016-01-11 15:06:10 +03:00
foreach ($changedMounts as $mount) {
$this->updateCachedMount($mount);
}
}
/**
* @param ICachedMountInfo[] $newMounts
* @param ICachedMountInfo[] $cachedMounts
* @return ICachedMountInfo[]
*/
private function findChangedMounts(array $newMounts, array $cachedMounts) {
$changed = [];
foreach ($newMounts as $newMount) {
foreach ($cachedMounts as $cachedMount) {
if (
$newMount->getRootId() === $cachedMount->getRootId() &&
($newMount->getMountPoint() !== $cachedMount->getMountPoint() || $newMount->getMountId() !== $cachedMount->getMountId())
) {
$changed[] = $newMount;
}
}
2016-01-11 15:06:10 +03:00
}
return $changed;
2015-11-26 19:47:53 +03:00
}
private function addToCache(ICachedMountInfo $mount) {
if ($mount->getStorageId() !== -1) {
$this->connection->insertIfNotExist('*PREFIX*mounts', [
'storage_id' => $mount->getStorageId(),
'root_id' => $mount->getRootId(),
'user_id' => $mount->getUser()->getUID(),
'mount_point' => $mount->getMountPoint(),
'mount_id' => $mount->getMountId()
], ['root_id', 'user_id']);
} else {
$this->logger->error('Error getting storage info for mount at ' . $mount->getMountPoint());
}
2015-11-26 19:47:53 +03:00
}
private function updateCachedMount(ICachedMountInfo $mount) {
2016-01-11 15:06:10 +03:00
$builder = $this->connection->getQueryBuilder();
$query = $builder->update('mounts')
->set('mount_point', $builder->createNamedParameter($mount->getMountPoint()))
->set('mount_id', $builder->createNamedParameter($mount->getMountId(), IQueryBuilder::PARAM_INT))
2016-01-11 15:06:10 +03:00
->where($builder->expr()->eq('user_id', $builder->createNamedParameter($mount->getUser()->getUID())))
2016-01-26 16:50:57 +03:00
->andWhere($builder->expr()->eq('root_id', $builder->createNamedParameter($mount->getRootId(), IQueryBuilder::PARAM_INT)));
2016-01-11 15:06:10 +03:00
$query->execute();
}
2015-11-26 19:47:53 +03:00
private function removeFromCache(ICachedMountInfo $mount) {
$builder = $this->connection->getQueryBuilder();
$query = $builder->delete('mounts')
->where($builder->expr()->eq('user_id', $builder->createNamedParameter($mount->getUser()->getUID())))
2016-01-26 16:50:57 +03:00
->andWhere($builder->expr()->eq('root_id', $builder->createNamedParameter($mount->getRootId(), IQueryBuilder::PARAM_INT)));
2015-11-26 19:47:53 +03:00
$query->execute();
}
private function dbRowToMountInfo(array $row) {
$user = $this->userManager->get($row['user_id']);
return new CachedMountInfo($user, (int)$row['storage_id'], (int)$row['root_id'], $row['mount_point'], $row['mount_id']);
2015-11-26 19:47:53 +03:00
}
/**
* @param IUser $user
* @return ICachedMountInfo[]
*/
public function getMountsForUser(IUser $user) {
2015-12-02 15:36:33 +03:00
if (!isset($this->mountsForUsers[$user->getUID()])) {
$builder = $this->connection->getQueryBuilder();
$query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id')
2015-12-02 15:36:33 +03:00
->from('mounts')
->where($builder->expr()->eq('user_id', $builder->createPositionalParameter($user->getUID())));
2015-11-26 19:47:53 +03:00
2015-12-02 15:36:33 +03:00
$rows = $query->execute()->fetchAll();
2015-11-26 19:47:53 +03:00
2015-12-02 15:36:33 +03:00
$this->mountsForUsers[$user->getUID()] = array_map([$this, 'dbRowToMountInfo'], $rows);
}
return $this->mountsForUsers[$user->getUID()];
2015-11-26 19:47:53 +03:00
}
/**
* @param int $numericStorageId
* @return CachedMountInfo[]
*/
public function getMountsForStorageId($numericStorageId) {
$builder = $this->connection->getQueryBuilder();
$query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id')
2015-11-26 19:47:53 +03:00
->from('mounts')
2016-01-26 16:50:57 +03:00
->where($builder->expr()->eq('storage_id', $builder->createPositionalParameter($numericStorageId, IQueryBuilder::PARAM_INT)));
2015-11-26 19:47:53 +03:00
$rows = $query->execute()->fetchAll();
return array_map([$this, 'dbRowToMountInfo'], $rows);
}
/**
* @param int $rootFileId
* @return CachedMountInfo[]
*/
public function getMountsForRootId($rootFileId) {
$builder = $this->connection->getQueryBuilder();
$query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id')
2015-11-26 19:47:53 +03:00
->from('mounts')
2016-01-26 16:50:57 +03:00
->where($builder->expr()->eq('root_id', $builder->createPositionalParameter($rootFileId, IQueryBuilder::PARAM_INT)));
2015-11-26 19:47:53 +03:00
$rows = $query->execute()->fetchAll();
return array_map([$this, 'dbRowToMountInfo'], $rows);
}
2015-12-03 16:10:05 +03:00
/**
* @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')
2016-01-26 16:50:57 +03:00
->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 . '/';
});
}
2015-12-03 16:10:05 +03:00
/**
* Remove all cached mounts for a user
*
* @param IUser $user
*/
public function removeUserMounts(IUser $user) {
$builder = $this->connection->getQueryBuilder();
$query = $builder->delete('mounts')
2016-01-11 15:06:10 +03:00
->where($builder->expr()->eq('user_id', $builder->createNamedParameter($user->getUID())));
2015-12-03 16:10:05 +03:00
$query->execute();
}
public function removeUserStorageMount($storageId, $userId) {
$builder = $this->connection->getQueryBuilder();
$query = $builder->delete('mounts')
->where($builder->expr()->eq('user_id', $builder->createNamedParameter($userId)))
2016-01-26 16:50:57 +03:00
->andWhere($builder->expr()->eq('storage_id', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)));
$query->execute();
}
public function remoteStorageMounts($storageId) {
$builder = $this->connection->getQueryBuilder();
$query = $builder->delete('mounts')
2016-01-26 16:50:57 +03:00
->where($builder->expr()->eq('storage_id', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)));
$query->execute();
}
2015-11-26 19:47:53 +03:00
}