FileInfo.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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 <vincent@nextcloud.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. if (isset($this->data['unencrypted_size']) && $this->data['unencrypted_size'] !== 0) {
  93. $this->rawSize = $this->data['unencrypted_size'];
  94. } else {
  95. $this->rawSize = $this->data['size'] ?? 0;
  96. }
  97. }
  98. public function offsetSet($offset, $value): void {
  99. $this->data[$offset] = $value;
  100. }
  101. public function offsetExists($offset): bool {
  102. return isset($this->data[$offset]);
  103. }
  104. public function offsetUnset($offset): void {
  105. unset($this->data[$offset]);
  106. }
  107. /**
  108. * @return mixed
  109. */
  110. #[\ReturnTypeWillChange]
  111. public function offsetGet($offset) {
  112. if ($offset === 'type') {
  113. return $this->getType();
  114. } elseif ($offset === 'etag') {
  115. return $this->getEtag();
  116. } elseif ($offset === 'size') {
  117. return $this->getSize();
  118. } elseif ($offset === 'mtime') {
  119. return $this->getMTime();
  120. } elseif ($offset === 'permissions') {
  121. return $this->getPermissions();
  122. } elseif (isset($this->data[$offset])) {
  123. return $this->data[$offset];
  124. } else {
  125. return null;
  126. }
  127. }
  128. /**
  129. * @return string
  130. */
  131. public function getPath() {
  132. return $this->path;
  133. }
  134. /**
  135. * @return \OCP\Files\Storage
  136. */
  137. public function getStorage() {
  138. return $this->storage;
  139. }
  140. /**
  141. * @return string
  142. */
  143. public function getInternalPath() {
  144. return $this->internalPath;
  145. }
  146. /**
  147. * Get FileInfo ID or null in case of part file
  148. *
  149. * @return int|null
  150. */
  151. public function getId() {
  152. return isset($this->data['fileid']) ? (int) $this->data['fileid'] : null;
  153. }
  154. /**
  155. * @return string
  156. */
  157. public function getMimetype() {
  158. return $this->data['mimetype'];
  159. }
  160. /**
  161. * @return string
  162. */
  163. public function getMimePart() {
  164. return $this->data['mimepart'];
  165. }
  166. /**
  167. * @return string
  168. */
  169. public function getName() {
  170. return isset($this->data['name']) ? $this->data['name'] : basename($this->getPath());
  171. }
  172. /**
  173. * @return string
  174. */
  175. public function getEtag() {
  176. $this->updateEntryfromSubMounts();
  177. if (count($this->childEtags) > 0) {
  178. $combinedEtag = $this->data['etag'] . '::' . implode('::', $this->childEtags);
  179. return md5($combinedEtag);
  180. } else {
  181. return $this->data['etag'];
  182. }
  183. }
  184. /**
  185. * @return int
  186. */
  187. public function getSize($includeMounts = true) {
  188. if ($includeMounts) {
  189. $this->updateEntryfromSubMounts();
  190. if (isset($this->data['unencrypted_size']) && $this->data['unencrypted_size'] > 0) {
  191. return $this->data['unencrypted_size'];
  192. } else {
  193. return isset($this->data['size']) ? 0 + $this->data['size'] : 0;
  194. }
  195. } else {
  196. return $this->rawSize;
  197. }
  198. }
  199. /**
  200. * @return int
  201. */
  202. public function getMTime() {
  203. $this->updateEntryfromSubMounts();
  204. return (int) $this->data['mtime'];
  205. }
  206. /**
  207. * @return bool
  208. */
  209. public function isEncrypted() {
  210. return $this->data['encrypted'];
  211. }
  212. /**
  213. * Return the currently version used for the HMAC in the encryption app
  214. *
  215. * @return int
  216. */
  217. public function getEncryptedVersion() {
  218. return isset($this->data['encryptedVersion']) ? (int) $this->data['encryptedVersion'] : 1;
  219. }
  220. /**
  221. * @return int
  222. */
  223. public function getPermissions() {
  224. $perms = (int) $this->data['permissions'];
  225. if (\OCP\Util::isSharingDisabledForUser() || ($this->isShared() && !\OC\Share\Share::isResharingAllowed())) {
  226. $perms = $perms & ~\OCP\Constants::PERMISSION_SHARE;
  227. }
  228. return $perms;
  229. }
  230. /**
  231. * @return string \OCP\Files\FileInfo::TYPE_FILE|\OCP\Files\FileInfo::TYPE_FOLDER
  232. */
  233. public function getType() {
  234. if (!isset($this->data['type'])) {
  235. $this->data['type'] = ($this->getMimetype() === self::MIMETYPE_FOLDER) ? self::TYPE_FOLDER : self::TYPE_FILE;
  236. }
  237. return $this->data['type'];
  238. }
  239. public function getData() {
  240. return $this->data;
  241. }
  242. /**
  243. * @param int $permissions
  244. * @return bool
  245. */
  246. protected function checkPermissions($permissions) {
  247. return ($this->getPermissions() & $permissions) === $permissions;
  248. }
  249. /**
  250. * @return bool
  251. */
  252. public function isReadable() {
  253. return $this->checkPermissions(\OCP\Constants::PERMISSION_READ);
  254. }
  255. /**
  256. * @return bool
  257. */
  258. public function isUpdateable() {
  259. return $this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE);
  260. }
  261. /**
  262. * Check whether new files or folders can be created inside this folder
  263. *
  264. * @return bool
  265. */
  266. public function isCreatable() {
  267. return $this->checkPermissions(\OCP\Constants::PERMISSION_CREATE);
  268. }
  269. /**
  270. * @return bool
  271. */
  272. public function isDeletable() {
  273. return $this->checkPermissions(\OCP\Constants::PERMISSION_DELETE);
  274. }
  275. /**
  276. * @return bool
  277. */
  278. public function isShareable() {
  279. return $this->checkPermissions(\OCP\Constants::PERMISSION_SHARE);
  280. }
  281. /**
  282. * Check if a file or folder is shared
  283. *
  284. * @return bool
  285. */
  286. public function isShared() {
  287. $sid = $this->getStorage()->getId();
  288. if (!is_null($sid)) {
  289. $sid = explode(':', $sid);
  290. return ($sid[0] === 'shared');
  291. }
  292. return false;
  293. }
  294. public function isMounted() {
  295. $storage = $this->getStorage();
  296. if ($storage->instanceOfStorage('\OCP\Files\IHomeStorage')) {
  297. return false;
  298. }
  299. $sid = $storage->getId();
  300. if (!is_null($sid)) {
  301. $sid = explode(':', $sid);
  302. return ($sid[0] !== 'home' and $sid[0] !== 'shared');
  303. }
  304. return false;
  305. }
  306. /**
  307. * Get the mountpoint the file belongs to
  308. *
  309. * @return \OCP\Files\Mount\IMountPoint
  310. */
  311. public function getMountPoint() {
  312. return $this->mount;
  313. }
  314. /**
  315. * Get the owner of the file
  316. *
  317. * @return \OCP\IUser
  318. */
  319. public function getOwner() {
  320. return $this->owner;
  321. }
  322. /**
  323. * @param IMountPoint[] $mounts
  324. */
  325. public function setSubMounts(array $mounts) {
  326. $this->subMounts = $mounts;
  327. }
  328. private function updateEntryfromSubMounts() {
  329. if ($this->subMountsUsed) {
  330. return;
  331. }
  332. $this->subMountsUsed = true;
  333. foreach ($this->subMounts as $mount) {
  334. $subStorage = $mount->getStorage();
  335. if ($subStorage) {
  336. $subCache = $subStorage->getCache('');
  337. $rootEntry = $subCache->get('');
  338. $this->addSubEntry($rootEntry, $mount->getMountPoint());
  339. }
  340. }
  341. }
  342. /**
  343. * Add a cache entry which is the child of this folder
  344. *
  345. * Sets the size, etag and size to for cross-storage childs
  346. *
  347. * @param array|ICacheEntry $data cache entry for the child
  348. * @param string $entryPath full path of the child entry
  349. */
  350. public function addSubEntry($data, $entryPath) {
  351. if (!$data) {
  352. return;
  353. }
  354. $hasUnencryptedSize = isset($data['unencrypted_size']) && $data['unencrypted_size'] > 0;
  355. if ($hasUnencryptedSize) {
  356. $subSize = $data['unencrypted_size'];
  357. } else {
  358. $subSize = $data['size'] ?: 0;
  359. }
  360. $this->data['size'] += $subSize;
  361. if ($hasUnencryptedSize) {
  362. $this->data['unencrypted_size'] += $subSize;
  363. }
  364. if (isset($data['mtime'])) {
  365. $this->data['mtime'] = max($this->data['mtime'], $data['mtime']);
  366. }
  367. if (isset($data['etag'])) {
  368. // prefix the etag with the relative path of the subentry to propagate etag on mount moves
  369. $relativeEntryPath = substr($entryPath, strlen($this->getPath()));
  370. // attach the permissions to propagate etag on permission changes of submounts
  371. $permissions = isset($data['permissions']) ? $data['permissions'] : 0;
  372. $this->childEtags[] = $relativeEntryPath . '/' . $data['etag'] . $permissions;
  373. }
  374. }
  375. /**
  376. * @inheritdoc
  377. */
  378. public function getChecksum() {
  379. return $this->data['checksum'];
  380. }
  381. public function getExtension(): string {
  382. return pathinfo($this->getName(), PATHINFO_EXTENSION);
  383. }
  384. public function getCreationTime(): int {
  385. return (int) $this->data['creation_time'];
  386. }
  387. public function getUploadTime(): int {
  388. return (int) $this->data['upload_time'];
  389. }
  390. }