Node.php 12 KB

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