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.

357 lines
7.4 KiB

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