You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

403 lines
8.9 KiB

3 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  9. * @author Julius Härtl <jus@bitgrid.net>
  10. * @author Lukas Reschke <lukas@statuscode.ch>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Robin Appelman <robin@icewind.nl>
  13. * @author Roeland Jago Douma <roeland@famdouma.nl>
  14. * @author Stefan Weil <sw@weilnetz.de>
  15. * @author Vincent Petry <pvince81@owncloud.com>
  16. *
  17. * @license AGPL-3.0
  18. *
  19. * This code is free software: you can redistribute it and/or modify
  20. * it under the terms of the GNU Affero General Public License, version 3,
  21. * as published by the Free Software Foundation.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU Affero General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Affero General Public License, version 3,
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>
  30. *
  31. */
  32. namespace OC\Files\Node;
  33. use OC\Cache\CappedMemoryCache;
  34. use OC\Files\Mount\Manager;
  35. use OC\Files\Mount\MountPoint;
  36. use OC\Hooks\PublicEmitter;
  37. use OC\User\NoUserException;
  38. use OCP\Files\Config\IUserMountCache;
  39. use OCP\Files\IRootFolder;
  40. use OCP\Files\NotFoundException;
  41. use OCP\Files\NotPermittedException;
  42. use OCP\ILogger;
  43. use OCP\IUserManager;
  44. /**
  45. * Class Root
  46. *
  47. * Hooks available in scope \OC\Files
  48. * - preWrite(\OCP\Files\Node $node)
  49. * - postWrite(\OCP\Files\Node $node)
  50. * - preCreate(\OCP\Files\Node $node)
  51. * - postCreate(\OCP\Files\Node $node)
  52. * - preDelete(\OCP\Files\Node $node)
  53. * - postDelete(\OCP\Files\Node $node)
  54. * - preTouch(\OC\FilesP\Node $node, int $mtime)
  55. * - postTouch(\OCP\Files\Node $node)
  56. * - preCopy(\OCP\Files\Node $source, \OCP\Files\Node $target)
  57. * - postCopy(\OCP\Files\Node $source, \OCP\Files\Node $target)
  58. * - preRename(\OCP\Files\Node $source, \OCP\Files\Node $target)
  59. * - postRename(\OCP\Files\Node $source, \OCP\Files\Node $target)
  60. *
  61. * @package OC\Files\Node
  62. */
  63. class Root extends Folder implements IRootFolder {
  64. /** @var Manager */
  65. private $mountManager;
  66. /** @var PublicEmitter */
  67. private $emitter;
  68. /** @var null|\OC\User\User */
  69. private $user;
  70. /** @var CappedMemoryCache */
  71. private $userFolderCache;
  72. /** @var IUserMountCache */
  73. private $userMountCache;
  74. /** @var ILogger */
  75. private $logger;
  76. /** @var IUserManager */
  77. private $userManager;
  78. /**
  79. * @param \OC\Files\Mount\Manager $manager
  80. * @param \OC\Files\View $view
  81. * @param \OC\User\User|null $user
  82. * @param IUserMountCache $userMountCache
  83. * @param ILogger $logger
  84. * @param IUserManager $userManager
  85. */
  86. public function __construct($manager,
  87. $view,
  88. $user,
  89. IUserMountCache $userMountCache,
  90. ILogger $logger,
  91. IUserManager $userManager) {
  92. parent::__construct($this, $view, '');
  93. $this->mountManager = $manager;
  94. $this->user = $user;
  95. $this->emitter = new PublicEmitter();
  96. $this->userFolderCache = new CappedMemoryCache();
  97. $this->userMountCache = $userMountCache;
  98. $this->logger = $logger;
  99. $this->userManager = $userManager;
  100. }
  101. /**
  102. * Get the user for which the filesystem is setup
  103. *
  104. * @return \OC\User\User
  105. */
  106. public function getUser() {
  107. return $this->user;
  108. }
  109. /**
  110. * @param string $scope
  111. * @param string $method
  112. * @param callable $callback
  113. */
  114. public function listen($scope, $method, callable $callback) {
  115. $this->emitter->listen($scope, $method, $callback);
  116. }
  117. /**
  118. * @param string $scope optional
  119. * @param string $method optional
  120. * @param callable $callback optional
  121. */
  122. public function removeListener($scope = null, $method = null, callable $callback = null) {
  123. $this->emitter->removeListener($scope, $method, $callback);
  124. }
  125. /**
  126. * @param string $scope
  127. * @param string $method
  128. * @param Node[] $arguments
  129. */
  130. public function emit($scope, $method, $arguments = []) {
  131. $this->emitter->emit($scope, $method, $arguments);
  132. }
  133. /**
  134. * @param \OC\Files\Storage\Storage $storage
  135. * @param string $mountPoint
  136. * @param array $arguments
  137. */
  138. public function mount($storage, $mountPoint, $arguments = []) {
  139. $mount = new MountPoint($storage, $mountPoint, $arguments);
  140. $this->mountManager->addMount($mount);
  141. }
  142. /**
  143. * @param string $mountPoint
  144. * @return \OC\Files\Mount\MountPoint
  145. */
  146. public function getMount($mountPoint) {
  147. return $this->mountManager->find($mountPoint);
  148. }
  149. /**
  150. * @param string $mountPoint
  151. * @return \OC\Files\Mount\MountPoint[]
  152. */
  153. public function getMountsIn($mountPoint) {
  154. return $this->mountManager->findIn($mountPoint);
  155. }
  156. /**
  157. * @param string $storageId
  158. * @return \OC\Files\Mount\MountPoint[]
  159. */
  160. public function getMountByStorageId($storageId) {
  161. return $this->mountManager->findByStorageId($storageId);
  162. }
  163. /**
  164. * @param int $numericId
  165. * @return MountPoint[]
  166. */
  167. public function getMountByNumericStorageId($numericId) {
  168. return $this->mountManager->findByNumericId($numericId);
  169. }
  170. /**
  171. * @param \OC\Files\Mount\MountPoint $mount
  172. */
  173. public function unMount($mount) {
  174. $this->mountManager->remove($mount);
  175. }
  176. /**
  177. * @param string $path
  178. * @throws \OCP\Files\NotFoundException
  179. * @throws \OCP\Files\NotPermittedException
  180. * @return string
  181. */
  182. public function get($path) {
  183. $path = $this->normalizePath($path);
  184. if ($this->isValidPath($path)) {
  185. $fullPath = $this->getFullPath($path);
  186. $fileInfo = $this->view->getFileInfo($fullPath);
  187. if ($fileInfo) {
  188. return $this->createNode($fullPath, $fileInfo);
  189. } else {
  190. throw new NotFoundException($path);
  191. }
  192. } else {
  193. throw new NotPermittedException();
  194. }
  195. }
  196. //most operations can't be done on the root
  197. /**
  198. * @param string $targetPath
  199. * @throws \OCP\Files\NotPermittedException
  200. * @return \OC\Files\Node\Node
  201. */
  202. public function rename($targetPath) {
  203. throw new NotPermittedException();
  204. }
  205. public function delete() {
  206. throw new NotPermittedException();
  207. }
  208. /**
  209. * @param string $targetPath
  210. * @throws \OCP\Files\NotPermittedException
  211. * @return \OC\Files\Node\Node
  212. */
  213. public function copy($targetPath) {
  214. throw new NotPermittedException();
  215. }
  216. /**
  217. * @param int $mtime
  218. * @throws \OCP\Files\NotPermittedException
  219. */
  220. public function touch($mtime = null) {
  221. throw new NotPermittedException();
  222. }
  223. /**
  224. * @return \OC\Files\Storage\Storage
  225. * @throws \OCP\Files\NotFoundException
  226. */
  227. public function getStorage() {
  228. throw new NotFoundException();
  229. }
  230. /**
  231. * @return string
  232. */
  233. public function getPath() {
  234. return '/';
  235. }
  236. /**
  237. * @return string
  238. */
  239. public function getInternalPath() {
  240. return '';
  241. }
  242. /**
  243. * @return int
  244. */
  245. public function getId() {
  246. return null;
  247. }
  248. /**
  249. * @return array
  250. */
  251. public function stat() {
  252. return null;
  253. }
  254. /**
  255. * @return int
  256. */
  257. public function getMTime() {
  258. return null;
  259. }
  260. /**
  261. * @param bool $includeMounts
  262. * @return int
  263. */
  264. public function getSize($includeMounts = true) {
  265. return null;
  266. }
  267. /**
  268. * @return string
  269. */
  270. public function getEtag() {
  271. return null;
  272. }
  273. /**
  274. * @return int
  275. */
  276. public function getPermissions() {
  277. return \OCP\Constants::PERMISSION_CREATE;
  278. }
  279. /**
  280. * @return bool
  281. */
  282. public function isReadable() {
  283. return false;
  284. }
  285. /**
  286. * @return bool
  287. */
  288. public function isUpdateable() {
  289. return false;
  290. }
  291. /**
  292. * @return bool
  293. */
  294. public function isDeletable() {
  295. return false;
  296. }
  297. /**
  298. * @return bool
  299. */
  300. public function isShareable() {
  301. return false;
  302. }
  303. /**
  304. * @return Node
  305. * @throws \OCP\Files\NotFoundException
  306. */
  307. public function getParent() {
  308. throw new NotFoundException();
  309. }
  310. /**
  311. * @return string
  312. */
  313. public function getName() {
  314. return '';
  315. }
  316. /**
  317. * Returns a view to user's files folder
  318. *
  319. * @param string $userId user ID
  320. * @return \OCP\Files\Folder
  321. * @throws NoUserException
  322. * @throws NotPermittedException
  323. */
  324. public function getUserFolder($userId) {
  325. $userObject = $this->userManager->get($userId);
  326. if (is_null($userObject)) {
  327. $this->logger->error(
  328. sprintf(
  329. 'Backends provided no user object for %s',
  330. $userId
  331. ),
  332. [
  333. 'app' => 'files',
  334. ]
  335. );
  336. throw new NoUserException('Backends provided no user object');
  337. }
  338. $userId = $userObject->getUID();
  339. if (!$this->userFolderCache->hasKey($userId)) {
  340. \OC\Files\Filesystem::initMountPoints($userId);
  341. try {
  342. $folder = $this->get('/' . $userId . '/files');
  343. } catch (NotFoundException $e) {
  344. if (!$this->nodeExists('/' . $userId)) {
  345. $this->newFolder('/' . $userId);
  346. }
  347. $folder = $this->newFolder('/' . $userId . '/files');
  348. }
  349. $this->userFolderCache->set($userId, $folder);
  350. }
  351. return $this->userFolderCache->get($userId);
  352. }
  353. public function clearCache() {
  354. $this->userFolderCache = new CappedMemoryCache();
  355. }
  356. public function getUserMountCache() {
  357. return $this->userMountCache;
  358. }
  359. }