Node.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. protected ?INode $parent;
  57. private bool $infoHasSubMountsIncluded;
  58. /**
  59. * @param \OC\Files\View $view
  60. * @param \OCP\Files\IRootFolder $root
  61. * @param string $path
  62. * @param FileInfo $fileInfo
  63. */
  64. public function __construct(IRootFolder $root, $view, $path, $fileInfo = null, ?Node $parent = null, bool $infoHasSubMountsIncluded = true) {
  65. if (Filesystem::normalizePath($view->getRoot()) !== '/') {
  66. throw new PreConditionNotMetException('The view passed to the node should not have any fake root set');
  67. }
  68. $this->view = $view;
  69. $this->root = $root;
  70. $this->path = $path;
  71. $this->fileInfo = $fileInfo;
  72. $this->parent = $parent;
  73. $this->infoHasSubMountsIncluded = $infoHasSubMountsIncluded;
  74. }
  75. /**
  76. * Creates a Node of the same type that represents a non-existing path
  77. *
  78. * @param string $path path
  79. * @return Node non-existing node
  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(bool $includeMountPoint = true) {
  93. if (!$this->fileInfo) {
  94. if (!Filesystem::isValidPath($this->path)) {
  95. throw new InvalidPathException();
  96. }
  97. $fileInfo = $this->view->getFileInfo($this->path, $includeMountPoint);
  98. $this->infoHasSubMountsIncluded = $includeMountPoint;
  99. if ($fileInfo instanceof FileInfo) {
  100. $this->fileInfo = $fileInfo;
  101. } else {
  102. throw new NotFoundException();
  103. }
  104. } elseif ($includeMountPoint && !$this->infoHasSubMountsIncluded && $this instanceof Folder) {
  105. if ($this->fileInfo instanceof \OC\Files\FileInfo) {
  106. $this->view->addSubMounts($this->fileInfo);
  107. }
  108. $this->infoHasSubMountsIncluded = true;
  109. }
  110. return $this->fileInfo;
  111. }
  112. /**
  113. * @param string[] $hooks
  114. */
  115. protected function sendHooks($hooks, array $args = null) {
  116. $args = !empty($args) ? $args : [$this];
  117. /** @var IEventDispatcher $dispatcher */
  118. $dispatcher = \OC::$server->get(IEventDispatcher::class);
  119. foreach ($hooks as $hook) {
  120. if (method_exists($this->root, 'emit')) {
  121. $this->root->emit('\OC\Files', $hook, $args);
  122. }
  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. public function getParent(): INode|IRootFolder {
  265. if ($this->parent === null) {
  266. $newPath = dirname($this->path);
  267. if ($newPath === '' || $newPath === '.' || $newPath === '/') {
  268. return $this->root;
  269. }
  270. // Manually fetch the parent if the current node doesn't have a file info yet
  271. try {
  272. $fileInfo = $this->getFileInfo();
  273. } catch (NotFoundException) {
  274. $this->parent = $this->root->get($newPath);
  275. /** @var \OCP\Files\Folder $this->parent */
  276. return $this->parent;
  277. }
  278. // gather the metadata we already know about our parent
  279. $parentData = [
  280. 'path' => $newPath,
  281. 'fileid' => $fileInfo->getParentId(),
  282. ];
  283. // and create lazy folder with it instead of always querying
  284. $this->parent = new LazyFolder($this->root, function () use ($newPath) {
  285. return $this->root->get($newPath);
  286. }, $parentData);
  287. }
  288. return $this->parent;
  289. }
  290. /**
  291. * @return string
  292. */
  293. public function getName() {
  294. return basename($this->path);
  295. }
  296. /**
  297. * @param string $path
  298. * @return string
  299. */
  300. protected function normalizePath($path) {
  301. return PathHelper::normalizePath($path);
  302. }
  303. /**
  304. * check if the requested path is valid
  305. *
  306. * @param string $path
  307. * @return bool
  308. */
  309. public function isValidPath($path) {
  310. return Filesystem::isValidPath($path);
  311. }
  312. public function isMounted() {
  313. return $this->getFileInfo(false)->isMounted();
  314. }
  315. public function isShared() {
  316. return $this->getFileInfo(false)->isShared();
  317. }
  318. public function getMimeType() {
  319. return $this->getFileInfo(false)->getMimetype();
  320. }
  321. public function getMimePart() {
  322. return $this->getFileInfo(false)->getMimePart();
  323. }
  324. public function getType() {
  325. return $this->getFileInfo(false)->getType();
  326. }
  327. public function isEncrypted() {
  328. return $this->getFileInfo(false)->isEncrypted();
  329. }
  330. public function getMountPoint() {
  331. return $this->getFileInfo(false)->getMountPoint();
  332. }
  333. public function getOwner() {
  334. return $this->getFileInfo(false)->getOwner();
  335. }
  336. public function getChecksum() {
  337. }
  338. public function getExtension(): string {
  339. return $this->getFileInfo(false)->getExtension();
  340. }
  341. /**
  342. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  343. * @throws LockedException
  344. */
  345. public function lock($type) {
  346. $this->view->lockFile($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 changeLock($type) {
  353. $this->view->changeLock($this->path, $type);
  354. }
  355. /**
  356. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  357. * @throws LockedException
  358. */
  359. public function unlock($type) {
  360. $this->view->unlockFile($this->path, $type);
  361. }
  362. /**
  363. * @param string $targetPath
  364. * @return INode
  365. * @throws InvalidPathException
  366. * @throws NotFoundException
  367. * @throws NotPermittedException if copy not allowed or failed
  368. */
  369. public function copy($targetPath) {
  370. $targetPath = $this->normalizePath($targetPath);
  371. $parent = $this->root->get(dirname($targetPath));
  372. if ($parent instanceof Folder and $this->isValidPath($targetPath) and $parent->isCreatable()) {
  373. $nonExisting = $this->createNonExistingNode($targetPath);
  374. $this->sendHooks(['preCopy'], [$this, $nonExisting]);
  375. $this->sendHooks(['preWrite'], [$nonExisting]);
  376. if (!$this->view->copy($this->path, $targetPath)) {
  377. throw new NotPermittedException('Could not copy ' . $this->path . ' to ' . $targetPath);
  378. }
  379. $targetNode = $this->root->get($targetPath);
  380. $this->sendHooks(['postCopy'], [$this, $targetNode]);
  381. $this->sendHooks(['postWrite'], [$targetNode]);
  382. return $targetNode;
  383. } else {
  384. throw new NotPermittedException('No permission to copy to path ' . $targetPath);
  385. }
  386. }
  387. /**
  388. * @param string $targetPath
  389. * @return INode
  390. * @throws InvalidPathException
  391. * @throws NotFoundException
  392. * @throws NotPermittedException if move not allowed or failed
  393. * @throws LockedException
  394. */
  395. public function move($targetPath) {
  396. $targetPath = $this->normalizePath($targetPath);
  397. $parent = $this->root->get(dirname($targetPath));
  398. if (
  399. $parent instanceof Folder and
  400. $this->isValidPath($targetPath) and
  401. (
  402. $parent->isCreatable() ||
  403. ($parent->getInternalPath() === '' && $parent->getMountPoint() instanceof MoveableMount)
  404. )
  405. ) {
  406. $nonExisting = $this->createNonExistingNode($targetPath);
  407. $this->sendHooks(['preRename'], [$this, $nonExisting]);
  408. $this->sendHooks(['preWrite'], [$nonExisting]);
  409. if (!$this->view->rename($this->path, $targetPath)) {
  410. throw new NotPermittedException('Could not move ' . $this->path . ' to ' . $targetPath);
  411. }
  412. $mountPoint = $this->getMountPoint();
  413. if ($mountPoint) {
  414. // update the cached fileinfo with the new (internal) path
  415. /** @var \OC\Files\FileInfo $oldFileInfo */
  416. $oldFileInfo = $this->getFileInfo();
  417. $this->fileInfo = new \OC\Files\FileInfo($targetPath, $oldFileInfo->getStorage(), $mountPoint->getInternalPath($targetPath), $oldFileInfo->getData(), $mountPoint, $oldFileInfo->getOwner());
  418. }
  419. $targetNode = $this->root->get($targetPath);
  420. $this->sendHooks(['postRename'], [$this, $targetNode]);
  421. $this->sendHooks(['postWrite'], [$targetNode]);
  422. $this->path = $targetPath;
  423. return $targetNode;
  424. } else {
  425. throw new NotPermittedException('No permission to move to path ' . $targetPath);
  426. }
  427. }
  428. public function getCreationTime(): int {
  429. return $this->getFileInfo()->getCreationTime();
  430. }
  431. public function getUploadTime(): int {
  432. return $this->getFileInfo()->getUploadTime();
  433. }
  434. public function getParentId(): int {
  435. return $this->fileInfo->getParentId();
  436. }
  437. }