1
0

UserMountCache.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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 OCP\Cache\CappedMemoryCache;
  31. use OCA\Files_Sharing\SharedMount;
  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\Mount\IMountPoint;
  38. use OCP\Files\NotFoundException;
  39. use OCP\IDBConnection;
  40. use OCP\IUser;
  41. use OCP\IUserManager;
  42. use Psr\Log\LoggerInterface;
  43. /**
  44. * Cache mounts points per user in the cache so we can easily look them up
  45. */
  46. class UserMountCache implements IUserMountCache {
  47. private IDBConnection $connection;
  48. private IUserManager $userManager;
  49. /**
  50. * Cached mount info.
  51. * @var CappedMemoryCache<ICachedMountInfo[]>
  52. **/
  53. private CappedMemoryCache $mountsForUsers;
  54. private LoggerInterface $logger;
  55. /** @var CappedMemoryCache<array> */
  56. private CappedMemoryCache $cacheInfoCache;
  57. private IEventLogger $eventLogger;
  58. /**
  59. * UserMountCache constructor.
  60. */
  61. public function __construct(
  62. IDBConnection $connection,
  63. IUserManager $userManager,
  64. LoggerInterface $logger,
  65. IEventLogger $eventLogger
  66. ) {
  67. $this->connection = $connection;
  68. $this->userManager = $userManager;
  69. $this->logger = $logger;
  70. $this->eventLogger = $eventLogger;
  71. $this->cacheInfoCache = new CappedMemoryCache();
  72. $this->mountsForUsers = new CappedMemoryCache();
  73. }
  74. public function registerMounts(IUser $user, array $mounts, array $mountProviderClasses = null) {
  75. $this->eventLogger->start('fs:setup:user:register', 'Registering mounts for user');
  76. // filter out non-proper storages coming from unit tests
  77. $mounts = array_filter($mounts, function (IMountPoint $mount) {
  78. return $mount instanceof SharedMount || ($mount->getStorage() && $mount->getStorage()->getCache());
  79. });
  80. /** @var ICachedMountInfo[] $newMounts */
  81. $newMounts = array_map(function (IMountPoint $mount) use ($user) {
  82. // filter out any storages which aren't scanned yet since we aren't interested in files from those storages (yet)
  83. if ($mount->getStorageRootId() === -1) {
  84. return null;
  85. } else {
  86. return new LazyStorageMountInfo($user, $mount);
  87. }
  88. }, $mounts);
  89. $newMounts = array_values(array_filter($newMounts));
  90. $newMountKeys = array_map(function (ICachedMountInfo $mount) {
  91. return $mount->getRootId() . '::' . $mount->getMountPoint();
  92. }, $newMounts);
  93. $newMounts = array_combine($newMountKeys, $newMounts);
  94. $cachedMounts = $this->getMountsForUser($user);
  95. if (is_array($mountProviderClasses)) {
  96. $cachedMounts = array_filter($cachedMounts, function (ICachedMountInfo $mountInfo) use ($mountProviderClasses, $newMounts) {
  97. // for existing mounts that didn't have a mount provider set
  98. // we still want the ones that map to new mounts
  99. $mountKey = $mountInfo->getRootId() . '::' . $mountInfo->getMountPoint();
  100. if ($mountInfo->getMountProvider() === '' && isset($newMounts[$mountKey])) {
  101. return true;
  102. }
  103. return in_array($mountInfo->getMountProvider(), $mountProviderClasses);
  104. });
  105. }
  106. $cachedRootKeys = array_map(function (ICachedMountInfo $mount) {
  107. return $mount->getRootId() . '::' . $mount->getMountPoint();
  108. }, $cachedMounts);
  109. $cachedMounts = array_combine($cachedRootKeys, $cachedMounts);
  110. $addedMounts = [];
  111. $removedMounts = [];
  112. foreach ($newMounts as $mountKey => $newMount) {
  113. if (!isset($cachedMounts[$mountKey])) {
  114. $addedMounts[] = $newMount;
  115. }
  116. }
  117. foreach ($cachedMounts as $mountKey => $cachedMount) {
  118. if (!isset($newMounts[$mountKey])) {
  119. $removedMounts[] = $cachedMount;
  120. }
  121. }
  122. $changedMounts = $this->findChangedMounts($newMounts, $cachedMounts);
  123. $this->connection->beginTransaction();
  124. try {
  125. foreach ($addedMounts as $mount) {
  126. $this->addToCache($mount);
  127. /** @psalm-suppress InvalidArgument */
  128. $this->mountsForUsers[$user->getUID()][] = $mount;
  129. }
  130. foreach ($removedMounts as $mount) {
  131. $this->removeFromCache($mount);
  132. $index = array_search($mount, $this->mountsForUsers[$user->getUID()]);
  133. unset($this->mountsForUsers[$user->getUID()][$index]);
  134. }
  135. foreach ($changedMounts as $mount) {
  136. $this->updateCachedMount($mount);
  137. }
  138. $this->connection->commit();
  139. } catch (\Throwable $e) {
  140. $this->connection->rollBack();
  141. throw $e;
  142. }
  143. $this->eventLogger->end('fs:setup:user:register');
  144. }
  145. /**
  146. * @param ICachedMountInfo[] $newMounts
  147. * @param ICachedMountInfo[] $cachedMounts
  148. * @return ICachedMountInfo[]
  149. */
  150. private function findChangedMounts(array $newMounts, array $cachedMounts) {
  151. $new = [];
  152. foreach ($newMounts as $mount) {
  153. $new[$mount->getRootId() . '::' . $mount->getMountPoint()] = $mount;
  154. }
  155. $changed = [];
  156. foreach ($cachedMounts as $cachedMount) {
  157. $key = $cachedMount->getRootId() . '::' . $cachedMount->getMountPoint();
  158. if (isset($new[$key])) {
  159. $newMount = $new[$key];
  160. if (
  161. $newMount->getMountPoint() !== $cachedMount->getMountPoint() ||
  162. $newMount->getStorageId() !== $cachedMount->getStorageId() ||
  163. $newMount->getMountId() !== $cachedMount->getMountId() ||
  164. $newMount->getMountProvider() !== $cachedMount->getMountProvider()
  165. ) {
  166. $changed[] = $newMount;
  167. }
  168. }
  169. }
  170. return $changed;
  171. }
  172. private function addToCache(ICachedMountInfo $mount) {
  173. if ($mount->getStorageId() !== -1) {
  174. $this->connection->insertIfNotExist('*PREFIX*mounts', [
  175. 'storage_id' => $mount->getStorageId(),
  176. 'root_id' => $mount->getRootId(),
  177. 'user_id' => $mount->getUser()->getUID(),
  178. 'mount_point' => $mount->getMountPoint(),
  179. 'mount_id' => $mount->getMountId(),
  180. 'mount_provider_class' => $mount->getMountProvider(),
  181. ], ['root_id', 'user_id', 'mount_point']);
  182. } else {
  183. // in some cases this is legitimate, like orphaned shares
  184. $this->logger->debug('Could not get storage info for mount at ' . $mount->getMountPoint());
  185. }
  186. }
  187. private function updateCachedMount(ICachedMountInfo $mount) {
  188. $builder = $this->connection->getQueryBuilder();
  189. $query = $builder->update('mounts')
  190. ->set('storage_id', $builder->createNamedParameter($mount->getStorageId()))
  191. ->set('mount_point', $builder->createNamedParameter($mount->getMountPoint()))
  192. ->set('mount_id', $builder->createNamedParameter($mount->getMountId(), IQueryBuilder::PARAM_INT))
  193. ->set('mount_provider_class', $builder->createNamedParameter($mount->getMountProvider()))
  194. ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($mount->getUser()->getUID())))
  195. ->andWhere($builder->expr()->eq('root_id', $builder->createNamedParameter($mount->getRootId(), IQueryBuilder::PARAM_INT)));
  196. $query->execute();
  197. }
  198. private function removeFromCache(ICachedMountInfo $mount) {
  199. $builder = $this->connection->getQueryBuilder();
  200. $query = $builder->delete('mounts')
  201. ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($mount->getUser()->getUID())))
  202. ->andWhere($builder->expr()->eq('root_id', $builder->createNamedParameter($mount->getRootId(), IQueryBuilder::PARAM_INT)))
  203. ->andWhere($builder->expr()->eq('mount_point', $builder->createNamedParameter($mount->getMountPoint())));
  204. $query->execute();
  205. }
  206. private function dbRowToMountInfo(array $row) {
  207. $user = $this->userManager->get($row['user_id']);
  208. if (is_null($user)) {
  209. return null;
  210. }
  211. $mount_id = $row['mount_id'];
  212. if (!is_null($mount_id)) {
  213. $mount_id = (int)$mount_id;
  214. }
  215. return new CachedMountInfo(
  216. $user,
  217. (int)$row['storage_id'],
  218. (int)$row['root_id'],
  219. $row['mount_point'],
  220. $row['mount_provider_class'] ?? '',
  221. $mount_id,
  222. isset($row['path']) ? $row['path'] : '',
  223. );
  224. }
  225. /**
  226. * @param IUser $user
  227. * @return ICachedMountInfo[]
  228. */
  229. public function getMountsForUser(IUser $user) {
  230. if (!isset($this->mountsForUsers[$user->getUID()])) {
  231. $builder = $this->connection->getQueryBuilder();
  232. $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path', 'mount_provider_class')
  233. ->from('mounts', 'm')
  234. ->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid'))
  235. ->where($builder->expr()->eq('user_id', $builder->createPositionalParameter($user->getUID())));
  236. $result = $query->execute();
  237. $rows = $result->fetchAll();
  238. $result->closeCursor();
  239. $this->mountsForUsers[$user->getUID()] = array_filter(array_map([$this, 'dbRowToMountInfo'], $rows));
  240. }
  241. return $this->mountsForUsers[$user->getUID()];
  242. }
  243. /**
  244. * @param int $numericStorageId
  245. * @param string|null $user limit the results to a single user
  246. * @return CachedMountInfo[]
  247. */
  248. public function getMountsForStorageId($numericStorageId, $user = null) {
  249. $builder = $this->connection->getQueryBuilder();
  250. $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path', 'mount_provider_class')
  251. ->from('mounts', 'm')
  252. ->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid'))
  253. ->where($builder->expr()->eq('storage_id', $builder->createPositionalParameter($numericStorageId, IQueryBuilder::PARAM_INT)));
  254. if ($user) {
  255. $query->andWhere($builder->expr()->eq('user_id', $builder->createPositionalParameter($user)));
  256. }
  257. $result = $query->execute();
  258. $rows = $result->fetchAll();
  259. $result->closeCursor();
  260. return array_filter(array_map([$this, 'dbRowToMountInfo'], $rows));
  261. }
  262. /**
  263. * @param int $rootFileId
  264. * @return CachedMountInfo[]
  265. */
  266. public function getMountsForRootId($rootFileId) {
  267. $builder = $this->connection->getQueryBuilder();
  268. $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path', 'mount_provider_class')
  269. ->from('mounts', 'm')
  270. ->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid'))
  271. ->where($builder->expr()->eq('root_id', $builder->createPositionalParameter($rootFileId, IQueryBuilder::PARAM_INT)));
  272. $result = $query->execute();
  273. $rows = $result->fetchAll();
  274. $result->closeCursor();
  275. return array_filter(array_map([$this, 'dbRowToMountInfo'], $rows));
  276. }
  277. /**
  278. * @param $fileId
  279. * @return array{int, string, int}
  280. * @throws \OCP\Files\NotFoundException
  281. */
  282. private function getCacheInfoFromFileId($fileId): array {
  283. if (!isset($this->cacheInfoCache[$fileId])) {
  284. $builder = $this->connection->getQueryBuilder();
  285. $query = $builder->select('storage', 'path', 'mimetype')
  286. ->from('filecache')
  287. ->where($builder->expr()->eq('fileid', $builder->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)));
  288. $result = $query->execute();
  289. $row = $result->fetch();
  290. $result->closeCursor();
  291. if (is_array($row)) {
  292. $this->cacheInfoCache[$fileId] = [
  293. (int)$row['storage'],
  294. (string)$row['path'],
  295. (int)$row['mimetype']
  296. ];
  297. } else {
  298. throw new NotFoundException('File with id "' . $fileId . '" not found');
  299. }
  300. }
  301. return $this->cacheInfoCache[$fileId];
  302. }
  303. /**
  304. * @param int $fileId
  305. * @param string|null $user optionally restrict the results to a single user
  306. * @return ICachedMountFileInfo[]
  307. * @since 9.0.0
  308. */
  309. public function getMountsForFileId($fileId, $user = null) {
  310. try {
  311. [$storageId, $internalPath] = $this->getCacheInfoFromFileId($fileId);
  312. } catch (NotFoundException $e) {
  313. return [];
  314. }
  315. $builder = $this->connection->getQueryBuilder();
  316. $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path', 'mount_provider_class')
  317. ->from('mounts', 'm')
  318. ->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid'))
  319. ->where($builder->expr()->eq('storage_id', $builder->createPositionalParameter($storageId, IQueryBuilder::PARAM_INT)));
  320. if ($user) {
  321. $query->andWhere($builder->expr()->eq('user_id', $builder->createPositionalParameter($user)));
  322. }
  323. $result = $query->execute();
  324. $rows = $result->fetchAll();
  325. $result->closeCursor();
  326. // filter mounts that are from the same storage but a different directory
  327. $filteredMounts = array_filter($rows, function (array $row) use ($internalPath, $fileId) {
  328. if ($fileId === (int)$row['root_id']) {
  329. return true;
  330. }
  331. $internalMountPath = $row['path'] ?? '';
  332. return $internalMountPath === '' || substr($internalPath, 0, strlen($internalMountPath) + 1) === $internalMountPath . '/';
  333. });
  334. $filteredMounts = array_filter(array_map([$this, 'dbRowToMountInfo'], $filteredMounts));
  335. return array_map(function (ICachedMountInfo $mount) use ($internalPath) {
  336. return new CachedMountFileInfo(
  337. $mount->getUser(),
  338. $mount->getStorageId(),
  339. $mount->getRootId(),
  340. $mount->getMountPoint(),
  341. $mount->getMountId(),
  342. $mount->getMountProvider(),
  343. $mount->getRootInternalPath(),
  344. $internalPath
  345. );
  346. }, $filteredMounts);
  347. }
  348. /**
  349. * Remove all cached mounts for a user
  350. *
  351. * @param IUser $user
  352. */
  353. public function removeUserMounts(IUser $user) {
  354. $builder = $this->connection->getQueryBuilder();
  355. $query = $builder->delete('mounts')
  356. ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($user->getUID())));
  357. $query->execute();
  358. }
  359. public function removeUserStorageMount($storageId, $userId) {
  360. $builder = $this->connection->getQueryBuilder();
  361. $query = $builder->delete('mounts')
  362. ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($userId)))
  363. ->andWhere($builder->expr()->eq('storage_id', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)));
  364. $query->execute();
  365. }
  366. public function remoteStorageMounts($storageId) {
  367. $builder = $this->connection->getQueryBuilder();
  368. $query = $builder->delete('mounts')
  369. ->where($builder->expr()->eq('storage_id', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)));
  370. $query->execute();
  371. }
  372. /**
  373. * @param array $users
  374. * @return array
  375. */
  376. public function getUsedSpaceForUsers(array $users) {
  377. $builder = $this->connection->getQueryBuilder();
  378. $slash = $builder->createNamedParameter('/');
  379. $mountPoint = $builder->func()->concat(
  380. $builder->func()->concat($slash, 'user_id'),
  381. $slash
  382. );
  383. $userIds = array_map(function (IUser $user) {
  384. return $user->getUID();
  385. }, $users);
  386. $query = $builder->select('m.user_id', 'f.size')
  387. ->from('mounts', 'm')
  388. ->innerJoin('m', 'filecache', 'f',
  389. $builder->expr()->andX(
  390. $builder->expr()->eq('m.storage_id', 'f.storage'),
  391. $builder->expr()->eq('f.path_hash', $builder->createNamedParameter(md5('files')))
  392. ))
  393. ->where($builder->expr()->eq('m.mount_point', $mountPoint))
  394. ->andWhere($builder->expr()->in('m.user_id', $builder->createNamedParameter($userIds, IQueryBuilder::PARAM_STR_ARRAY)));
  395. $result = $query->execute();
  396. $results = [];
  397. while ($row = $result->fetch()) {
  398. $results[$row['user_id']] = $row['size'];
  399. }
  400. $result->closeCursor();
  401. return $results;
  402. }
  403. public function clear(): void {
  404. $this->cacheInfoCache = new CappedMemoryCache();
  405. $this->mountsForUsers = new CappedMemoryCache();
  406. }
  407. public function getMountForPath(IUser $user, string $path): ICachedMountInfo {
  408. $mounts = $this->getMountsForUser($user);
  409. $mountPoints = array_map(function (ICachedMountInfo $mount) {
  410. return $mount->getMountPoint();
  411. }, $mounts);
  412. $mounts = array_combine($mountPoints, $mounts);
  413. $current = $path;
  414. // walk up the directory tree until we find a path that has a mountpoint set
  415. // the loop will return if a mountpoint is found or break if none are found
  416. while (true) {
  417. $mountPoint = $current . '/';
  418. if (isset($mounts[$mountPoint])) {
  419. return $mounts[$mountPoint];
  420. } elseif ($current === '') {
  421. break;
  422. }
  423. $current = dirname($current);
  424. if ($current === '.' || $current === '/') {
  425. $current = '';
  426. }
  427. }
  428. throw new NotFoundException("No cached mount for path " . $path);
  429. }
  430. public function getMountsInPath(IUser $user, string $path): array {
  431. $path = rtrim($path, '/') . '/';
  432. $mounts = $this->getMountsForUser($user);
  433. return array_filter($mounts, function (ICachedMountInfo $mount) use ($path) {
  434. return $mount->getMountPoint() !== $path && str_starts_with($mount->getMountPoint(), $path);
  435. });
  436. }
  437. }