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.

428 lines
10 KiB

10 years ago
10 years ago
10 years ago
10 years ago
12 years ago
  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 Morris Jobke <hey@morrisjobke.de>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Vincent Petry <pvince81@owncloud.com>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC\Files\Node;
  28. use OC\Files\Filesystem;
  29. use OCP\Files\FileInfo;
  30. use OCP\Files\InvalidPathException;
  31. use OCP\Files\NotFoundException;
  32. use OCP\Files\NotPermittedException;
  33. // FIXME: this class really should be abstract
  34. class Node implements \OCP\Files\Node {
  35. /**
  36. * @var \OC\Files\View $view
  37. */
  38. protected $view;
  39. /**
  40. * @var \OC\Files\Node\Root $root
  41. */
  42. protected $root;
  43. /**
  44. * @var string $path
  45. */
  46. protected $path;
  47. /**
  48. * @var \OCP\Files\FileInfo
  49. */
  50. protected $fileInfo;
  51. /**
  52. * @param \OC\Files\View $view
  53. * @param \OCP\Files\IRootFolder $root
  54. * @param string $path
  55. * @param FileInfo $fileInfo
  56. */
  57. public function __construct($root, $view, $path, $fileInfo = null) {
  58. $this->view = $view;
  59. $this->root = $root;
  60. $this->path = $path;
  61. $this->fileInfo = $fileInfo;
  62. }
  63. /**
  64. * Creates a Node of the same type that represents a non-existing path
  65. *
  66. * @param string $path path
  67. * @return string non-existing node class
  68. */
  69. protected function createNonExistingNode($path) {
  70. throw new \Exception('Must be implemented by subclasses');
  71. }
  72. /**
  73. * Returns the matching file info
  74. *
  75. * @return FileInfo
  76. * @throws InvalidPathException
  77. * @throws NotFoundException
  78. */
  79. public function getFileInfo() {
  80. if (!Filesystem::isValidPath($this->path)) {
  81. throw new InvalidPathException();
  82. }
  83. if (!$this->fileInfo) {
  84. $fileInfo = $this->view->getFileInfo($this->path);
  85. if ($fileInfo instanceof FileInfo) {
  86. $this->fileInfo = $fileInfo;
  87. } else {
  88. throw new NotFoundException();
  89. }
  90. }
  91. return $this->fileInfo;
  92. }
  93. /**
  94. * @param string[] $hooks
  95. */
  96. protected function sendHooks($hooks) {
  97. foreach ($hooks as $hook) {
  98. $this->root->emit('\OC\Files', $hook, array($this));
  99. }
  100. }
  101. /**
  102. * @param int $permissions
  103. * @return bool
  104. */
  105. protected function checkPermissions($permissions) {
  106. return ($this->getPermissions() & $permissions) === $permissions;
  107. }
  108. public function delete() {
  109. return;
  110. }
  111. /**
  112. * @param int $mtime
  113. * @throws \OCP\Files\NotPermittedException
  114. */
  115. public function touch($mtime = null) {
  116. if ($this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE)) {
  117. $this->sendHooks(array('preTouch'));
  118. $this->view->touch($this->path, $mtime);
  119. $this->sendHooks(array('postTouch'));
  120. if ($this->fileInfo) {
  121. if (is_null($mtime)) {
  122. $mtime = time();
  123. }
  124. $this->fileInfo['mtime'] = $mtime;
  125. }
  126. } else {
  127. throw new NotPermittedException();
  128. }
  129. }
  130. /**
  131. * @return \OC\Files\Storage\Storage
  132. * @throws \OCP\Files\NotFoundException
  133. */
  134. public function getStorage() {
  135. list($storage,) = $this->view->resolvePath($this->path);
  136. return $storage;
  137. }
  138. /**
  139. * @return string
  140. */
  141. public function getPath() {
  142. return $this->path;
  143. }
  144. /**
  145. * @return string
  146. */
  147. public function getInternalPath() {
  148. list(, $internalPath) = $this->view->resolvePath($this->path);
  149. return $internalPath;
  150. }
  151. /**
  152. * @return int
  153. * @throws InvalidPathException
  154. * @throws NotFoundException
  155. */
  156. public function getId() {
  157. return $this->getFileInfo()->getId();
  158. }
  159. /**
  160. * @return array
  161. */
  162. public function stat() {
  163. return $this->view->stat($this->path);
  164. }
  165. /**
  166. * @return int
  167. * @throws InvalidPathException
  168. * @throws NotFoundException
  169. */
  170. public function getMTime() {
  171. return $this->getFileInfo()->getMTime();
  172. }
  173. /**
  174. * @return int
  175. * @throws InvalidPathException
  176. * @throws NotFoundException
  177. */
  178. public function getSize() {
  179. return $this->getFileInfo()->getSize();
  180. }
  181. /**
  182. * @return string
  183. * @throws InvalidPathException
  184. * @throws NotFoundException
  185. */
  186. public function getEtag() {
  187. return $this->getFileInfo()->getEtag();
  188. }
  189. /**
  190. * @return int
  191. * @throws InvalidPathException
  192. * @throws NotFoundException
  193. */
  194. public function getPermissions() {
  195. return $this->getFileInfo()->getPermissions();
  196. }
  197. /**
  198. * @return bool
  199. * @throws InvalidPathException
  200. * @throws NotFoundException
  201. */
  202. public function isReadable() {
  203. return $this->getFileInfo()->isReadable();
  204. }
  205. /**
  206. * @return bool
  207. * @throws InvalidPathException
  208. * @throws NotFoundException
  209. */
  210. public function isUpdateable() {
  211. return $this->getFileInfo()->isUpdateable();
  212. }
  213. /**
  214. * @return bool
  215. * @throws InvalidPathException
  216. * @throws NotFoundException
  217. */
  218. public function isDeletable() {
  219. return $this->getFileInfo()->isDeletable();
  220. }
  221. /**
  222. * @return bool
  223. * @throws InvalidPathException
  224. * @throws NotFoundException
  225. */
  226. public function isShareable() {
  227. return $this->getFileInfo()->isShareable();
  228. }
  229. /**
  230. * @return bool
  231. * @throws InvalidPathException
  232. * @throws NotFoundException
  233. */
  234. public function isCreatable() {
  235. return $this->getFileInfo()->isCreatable();
  236. }
  237. /**
  238. * @return Node
  239. */
  240. public function getParent() {
  241. return $this->root->get(dirname($this->path));
  242. }
  243. /**
  244. * @return string
  245. */
  246. public function getName() {
  247. return basename($this->path);
  248. }
  249. /**
  250. * @param string $path
  251. * @return string
  252. */
  253. protected function normalizePath($path) {
  254. if ($path === '' or $path === '/') {
  255. return '/';
  256. }
  257. //no windows style slashes
  258. $path = str_replace('\\', '/', $path);
  259. //add leading slash
  260. if ($path[0] !== '/') {
  261. $path = '/' . $path;
  262. }
  263. //remove duplicate slashes
  264. while (strpos($path, '//') !== false) {
  265. $path = str_replace('//', '/', $path);
  266. }
  267. //remove trailing slash
  268. $path = rtrim($path, '/');
  269. return $path;
  270. }
  271. /**
  272. * check if the requested path is valid
  273. *
  274. * @param string $path
  275. * @return bool
  276. */
  277. public function isValidPath($path) {
  278. if (!$path || $path[0] !== '/') {
  279. $path = '/' . $path;
  280. }
  281. if (strstr($path, '/../') || strrchr($path, '/') === '/..') {
  282. return false;
  283. }
  284. return true;
  285. }
  286. public function isMounted() {
  287. return $this->getFileInfo()->isMounted();
  288. }
  289. public function isShared() {
  290. return $this->getFileInfo()->isShared();
  291. }
  292. public function getMimeType() {
  293. return $this->getFileInfo()->getMimetype();
  294. }
  295. public function getMimePart() {
  296. return $this->getFileInfo()->getMimePart();
  297. }
  298. public function getType() {
  299. return $this->getFileInfo()->getType();
  300. }
  301. public function isEncrypted() {
  302. return $this->getFileInfo()->isEncrypted();
  303. }
  304. public function getMountPoint() {
  305. return $this->getFileInfo()->getMountPoint();
  306. }
  307. public function getOwner() {
  308. return $this->getFileInfo()->getOwner();
  309. }
  310. public function getChecksum() {
  311. return;
  312. }
  313. /**
  314. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  315. * @throws \OCP\Lock\LockedException
  316. */
  317. public function lock($type) {
  318. $this->view->lockFile($this->path, $type);
  319. }
  320. /**
  321. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  322. * @throws \OCP\Lock\LockedException
  323. */
  324. public function changeLock($type) {
  325. $this->view->changeLock($this->path, $type);
  326. }
  327. /**
  328. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  329. * @throws \OCP\Lock\LockedException
  330. */
  331. public function unlock($type) {
  332. $this->view->unlockFile($this->path, $type);
  333. }
  334. /**
  335. * @param string $targetPath
  336. * @throws \OCP\Files\NotPermittedException if copy not allowed or failed
  337. * @return \OC\Files\Node\Node
  338. */
  339. public function copy($targetPath) {
  340. $targetPath = $this->normalizePath($targetPath);
  341. $parent = $this->root->get(dirname($targetPath));
  342. if ($parent instanceof Folder and $this->isValidPath($targetPath) and $parent->isCreatable()) {
  343. $nonExisting = $this->createNonExistingNode($targetPath);
  344. $this->root->emit('\OC\Files', 'preCopy', [$this, $nonExisting]);
  345. $this->root->emit('\OC\Files', 'preWrite', [$nonExisting]);
  346. if (!$this->view->copy($this->path, $targetPath)) {
  347. throw new NotPermittedException('Could not copy ' . $this->path . ' to ' . $targetPath);
  348. }
  349. $targetNode = $this->root->get($targetPath);
  350. $this->root->emit('\OC\Files', 'postCopy', [$this, $targetNode]);
  351. $this->root->emit('\OC\Files', 'postWrite', [$targetNode]);
  352. return $targetNode;
  353. } else {
  354. throw new NotPermittedException('No permission to copy to path ' . $targetPath);
  355. }
  356. }
  357. /**
  358. * @param string $targetPath
  359. * @throws \OCP\Files\NotPermittedException if move not allowed or failed
  360. * @return \OC\Files\Node\Node
  361. */
  362. public function move($targetPath) {
  363. $targetPath = $this->normalizePath($targetPath);
  364. $parent = $this->root->get(dirname($targetPath));
  365. if ($parent instanceof Folder and $this->isValidPath($targetPath) and $parent->isCreatable()) {
  366. $nonExisting = $this->createNonExistingNode($targetPath);
  367. $this->root->emit('\OC\Files', 'preRename', [$this, $nonExisting]);
  368. $this->root->emit('\OC\Files', 'preWrite', [$nonExisting]);
  369. if (!$this->view->rename($this->path, $targetPath)) {
  370. throw new NotPermittedException('Could not move ' . $this->path . ' to ' . $targetPath);
  371. }
  372. $targetNode = $this->root->get($targetPath);
  373. $this->root->emit('\OC\Files', 'postRename', [$this, $targetNode]);
  374. $this->root->emit('\OC\Files', 'postWrite', [$targetNode]);
  375. $this->path = $targetPath;
  376. return $targetNode;
  377. } else {
  378. throw new NotPermittedException('No permission to move to path ' . $targetPath);
  379. }
  380. }
  381. }