1
0

UserMountCache.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Dariusz Olszewski <starypatyk@users.noreply.github.com>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Julius Härtl <jus@bitgrid.net>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Vincent Petry <vincent@nextcloud.com>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OC\Files\Config;
  30. use OC\User\LazyUser;
  31. use OCP\Cache\CappedMemoryCache;
  32. use OCP\DB\QueryBuilder\IQueryBuilder;
  33. use OCP\Diagnostics\IEventLogger;
  34. use OCP\Files\Config\ICachedMountFileInfo;
  35. use OCP\Files\Config\ICachedMountInfo;
  36. use OCP\Files\Config\IUserMountCache;
  37. use OCP\Files\NotFoundException;
  38. use OCP\IDBConnection;
  39. use OCP\IUser;
  40. use OCP\IUserManager;
  41. use Psr\Log\LoggerInterface;
  42. /**
  43. * Cache mounts points per user in the cache so we can easily look them up
  44. */
  45. class UserMountCache implements IUserMountCache {
  46. private IDBConnection $connection;
  47. private IUserManager $userManager;
  48. /**
  49. * Cached mount info.
  50. * @var CappedMemoryCache<ICachedMountInfo[]>
  51. **/
  52. private CappedMemoryCache $mountsForUsers;
  53. /**
  54. * fileid => internal path mapping for cached mount info.
  55. * @var CappedMemoryCache<string>
  56. **/
  57. private CappedMemoryCache $internalPathCache;
  58. private LoggerInterface $logger;
  59. /** @var CappedMemoryCache<array> */
  60. private CappedMemoryCache $cacheInfoCache;
  61. private IEventLogger $eventLogger;
  62. /**
  63. * UserMountCache constructor.
  64. */
  65. public function __construct(
  66. IDBConnection $connection,
  67. IUserManager $userManager,
  68. LoggerInterface $logger,
  69. IEventLogger $eventLogger
  70. ) {
  71. $this->connection = $connection;
  72. $this->userManager = $userManager;
  73. $this->logger = $logger;
  74. $this->eventLogger = $eventLogger;
  75. $this->cacheInfoCache = new CappedMemoryCache();
  76. $this->internalPathCache = new CappedMemoryCache();
  77. $this->mountsForUsers = new CappedMemoryCache();
  78. }
  79. public function registerMounts(IUser $user, array $mounts, array $mountProviderClasses = null) {
  80. $this->eventLogger->start('fs:setup:user:register', 'Registering mounts for user');
  81. /** @var array<string, ICachedMountInfo> $newMounts */
  82. $newMounts = [];
  83. foreach ($mounts as $mount) {
  84. // filter out any storages which aren't scanned yet since we aren't interested in files from those storages (yet)
  85. if ($mount->getStorageRootId() !== -1) {
  86. $mountInfo = new LazyStorageMountInfo($user, $mount);
  87. $newMounts[$mountInfo->getKey()] = $mountInfo;
  88. }
  89. }
  90. $cachedMounts = $this->getMountsForUser($user);
  91. if (is_array($mountProviderClasses)) {
  92. $cachedMounts = array_filter($cachedMounts, function (ICachedMountInfo $mountInfo) use ($mountProviderClasses, $newMounts) {
  93. // for existing mounts that didn't have a mount provider set
  94. // we still want the ones that map to new mounts
  95. if ($mountInfo->getMountProvider() === '' && isset($newMounts[$mountInfo->getKey()])) {
  96. return true;
  97. }
  98. return in_array($mountInfo->getMountProvider(), $mountProviderClasses);
  99. });
  100. }
  101. $addedMounts = [];
  102. $removedMounts = [];
  103. foreach ($newMounts as $mountKey => $newMount) {
  104. if (!isset($cachedMounts[$mountKey])) {
  105. $addedMounts[] = $newMount;
  106. }
  107. }
  108. foreach ($cachedMounts as $mountKey => $cachedMount) {
  109. if (!isset($newMounts[$mountKey])) {
  110. $removedMounts[] = $cachedMount;
  111. }
  112. }
  113. $changedMounts = $this->findChangedMounts($newMounts, $cachedMounts);
  114. if ($addedMounts || $removedMounts || $changedMounts) {
  115. $this->connection->beginTransaction();
  116. $userUID = $user->getUID();
  117. try {
  118. foreach ($addedMounts as $mount) {
  119. $this->addToCache($mount);
  120. /** @psalm-suppress InvalidArgument */
  121. $this->mountsForUsers[$userUID][$mount->getKey()] = $mount;
  122. }
  123. foreach ($removedMounts as $mount) {
  124. $this->removeFromCache($mount);
  125. unset($this->mountsForUsers[$userUID][$mount->getKey()]);
  126. }
  127. foreach ($changedMounts as $mount) {
  128. $this->updateCachedMount($mount);
  129. /** @psalm-suppress InvalidArgument */
  130. $this->mountsForUsers[$userUID][$mount->getKey()] = $mount;
  131. }
  132. $this->connection->commit();
  133. } catch (\Throwable $e) {
  134. $this->connection->rollBack();
  135. throw $e;
  136. }
  137. }
  138. $this->eventLogger->end('fs:setup:user:register');
  139. }
  140. /**
  141. * @param array<string, ICachedMountInfo> $newMounts
  142. * @param array<string, ICachedMountInfo> $cachedMounts
  143. * @return ICachedMountInfo[]
  144. */
  145. private function findChangedMounts(array $newMounts, array $cachedMounts) {
  146. $changed = [];
  147. foreach ($cachedMounts as $key => $cachedMount) {
  148. if (isset($newMounts[$key])) {
  149. $newMount = $newMounts[$key];
  150. if (
  151. $newMount->getStorageId() !== $cachedMount->getStorageId() ||
  152. $newMount->getMountId() !== $cachedMount->getMountId() ||
  153. $newMount->getMountProvider() !== $cachedMount->getMountProvider()
  154. ) {
  155. $changed[] = $newMount;
  156. }
  157. }
  158. }
  159. return $changed;
  160. }
  161. private function addToCache(ICachedMountInfo $mount) {
  162. if ($mount->getStorageId() !== -1) {
  163. $this->connection->insertIfNotExist('*PREFIX*mounts', [
  164. 'storage_id' => $mount->getStorageId(),
  165. 'root_id' => $mount->getRootId(),
  166. 'user_id' => $mount->getUser()->getUID(),
  167. 'mount_point' => $mount->getMountPoint(),
  168. 'mount_id' => $mount->getMountId(),
  169. 'mount_provider_class' => $mount->getMountProvider(),
  170. ], ['root_id', 'user_id', 'mount_point']);
  171. } else {
  172. // in some cases this is legitimate, like orphaned shares
  173. $this->logger->debug('Could not get storage info for mount at ' . $mount->getMountPoint());
  174. }
  175. }
  176. private function updateCachedMount(ICachedMountInfo $mount) {
  177. $builder = $this->connection->getQueryBuilder();
  178. $query = $builder->update('mounts')
  179. ->set('storage_id', $builder->createNamedParameter($mount->getStorageId()))
  180. ->set('mount_point', $builder->createNamedParameter($mount->getMountPoint()))
  181. ->set('mount_id', $builder->createNamedParameter($mount->getMountId(), IQueryBuilder::PARAM_INT))
  182. ->set('mount_provider_class', $builder->createNamedParameter($mount->getMountProvider()))
  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. private function removeFromCache(ICachedMountInfo $mount) {
  188. $builder = $this->connection->getQueryBuilder();
  189. $query = $builder->delete('mounts')
  190. ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($mount->getUser()->getUID())))
  191. ->andWhere($builder->expr()->eq('root_id', $builder->createNamedParameter($mount->getRootId(), IQueryBuilder::PARAM_INT)))
  192. ->andWhere($builder->expr()->eq('mount_point', $builder->createNamedParameter($mount->getMountPoint())));
  193. $query->execute();
  194. }
  195. /**
  196. * @param array $row
  197. * @param (callable(CachedMountInfo): string)|null $pathCallback
  198. * @return CachedMountInfo
  199. */
  200. private function dbRowToMountInfo(array $row, ?callable $pathCallback = null): ICachedMountInfo {
  201. $user = new LazyUser($row['user_id'], $this->userManager);
  202. $mount_id = $row['mount_id'];
  203. if (!is_null($mount_id)) {
  204. $mount_id = (int)$mount_id;
  205. }
  206. if ($pathCallback) {
  207. return new LazyPathCachedMountInfo(
  208. $user,
  209. (int)$row['storage_id'],
  210. (int)$row['root_id'],
  211. $row['mount_point'],
  212. $row['mount_provider_class'] ?? '',
  213. $mount_id,
  214. $pathCallback,
  215. );
  216. } else {
  217. return new CachedMountInfo(
  218. $user,
  219. (int)$row['storage_id'],
  220. (int)$row['root_id'],
  221. $row['mount_point'],
  222. $row['mount_provider_class'] ?? '',
  223. $mount_id,
  224. $row['path'] ?? '',
  225. );
  226. }
  227. }
  228. /**
  229. * @param IUser $user
  230. * @return ICachedMountInfo[]
  231. */
  232. public function getMountsForUser(IUser $user) {
  233. $userUID = $user->getUID();
  234. if (!$this->userManager->userExists($userUID)) {
  235. return [];
  236. }
  237. if (!isset($this->mountsForUsers[$userUID])) {
  238. $builder = $this->connection->getQueryBuilder();
  239. $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'mount_provider_class')
  240. ->from('mounts', 'm')
  241. ->where($builder->expr()->eq('user_id', $builder->createPositionalParameter($userUID)));
  242. $result = $query->execute();
  243. $rows = $result->fetchAll();
  244. $result->closeCursor();
  245. /** @var array<string, ICachedMountInfo> $mounts */
  246. $mounts = [];
  247. foreach ($rows as $row) {
  248. $mount = $this->dbRowToMountInfo($row, [$this, 'getInternalPathForMountInfo']);
  249. if ($mount !== null) {
  250. $mounts[$mount->getKey()] = $mount;
  251. }
  252. }
  253. $this->mountsForUsers[$userUID] = $mounts;
  254. }
  255. return $this->mountsForUsers[$userUID];
  256. }
  257. public function getInternalPathForMountInfo(CachedMountInfo $info): string {
  258. $cached = $this->internalPathCache->get($info->getRootId());
  259. if ($cached !== null) {
  260. return $cached;
  261. }
  262. $builder = $this->connection->getQueryBuilder();
  263. $query = $builder->select('path')
  264. ->from('filecache')
  265. ->where($builder->expr()->eq('fileid', $builder->createPositionalParameter($info->getRootId())));
  266. return $query->executeQuery()->fetchOne() ?: '';
  267. }
  268. /**
  269. * @param int $numericStorageId
  270. * @param string|null $user limit the results to a single user
  271. * @return CachedMountInfo[]
  272. */
  273. public function getMountsForStorageId($numericStorageId, $user = null) {
  274. $builder = $this->connection->getQueryBuilder();
  275. $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path', 'mount_provider_class')
  276. ->from('mounts', 'm')
  277. ->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid'))
  278. ->where($builder->expr()->eq('storage_id', $builder->createPositionalParameter($numericStorageId, IQueryBuilder::PARAM_INT)));
  279. if ($user) {
  280. $query->andWhere($builder->expr()->eq('user_id', $builder->createPositionalParameter($user)));
  281. }
  282. $result = $query->execute();
  283. $rows = $result->fetchAll();
  284. $result->closeCursor();
  285. return array_filter(array_map([$this, 'dbRowToMountInfo'], $rows));
  286. }
  287. /**
  288. * @param int $rootFileId
  289. * @return CachedMountInfo[]
  290. */
  291. public function getMountsForRootId($rootFileId) {
  292. $builder = $this->connection->getQueryBuilder();
  293. $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path', 'mount_provider_class')
  294. ->from('mounts', 'm')
  295. ->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid'))
  296. ->where($builder->expr()->eq('root_id', $builder->createPositionalParameter($rootFileId, IQueryBuilder::PARAM_INT)));
  297. $result = $query->execute();
  298. $rows = $result->fetchAll();
  299. $result->closeCursor();
  300. return array_filter(array_map([$this, 'dbRowToMountInfo'], $rows));
  301. }
  302. /**
  303. * @param $fileId
  304. * @return array{int, string, int}
  305. * @throws \OCP\Files\NotFoundException
  306. */
  307. private function getCacheInfoFromFileId($fileId): array {
  308. if (!isset($this->cacheInfoCache[$fileId])) {
  309. $builder = $this->connection->getQueryBuilder();
  310. $query = $builder->select('storage', 'path', 'mimetype')
  311. ->from('filecache')
  312. ->where($builder->expr()->eq('fileid', $builder->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)));
  313. $result = $query->execute();
  314. $row = $result->fetch();
  315. $result->closeCursor();
  316. if (is_array($row)) {
  317. $this->cacheInfoCache[$fileId] = [
  318. (int)$row['storage'],
  319. (string)$row['path'],
  320. (int)$row['mimetype']
  321. ];
  322. } else {
  323. throw new NotFoundException('File with id "' . $fileId . '" not found');
  324. }
  325. }
  326. return $this->cacheInfoCache[$fileId];
  327. }
  328. /**
  329. * @param int $fileId
  330. * @param string|null $user optionally restrict the results to a single user
  331. * @return ICachedMountFileInfo[]
  332. * @since 9.0.0
  333. */
  334. public function getMountsForFileId($fileId, $user = null) {
  335. try {
  336. [$storageId, $internalPath] = $this->getCacheInfoFromFileId($fileId);
  337. } catch (NotFoundException $e) {
  338. return [];
  339. }
  340. $mountsForStorage = $this->getMountsForStorageId($storageId, $user);
  341. // filter mounts that are from the same storage but not a parent of the file we care about
  342. $filteredMounts = array_filter($mountsForStorage, function (ICachedMountInfo $mount) use ($internalPath, $fileId) {
  343. if ($fileId === $mount->getRootId()) {
  344. return true;
  345. }
  346. $internalMountPath = $mount->getRootInternalPath();
  347. return $internalMountPath === '' || str_starts_with($internalPath, $internalMountPath . '/');
  348. });
  349. $filteredMounts = array_filter($filteredMounts, function (ICachedMountInfo $mount) {
  350. return $this->userManager->userExists($mount->getUser()->getUID());
  351. });
  352. return array_map(function (ICachedMountInfo $mount) use ($internalPath) {
  353. return new CachedMountFileInfo(
  354. $mount->getUser(),
  355. $mount->getStorageId(),
  356. $mount->getRootId(),
  357. $mount->getMountPoint(),
  358. $mount->getMountId(),
  359. $mount->getMountProvider(),
  360. $mount->getRootInternalPath(),
  361. $internalPath
  362. );
  363. }, $filteredMounts);
  364. }
  365. /**
  366. * Remove all cached mounts for a user
  367. *
  368. * @param IUser $user
  369. */
  370. public function removeUserMounts(IUser $user) {
  371. $builder = $this->connection->getQueryBuilder();
  372. $query = $builder->delete('mounts')
  373. ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($user->getUID())));
  374. $query->execute();
  375. }
  376. public function removeUserStorageMount($storageId, $userId) {
  377. $builder = $this->connection->getQueryBuilder();
  378. $query = $builder->delete('mounts')
  379. ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($userId)))
  380. ->andWhere($builder->expr()->eq('storage_id', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)));
  381. $query->execute();
  382. }
  383. public function remoteStorageMounts($storageId) {
  384. $builder = $this->connection->getQueryBuilder();
  385. $query = $builder->delete('mounts')
  386. ->where($builder->expr()->eq('storage_id', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)));
  387. $query->execute();
  388. }
  389. /**
  390. * @param array $users
  391. * @return array
  392. */
  393. public function getUsedSpaceForUsers(array $users) {
  394. $builder = $this->connection->getQueryBuilder();
  395. $slash = $builder->createNamedParameter('/');
  396. $mountPoint = $builder->func()->concat(
  397. $builder->func()->concat($slash, 'user_id'),
  398. $slash
  399. );
  400. $userIds = array_map(function (IUser $user) {
  401. return $user->getUID();
  402. }, $users);
  403. $query = $builder->select('m.user_id', 'f.size')
  404. ->from('mounts', 'm')
  405. ->innerJoin('m', 'filecache', 'f',
  406. $builder->expr()->andX(
  407. $builder->expr()->eq('m.storage_id', 'f.storage'),
  408. $builder->expr()->eq('f.path_hash', $builder->createNamedParameter(md5('files')))
  409. ))
  410. ->where($builder->expr()->eq('m.mount_point', $mountPoint))
  411. ->andWhere($builder->expr()->in('m.user_id', $builder->createNamedParameter($userIds, IQueryBuilder::PARAM_STR_ARRAY)));
  412. $result = $query->execute();
  413. $results = [];
  414. while ($row = $result->fetch()) {
  415. $results[$row['user_id']] = $row['size'];
  416. }
  417. $result->closeCursor();
  418. return $results;
  419. }
  420. public function clear(): void {
  421. $this->cacheInfoCache = new CappedMemoryCache();
  422. $this->mountsForUsers = new CappedMemoryCache();
  423. }
  424. public function getMountForPath(IUser $user, string $path): ICachedMountInfo {
  425. $mounts = $this->getMountsForUser($user);
  426. $mountPoints = array_map(function (ICachedMountInfo $mount) {
  427. return $mount->getMountPoint();
  428. }, $mounts);
  429. $mounts = array_combine($mountPoints, $mounts);
  430. $current = rtrim($path, '/');
  431. // walk up the directory tree until we find a path that has a mountpoint set
  432. // the loop will return if a mountpoint is found or break if none are found
  433. while (true) {
  434. $mountPoint = $current . '/';
  435. if (isset($mounts[$mountPoint])) {
  436. return $mounts[$mountPoint];
  437. } elseif ($current === '') {
  438. break;
  439. }
  440. $current = dirname($current);
  441. if ($current === '.' || $current === '/') {
  442. $current = '';
  443. }
  444. }
  445. throw new NotFoundException("No cached mount for path " . $path);
  446. }
  447. public function getMountsInPath(IUser $user, string $path): array {
  448. $path = rtrim($path, '/') . '/';
  449. $mounts = $this->getMountsForUser($user);
  450. return array_filter($mounts, function (ICachedMountInfo $mount) use ($path) {
  451. return $mount->getMountPoint() !== $path && str_starts_with($mount->getMountPoint(), $path);
  452. });
  453. }
  454. }