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\EventDispatcher\GenericEvent;
  35. use OCP\EventDispatcher\IEventDispatcher;
  36. use OCP\Files\FileInfo;
  37. use OCP\Files\InvalidPathException;
  38. use OCP\Files\IRootFolder;
  39. use OCP\Files\Node as INode;
  40. use OCP\Files\NotFoundException;
  41. use OCP\Files\NotPermittedException;
  42. use OCP\Lock\LockedException;
  43. use OCP\PreConditionNotMetException;
  44. // FIXME: this class really should be abstract
  45. class Node implements INode {
  46. /**
  47. * @var \OC\Files\View $view
  48. */
  49. protected $view;
  50. protected IRootFolder $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(IRootFolder $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. /** @var IEventDispatcher $dispatcher */
  121. $dispatcher = \OC::$server->get(IEventDispatcher::class);
  122. foreach ($hooks as $hook) {
  123. if (method_exists($this->root, 'emit')) {
  124. $this->root->emit('\OC\Files', $hook, $args);
  125. }
  126. $dispatcher->dispatch('\OCP\Files::' . $hook, new GenericEvent($args));
  127. }
  128. }
  129. /**
  130. * @param int $permissions
  131. * @return bool
  132. * @throws InvalidPathException
  133. * @throws NotFoundException
  134. */
  135. protected function checkPermissions($permissions) {
  136. return ($this->getPermissions() & $permissions) === $permissions;
  137. }
  138. public function delete() {
  139. }
  140. /**
  141. * @param int $mtime
  142. * @throws InvalidPathException
  143. * @throws NotFoundException
  144. * @throws NotPermittedException
  145. */
  146. public function touch($mtime = null) {
  147. if ($this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE)) {
  148. $this->sendHooks(['preTouch']);
  149. $this->view->touch($this->path, $mtime);
  150. $this->sendHooks(['postTouch']);
  151. if ($this->fileInfo) {
  152. if (is_null($mtime)) {
  153. $mtime = time();
  154. }
  155. $this->fileInfo['mtime'] = $mtime;
  156. }
  157. } else {
  158. throw new NotPermittedException();
  159. }
  160. }
  161. public function getStorage() {
  162. $storage = $this->getMountPoint()->getStorage();
  163. if (!$storage) {
  164. throw new \Exception("No storage for node");
  165. }
  166. return $storage;
  167. }
  168. /**
  169. * @return string
  170. */
  171. public function getPath() {
  172. return $this->path;
  173. }
  174. /**
  175. * @return string
  176. */
  177. public function getInternalPath() {
  178. return $this->getFileInfo(false)->getInternalPath();
  179. }
  180. /**
  181. * @return int
  182. * @throws InvalidPathException
  183. * @throws NotFoundException
  184. */
  185. public function getId() {
  186. return $this->getFileInfo(false)->getId() ?? -1;
  187. }
  188. /**
  189. * @return array
  190. */
  191. public function stat() {
  192. return $this->view->stat($this->path);
  193. }
  194. /**
  195. * @return int
  196. * @throws InvalidPathException
  197. * @throws NotFoundException
  198. */
  199. public function getMTime() {
  200. return $this->getFileInfo()->getMTime();
  201. }
  202. /**
  203. * @param bool $includeMounts
  204. * @return int|float
  205. * @throws InvalidPathException
  206. * @throws NotFoundException
  207. */
  208. public function getSize($includeMounts = true): int|float {
  209. return $this->getFileInfo()->getSize($includeMounts);
  210. }
  211. /**
  212. * @return string
  213. * @throws InvalidPathException
  214. * @throws NotFoundException
  215. */
  216. public function getEtag() {
  217. return $this->getFileInfo()->getEtag();
  218. }
  219. /**
  220. * @return int
  221. * @throws InvalidPathException
  222. * @throws NotFoundException
  223. */
  224. public function getPermissions() {
  225. return $this->getFileInfo(false)->getPermissions();
  226. }
  227. /**
  228. * @return bool
  229. * @throws InvalidPathException
  230. * @throws NotFoundException
  231. */
  232. public function isReadable() {
  233. return $this->getFileInfo(false)->isReadable();
  234. }
  235. /**
  236. * @return bool
  237. * @throws InvalidPathException
  238. * @throws NotFoundException
  239. */
  240. public function isUpdateable() {
  241. return $this->getFileInfo(false)->isUpdateable();
  242. }
  243. /**
  244. * @return bool
  245. * @throws InvalidPathException
  246. * @throws NotFoundException
  247. */
  248. public function isDeletable() {
  249. return $this->getFileInfo(false)->isDeletable();
  250. }
  251. /**
  252. * @return bool
  253. * @throws InvalidPathException
  254. * @throws NotFoundException
  255. */
  256. public function isShareable() {
  257. return $this->getFileInfo(false)->isShareable();
  258. }
  259. /**
  260. * @return bool
  261. * @throws InvalidPathException
  262. * @throws NotFoundException
  263. */
  264. public function isCreatable() {
  265. return $this->getFileInfo(false)->isCreatable();
  266. }
  267. public function getParent(): INode|IRootFolder {
  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 INode
  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 INode
  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. }