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.

428 lines
9.8 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 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 Maxence Lange <maxence@artificial-owl.com>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Piotr M <mrow4a@yahoo.com>
  12. * @author Robin Appelman <robin@icewind.nl>
  13. * @author Robin McCorkell <robin@mccorkell.me.uk>
  14. * @author Roeland Jago Douma <roeland@famdouma.nl>
  15. * @author tbartenstein <tbartenstein@users.noreply.github.com>
  16. * @author Thomas Müller <thomas.mueller@tmit.eu>
  17. * @author Vincent Petry <vincent@nextcloud.com>
  18. *
  19. * @license AGPL-3.0
  20. *
  21. * This code is free software: you can redistribute it and/or modify
  22. * it under the terms of the GNU Affero General Public License, version 3,
  23. * as published by the Free Software Foundation.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU Affero General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU Affero General Public License, version 3,
  31. * along with this program. If not, see <http://www.gnu.org/licenses/>
  32. *
  33. */
  34. namespace OC\Files;
  35. use OCA\Files_Sharing\ISharedStorage;
  36. use OCP\Files\Cache\ICacheEntry;
  37. use OCP\Files\IHomeStorage;
  38. use OCP\Files\Mount\IMountPoint;
  39. use OCP\IUser;
  40. class FileInfo implements \OCP\Files\FileInfo, \ArrayAccess {
  41. private array|ICacheEntry $data;
  42. /**
  43. * @var string
  44. */
  45. private $path;
  46. /**
  47. * @var \OC\Files\Storage\Storage $storage
  48. */
  49. private $storage;
  50. /**
  51. * @var string
  52. */
  53. private $internalPath;
  54. /**
  55. * @var \OCP\Files\Mount\IMountPoint
  56. */
  57. private $mount;
  58. private ?IUser $owner;
  59. /**
  60. * @var string[]
  61. */
  62. private array $childEtags = [];
  63. /**
  64. * @var IMountPoint[]
  65. */
  66. private array $subMounts = [];
  67. private bool $subMountsUsed = false;
  68. /**
  69. * The size of the file/folder without any sub mount
  70. */
  71. private int|float $rawSize = 0;
  72. /**
  73. * @param string|boolean $path
  74. * @param Storage\Storage $storage
  75. * @param string $internalPath
  76. * @param array|ICacheEntry $data
  77. * @param IMountPoint $mount
  78. * @param ?IUser $owner
  79. */
  80. public function __construct($path, $storage, $internalPath, $data, $mount, $owner = null) {
  81. $this->path = $path;
  82. $this->storage = $storage;
  83. $this->internalPath = $internalPath;
  84. $this->data = $data;
  85. $this->mount = $mount;
  86. $this->owner = $owner;
  87. if (isset($this->data['unencrypted_size']) && $this->data['unencrypted_size'] !== 0) {
  88. $this->rawSize = $this->data['unencrypted_size'];
  89. } else {
  90. $this->rawSize = $this->data['size'] ?? 0;
  91. }
  92. }
  93. public function offsetSet($offset, $value): void {
  94. if (is_null($offset)) {
  95. throw new \TypeError('Null offset not supported');
  96. }
  97. $this->data[$offset] = $value;
  98. }
  99. public function offsetExists($offset): bool {
  100. return isset($this->data[$offset]);
  101. }
  102. public function offsetUnset($offset): void {
  103. unset($this->data[$offset]);
  104. }
  105. /**
  106. * @return mixed
  107. */
  108. #[\ReturnTypeWillChange]
  109. public function offsetGet($offset) {
  110. if ($offset === 'type') {
  111. return $this->getType();
  112. } elseif ($offset === 'etag') {
  113. return $this->getEtag();
  114. } elseif ($offset === 'size') {
  115. return $this->getSize();
  116. } elseif ($offset === 'mtime') {
  117. return $this->getMTime();
  118. } elseif ($offset === 'permissions') {
  119. return $this->getPermissions();
  120. } elseif (isset($this->data[$offset])) {
  121. return $this->data[$offset];
  122. } else {
  123. return null;
  124. }
  125. }
  126. /**
  127. * @return string
  128. */
  129. public function getPath() {
  130. return $this->path;
  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 ($this->isEncrypted() && 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'] ?? false;
  207. }
  208. /**
  209. * Return the current 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. return (int) $this->data['permissions'];
  219. }
  220. /**
  221. * @return string \OCP\Files\FileInfo::TYPE_FILE|\OCP\Files\FileInfo::TYPE_FOLDER
  222. */
  223. public function getType() {
  224. if (!isset($this->data['type'])) {
  225. $this->data['type'] = ($this->getMimetype() === self::MIMETYPE_FOLDER) ? self::TYPE_FOLDER : self::TYPE_FILE;
  226. }
  227. return $this->data['type'];
  228. }
  229. public function getData() {
  230. return $this->data;
  231. }
  232. /**
  233. * @param int $permissions
  234. * @return bool
  235. */
  236. protected function checkPermissions($permissions) {
  237. return ($this->getPermissions() & $permissions) === $permissions;
  238. }
  239. /**
  240. * @return bool
  241. */
  242. public function isReadable() {
  243. return $this->checkPermissions(\OCP\Constants::PERMISSION_READ);
  244. }
  245. /**
  246. * @return bool
  247. */
  248. public function isUpdateable() {
  249. return $this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE);
  250. }
  251. /**
  252. * Check whether new files or folders can be created inside this folder
  253. *
  254. * @return bool
  255. */
  256. public function isCreatable() {
  257. return $this->checkPermissions(\OCP\Constants::PERMISSION_CREATE);
  258. }
  259. /**
  260. * @return bool
  261. */
  262. public function isDeletable() {
  263. return $this->checkPermissions(\OCP\Constants::PERMISSION_DELETE);
  264. }
  265. /**
  266. * @return bool
  267. */
  268. public function isShareable() {
  269. return $this->checkPermissions(\OCP\Constants::PERMISSION_SHARE);
  270. }
  271. /**
  272. * Check if a file or folder is shared
  273. *
  274. * @return bool
  275. */
  276. public function isShared() {
  277. $storage = $this->getStorage();
  278. return $storage->instanceOfStorage(ISharedStorage::class);
  279. }
  280. public function isMounted() {
  281. $storage = $this->getStorage();
  282. return !($storage->instanceOfStorage(IHomeStorage::class) || $storage->instanceOfStorage(ISharedStorage::class));
  283. }
  284. /**
  285. * Get the mountpoint the file belongs to
  286. *
  287. * @return \OCP\Files\Mount\IMountPoint
  288. */
  289. public function getMountPoint() {
  290. return $this->mount;
  291. }
  292. /**
  293. * Get the owner of the file
  294. *
  295. * @return ?IUser
  296. */
  297. public function getOwner() {
  298. return $this->owner;
  299. }
  300. /**
  301. * @param IMountPoint[] $mounts
  302. */
  303. public function setSubMounts(array $mounts) {
  304. $this->subMounts = $mounts;
  305. }
  306. private function updateEntryfromSubMounts(): void {
  307. if ($this->subMountsUsed) {
  308. return;
  309. }
  310. $this->subMountsUsed = true;
  311. foreach ($this->subMounts as $mount) {
  312. $subStorage = $mount->getStorage();
  313. if ($subStorage) {
  314. $subCache = $subStorage->getCache('');
  315. $rootEntry = $subCache->get('');
  316. $this->addSubEntry($rootEntry, $mount->getMountPoint());
  317. }
  318. }
  319. }
  320. /**
  321. * Add a cache entry which is the child of this folder
  322. *
  323. * Sets the size, etag and size to for cross-storage childs
  324. *
  325. * @param array|ICacheEntry $data cache entry for the child
  326. * @param string $entryPath full path of the child entry
  327. */
  328. public function addSubEntry($data, $entryPath) {
  329. if (!$data) {
  330. return;
  331. }
  332. $hasUnencryptedSize = isset($data['unencrypted_size']) && $data['unencrypted_size'] > 0;
  333. if ($hasUnencryptedSize) {
  334. $subSize = $data['unencrypted_size'];
  335. } else {
  336. $subSize = $data['size'] ?: 0;
  337. }
  338. $this->data['size'] += $subSize;
  339. if ($hasUnencryptedSize) {
  340. $this->data['unencrypted_size'] += $subSize;
  341. }
  342. if (isset($data['mtime'])) {
  343. $this->data['mtime'] = max($this->data['mtime'], $data['mtime']);
  344. }
  345. if (isset($data['etag'])) {
  346. // prefix the etag with the relative path of the subentry to propagate etag on mount moves
  347. $relativeEntryPath = substr($entryPath, strlen($this->getPath()));
  348. // attach the permissions to propagate etag on permission changes of submounts
  349. $permissions = isset($data['permissions']) ? $data['permissions'] : 0;
  350. $this->childEtags[] = $relativeEntryPath . '/' . $data['etag'] . $permissions;
  351. }
  352. }
  353. /**
  354. * @inheritdoc
  355. */
  356. public function getChecksum() {
  357. return $this->data['checksum'];
  358. }
  359. public function getExtension(): string {
  360. return pathinfo($this->getName(), PATHINFO_EXTENSION);
  361. }
  362. public function getCreationTime(): int {
  363. return (int) $this->data['creation_time'];
  364. }
  365. public function getUploadTime(): int {
  366. return (int) $this->data['upload_time'];
  367. }
  368. public function getParentId(): int {
  369. return $this->data['parent'] ?? -1;
  370. }
  371. /**
  372. * @inheritDoc
  373. * @return array<string, int|string|bool|float|string[]|int[]>
  374. */
  375. public function getMetadata(): array {
  376. return $this->data['metadata'] ?? [];
  377. }
  378. }