1
0

Root.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Files\Node;
  8. use OC\Files\FileInfo;
  9. use OC\Files\Mount\Manager;
  10. use OC\Files\Mount\MountPoint;
  11. use OC\Files\Utils\PathHelper;
  12. use OC\Files\View;
  13. use OC\Hooks\PublicEmitter;
  14. use OC\User\NoUserException;
  15. use OCP\Cache\CappedMemoryCache;
  16. use OCP\EventDispatcher\IEventDispatcher;
  17. use OCP\Files\Cache\ICacheEntry;
  18. use OCP\Files\Config\IUserMountCache;
  19. use OCP\Files\Events\Node\FilesystemTornDownEvent;
  20. use OCP\Files\IRootFolder;
  21. use OCP\Files\Mount\IMountPoint;
  22. use OCP\Files\Node as INode;
  23. use OCP\Files\NotFoundException;
  24. use OCP\Files\NotPermittedException;
  25. use OCP\ICache;
  26. use OCP\ICacheFactory;
  27. use OCP\IUser;
  28. use OCP\IUserManager;
  29. use OCP\Server;
  30. use Psr\Log\LoggerInterface;
  31. /**
  32. * Class Root
  33. *
  34. * Hooks available in scope \OC\Files
  35. * - preWrite(\OCP\Files\Node $node)
  36. * - postWrite(\OCP\Files\Node $node)
  37. * - preCreate(\OCP\Files\Node $node)
  38. * - postCreate(\OCP\Files\Node $node)
  39. * - preDelete(\OCP\Files\Node $node)
  40. * - postDelete(\OCP\Files\Node $node)
  41. * - preTouch(\OC\FilesP\Node $node, int $mtime)
  42. * - postTouch(\OCP\Files\Node $node)
  43. * - preCopy(\OCP\Files\Node $source, \OCP\Files\Node $target)
  44. * - postCopy(\OCP\Files\Node $source, \OCP\Files\Node $target)
  45. * - preRename(\OCP\Files\Node $source, \OCP\Files\Node $target)
  46. * - postRename(\OCP\Files\Node $source, \OCP\Files\Node $target)
  47. *
  48. * @package OC\Files\Node
  49. */
  50. class Root extends Folder implements IRootFolder {
  51. private Manager $mountManager;
  52. private PublicEmitter $emitter;
  53. private ?IUser $user;
  54. private CappedMemoryCache $userFolderCache;
  55. private IUserMountCache $userMountCache;
  56. private LoggerInterface $logger;
  57. private IUserManager $userManager;
  58. private IEventDispatcher $eventDispatcher;
  59. private ICache $pathByIdCache;
  60. /**
  61. * @param Manager $manager
  62. * @param View $view
  63. * @param IUser|null $user
  64. */
  65. public function __construct(
  66. $manager,
  67. $view,
  68. $user,
  69. IUserMountCache $userMountCache,
  70. LoggerInterface $logger,
  71. IUserManager $userManager,
  72. IEventDispatcher $eventDispatcher,
  73. ICacheFactory $cacheFactory,
  74. ) {
  75. parent::__construct($this, $view, '');
  76. $this->mountManager = $manager;
  77. $this->user = $user;
  78. $this->emitter = new PublicEmitter();
  79. $this->userFolderCache = new CappedMemoryCache();
  80. $this->userMountCache = $userMountCache;
  81. $this->logger = $logger;
  82. $this->userManager = $userManager;
  83. $eventDispatcher->addListener(FilesystemTornDownEvent::class, function () {
  84. $this->userFolderCache = new CappedMemoryCache();
  85. });
  86. $this->pathByIdCache = $cacheFactory->createLocal('path-by-id');
  87. }
  88. /**
  89. * Get the user for which the filesystem is setup
  90. *
  91. * @return \OC\User\User
  92. */
  93. public function getUser() {
  94. return $this->user;
  95. }
  96. /**
  97. * @param string $scope
  98. * @param string $method
  99. * @param callable $callback
  100. */
  101. public function listen($scope, $method, callable $callback) {
  102. $this->emitter->listen($scope, $method, $callback);
  103. }
  104. /**
  105. * @param string $scope optional
  106. * @param string $method optional
  107. * @param callable $callback optional
  108. */
  109. public function removeListener($scope = null, $method = null, ?callable $callback = null) {
  110. $this->emitter->removeListener($scope, $method, $callback);
  111. }
  112. /**
  113. * @param string $scope
  114. * @param string $method
  115. * @param Node[] $arguments
  116. */
  117. public function emit($scope, $method, $arguments = []) {
  118. $this->emitter->emit($scope, $method, $arguments);
  119. }
  120. /**
  121. * @param \OC\Files\Storage\Storage $storage
  122. * @param string $mountPoint
  123. * @param array $arguments
  124. */
  125. public function mount($storage, $mountPoint, $arguments = []) {
  126. $mount = new MountPoint($storage, $mountPoint, $arguments);
  127. $this->mountManager->addMount($mount);
  128. }
  129. public function getMount(string $mountPoint): IMountPoint {
  130. return $this->mountManager->find($mountPoint);
  131. }
  132. /**
  133. * @param string $mountPoint
  134. * @return \OC\Files\Mount\MountPoint[]
  135. */
  136. public function getMountsIn(string $mountPoint): array {
  137. return $this->mountManager->findIn($mountPoint);
  138. }
  139. /**
  140. * @param string $storageId
  141. * @return \OC\Files\Mount\MountPoint[]
  142. */
  143. public function getMountByStorageId($storageId) {
  144. return $this->mountManager->findByStorageId($storageId);
  145. }
  146. /**
  147. * @param int $numericId
  148. * @return MountPoint[]
  149. */
  150. public function getMountByNumericStorageId($numericId) {
  151. return $this->mountManager->findByNumericId($numericId);
  152. }
  153. /**
  154. * @param \OC\Files\Mount\MountPoint $mount
  155. */
  156. public function unMount($mount) {
  157. $this->mountManager->remove($mount);
  158. }
  159. public function get($path) {
  160. $path = $this->normalizePath($path);
  161. if ($this->isValidPath($path)) {
  162. $fullPath = $this->getFullPath($path);
  163. $fileInfo = $this->view->getFileInfo($fullPath, false);
  164. if ($fileInfo) {
  165. return $this->createNode($fullPath, $fileInfo, false);
  166. } else {
  167. throw new NotFoundException($path);
  168. }
  169. } else {
  170. throw new NotPermittedException();
  171. }
  172. }
  173. //most operations can't be done on the root
  174. /**
  175. * @param string $targetPath
  176. * @return Node
  177. * @throws \OCP\Files\NotPermittedException
  178. */
  179. public function rename($targetPath) {
  180. throw new NotPermittedException();
  181. }
  182. public function delete() {
  183. throw new NotPermittedException();
  184. }
  185. /**
  186. * @param string $targetPath
  187. * @return Node
  188. * @throws \OCP\Files\NotPermittedException
  189. */
  190. public function copy($targetPath) {
  191. throw new NotPermittedException();
  192. }
  193. /**
  194. * @param int $mtime
  195. * @throws \OCP\Files\NotPermittedException
  196. */
  197. public function touch($mtime = null) {
  198. throw new NotPermittedException();
  199. }
  200. /**
  201. * @return \OC\Files\Storage\Storage
  202. * @throws \OCP\Files\NotFoundException
  203. */
  204. public function getStorage() {
  205. throw new NotFoundException();
  206. }
  207. /**
  208. * @return string
  209. */
  210. public function getPath() {
  211. return '/';
  212. }
  213. /**
  214. * @return string
  215. */
  216. public function getInternalPath() {
  217. return '';
  218. }
  219. /**
  220. * @return int
  221. */
  222. public function getId() {
  223. return 0;
  224. }
  225. /**
  226. * @return array
  227. */
  228. public function stat() {
  229. return [];
  230. }
  231. /**
  232. * @return int
  233. */
  234. public function getMTime() {
  235. return 0;
  236. }
  237. /**
  238. * @param bool $includeMounts
  239. * @return int|float
  240. */
  241. public function getSize($includeMounts = true): int|float {
  242. return 0;
  243. }
  244. /**
  245. * @return string
  246. */
  247. public function getEtag() {
  248. return '';
  249. }
  250. /**
  251. * @return int
  252. */
  253. public function getPermissions() {
  254. return \OCP\Constants::PERMISSION_CREATE;
  255. }
  256. /**
  257. * @return bool
  258. */
  259. public function isReadable() {
  260. return false;
  261. }
  262. /**
  263. * @return bool
  264. */
  265. public function isUpdateable() {
  266. return false;
  267. }
  268. /**
  269. * @return bool
  270. */
  271. public function isDeletable() {
  272. return false;
  273. }
  274. /**
  275. * @return bool
  276. */
  277. public function isShareable() {
  278. return false;
  279. }
  280. /**
  281. * @throws \OCP\Files\NotFoundException
  282. */
  283. public function getParent(): INode|IRootFolder {
  284. throw new NotFoundException();
  285. }
  286. /**
  287. * @return string
  288. */
  289. public function getName() {
  290. return '';
  291. }
  292. /**
  293. * Returns a view to user's files folder
  294. *
  295. * @param string $userId user ID
  296. * @return \OCP\Files\Folder
  297. * @throws NoUserException
  298. * @throws NotPermittedException
  299. */
  300. public function getUserFolder($userId) {
  301. $userObject = $this->userManager->get($userId);
  302. if (is_null($userObject)) {
  303. $e = new NoUserException('Backends provided no user object');
  304. $this->logger->error(
  305. sprintf(
  306. 'Backends provided no user object for %s',
  307. $userId
  308. ),
  309. [
  310. 'app' => 'files',
  311. 'exception' => $e,
  312. ]
  313. );
  314. throw $e;
  315. }
  316. $userId = $userObject->getUID();
  317. if (!$this->userFolderCache->hasKey($userId)) {
  318. if ($this->mountManager->getSetupManager()->isSetupComplete($userObject)) {
  319. try {
  320. $folder = $this->get('/' . $userId . '/files');
  321. if (!$folder instanceof \OCP\Files\Folder) {
  322. throw new \Exception("Account folder for \"$userId\" exists as a file");
  323. }
  324. } catch (NotFoundException $e) {
  325. if (!$this->nodeExists('/' . $userId)) {
  326. $this->newFolder('/' . $userId);
  327. }
  328. $folder = $this->newFolder('/' . $userId . '/files');
  329. }
  330. } else {
  331. $folder = new LazyUserFolder($this, $userObject, $this->mountManager);
  332. }
  333. $this->userFolderCache->set($userId, $folder);
  334. }
  335. return $this->userFolderCache->get($userId);
  336. }
  337. public function getUserMountCache() {
  338. return $this->userMountCache;
  339. }
  340. public function getFirstNodeByIdInPath(int $id, string $path): ?INode {
  341. // scope the cache by user, so we don't return nodes for different users
  342. if ($this->user) {
  343. $cachedPath = $this->pathByIdCache->get($this->user->getUID() . '::' . $id);
  344. if ($cachedPath && str_starts_with($path, $cachedPath)) {
  345. // getting the node by path is significantly cheaper than finding it by id
  346. $node = $this->get($cachedPath);
  347. // by validating that the cached path still has the requested fileid we can work around the need to invalidate the cached path
  348. // if the cached path is invalid or a different file now we fall back to the uncached logic
  349. if ($node && $node->getId() === $id) {
  350. return $node;
  351. }
  352. }
  353. }
  354. $node = current($this->getByIdInPath($id, $path));
  355. if (!$node) {
  356. return null;
  357. }
  358. if ($this->user) {
  359. $this->pathByIdCache->set($this->user->getUID() . '::' . $id, $node->getPath());
  360. }
  361. return $node;
  362. }
  363. /**
  364. * @param int $id
  365. * @return Node[]
  366. */
  367. public function getByIdInPath(int $id, string $path): array {
  368. $mountCache = $this->getUserMountCache();
  369. if (strpos($path, '/', 1) > 0) {
  370. [, $user] = explode('/', $path);
  371. } else {
  372. $user = null;
  373. }
  374. $mountsContainingFile = $mountCache->getMountsForFileId($id, $user);
  375. // if the mount isn't in the cache yet, perform a setup first, then try again
  376. if (count($mountsContainingFile) === 0) {
  377. $this->mountManager->getSetupManager()->setupForPath($path, true);
  378. $mountsContainingFile = $mountCache->getMountsForFileId($id, $user);
  379. }
  380. // when a user has access through the same storage through multiple paths
  381. // (such as an external storage that is both mounted for a user and shared to the user)
  382. // the mount cache will only hold a single entry for the storage
  383. // this can lead to issues as the different ways the user has access to a storage can have different permissions
  384. //
  385. // so instead of using the cached entries directly, we instead filter the current mounts by the rootid of the cache entry
  386. $mountRootIds = array_map(function ($mount) {
  387. return $mount->getRootId();
  388. }, $mountsContainingFile);
  389. $mountRootPaths = array_map(function ($mount) {
  390. return $mount->getRootInternalPath();
  391. }, $mountsContainingFile);
  392. $mountProviders = array_unique(array_map(function ($mount) {
  393. return $mount->getMountProvider();
  394. }, $mountsContainingFile));
  395. $mountRoots = array_combine($mountRootIds, $mountRootPaths);
  396. $mounts = $this->mountManager->getMountsByMountProvider($path, $mountProviders);
  397. $mountsContainingFile = array_filter($mounts, function ($mount) use ($mountRoots) {
  398. return isset($mountRoots[$mount->getStorageRootId()]);
  399. });
  400. if (count($mountsContainingFile) === 0) {
  401. if ($user === $this->getAppDataDirectoryName()) {
  402. $folder = $this->get($path);
  403. if ($folder instanceof Folder) {
  404. return $folder->getByIdInRootMount($id);
  405. } else {
  406. throw new \Exception('getByIdInPath with non folder');
  407. }
  408. }
  409. return [];
  410. }
  411. $nodes = array_map(function (IMountPoint $mount) use ($id, $mountRoots) {
  412. $rootInternalPath = $mountRoots[$mount->getStorageRootId()];
  413. $cacheEntry = $mount->getStorage()->getCache()->get($id);
  414. if (!$cacheEntry) {
  415. return null;
  416. }
  417. // cache jails will hide the "true" internal path
  418. $internalPath = ltrim($rootInternalPath . '/' . $cacheEntry->getPath(), '/');
  419. $pathRelativeToMount = substr($internalPath, strlen($rootInternalPath));
  420. $pathRelativeToMount = ltrim($pathRelativeToMount, '/');
  421. $absolutePath = rtrim($mount->getMountPoint() . $pathRelativeToMount, '/');
  422. $storage = $mount->getStorage();
  423. if ($storage === null) {
  424. return null;
  425. }
  426. $ownerId = $storage->getOwner($pathRelativeToMount);
  427. if ($ownerId !== false) {
  428. $owner = Server::get(IUserManager::class)->get($ownerId);
  429. } else {
  430. $owner = null;
  431. }
  432. return $this->createNode($absolutePath, new FileInfo(
  433. $absolutePath,
  434. $storage,
  435. $cacheEntry->getPath(),
  436. $cacheEntry,
  437. $mount,
  438. $owner,
  439. ));
  440. }, $mountsContainingFile);
  441. $nodes = array_filter($nodes);
  442. $folders = array_filter($nodes, function (Node $node) use ($path) {
  443. return PathHelper::getRelativePath($path, $node->getPath()) !== null;
  444. });
  445. usort($folders, function ($a, $b) {
  446. return $b->getPath() <=> $a->getPath();
  447. });
  448. return $folders;
  449. }
  450. public function getNodeFromCacheEntryAndMount(ICacheEntry $cacheEntry, IMountPoint $mountPoint): INode {
  451. $path = $cacheEntry->getPath();
  452. $fullPath = $mountPoint->getMountPoint() . $path;
  453. // todo: LazyNode?
  454. $info = new FileInfo($fullPath, $mountPoint->getStorage(), $path, $cacheEntry, $mountPoint);
  455. $parentPath = dirname($fullPath);
  456. $parent = new LazyFolder($this, function () use ($parentPath) {
  457. $parent = $this->get($parentPath);
  458. if ($parent instanceof \OCP\Files\Folder) {
  459. return $parent;
  460. } else {
  461. throw new \Exception("parent $parentPath is not a folder");
  462. }
  463. }, [
  464. 'path' => $parentPath,
  465. ]);
  466. $isDir = $info->getType() === FileInfo::TYPE_FOLDER;
  467. $view = new View('');
  468. if ($isDir) {
  469. return new Folder($this, $view, $path, $info, $parent);
  470. } else {
  471. return new File($this, $view, $path, $info, $parent);
  472. }
  473. }
  474. }