You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

417 lines
9.2 KiB

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