Root.php 8.7 KB

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