Node.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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 Maxence Lange <maxence@artificial-owl.com>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Robin Appelman <robin@icewind.nl>
  13. * @author Roeland Jago Douma <roeland@famdouma.nl>
  14. * @author Vincent Petry <vincent@nextcloud.com>
  15. *
  16. * @license AGPL-3.0
  17. *
  18. * This code is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License, version 3,
  20. * as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License, version 3,
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>
  29. *
  30. */
  31. namespace OC\Files\Node;
  32. use OC\Files\Filesystem;
  33. use OC\Files\Mount\MoveableMount;
  34. use OC\Files\Utils\PathHelper;
  35. use OCP\EventDispatcher\GenericEvent;
  36. use OCP\EventDispatcher\IEventDispatcher;
  37. use OCP\Files\FileInfo;
  38. use OCP\Files\InvalidPathException;
  39. use OCP\Files\IRootFolder;
  40. use OCP\Files\Node as INode;
  41. use OCP\Files\NotFoundException;
  42. use OCP\Files\NotPermittedException;
  43. use OCP\Lock\LockedException;
  44. use OCP\PreConditionNotMetException;
  45. // FIXME: this class really should be abstract (+1)
  46. class Node implements INode {
  47. /**
  48. * @var \OC\Files\View $view
  49. */
  50. protected $view;
  51. protected IRootFolder $root;
  52. /**
  53. * @var string $path Absolute path to the node (e.g. /admin/files/folder/file)
  54. */
  55. protected $path;
  56. protected ?FileInfo $fileInfo;
  57. protected ?INode $parent;
  58. private bool $infoHasSubMountsIncluded;
  59. /**
  60. * @param \OC\Files\View $view
  61. * @param \OCP\Files\IRootFolder $root
  62. * @param string $path
  63. * @param FileInfo $fileInfo
  64. */
  65. public function __construct(IRootFolder $root, $view, $path, $fileInfo = null, ?INode $parent = null, bool $infoHasSubMountsIncluded = true) {
  66. if (Filesystem::normalizePath($view->getRoot()) !== '/') {
  67. throw new PreConditionNotMetException('The view passed to the node should not have any fake root set');
  68. }
  69. $this->view = $view;
  70. $this->root = $root;
  71. $this->path = $path;
  72. $this->fileInfo = $fileInfo;
  73. $this->parent = $parent;
  74. $this->infoHasSubMountsIncluded = $infoHasSubMountsIncluded;
  75. }
  76. /**
  77. * Creates a Node of the same type that represents a non-existing path
  78. *
  79. * @param string $path path
  80. * @return Node non-existing node
  81. * @throws \Exception
  82. */
  83. protected function createNonExistingNode($path) {
  84. throw new \Exception('Must be implemented by subclasses');
  85. }
  86. /**
  87. * Returns the matching file info
  88. *
  89. * @return FileInfo
  90. * @throws InvalidPathException
  91. * @throws NotFoundException
  92. */
  93. public function getFileInfo(bool $includeMountPoint = true) {
  94. if (!$this->fileInfo) {
  95. if (!Filesystem::isValidPath($this->path)) {
  96. throw new InvalidPathException();
  97. }
  98. $fileInfo = $this->view->getFileInfo($this->path, $includeMountPoint);
  99. $this->infoHasSubMountsIncluded = $includeMountPoint;
  100. if ($fileInfo instanceof FileInfo) {
  101. $this->fileInfo = $fileInfo;
  102. } else {
  103. throw new NotFoundException();
  104. }
  105. } elseif ($includeMountPoint && !$this->infoHasSubMountsIncluded && $this instanceof Folder) {
  106. if ($this->fileInfo instanceof \OC\Files\FileInfo) {
  107. $this->view->addSubMounts($this->fileInfo);
  108. }
  109. $this->infoHasSubMountsIncluded = true;
  110. }
  111. return $this->fileInfo;
  112. }
  113. /**
  114. * @param string[] $hooks
  115. */
  116. protected function sendHooks($hooks, array $args = null) {
  117. $args = !empty($args) ? $args : [$this];
  118. /** @var IEventDispatcher $dispatcher */
  119. $dispatcher = \OC::$server->get(IEventDispatcher::class);
  120. foreach ($hooks as $hook) {
  121. if (method_exists($this->root, 'emit')) {
  122. $this->root->emit('\OC\Files', $hook, $args);
  123. }
  124. if (in_array($hook, ['preWrite', 'postWrite', 'preCreate', 'postCreate', 'preTouch', 'postTouch', 'preDelete', 'postDelete'], true)) {
  125. $event = new GenericEvent($args[0]);
  126. } else {
  127. $event = new GenericEvent($args);
  128. }
  129. $dispatcher->dispatch('\OCP\Files::' . $hook, $event);
  130. }
  131. }
  132. /**
  133. * @param int $permissions
  134. * @return bool
  135. * @throws InvalidPathException
  136. * @throws NotFoundException
  137. */
  138. protected function checkPermissions($permissions) {
  139. return ($this->getPermissions() & $permissions) === $permissions;
  140. }
  141. public function delete() {
  142. }
  143. /**
  144. * @param int $mtime
  145. * @throws InvalidPathException
  146. * @throws NotFoundException
  147. * @throws NotPermittedException
  148. */
  149. public function touch($mtime = null) {
  150. if ($this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE)) {
  151. $this->sendHooks(['preTouch']);
  152. $this->view->touch($this->path, $mtime);
  153. $this->sendHooks(['postTouch']);
  154. if ($this->fileInfo) {
  155. if (is_null($mtime)) {
  156. $mtime = time();
  157. }
  158. $this->fileInfo['mtime'] = $mtime;
  159. }
  160. } else {
  161. throw new NotPermittedException();
  162. }
  163. }
  164. public function getStorage() {
  165. $storage = $this->getMountPoint()->getStorage();
  166. if (!$storage) {
  167. throw new \Exception("No storage for node");
  168. }
  169. return $storage;
  170. }
  171. /**
  172. * @return string
  173. */
  174. public function getPath() {
  175. return $this->path;
  176. }
  177. /**
  178. * @return string
  179. */
  180. public function getInternalPath() {
  181. return $this->getFileInfo(false)->getInternalPath();
  182. }
  183. /**
  184. * @return int
  185. * @throws InvalidPathException
  186. * @throws NotFoundException
  187. */
  188. public function getId() {
  189. return $this->getFileInfo(false)->getId() ?? -1;
  190. }
  191. /**
  192. * @return array
  193. */
  194. public function stat() {
  195. return $this->view->stat($this->path);
  196. }
  197. /**
  198. * @return int
  199. * @throws InvalidPathException
  200. * @throws NotFoundException
  201. */
  202. public function getMTime() {
  203. return $this->getFileInfo()->getMTime();
  204. }
  205. /**
  206. * @param bool $includeMounts
  207. * @return int|float
  208. * @throws InvalidPathException
  209. * @throws NotFoundException
  210. */
  211. public function getSize($includeMounts = true): int|float {
  212. return $this->getFileInfo()->getSize($includeMounts);
  213. }
  214. /**
  215. * @return string
  216. * @throws InvalidPathException
  217. * @throws NotFoundException
  218. */
  219. public function getEtag() {
  220. return $this->getFileInfo()->getEtag();
  221. }
  222. /**
  223. * @return int
  224. * @throws InvalidPathException
  225. * @throws NotFoundException
  226. */
  227. public function getPermissions() {
  228. return $this->getFileInfo(false)->getPermissions();
  229. }
  230. /**
  231. * @return bool
  232. * @throws InvalidPathException
  233. * @throws NotFoundException
  234. */
  235. public function isReadable() {
  236. return $this->getFileInfo(false)->isReadable();
  237. }
  238. /**
  239. * @return bool
  240. * @throws InvalidPathException
  241. * @throws NotFoundException
  242. */
  243. public function isUpdateable() {
  244. return $this->getFileInfo(false)->isUpdateable();
  245. }
  246. /**
  247. * @return bool
  248. * @throws InvalidPathException
  249. * @throws NotFoundException
  250. */
  251. public function isDeletable() {
  252. return $this->getFileInfo(false)->isDeletable();
  253. }
  254. /**
  255. * @return bool
  256. * @throws InvalidPathException
  257. * @throws NotFoundException
  258. */
  259. public function isShareable() {
  260. return $this->getFileInfo(false)->isShareable();
  261. }
  262. /**
  263. * @return bool
  264. * @throws InvalidPathException
  265. * @throws NotFoundException
  266. */
  267. public function isCreatable() {
  268. return $this->getFileInfo(false)->isCreatable();
  269. }
  270. public function getParent(): INode|IRootFolder {
  271. if ($this->parent === null) {
  272. $newPath = dirname($this->path);
  273. if ($newPath === '' || $newPath === '.' || $newPath === '/') {
  274. return $this->root;
  275. }
  276. // Manually fetch the parent if the current node doesn't have a file info yet
  277. try {
  278. $fileInfo = $this->getFileInfo();
  279. } catch (NotFoundException) {
  280. $this->parent = $this->root->get($newPath);
  281. /** @var \OCP\Files\Folder $this->parent */
  282. return $this->parent;
  283. }
  284. // gather the metadata we already know about our parent
  285. $parentData = [
  286. 'path' => $newPath,
  287. 'fileid' => $fileInfo->getParentId(),
  288. ];
  289. // and create lazy folder with it instead of always querying
  290. $this->parent = new LazyFolder($this->root, function () use ($newPath) {
  291. return $this->root->get($newPath);
  292. }, $parentData);
  293. }
  294. return $this->parent;
  295. }
  296. /**
  297. * @return string
  298. */
  299. public function getName() {
  300. return basename($this->path);
  301. }
  302. /**
  303. * @param string $path
  304. * @return string
  305. */
  306. protected function normalizePath($path) {
  307. return PathHelper::normalizePath($path);
  308. }
  309. /**
  310. * check if the requested path is valid
  311. *
  312. * @param string $path
  313. * @return bool
  314. */
  315. public function isValidPath($path) {
  316. return Filesystem::isValidPath($path);
  317. }
  318. public function isMounted() {
  319. return $this->getFileInfo(false)->isMounted();
  320. }
  321. public function isShared() {
  322. return $this->getFileInfo(false)->isShared();
  323. }
  324. public function getMimeType() {
  325. return $this->getFileInfo(false)->getMimetype();
  326. }
  327. public function getMimePart() {
  328. return $this->getFileInfo(false)->getMimePart();
  329. }
  330. public function getType() {
  331. return $this->getFileInfo(false)->getType();
  332. }
  333. public function isEncrypted() {
  334. return $this->getFileInfo(false)->isEncrypted();
  335. }
  336. public function getMountPoint() {
  337. return $this->getFileInfo(false)->getMountPoint();
  338. }
  339. public function getOwner() {
  340. return $this->getFileInfo(false)->getOwner();
  341. }
  342. public function getChecksum() {
  343. }
  344. public function getExtension(): string {
  345. return $this->getFileInfo(false)->getExtension();
  346. }
  347. /**
  348. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  349. * @throws LockedException
  350. */
  351. public function lock($type) {
  352. $this->view->lockFile($this->path, $type);
  353. }
  354. /**
  355. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  356. * @throws LockedException
  357. */
  358. public function changeLock($type) {
  359. $this->view->changeLock($this->path, $type);
  360. }
  361. /**
  362. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  363. * @throws LockedException
  364. */
  365. public function unlock($type) {
  366. $this->view->unlockFile($this->path, $type);
  367. }
  368. /**
  369. * @param string $targetPath
  370. * @return INode
  371. * @throws InvalidPathException
  372. * @throws NotFoundException
  373. * @throws NotPermittedException if copy not allowed or failed
  374. */
  375. public function copy($targetPath) {
  376. $targetPath = $this->normalizePath($targetPath);
  377. $parent = $this->root->get(dirname($targetPath));
  378. if ($parent instanceof Folder and $this->isValidPath($targetPath) and $parent->isCreatable()) {
  379. $nonExisting = $this->createNonExistingNode($targetPath);
  380. $this->sendHooks(['preCopy'], [$this, $nonExisting]);
  381. $this->sendHooks(['preWrite'], [$nonExisting]);
  382. if (!$this->view->copy($this->path, $targetPath)) {
  383. throw new NotPermittedException('Could not copy ' . $this->path . ' to ' . $targetPath);
  384. }
  385. $targetNode = $this->root->get($targetPath);
  386. $this->sendHooks(['postCopy'], [$this, $targetNode]);
  387. $this->sendHooks(['postWrite'], [$targetNode]);
  388. return $targetNode;
  389. } else {
  390. throw new NotPermittedException('No permission to copy to path ' . $targetPath);
  391. }
  392. }
  393. /**
  394. * @param string $targetPath
  395. * @return INode
  396. * @throws InvalidPathException
  397. * @throws NotFoundException
  398. * @throws NotPermittedException if move not allowed or failed
  399. * @throws LockedException
  400. */
  401. public function move($targetPath) {
  402. $targetPath = $this->normalizePath($targetPath);
  403. $parent = $this->root->get(dirname($targetPath));
  404. if (
  405. $parent instanceof Folder and
  406. $this->isValidPath($targetPath) and
  407. (
  408. $parent->isCreatable() ||
  409. ($parent->getInternalPath() === '' && $parent->getMountPoint() instanceof MoveableMount)
  410. )
  411. ) {
  412. $nonExisting = $this->createNonExistingNode($targetPath);
  413. $this->sendHooks(['preRename'], [$this, $nonExisting]);
  414. $this->sendHooks(['preWrite'], [$nonExisting]);
  415. if (!$this->view->rename($this->path, $targetPath)) {
  416. throw new NotPermittedException('Could not move ' . $this->path . ' to ' . $targetPath);
  417. }
  418. $mountPoint = $this->getMountPoint();
  419. if ($mountPoint) {
  420. // update the cached fileinfo with the new (internal) path
  421. /** @var \OC\Files\FileInfo $oldFileInfo */
  422. $oldFileInfo = $this->getFileInfo();
  423. $this->fileInfo = new \OC\Files\FileInfo($targetPath, $oldFileInfo->getStorage(), $mountPoint->getInternalPath($targetPath), $oldFileInfo->getData(), $mountPoint, $oldFileInfo->getOwner());
  424. }
  425. $targetNode = $this->root->get($targetPath);
  426. $this->sendHooks(['postRename'], [$this, $targetNode]);
  427. $this->sendHooks(['postWrite'], [$targetNode]);
  428. $this->path = $targetPath;
  429. return $targetNode;
  430. } else {
  431. throw new NotPermittedException('No permission to move to path ' . $targetPath);
  432. }
  433. }
  434. public function getCreationTime(): int {
  435. return $this->getFileInfo()->getCreationTime();
  436. }
  437. public function getUploadTime(): int {
  438. return $this->getFileInfo()->getUploadTime();
  439. }
  440. public function getParentId(): int {
  441. return $this->fileInfo->getParentId();
  442. }
  443. /**
  444. * @inheritDoc
  445. * @return array<string, int|string|bool|float|string[]|int[]>
  446. */
  447. public function getMetadata(): array {
  448. return $this->fileInfo->getMetadata();
  449. }
  450. }