PageRenderTime 23ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/private/Files/FileInfo.php

https://gitlab.com/wuhang2003/core
PHP | 346 lines | 154 code | 43 blank | 149 comment | 14 complexity | 7786d6bfb688a7f665b3c6b99f318dfb MD5 | raw file
  1. <?php
  2. /**
  3. * @author Joas Schilling <nickvergessen@owncloud.com>
  4. * @author Lukas Reschke <lukas@owncloud.com>
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Robin Appelman <icewind@owncloud.com>
  7. * @author Robin McCorkell <robin@mccorkell.me.uk>
  8. * @author Roeland Jago Douma <rullzer@owncloud.com>
  9. * @author tbartenstein <tbartenstein@users.noreply.github.com>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. * @author Vincent Petry <pvince81@owncloud.com>
  12. *
  13. * @copyright Copyright (c) 2016, ownCloud, Inc.
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OC\Files;
  30. use OCP\Files\Cache\ICacheEntry;
  31. use OCP\IUser;
  32. class FileInfo implements \OCP\Files\FileInfo, \ArrayAccess {
  33. /**
  34. * @var array $data
  35. */
  36. private $data;
  37. /**
  38. * @var string $path
  39. */
  40. private $path;
  41. /**
  42. * @var \OC\Files\Storage\Storage $storage
  43. */
  44. private $storage;
  45. /**
  46. * @var string $internalPath
  47. */
  48. private $internalPath;
  49. /**
  50. * @var \OCP\Files\Mount\IMountPoint
  51. */
  52. private $mount;
  53. /**
  54. * @var IUser
  55. */
  56. private $owner;
  57. /**
  58. * @var string[]
  59. */
  60. private $childEtags = [];
  61. /**
  62. * @param string|boolean $path
  63. * @param Storage\Storage $storage
  64. * @param string $internalPath
  65. * @param array|ICacheEntry $data
  66. * @param \OCP\Files\Mount\IMountPoint $mount
  67. * @param \OCP\IUser|null $owner
  68. */
  69. public function __construct($path, $storage, $internalPath, $data, $mount, $owner= null) {
  70. $this->path = $path;
  71. $this->storage = $storage;
  72. $this->internalPath = $internalPath;
  73. $this->data = $data;
  74. $this->mount = $mount;
  75. $this->owner = $owner;
  76. }
  77. public function offsetSet($offset, $value) {
  78. $this->data[$offset] = $value;
  79. }
  80. public function offsetExists($offset) {
  81. return isset($this->data[$offset]);
  82. }
  83. public function offsetUnset($offset) {
  84. unset($this->data[$offset]);
  85. }
  86. public function offsetGet($offset) {
  87. if ($offset === 'type') {
  88. return $this->getType();
  89. } else if ($offset === 'etag') {
  90. return $this->getEtag();
  91. } elseif ($offset === 'permissions') {
  92. return $this->getPermissions();
  93. } elseif (isset($this->data[$offset])) {
  94. return $this->data[$offset];
  95. } else {
  96. return null;
  97. }
  98. }
  99. /**
  100. * @return string
  101. */
  102. public function getPath() {
  103. return $this->path;
  104. }
  105. /**
  106. * @return \OCP\Files\Storage
  107. */
  108. public function getStorage() {
  109. return $this->storage;
  110. }
  111. /**
  112. * @return string
  113. */
  114. public function getInternalPath() {
  115. return $this->internalPath;
  116. }
  117. /**
  118. * @return int
  119. */
  120. public function getId() {
  121. return $this->data['fileid'];
  122. }
  123. /**
  124. * @return string
  125. */
  126. public function getMimetype() {
  127. return $this->data['mimetype'];
  128. }
  129. /**
  130. * @return string
  131. */
  132. public function getMimePart() {
  133. return $this->data['mimepart'];
  134. }
  135. /**
  136. * @return string
  137. */
  138. public function getName() {
  139. return basename($this->getPath());
  140. }
  141. /**
  142. * @return string
  143. */
  144. public function getEtag() {
  145. if (count($this->childEtags) > 0) {
  146. $combinedEtag = $this->data['etag'] . '::' . implode('::', $this->childEtags);
  147. return md5($combinedEtag);
  148. } else {
  149. return $this->data['etag'];
  150. }
  151. }
  152. /**
  153. * @return int
  154. */
  155. public function getSize() {
  156. return isset($this->data['size']) ? $this->data['size'] : 0;
  157. }
  158. /**
  159. * @return int
  160. */
  161. public function getMTime() {
  162. return $this->data['mtime'];
  163. }
  164. /**
  165. * @return bool
  166. */
  167. public function isEncrypted() {
  168. return $this->data['encrypted'];
  169. }
  170. /**
  171. * Return the currently version used for the HMAC in the encryption app
  172. *
  173. * @return int
  174. */
  175. public function getEncryptedVersion() {
  176. return isset($this->data['encryptedVersion']) ? (int) $this->data['encryptedVersion'] : 1;
  177. }
  178. /**
  179. * @return int
  180. */
  181. public function getPermissions() {
  182. $perms = $this->data['permissions'];
  183. if (\OCP\Util::isSharingDisabledForUser() || ($this->isShared() && !\OC\Share\Share::isResharingAllowed())) {
  184. $perms = $perms & ~\OCP\Constants::PERMISSION_SHARE;
  185. }
  186. return $perms;
  187. }
  188. /**
  189. * @return \OCP\Files\FileInfo::TYPE_FILE|\OCP\Files\FileInfo::TYPE_FOLDER
  190. */
  191. public function getType() {
  192. if (!isset($this->data['type'])) {
  193. $this->data['type'] = ($this->getMimetype() === 'httpd/unix-directory') ? self::TYPE_FOLDER : self::TYPE_FILE;
  194. }
  195. return $this->data['type'];
  196. }
  197. public function getData() {
  198. return $this->data;
  199. }
  200. /**
  201. * @param int $permissions
  202. * @return bool
  203. */
  204. protected function checkPermissions($permissions) {
  205. return ($this->getPermissions() & $permissions) === $permissions;
  206. }
  207. /**
  208. * @return bool
  209. */
  210. public function isReadable() {
  211. return $this->checkPermissions(\OCP\Constants::PERMISSION_READ);
  212. }
  213. /**
  214. * @return bool
  215. */
  216. public function isUpdateable() {
  217. return $this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE);
  218. }
  219. /**
  220. * Check whether new files or folders can be created inside this folder
  221. *
  222. * @return bool
  223. */
  224. public function isCreatable() {
  225. return $this->checkPermissions(\OCP\Constants::PERMISSION_CREATE);
  226. }
  227. /**
  228. * @return bool
  229. */
  230. public function isDeletable() {
  231. return $this->checkPermissions(\OCP\Constants::PERMISSION_DELETE);
  232. }
  233. /**
  234. * @return bool
  235. */
  236. public function isShareable() {
  237. return $this->checkPermissions(\OCP\Constants::PERMISSION_SHARE);
  238. }
  239. /**
  240. * Check if a file or folder is shared
  241. *
  242. * @return bool
  243. */
  244. public function isShared() {
  245. $sid = $this->getStorage()->getId();
  246. if (!is_null($sid)) {
  247. $sid = explode(':', $sid);
  248. return ($sid[0] === 'shared');
  249. }
  250. return false;
  251. }
  252. public function isMounted() {
  253. $sid = $this->getStorage()->getId();
  254. if (!is_null($sid)) {
  255. $sid = explode(':', $sid);
  256. return ($sid[0] !== 'home' and $sid[0] !== 'shared');
  257. }
  258. return false;
  259. }
  260. /**
  261. * Get the mountpoint the file belongs to
  262. *
  263. * @return \OCP\Files\Mount\IMountPoint
  264. */
  265. public function getMountPoint() {
  266. return $this->mount;
  267. }
  268. /**
  269. * Get the owner of the file
  270. *
  271. * @return \OCP\IUser
  272. */
  273. public function getOwner() {
  274. return $this->owner;
  275. }
  276. /**
  277. * Add a cache entry which is the child of this folder
  278. *
  279. * Sets the size, etag and size to for cross-storage childs
  280. *
  281. * @param array $data cache entry for the child
  282. * @param string $entryPath full path of the child entry
  283. */
  284. public function addSubEntry($data, $entryPath) {
  285. $this->data['size'] += isset($data['size']) ? $data['size'] : 0;
  286. if (isset($data['mtime'])) {
  287. $this->data['mtime'] = max($this->data['mtime'], $data['mtime']);
  288. }
  289. if (isset($data['etag'])) {
  290. // prefix the etag with the relative path of the subentry to propagate etag on mount moves
  291. $relativeEntryPath = substr($entryPath, strlen($this->getPath()));
  292. // attach the permissions to propagate etag on permision changes of submounts
  293. $permissions = isset($data['permissions']) ? $data['permissions'] : 0;
  294. $this->childEtags[] = $relativeEntryPath . '/' . $data['etag'] . $permissions;
  295. }
  296. }
  297. /**
  298. * @inheritdoc
  299. */
  300. public function getChecksum() {
  301. return $this->data['checksum'];
  302. }
  303. }