1
0

Node.php 12 KB

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