PageRenderTime 24ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/private/Files/Node/Node.php

https://gitlab.com/wuhang2003/core
PHP | 383 lines | 176 code | 45 blank | 162 comment | 13 complexity | fb86ad6ff5665a1016cb9f25d625ffa5 MD5 | raw file
  1. <?php
  2. /**
  3. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  4. * @author Joas Schilling <nickvergessen@owncloud.com>
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Robin Appelman <icewind@owncloud.com>
  7. * @author Roeland Jago Douma <rullzer@owncloud.com>
  8. * @author Vincent Petry <pvince81@owncloud.com>
  9. *
  10. * @copyright Copyright (c) 2016, ownCloud, Inc.
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\Files\Node;
  27. use OC\Files\Filesystem;
  28. use OCP\Files\FileInfo;
  29. use OCP\Files\InvalidPathException;
  30. use OCP\Files\NotFoundException;
  31. use OCP\Files\NotPermittedException;
  32. class Node implements \OCP\Files\Node {
  33. /**
  34. * @var \OC\Files\View $view
  35. */
  36. protected $view;
  37. /**
  38. * @var \OC\Files\Node\Root $root
  39. */
  40. protected $root;
  41. /**
  42. * @var string $path
  43. */
  44. protected $path;
  45. /**
  46. * @var \OCP\Files\FileInfo
  47. */
  48. protected $fileInfo;
  49. /**
  50. * @param \OC\Files\View $view
  51. * @param \OC\Files\Node\Root $root
  52. * @param string $path
  53. * @param FileInfo $fileInfo
  54. */
  55. public function __construct($root, $view, $path, $fileInfo = null) {
  56. $this->view = $view;
  57. $this->root = $root;
  58. $this->path = $path;
  59. $this->fileInfo = $fileInfo;
  60. }
  61. /**
  62. * Returns the matching file info
  63. *
  64. * @return FileInfo
  65. * @throws InvalidPathException
  66. * @throws NotFoundException
  67. */
  68. public function getFileInfo() {
  69. if (!Filesystem::isValidPath($this->path)) {
  70. throw new InvalidPathException();
  71. }
  72. if (!$this->fileInfo) {
  73. $fileInfo = $this->view->getFileInfo($this->path);
  74. if ($fileInfo instanceof FileInfo) {
  75. $this->fileInfo = $fileInfo;
  76. } else {
  77. throw new NotFoundException();
  78. }
  79. }
  80. return $this->fileInfo;
  81. }
  82. /**
  83. * @param string[] $hooks
  84. */
  85. protected function sendHooks($hooks) {
  86. foreach ($hooks as $hook) {
  87. $this->root->emit('\OC\Files', $hook, array($this));
  88. }
  89. }
  90. /**
  91. * @param int $permissions
  92. * @return bool
  93. */
  94. protected function checkPermissions($permissions) {
  95. return ($this->getPermissions() & $permissions) === $permissions;
  96. }
  97. /**
  98. * @param string $targetPath
  99. * @throws \OCP\Files\NotPermittedException
  100. * @return \OC\Files\Node\Node
  101. */
  102. public function move($targetPath) {
  103. return;
  104. }
  105. public function delete() {
  106. return;
  107. }
  108. /**
  109. * @param string $targetPath
  110. * @return \OC\Files\Node\Node
  111. */
  112. public function copy($targetPath) {
  113. return;
  114. }
  115. /**
  116. * @param int $mtime
  117. * @throws \OCP\Files\NotPermittedException
  118. */
  119. public function touch($mtime = null) {
  120. if ($this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE)) {
  121. $this->sendHooks(array('preTouch'));
  122. $this->view->touch($this->path, $mtime);
  123. $this->sendHooks(array('postTouch'));
  124. if ($this->fileInfo) {
  125. if (is_null($mtime)) {
  126. $mtime = time();
  127. }
  128. $this->fileInfo['mtime'] = $mtime;
  129. }
  130. } else {
  131. throw new NotPermittedException();
  132. }
  133. }
  134. /**
  135. * @return \OC\Files\Storage\Storage
  136. * @throws \OCP\Files\NotFoundException
  137. */
  138. public function getStorage() {
  139. list($storage,) = $this->view->resolvePath($this->path);
  140. return $storage;
  141. }
  142. /**
  143. * @return string
  144. */
  145. public function getPath() {
  146. return $this->path;
  147. }
  148. /**
  149. * @return string
  150. */
  151. public function getInternalPath() {
  152. list(, $internalPath) = $this->view->resolvePath($this->path);
  153. return $internalPath;
  154. }
  155. /**
  156. * @return int
  157. * @throws InvalidPathException
  158. * @throws NotFoundException
  159. */
  160. public function getId() {
  161. return $this->getFileInfo()->getId();
  162. }
  163. /**
  164. * @return array
  165. */
  166. public function stat() {
  167. return $this->view->stat($this->path);
  168. }
  169. /**
  170. * @return int
  171. * @throws InvalidPathException
  172. * @throws NotFoundException
  173. */
  174. public function getMTime() {
  175. return $this->getFileInfo()->getMTime();
  176. }
  177. /**
  178. * @return int
  179. * @throws InvalidPathException
  180. * @throws NotFoundException
  181. */
  182. public function getSize() {
  183. return $this->getFileInfo()->getSize();
  184. }
  185. /**
  186. * @return string
  187. * @throws InvalidPathException
  188. * @throws NotFoundException
  189. */
  190. public function getEtag() {
  191. return $this->getFileInfo()->getEtag();
  192. }
  193. /**
  194. * @return int
  195. * @throws InvalidPathException
  196. * @throws NotFoundException
  197. */
  198. public function getPermissions() {
  199. return $this->getFileInfo()->getPermissions();
  200. }
  201. /**
  202. * @return bool
  203. * @throws InvalidPathException
  204. * @throws NotFoundException
  205. */
  206. public function isReadable() {
  207. return $this->getFileInfo()->isReadable();
  208. }
  209. /**
  210. * @return bool
  211. * @throws InvalidPathException
  212. * @throws NotFoundException
  213. */
  214. public function isUpdateable() {
  215. return $this->getFileInfo()->isUpdateable();
  216. }
  217. /**
  218. * @return bool
  219. * @throws InvalidPathException
  220. * @throws NotFoundException
  221. */
  222. public function isDeletable() {
  223. return $this->getFileInfo()->isDeletable();
  224. }
  225. /**
  226. * @return bool
  227. * @throws InvalidPathException
  228. * @throws NotFoundException
  229. */
  230. public function isShareable() {
  231. return $this->getFileInfo()->isShareable();
  232. }
  233. /**
  234. * @return bool
  235. * @throws InvalidPathException
  236. * @throws NotFoundException
  237. */
  238. public function isCreatable() {
  239. return $this->getFileInfo()->isCreatable();
  240. }
  241. /**
  242. * @return Node
  243. */
  244. public function getParent() {
  245. return $this->root->get(dirname($this->path));
  246. }
  247. /**
  248. * @return string
  249. */
  250. public function getName() {
  251. return basename($this->path);
  252. }
  253. /**
  254. * @param string $path
  255. * @return string
  256. */
  257. protected function normalizePath($path) {
  258. if ($path === '' or $path === '/') {
  259. return '/';
  260. }
  261. //no windows style slashes
  262. $path = str_replace('\\', '/', $path);
  263. //add leading slash
  264. if ($path[0] !== '/') {
  265. $path = '/' . $path;
  266. }
  267. //remove duplicate slashes
  268. while (strpos($path, '//') !== false) {
  269. $path = str_replace('//', '/', $path);
  270. }
  271. //remove trailing slash
  272. $path = rtrim($path, '/');
  273. return $path;
  274. }
  275. /**
  276. * check if the requested path is valid
  277. *
  278. * @param string $path
  279. * @return bool
  280. */
  281. public function isValidPath($path) {
  282. if (!$path || $path[0] !== '/') {
  283. $path = '/' . $path;
  284. }
  285. if (strstr($path, '/../') || strrchr($path, '/') === '/..') {
  286. return false;
  287. }
  288. return true;
  289. }
  290. public function isMounted() {
  291. return $this->getFileInfo()->isMounted();
  292. }
  293. public function isShared() {
  294. return $this->getFileInfo()->isShared();
  295. }
  296. public function getMimeType() {
  297. return $this->getFileInfo()->getMimetype();
  298. }
  299. public function getMimePart() {
  300. return $this->getFileInfo()->getMimePart();
  301. }
  302. public function getType() {
  303. return $this->getFileInfo()->getType();
  304. }
  305. public function isEncrypted() {
  306. return $this->getFileInfo()->isEncrypted();
  307. }
  308. public function getMountPoint() {
  309. return $this->getFileInfo()->getMountPoint();
  310. }
  311. public function getOwner() {
  312. return $this->getFileInfo()->getOwner();
  313. }
  314. public function getChecksum() {
  315. return;
  316. }
  317. /**
  318. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  319. * @throws \OCP\Lock\LockedException
  320. */
  321. public function lock($type) {
  322. $this->view->lockFile($this->path, $type);
  323. }
  324. /**
  325. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  326. * @throws \OCP\Lock\LockedException
  327. */
  328. public function changeLock($type) {
  329. $this->view->changeLock($this->path, $type);
  330. }
  331. /**
  332. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  333. * @throws \OCP\Lock\LockedException
  334. */
  335. public function unlock($type) {
  336. $this->view->unlockFile($this->path, $type);
  337. }
  338. }