FileInfo.php 9.9 KB

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