1
0

Node.php 12 KB

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