@@ -43,285 +43,285 @@ |
||
| 43 | 43 | * Cache mounts points per user in the cache so we can easilly look them up |
| 44 | 44 | */ |
| 45 | 45 | class UserMountCache implements IUserMountCache { |
| 46 | - /** |
|
| 47 | - * @var IDBConnection |
|
| 48 | - */ |
|
| 49 | - private $connection; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * @var IUserManager |
|
| 53 | - */ |
|
| 54 | - private $userManager; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * Cached mount info. |
|
| 58 | - * Map of $userId to ICachedMountInfo. |
|
| 59 | - * |
|
| 60 | - * @var ICache |
|
| 61 | - **/ |
|
| 62 | - private $mountsForUsers; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * @var ILogger |
|
| 66 | - */ |
|
| 67 | - private $logger; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * @var ICache |
|
| 71 | - */ |
|
| 72 | - private $cacheInfoCache; |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * UserMountCache constructor. |
|
| 76 | - * |
|
| 77 | - * @param IDBConnection $connection |
|
| 78 | - * @param IUserManager $userManager |
|
| 79 | - * @param ILogger $logger |
|
| 80 | - */ |
|
| 81 | - public function __construct(IDBConnection $connection, IUserManager $userManager, ILogger $logger) { |
|
| 82 | - $this->connection = $connection; |
|
| 83 | - $this->userManager = $userManager; |
|
| 84 | - $this->logger = $logger; |
|
| 85 | - $this->cacheInfoCache = new CappedMemoryCache(); |
|
| 86 | - $this->mountsForUsers = new CappedMemoryCache(); |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - public function registerMounts(IUser $user, array $mounts) { |
|
| 90 | - // filter out non-proper storages coming from unit tests |
|
| 91 | - $mounts = array_filter($mounts, function (IMountPoint $mount) { |
|
| 92 | - return $mount instanceof SharedMount || $mount->getStorage() && $mount->getStorage()->getCache(); |
|
| 93 | - }); |
|
| 94 | - /** @var ICachedMountInfo[] $newMounts */ |
|
| 95 | - $newMounts = array_map(function (IMountPoint $mount) use ($user) { |
|
| 96 | - // filter out any storages which aren't scanned yet since we aren't interested in files from those storages (yet) |
|
| 97 | - if ($mount->getStorageRootId() === -1) { |
|
| 98 | - return null; |
|
| 99 | - } else { |
|
| 100 | - return new LazyStorageMountInfo($user, $mount); |
|
| 101 | - } |
|
| 102 | - }, $mounts); |
|
| 103 | - $newMounts = array_values(array_filter($newMounts)); |
|
| 104 | - |
|
| 105 | - $cachedMounts = $this->getMountsForUser($user); |
|
| 106 | - $mountDiff = function (ICachedMountInfo $mount1, ICachedMountInfo $mount2) { |
|
| 107 | - // since we are only looking for mounts for a specific user comparing on root id is enough |
|
| 108 | - return $mount1->getRootId() - $mount2->getRootId(); |
|
| 109 | - }; |
|
| 110 | - |
|
| 111 | - /** @var ICachedMountInfo[] $addedMounts */ |
|
| 112 | - $addedMounts = array_udiff($newMounts, $cachedMounts, $mountDiff); |
|
| 113 | - /** @var ICachedMountInfo[] $removedMounts */ |
|
| 114 | - $removedMounts = array_udiff($cachedMounts, $newMounts, $mountDiff); |
|
| 115 | - |
|
| 116 | - $changedMounts = $this->findChangedMounts($newMounts, $cachedMounts); |
|
| 117 | - |
|
| 118 | - foreach ($addedMounts as $mount) { |
|
| 119 | - $this->addToCache($mount); |
|
| 120 | - $this->mountsForUsers[$user->getUID()][] = $mount; |
|
| 121 | - } |
|
| 122 | - foreach ($removedMounts as $mount) { |
|
| 123 | - $this->removeFromCache($mount); |
|
| 124 | - $index = array_search($mount, $this->mountsForUsers[$user->getUID()]); |
|
| 125 | - unset($this->mountsForUsers[$user->getUID()][$index]); |
|
| 126 | - } |
|
| 127 | - foreach ($changedMounts as $mount) { |
|
| 128 | - $this->updateCachedMount($mount); |
|
| 129 | - } |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * @param ICachedMountInfo[] $newMounts |
|
| 134 | - * @param ICachedMountInfo[] $cachedMounts |
|
| 135 | - * @return ICachedMountInfo[] |
|
| 136 | - */ |
|
| 137 | - private function findChangedMounts(array $newMounts, array $cachedMounts) { |
|
| 138 | - $changed = []; |
|
| 139 | - foreach ($newMounts as $newMount) { |
|
| 140 | - foreach ($cachedMounts as $cachedMount) { |
|
| 141 | - if ( |
|
| 142 | - $newMount->getRootId() === $cachedMount->getRootId() && |
|
| 143 | - ($newMount->getMountPoint() !== $cachedMount->getMountPoint() || $newMount->getMountId() !== $cachedMount->getMountId()) |
|
| 144 | - ) { |
|
| 145 | - $changed[] = $newMount; |
|
| 146 | - } |
|
| 147 | - } |
|
| 148 | - } |
|
| 149 | - return $changed; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - private function addToCache(ICachedMountInfo $mount) { |
|
| 153 | - if ($mount->getStorageId() !== -1) { |
|
| 154 | - $this->connection->insertIfNotExist('*PREFIX*mounts', [ |
|
| 155 | - 'storage_id' => $mount->getStorageId(), |
|
| 156 | - 'root_id' => $mount->getRootId(), |
|
| 157 | - 'user_id' => $mount->getUser()->getUID(), |
|
| 158 | - 'mount_point' => $mount->getMountPoint(), |
|
| 159 | - 'mount_id' => $mount->getMountId() |
|
| 160 | - ], ['root_id', 'user_id']); |
|
| 161 | - } else { |
|
| 162 | - // in some cases this is legitimate, like orphaned shares |
|
| 163 | - $this->logger->debug('Could not get storage info for mount at ' . $mount->getMountPoint()); |
|
| 164 | - } |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - private function updateCachedMount(ICachedMountInfo $mount) { |
|
| 168 | - $builder = $this->connection->getQueryBuilder(); |
|
| 169 | - |
|
| 170 | - $query = $builder->update('mounts') |
|
| 171 | - ->set('mount_point', $builder->createNamedParameter($mount->getMountPoint())) |
|
| 172 | - ->set('mount_id', $builder->createNamedParameter($mount->getMountId(), IQueryBuilder::PARAM_INT)) |
|
| 173 | - ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($mount->getUser()->getUID()))) |
|
| 174 | - ->andWhere($builder->expr()->eq('root_id', $builder->createNamedParameter($mount->getRootId(), IQueryBuilder::PARAM_INT))); |
|
| 175 | - |
|
| 176 | - $query->execute(); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - private function removeFromCache(ICachedMountInfo $mount) { |
|
| 180 | - $builder = $this->connection->getQueryBuilder(); |
|
| 181 | - |
|
| 182 | - $query = $builder->delete('mounts') |
|
| 183 | - ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($mount->getUser()->getUID()))) |
|
| 184 | - ->andWhere($builder->expr()->eq('root_id', $builder->createNamedParameter($mount->getRootId(), IQueryBuilder::PARAM_INT))); |
|
| 185 | - $query->execute(); |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - private function dbRowToMountInfo(array $row) { |
|
| 189 | - $user = $this->userManager->get($row['user_id']); |
|
| 190 | - if (is_null($user)) { |
|
| 191 | - return null; |
|
| 192 | - } |
|
| 193 | - return new CachedMountInfo($user, (int)$row['storage_id'], (int)$row['root_id'], $row['mount_point'], $row['mount_id']); |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - /** |
|
| 197 | - * @param IUser $user |
|
| 198 | - * @return ICachedMountInfo[] |
|
| 199 | - */ |
|
| 200 | - public function getMountsForUser(IUser $user) { |
|
| 201 | - if (!isset($this->mountsForUsers[$user->getUID()])) { |
|
| 202 | - $builder = $this->connection->getQueryBuilder(); |
|
| 203 | - $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id') |
|
| 204 | - ->from('mounts') |
|
| 205 | - ->where($builder->expr()->eq('user_id', $builder->createPositionalParameter($user->getUID()))); |
|
| 206 | - |
|
| 207 | - $rows = $query->execute()->fetchAll(); |
|
| 208 | - |
|
| 209 | - $this->mountsForUsers[$user->getUID()] = array_filter(array_map([$this, 'dbRowToMountInfo'], $rows)); |
|
| 210 | - } |
|
| 211 | - return $this->mountsForUsers[$user->getUID()]; |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - /** |
|
| 215 | - * @param int $numericStorageId |
|
| 216 | - * @return CachedMountInfo[] |
|
| 217 | - */ |
|
| 218 | - public function getMountsForStorageId($numericStorageId) { |
|
| 219 | - $builder = $this->connection->getQueryBuilder(); |
|
| 220 | - $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id') |
|
| 221 | - ->from('mounts') |
|
| 222 | - ->where($builder->expr()->eq('storage_id', $builder->createPositionalParameter($numericStorageId, IQueryBuilder::PARAM_INT))); |
|
| 223 | - |
|
| 224 | - $rows = $query->execute()->fetchAll(); |
|
| 225 | - |
|
| 226 | - return array_filter(array_map([$this, 'dbRowToMountInfo'], $rows)); |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - /** |
|
| 230 | - * @param int $rootFileId |
|
| 231 | - * @return CachedMountInfo[] |
|
| 232 | - */ |
|
| 233 | - public function getMountsForRootId($rootFileId) { |
|
| 234 | - $builder = $this->connection->getQueryBuilder(); |
|
| 235 | - $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id') |
|
| 236 | - ->from('mounts') |
|
| 237 | - ->where($builder->expr()->eq('root_id', $builder->createPositionalParameter($rootFileId, IQueryBuilder::PARAM_INT))); |
|
| 238 | - |
|
| 239 | - $rows = $query->execute()->fetchAll(); |
|
| 240 | - |
|
| 241 | - return array_filter(array_map([$this, 'dbRowToMountInfo'], $rows)); |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - /** |
|
| 245 | - * @param $fileId |
|
| 246 | - * @return array |
|
| 247 | - * @throws \OCP\Files\NotFoundException |
|
| 248 | - */ |
|
| 249 | - private function getCacheInfoFromFileId($fileId) { |
|
| 250 | - if (!isset($this->cacheInfoCache[$fileId])) { |
|
| 251 | - $builder = $this->connection->getQueryBuilder(); |
|
| 252 | - $query = $builder->select('storage', 'path') |
|
| 253 | - ->from('filecache') |
|
| 254 | - ->where($builder->expr()->eq('fileid', $builder->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))); |
|
| 255 | - |
|
| 256 | - $row = $query->execute()->fetch(); |
|
| 257 | - if (is_array($row)) { |
|
| 258 | - $this->cacheInfoCache[$fileId] = [ |
|
| 259 | - (int)$row['storage'], |
|
| 260 | - $row['path'] |
|
| 261 | - ]; |
|
| 262 | - } else { |
|
| 263 | - throw new NotFoundException('File with id "' . $fileId . '" not found'); |
|
| 264 | - } |
|
| 265 | - } |
|
| 266 | - return $this->cacheInfoCache[$fileId]; |
|
| 267 | - } |
|
| 268 | - |
|
| 269 | - /** |
|
| 270 | - * @param int $fileId |
|
| 271 | - * @return ICachedMountInfo[] |
|
| 272 | - * @since 9.0.0 |
|
| 273 | - */ |
|
| 274 | - public function getMountsForFileId($fileId) { |
|
| 275 | - try { |
|
| 276 | - list($storageId, $internalPath) = $this->getCacheInfoFromFileId($fileId); |
|
| 277 | - } catch (NotFoundException $e) { |
|
| 278 | - return []; |
|
| 279 | - } |
|
| 280 | - $mountsForStorage = $this->getMountsForStorageId($storageId); |
|
| 281 | - |
|
| 282 | - // filter mounts that are from the same storage but a different directory |
|
| 283 | - return array_filter($mountsForStorage, function (ICachedMountInfo $mount) use ($internalPath, $fileId) { |
|
| 284 | - if ($fileId === $mount->getRootId()) { |
|
| 285 | - return true; |
|
| 286 | - } |
|
| 287 | - try { |
|
| 288 | - list(, $internalMountPath) = $this->getCacheInfoFromFileId($mount->getRootId()); |
|
| 289 | - } catch (NotFoundException $e) { |
|
| 290 | - return false; |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - return $internalMountPath === '' || substr($internalPath, 0, strlen($internalMountPath) + 1) === $internalMountPath . '/'; |
|
| 294 | - }); |
|
| 295 | - |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - /** |
|
| 299 | - * Remove all cached mounts for a user |
|
| 300 | - * |
|
| 301 | - * @param IUser $user |
|
| 302 | - */ |
|
| 303 | - public function removeUserMounts(IUser $user) { |
|
| 304 | - $builder = $this->connection->getQueryBuilder(); |
|
| 305 | - |
|
| 306 | - $query = $builder->delete('mounts') |
|
| 307 | - ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($user->getUID()))); |
|
| 308 | - $query->execute(); |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - public function removeUserStorageMount($storageId, $userId) { |
|
| 312 | - $builder = $this->connection->getQueryBuilder(); |
|
| 313 | - |
|
| 314 | - $query = $builder->delete('mounts') |
|
| 315 | - ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($userId))) |
|
| 316 | - ->andWhere($builder->expr()->eq('storage_id', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT))); |
|
| 317 | - $query->execute(); |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - public function remoteStorageMounts($storageId) { |
|
| 321 | - $builder = $this->connection->getQueryBuilder(); |
|
| 322 | - |
|
| 323 | - $query = $builder->delete('mounts') |
|
| 324 | - ->where($builder->expr()->eq('storage_id', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT))); |
|
| 325 | - $query->execute(); |
|
| 326 | - } |
|
| 46 | + /** |
|
| 47 | + * @var IDBConnection |
|
| 48 | + */ |
|
| 49 | + private $connection; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * @var IUserManager |
|
| 53 | + */ |
|
| 54 | + private $userManager; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * Cached mount info. |
|
| 58 | + * Map of $userId to ICachedMountInfo. |
|
| 59 | + * |
|
| 60 | + * @var ICache |
|
| 61 | + **/ |
|
| 62 | + private $mountsForUsers; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * @var ILogger |
|
| 66 | + */ |
|
| 67 | + private $logger; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * @var ICache |
|
| 71 | + */ |
|
| 72 | + private $cacheInfoCache; |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * UserMountCache constructor. |
|
| 76 | + * |
|
| 77 | + * @param IDBConnection $connection |
|
| 78 | + * @param IUserManager $userManager |
|
| 79 | + * @param ILogger $logger |
|
| 80 | + */ |
|
| 81 | + public function __construct(IDBConnection $connection, IUserManager $userManager, ILogger $logger) { |
|
| 82 | + $this->connection = $connection; |
|
| 83 | + $this->userManager = $userManager; |
|
| 84 | + $this->logger = $logger; |
|
| 85 | + $this->cacheInfoCache = new CappedMemoryCache(); |
|
| 86 | + $this->mountsForUsers = new CappedMemoryCache(); |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + public function registerMounts(IUser $user, array $mounts) { |
|
| 90 | + // filter out non-proper storages coming from unit tests |
|
| 91 | + $mounts = array_filter($mounts, function (IMountPoint $mount) { |
|
| 92 | + return $mount instanceof SharedMount || $mount->getStorage() && $mount->getStorage()->getCache(); |
|
| 93 | + }); |
|
| 94 | + /** @var ICachedMountInfo[] $newMounts */ |
|
| 95 | + $newMounts = array_map(function (IMountPoint $mount) use ($user) { |
|
| 96 | + // filter out any storages which aren't scanned yet since we aren't interested in files from those storages (yet) |
|
| 97 | + if ($mount->getStorageRootId() === -1) { |
|
| 98 | + return null; |
|
| 99 | + } else { |
|
| 100 | + return new LazyStorageMountInfo($user, $mount); |
|
| 101 | + } |
|
| 102 | + }, $mounts); |
|
| 103 | + $newMounts = array_values(array_filter($newMounts)); |
|
| 104 | + |
|
| 105 | + $cachedMounts = $this->getMountsForUser($user); |
|
| 106 | + $mountDiff = function (ICachedMountInfo $mount1, ICachedMountInfo $mount2) { |
|
| 107 | + // since we are only looking for mounts for a specific user comparing on root id is enough |
|
| 108 | + return $mount1->getRootId() - $mount2->getRootId(); |
|
| 109 | + }; |
|
| 110 | + |
|
| 111 | + /** @var ICachedMountInfo[] $addedMounts */ |
|
| 112 | + $addedMounts = array_udiff($newMounts, $cachedMounts, $mountDiff); |
|
| 113 | + /** @var ICachedMountInfo[] $removedMounts */ |
|
| 114 | + $removedMounts = array_udiff($cachedMounts, $newMounts, $mountDiff); |
|
| 115 | + |
|
| 116 | + $changedMounts = $this->findChangedMounts($newMounts, $cachedMounts); |
|
| 117 | + |
|
| 118 | + foreach ($addedMounts as $mount) { |
|
| 119 | + $this->addToCache($mount); |
|
| 120 | + $this->mountsForUsers[$user->getUID()][] = $mount; |
|
| 121 | + } |
|
| 122 | + foreach ($removedMounts as $mount) { |
|
| 123 | + $this->removeFromCache($mount); |
|
| 124 | + $index = array_search($mount, $this->mountsForUsers[$user->getUID()]); |
|
| 125 | + unset($this->mountsForUsers[$user->getUID()][$index]); |
|
| 126 | + } |
|
| 127 | + foreach ($changedMounts as $mount) { |
|
| 128 | + $this->updateCachedMount($mount); |
|
| 129 | + } |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * @param ICachedMountInfo[] $newMounts |
|
| 134 | + * @param ICachedMountInfo[] $cachedMounts |
|
| 135 | + * @return ICachedMountInfo[] |
|
| 136 | + */ |
|
| 137 | + private function findChangedMounts(array $newMounts, array $cachedMounts) { |
|
| 138 | + $changed = []; |
|
| 139 | + foreach ($newMounts as $newMount) { |
|
| 140 | + foreach ($cachedMounts as $cachedMount) { |
|
| 141 | + if ( |
|
| 142 | + $newMount->getRootId() === $cachedMount->getRootId() && |
|
| 143 | + ($newMount->getMountPoint() !== $cachedMount->getMountPoint() || $newMount->getMountId() !== $cachedMount->getMountId()) |
|
| 144 | + ) { |
|
| 145 | + $changed[] = $newMount; |
|
| 146 | + } |
|
| 147 | + } |
|
| 148 | + } |
|
| 149 | + return $changed; |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + private function addToCache(ICachedMountInfo $mount) { |
|
| 153 | + if ($mount->getStorageId() !== -1) { |
|
| 154 | + $this->connection->insertIfNotExist('*PREFIX*mounts', [ |
|
| 155 | + 'storage_id' => $mount->getStorageId(), |
|
| 156 | + 'root_id' => $mount->getRootId(), |
|
| 157 | + 'user_id' => $mount->getUser()->getUID(), |
|
| 158 | + 'mount_point' => $mount->getMountPoint(), |
|
| 159 | + 'mount_id' => $mount->getMountId() |
|
| 160 | + ], ['root_id', 'user_id']); |
|
| 161 | + } else { |
|
| 162 | + // in some cases this is legitimate, like orphaned shares |
|
| 163 | + $this->logger->debug('Could not get storage info for mount at ' . $mount->getMountPoint()); |
|
| 164 | + } |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + private function updateCachedMount(ICachedMountInfo $mount) { |
|
| 168 | + $builder = $this->connection->getQueryBuilder(); |
|
| 169 | + |
|
| 170 | + $query = $builder->update('mounts') |
|
| 171 | + ->set('mount_point', $builder->createNamedParameter($mount->getMountPoint())) |
|
| 172 | + ->set('mount_id', $builder->createNamedParameter($mount->getMountId(), IQueryBuilder::PARAM_INT)) |
|
| 173 | + ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($mount->getUser()->getUID()))) |
|
| 174 | + ->andWhere($builder->expr()->eq('root_id', $builder->createNamedParameter($mount->getRootId(), IQueryBuilder::PARAM_INT))); |
|
| 175 | + |
|
| 176 | + $query->execute(); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + private function removeFromCache(ICachedMountInfo $mount) { |
|
| 180 | + $builder = $this->connection->getQueryBuilder(); |
|
| 181 | + |
|
| 182 | + $query = $builder->delete('mounts') |
|
| 183 | + ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($mount->getUser()->getUID()))) |
|
| 184 | + ->andWhere($builder->expr()->eq('root_id', $builder->createNamedParameter($mount->getRootId(), IQueryBuilder::PARAM_INT))); |
|
| 185 | + $query->execute(); |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + private function dbRowToMountInfo(array $row) { |
|
| 189 | + $user = $this->userManager->get($row['user_id']); |
|
| 190 | + if (is_null($user)) { |
|
| 191 | + return null; |
|
| 192 | + } |
|
| 193 | + return new CachedMountInfo($user, (int)$row['storage_id'], (int)$row['root_id'], $row['mount_point'], $row['mount_id']); |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + /** |
|
| 197 | + * @param IUser $user |
|
| 198 | + * @return ICachedMountInfo[] |
|
| 199 | + */ |
|
| 200 | + public function getMountsForUser(IUser $user) { |
|
| 201 | + if (!isset($this->mountsForUsers[$user->getUID()])) { |
|
| 202 | + $builder = $this->connection->getQueryBuilder(); |
|
| 203 | + $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id') |
|
| 204 | + ->from('mounts') |
|
| 205 | + ->where($builder->expr()->eq('user_id', $builder->createPositionalParameter($user->getUID()))); |
|
| 206 | + |
|
| 207 | + $rows = $query->execute()->fetchAll(); |
|
| 208 | + |
|
| 209 | + $this->mountsForUsers[$user->getUID()] = array_filter(array_map([$this, 'dbRowToMountInfo'], $rows)); |
|
| 210 | + } |
|
| 211 | + return $this->mountsForUsers[$user->getUID()]; |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + /** |
|
| 215 | + * @param int $numericStorageId |
|
| 216 | + * @return CachedMountInfo[] |
|
| 217 | + */ |
|
| 218 | + public function getMountsForStorageId($numericStorageId) { |
|
| 219 | + $builder = $this->connection->getQueryBuilder(); |
|
| 220 | + $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id') |
|
| 221 | + ->from('mounts') |
|
| 222 | + ->where($builder->expr()->eq('storage_id', $builder->createPositionalParameter($numericStorageId, IQueryBuilder::PARAM_INT))); |
|
| 223 | + |
|
| 224 | + $rows = $query->execute()->fetchAll(); |
|
| 225 | + |
|
| 226 | + return array_filter(array_map([$this, 'dbRowToMountInfo'], $rows)); |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + /** |
|
| 230 | + * @param int $rootFileId |
|
| 231 | + * @return CachedMountInfo[] |
|
| 232 | + */ |
|
| 233 | + public function getMountsForRootId($rootFileId) { |
|
| 234 | + $builder = $this->connection->getQueryBuilder(); |
|
| 235 | + $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id') |
|
| 236 | + ->from('mounts') |
|
| 237 | + ->where($builder->expr()->eq('root_id', $builder->createPositionalParameter($rootFileId, IQueryBuilder::PARAM_INT))); |
|
| 238 | + |
|
| 239 | + $rows = $query->execute()->fetchAll(); |
|
| 240 | + |
|
| 241 | + return array_filter(array_map([$this, 'dbRowToMountInfo'], $rows)); |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + /** |
|
| 245 | + * @param $fileId |
|
| 246 | + * @return array |
|
| 247 | + * @throws \OCP\Files\NotFoundException |
|
| 248 | + */ |
|
| 249 | + private function getCacheInfoFromFileId($fileId) { |
|
| 250 | + if (!isset($this->cacheInfoCache[$fileId])) { |
|
| 251 | + $builder = $this->connection->getQueryBuilder(); |
|
| 252 | + $query = $builder->select('storage', 'path') |
|
| 253 | + ->from('filecache') |
|
| 254 | + ->where($builder->expr()->eq('fileid', $builder->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))); |
|
| 255 | + |
|
| 256 | + $row = $query->execute()->fetch(); |
|
| 257 | + if (is_array($row)) { |
|
| 258 | + $this->cacheInfoCache[$fileId] = [ |
|
| 259 | + (int)$row['storage'], |
|
| 260 | + $row['path'] |
|
| 261 | + ]; |
|
| 262 | + } else { |
|
| 263 | + throw new NotFoundException('File with id "' . $fileId . '" not found'); |
|
| 264 | + } |
|
| 265 | + } |
|
| 266 | + return $this->cacheInfoCache[$fileId]; |
|
| 267 | + } |
|
| 268 | + |
|
| 269 | + /** |
|
| 270 | + * @param int $fileId |
|
| 271 | + * @return ICachedMountInfo[] |
|
| 272 | + * @since 9.0.0 |
|
| 273 | + */ |
|
| 274 | + public function getMountsForFileId($fileId) { |
|
| 275 | + try { |
|
| 276 | + list($storageId, $internalPath) = $this->getCacheInfoFromFileId($fileId); |
|
| 277 | + } catch (NotFoundException $e) { |
|
| 278 | + return []; |
|
| 279 | + } |
|
| 280 | + $mountsForStorage = $this->getMountsForStorageId($storageId); |
|
| 281 | + |
|
| 282 | + // filter mounts that are from the same storage but a different directory |
|
| 283 | + return array_filter($mountsForStorage, function (ICachedMountInfo $mount) use ($internalPath, $fileId) { |
|
| 284 | + if ($fileId === $mount->getRootId()) { |
|
| 285 | + return true; |
|
| 286 | + } |
|
| 287 | + try { |
|
| 288 | + list(, $internalMountPath) = $this->getCacheInfoFromFileId($mount->getRootId()); |
|
| 289 | + } catch (NotFoundException $e) { |
|
| 290 | + return false; |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + return $internalMountPath === '' || substr($internalPath, 0, strlen($internalMountPath) + 1) === $internalMountPath . '/'; |
|
| 294 | + }); |
|
| 295 | + |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + /** |
|
| 299 | + * Remove all cached mounts for a user |
|
| 300 | + * |
|
| 301 | + * @param IUser $user |
|
| 302 | + */ |
|
| 303 | + public function removeUserMounts(IUser $user) { |
|
| 304 | + $builder = $this->connection->getQueryBuilder(); |
|
| 305 | + |
|
| 306 | + $query = $builder->delete('mounts') |
|
| 307 | + ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($user->getUID()))); |
|
| 308 | + $query->execute(); |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + public function removeUserStorageMount($storageId, $userId) { |
|
| 312 | + $builder = $this->connection->getQueryBuilder(); |
|
| 313 | + |
|
| 314 | + $query = $builder->delete('mounts') |
|
| 315 | + ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($userId))) |
|
| 316 | + ->andWhere($builder->expr()->eq('storage_id', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT))); |
|
| 317 | + $query->execute(); |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + public function remoteStorageMounts($storageId) { |
|
| 321 | + $builder = $this->connection->getQueryBuilder(); |
|
| 322 | + |
|
| 323 | + $query = $builder->delete('mounts') |
|
| 324 | + ->where($builder->expr()->eq('storage_id', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT))); |
|
| 325 | + $query->execute(); |
|
| 326 | + } |
|
| 327 | 327 | } |
@@ -88,11 +88,11 @@ discard block |
||
| 88 | 88 | |
| 89 | 89 | public function registerMounts(IUser $user, array $mounts) { |
| 90 | 90 | // filter out non-proper storages coming from unit tests |
| 91 | - $mounts = array_filter($mounts, function (IMountPoint $mount) { |
|
| 91 | + $mounts = array_filter($mounts, function(IMountPoint $mount) { |
|
| 92 | 92 | return $mount instanceof SharedMount || $mount->getStorage() && $mount->getStorage()->getCache(); |
| 93 | 93 | }); |
| 94 | 94 | /** @var ICachedMountInfo[] $newMounts */ |
| 95 | - $newMounts = array_map(function (IMountPoint $mount) use ($user) { |
|
| 95 | + $newMounts = array_map(function(IMountPoint $mount) use ($user) { |
|
| 96 | 96 | // filter out any storages which aren't scanned yet since we aren't interested in files from those storages (yet) |
| 97 | 97 | if ($mount->getStorageRootId() === -1) { |
| 98 | 98 | return null; |
@@ -103,7 +103,7 @@ discard block |
||
| 103 | 103 | $newMounts = array_values(array_filter($newMounts)); |
| 104 | 104 | |
| 105 | 105 | $cachedMounts = $this->getMountsForUser($user); |
| 106 | - $mountDiff = function (ICachedMountInfo $mount1, ICachedMountInfo $mount2) { |
|
| 106 | + $mountDiff = function(ICachedMountInfo $mount1, ICachedMountInfo $mount2) { |
|
| 107 | 107 | // since we are only looking for mounts for a specific user comparing on root id is enough |
| 108 | 108 | return $mount1->getRootId() - $mount2->getRootId(); |
| 109 | 109 | }; |
@@ -160,7 +160,7 @@ discard block |
||
| 160 | 160 | ], ['root_id', 'user_id']); |
| 161 | 161 | } else { |
| 162 | 162 | // in some cases this is legitimate, like orphaned shares |
| 163 | - $this->logger->debug('Could not get storage info for mount at ' . $mount->getMountPoint()); |
|
| 163 | + $this->logger->debug('Could not get storage info for mount at '.$mount->getMountPoint()); |
|
| 164 | 164 | } |
| 165 | 165 | } |
| 166 | 166 | |
@@ -190,7 +190,7 @@ discard block |
||
| 190 | 190 | if (is_null($user)) { |
| 191 | 191 | return null; |
| 192 | 192 | } |
| 193 | - return new CachedMountInfo($user, (int)$row['storage_id'], (int)$row['root_id'], $row['mount_point'], $row['mount_id']); |
|
| 193 | + return new CachedMountInfo($user, (int) $row['storage_id'], (int) $row['root_id'], $row['mount_point'], $row['mount_id']); |
|
| 194 | 194 | } |
| 195 | 195 | |
| 196 | 196 | /** |
@@ -256,11 +256,11 @@ discard block |
||
| 256 | 256 | $row = $query->execute()->fetch(); |
| 257 | 257 | if (is_array($row)) { |
| 258 | 258 | $this->cacheInfoCache[$fileId] = [ |
| 259 | - (int)$row['storage'], |
|
| 259 | + (int) $row['storage'], |
|
| 260 | 260 | $row['path'] |
| 261 | 261 | ]; |
| 262 | 262 | } else { |
| 263 | - throw new NotFoundException('File with id "' . $fileId . '" not found'); |
|
| 263 | + throw new NotFoundException('File with id "'.$fileId.'" not found'); |
|
| 264 | 264 | } |
| 265 | 265 | } |
| 266 | 266 | return $this->cacheInfoCache[$fileId]; |
@@ -280,7 +280,7 @@ discard block |
||
| 280 | 280 | $mountsForStorage = $this->getMountsForStorageId($storageId); |
| 281 | 281 | |
| 282 | 282 | // filter mounts that are from the same storage but a different directory |
| 283 | - return array_filter($mountsForStorage, function (ICachedMountInfo $mount) use ($internalPath, $fileId) { |
|
| 283 | + return array_filter($mountsForStorage, function(ICachedMountInfo $mount) use ($internalPath, $fileId) { |
|
| 284 | 284 | if ($fileId === $mount->getRootId()) { |
| 285 | 285 | return true; |
| 286 | 286 | } |
@@ -290,7 +290,7 @@ discard block |
||
| 290 | 290 | return false; |
| 291 | 291 | } |
| 292 | 292 | |
| 293 | - return $internalMountPath === '' || substr($internalPath, 0, strlen($internalMountPath) + 1) === $internalMountPath . '/'; |
|
| 293 | + return $internalMountPath === '' || substr($internalPath, 0, strlen($internalMountPath) + 1) === $internalMountPath.'/'; |
|
| 294 | 294 | }); |
| 295 | 295 | |
| 296 | 296 | } |