Node.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Julius Härtl <jus@bitgrid.net>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Robin Appelman <robin@icewind.nl>
  12. * @author Roeland Jago Douma <roeland@famdouma.nl>
  13. * @author Vincent Petry <vincent@nextcloud.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\Files\Filesystem;
  32. use OC\Files\Mount\MoveableMount;
  33. use OC\Files\Utils\PathHelper;
  34. use OCP\Files\FileInfo;
  35. use OCP\Files\InvalidPathException;
  36. use OCP\Files\NotFoundException;
  37. use OCP\Files\NotPermittedException;
  38. use OCP\Lock\LockedException;
  39. use Symfony\Component\EventDispatcher\GenericEvent;
  40. // FIXME: this class really should be abstract
  41. class Node implements \OCP\Files\Node {
  42. /**
  43. * @var \OC\Files\View $view
  44. */
  45. protected $view;
  46. /**
  47. * @var \OC\Files\Node\Root $root
  48. */
  49. protected $root;
  50. /**
  51. * @var string $path
  52. */
  53. protected $path;
  54. /**
  55. * @var \OCP\Files\FileInfo
  56. */
  57. protected $fileInfo;
  58. /**
  59. * @var Node|null
  60. */
  61. protected $parent;
  62. /**
  63. * @param \OC\Files\View $view
  64. * @param \OCP\Files\IRootFolder $root
  65. * @param string $path
  66. * @param FileInfo $fileInfo
  67. */
  68. public function __construct($root, $view, $path, $fileInfo = null, ?Node $parent = null) {
  69. $this->view = $view;
  70. $this->root = $root;
  71. $this->path = $path;
  72. $this->fileInfo = $fileInfo;
  73. $this->parent = $parent;
  74. }
  75. /**
  76. * Creates a Node of the same type that represents a non-existing path
  77. *
  78. * @param string $path path
  79. * @return string non-existing node class
  80. * @throws \Exception
  81. */
  82. protected function createNonExistingNode($path) {
  83. throw new \Exception('Must be implemented by subclasses');
  84. }
  85. /**
  86. * Returns the matching file info
  87. *
  88. * @return FileInfo
  89. * @throws InvalidPathException
  90. * @throws NotFoundException
  91. */
  92. public function getFileInfo() {
  93. if (!Filesystem::isValidPath($this->path)) {
  94. throw new InvalidPathException();
  95. }
  96. if (!$this->fileInfo) {
  97. $fileInfo = $this->view->getFileInfo($this->path);
  98. if ($fileInfo instanceof FileInfo) {
  99. $this->fileInfo = $fileInfo;
  100. } else {
  101. throw new NotFoundException();
  102. }
  103. }
  104. return $this->fileInfo;
  105. }
  106. /**
  107. * @param string[] $hooks
  108. */
  109. protected function sendHooks($hooks, array $args = null) {
  110. $args = !empty($args) ? $args : [$this];
  111. $dispatcher = \OC::$server->getEventDispatcher();
  112. foreach ($hooks as $hook) {
  113. $this->root->emit('\OC\Files', $hook, $args);
  114. $dispatcher->dispatch('\OCP\Files::' . $hook, new GenericEvent($args));
  115. }
  116. }
  117. /**
  118. * @param int $permissions
  119. * @return bool
  120. * @throws InvalidPathException
  121. * @throws NotFoundException
  122. */
  123. protected function checkPermissions($permissions) {
  124. return ($this->getPermissions() & $permissions) === $permissions;
  125. }
  126. public function delete() {
  127. }
  128. /**
  129. * @param int $mtime
  130. * @throws InvalidPathException
  131. * @throws NotFoundException
  132. * @throws NotPermittedException
  133. */
  134. public function touch($mtime = null) {
  135. if ($this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE)) {
  136. $this->sendHooks(['preTouch']);
  137. $this->view->touch($this->path, $mtime);
  138. $this->sendHooks(['postTouch']);
  139. if ($this->fileInfo) {
  140. if (is_null($mtime)) {
  141. $mtime = time();
  142. }
  143. $this->fileInfo['mtime'] = $mtime;
  144. }
  145. } else {
  146. throw new NotPermittedException();
  147. }
  148. }
  149. public function getStorage() {
  150. $storage = $this->getMountPoint()->getStorage();
  151. if (!$storage) {
  152. throw new \Exception("No storage for node");
  153. }
  154. return $storage;
  155. }
  156. /**
  157. * @return string
  158. */
  159. public function getPath() {
  160. return $this->path;
  161. }
  162. /**
  163. * @return string
  164. */
  165. public function getInternalPath() {
  166. return $this->getFileInfo()->getInternalPath();
  167. }
  168. /**
  169. * @return int
  170. * @throws InvalidPathException
  171. * @throws NotFoundException
  172. */
  173. public function getId() {
  174. return $this->getFileInfo()->getId();
  175. }
  176. /**
  177. * @return array
  178. */
  179. public function stat() {
  180. return $this->view->stat($this->path);
  181. }
  182. /**
  183. * @return int
  184. * @throws InvalidPathException
  185. * @throws NotFoundException
  186. */
  187. public function getMTime() {
  188. return $this->getFileInfo()->getMTime();
  189. }
  190. /**
  191. * @param bool $includeMounts
  192. * @return int
  193. * @throws InvalidPathException
  194. * @throws NotFoundException
  195. */
  196. public function getSize($includeMounts = true) {
  197. return $this->getFileInfo()->getSize($includeMounts);
  198. }
  199. /**
  200. * @return string
  201. * @throws InvalidPathException
  202. * @throws NotFoundException
  203. */
  204. public function getEtag() {
  205. return $this->getFileInfo()->getEtag();
  206. }
  207. /**
  208. * @return int
  209. * @throws InvalidPathException
  210. * @throws NotFoundException
  211. */
  212. public function getPermissions() {
  213. return $this->getFileInfo()->getPermissions();
  214. }
  215. /**
  216. * @return bool
  217. * @throws InvalidPathException
  218. * @throws NotFoundException
  219. */
  220. public function isReadable() {
  221. return $this->getFileInfo()->isReadable();
  222. }
  223. /**
  224. * @return bool
  225. * @throws InvalidPathException
  226. * @throws NotFoundException
  227. */
  228. public function isUpdateable() {
  229. return $this->getFileInfo()->isUpdateable();
  230. }
  231. /**
  232. * @return bool
  233. * @throws InvalidPathException
  234. * @throws NotFoundException
  235. */
  236. public function isDeletable() {
  237. return $this->getFileInfo()->isDeletable();
  238. }
  239. /**
  240. * @return bool
  241. * @throws InvalidPathException
  242. * @throws NotFoundException
  243. */
  244. public function isShareable() {
  245. return $this->getFileInfo()->isShareable();
  246. }
  247. /**
  248. * @return bool
  249. * @throws InvalidPathException
  250. * @throws NotFoundException
  251. */
  252. public function isCreatable() {
  253. return $this->getFileInfo()->isCreatable();
  254. }
  255. /**
  256. * @return Node
  257. */
  258. public function getParent() {
  259. if ($this->parent === null) {
  260. $newPath = dirname($this->path);
  261. if ($newPath === '' || $newPath === '.' || $newPath === '/') {
  262. return $this->root;
  263. }
  264. $this->parent = $this->root->get($newPath);
  265. }
  266. return $this->parent;
  267. }
  268. /**
  269. * @return string
  270. */
  271. public function getName() {
  272. return basename($this->path);
  273. }
  274. /**
  275. * @param string $path
  276. * @return string
  277. */
  278. protected function normalizePath($path) {
  279. return PathHelper::normalizePath($path);
  280. }
  281. /**
  282. * check if the requested path is valid
  283. *
  284. * @param string $path
  285. * @return bool
  286. */
  287. public function isValidPath($path) {
  288. if (!$path || $path[0] !== '/') {
  289. $path = '/' . $path;
  290. }
  291. if (strstr($path, '/../') || strrchr($path, '/') === '/..') {
  292. return false;
  293. }
  294. return true;
  295. }
  296. public function isMounted() {
  297. return $this->getFileInfo()->isMounted();
  298. }
  299. public function isShared() {
  300. return $this->getFileInfo()->isShared();
  301. }
  302. public function getMimeType() {
  303. return $this->getFileInfo()->getMimetype();
  304. }
  305. public function getMimePart() {
  306. return $this->getFileInfo()->getMimePart();
  307. }
  308. public function getType() {
  309. return $this->getFileInfo()->getType();
  310. }
  311. public function isEncrypted() {
  312. return $this->getFileInfo()->isEncrypted();
  313. }
  314. public function getMountPoint() {
  315. return $this->getFileInfo()->getMountPoint();
  316. }
  317. public function getOwner() {
  318. return $this->getFileInfo()->getOwner();
  319. }
  320. public function getChecksum() {
  321. }
  322. public function getExtension(): string {
  323. return $this->getFileInfo()->getExtension();
  324. }
  325. /**
  326. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  327. * @throws LockedException
  328. */
  329. public function lock($type) {
  330. $this->view->lockFile($this->path, $type);
  331. }
  332. /**
  333. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  334. * @throws LockedException
  335. */
  336. public function changeLock($type) {
  337. $this->view->changeLock($this->path, $type);
  338. }
  339. /**
  340. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  341. * @throws LockedException
  342. */
  343. public function unlock($type) {
  344. $this->view->unlockFile($this->path, $type);
  345. }
  346. /**
  347. * @param string $targetPath
  348. * @return \OC\Files\Node\Node
  349. * @throws InvalidPathException
  350. * @throws NotFoundException
  351. * @throws NotPermittedException if copy not allowed or failed
  352. */
  353. public function copy($targetPath) {
  354. $targetPath = $this->normalizePath($targetPath);
  355. $parent = $this->root->get(dirname($targetPath));
  356. if ($parent instanceof Folder and $this->isValidPath($targetPath) and $parent->isCreatable()) {
  357. $nonExisting = $this->createNonExistingNode($targetPath);
  358. $this->sendHooks(['preCopy'], [$this, $nonExisting]);
  359. $this->sendHooks(['preWrite'], [$nonExisting]);
  360. if (!$this->view->copy($this->path, $targetPath)) {
  361. throw new NotPermittedException('Could not copy ' . $this->path . ' to ' . $targetPath);
  362. }
  363. $targetNode = $this->root->get($targetPath);
  364. $this->sendHooks(['postCopy'], [$this, $targetNode]);
  365. $this->sendHooks(['postWrite'], [$targetNode]);
  366. return $targetNode;
  367. } else {
  368. throw new NotPermittedException('No permission to copy to path ' . $targetPath);
  369. }
  370. }
  371. /**
  372. * @param string $targetPath
  373. * @return \OC\Files\Node\Node
  374. * @throws InvalidPathException
  375. * @throws NotFoundException
  376. * @throws NotPermittedException if move not allowed or failed
  377. * @throws LockedException
  378. */
  379. public function move($targetPath) {
  380. $targetPath = $this->normalizePath($targetPath);
  381. $parent = $this->root->get(dirname($targetPath));
  382. if (
  383. $parent instanceof Folder and
  384. $this->isValidPath($targetPath) and
  385. (
  386. $parent->isCreatable() ||
  387. ($parent->getInternalPath() === '' && $parent->getMountPoint() instanceof MoveableMount)
  388. )
  389. ) {
  390. $nonExisting = $this->createNonExistingNode($targetPath);
  391. $this->sendHooks(['preRename'], [$this, $nonExisting]);
  392. $this->sendHooks(['preWrite'], [$nonExisting]);
  393. if (!$this->view->rename($this->path, $targetPath)) {
  394. throw new NotPermittedException('Could not move ' . $this->path . ' to ' . $targetPath);
  395. }
  396. $mountPoint = $this->getMountPoint();
  397. if ($mountPoint) {
  398. // update the cached fileinfo with the new (internal) path
  399. /** @var \OC\Files\FileInfo $oldFileInfo */
  400. $oldFileInfo = $this->getFileInfo();
  401. $this->fileInfo = new \OC\Files\FileInfo($targetPath, $oldFileInfo->getStorage(), $mountPoint->getInternalPath($targetPath), $oldFileInfo->getData(), $mountPoint, $oldFileInfo->getOwner());
  402. }
  403. $targetNode = $this->root->get($targetPath);
  404. $this->sendHooks(['postRename'], [$this, $targetNode]);
  405. $this->sendHooks(['postWrite'], [$targetNode]);
  406. $this->path = $targetPath;
  407. return $targetNode;
  408. } else {
  409. throw new NotPermittedException('No permission to move to path ' . $targetPath);
  410. }
  411. }
  412. public function getCreationTime(): int {
  413. return $this->getFileInfo()->getCreationTime();
  414. }
  415. public function getUploadTime(): int {
  416. return $this->getFileInfo()->getUploadTime();
  417. }
  418. }