FileInfo.php 8.4 KB

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