FileInfo.php 8.8 KB

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