cache.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Bjoern Schiessle, Michael Gapczynski
  6. * @copyright 2012 Michael Gapczynski <mtgap@owncloud.com>
  7. * 2014 Bjoern Schiessle <schiessle@owncloud.com>
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  11. * License as published by the Free Software Foundation; either
  12. * version 3 of the License, or any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public
  20. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. namespace OC\Files\Cache;
  23. use OCP\Share_Backend_Collection;
  24. /**
  25. * Metadata cache for shared files
  26. *
  27. * don't use this class directly if you need to get metadata, use \OC\Files\Filesystem::getFileInfo instead
  28. */
  29. class Shared_Cache extends Cache {
  30. private $storage;
  31. private $files = array();
  32. /**
  33. * @param \OC\Files\Storage\Shared $storage
  34. */
  35. public function __construct($storage) {
  36. $this->storage = $storage;
  37. }
  38. /**
  39. * Get the source cache of a shared file or folder
  40. * @param string $target Shared target file path
  41. * @return \OC\Files\Cache\Cache
  42. */
  43. private function getSourceCache($target) {
  44. if ($target === false || $target === $this->storage->getMountPoint()) {
  45. $target = '';
  46. }
  47. $source = \OC_Share_Backend_File::getSource($target, $this->storage->getMountPoint(), $this->storage->getItemType());
  48. if (isset($source['path']) && isset($source['fileOwner'])) {
  49. \OC\Files\Filesystem::initMountPoints($source['fileOwner']);
  50. $mounts = \OC\Files\Filesystem::getMountByNumericId($source['storage']);
  51. if (is_array($mounts) and !empty($mounts)) {
  52. $fullPath = $mounts[0]->getMountPoint() . $source['path'];
  53. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($fullPath);
  54. if ($storage) {
  55. $this->files[$target] = $internalPath;
  56. $cache = $storage->getCache();
  57. $this->storageId = $storage->getId();
  58. $this->numericId = $cache->getNumericStorageId();
  59. return $cache;
  60. }
  61. }
  62. }
  63. return false;
  64. }
  65. public function getNumericStorageId() {
  66. if (isset($this->numericId)) {
  67. return $this->numericId;
  68. } else {
  69. return false;
  70. }
  71. }
  72. /**
  73. * get the stored metadata of a file or folder
  74. *
  75. * @param string|int $file
  76. * @return array
  77. */
  78. public function get($file) {
  79. if (is_string($file)) {
  80. $cache = $this->getSourceCache($file);
  81. if ($cache) {
  82. $data = $cache->get($this->files[$file]);
  83. $data['displayname_owner'] = \OC_User::getDisplayName($this->storage->getSharedFrom());
  84. $data['path'] = $file;
  85. if ($file === '') {
  86. $data['is_share_mount_point'] = true;
  87. }
  88. $data['uid_owner'] = $this->storage->getOwner($file);
  89. if (isset($data['permissions'])) {
  90. $data['permissions'] &= $this->storage->getPermissions($file);
  91. } else {
  92. $data['permissions'] = $this->storage->getPermissions($file);
  93. }
  94. return $data;
  95. }
  96. } else {
  97. $sourceId = $file;
  98. // if we are at the root of the mount point we want to return the
  99. // cache information for the source item
  100. if (!is_int($sourceId) || $sourceId === 0) {
  101. $sourceId = $this->storage->getSourceId();
  102. }
  103. $query = \OC_DB::prepare(
  104. 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`,'
  105. . ' `size`, `mtime`, `encrypted`, `unencrypted_size`, `storage_mtime`, `etag`, `permissions`'
  106. . ' FROM `*PREFIX*filecache` WHERE `fileid` = ?');
  107. $result = $query->execute(array($sourceId));
  108. $data = $result->fetchRow();
  109. $data['fileid'] = (int)$data['fileid'];
  110. $data['mtime'] = (int)$data['mtime'];
  111. $data['storage_mtime'] = (int)$data['storage_mtime'];
  112. $data['encrypted'] = (bool)$data['encrypted'];
  113. $data['mimetype'] = $this->getMimetype($data['mimetype']);
  114. $data['mimepart'] = $this->getMimetype($data['mimepart']);
  115. if ($data['storage_mtime'] === 0) {
  116. $data['storage_mtime'] = $data['mtime'];
  117. }
  118. if ($data['encrypted'] or ($data['unencrypted_size'] > 0 and $data['mimetype'] === 'httpd/unix-directory')) {
  119. $data['encrypted_size'] = (int)$data['size'];
  120. $data['size'] = (int)$data['unencrypted_size'];
  121. } else {
  122. $data['size'] = (int)$data['size'];
  123. }
  124. $data['permissions'] = (int)$data['permissions'];
  125. if (!is_int($file) || $file === 0) {
  126. $data['path'] = '';
  127. $data['name'] = basename($this->storage->getMountPoint());
  128. $data['is_share_mount_point'] = true;
  129. }
  130. $data['permissions'] &= $this->storage->getPermissions('');
  131. return $data;
  132. }
  133. return false;
  134. }
  135. /**
  136. * get the metadata of all files stored in $folder
  137. *
  138. * @param string $folder
  139. * @return array
  140. */
  141. public function getFolderContents($folder) {
  142. if ($folder === false) {
  143. $folder = '';
  144. }
  145. $dir = ($folder !== '') ? $folder . '/' : '';
  146. $cache = $this->getSourceCache($folder);
  147. if ($cache) {
  148. $parent = $this->storage->getFile($folder);
  149. $sourceFolderContent = $cache->getFolderContents($this->files[$folder]);
  150. foreach ($sourceFolderContent as $key => $c) {
  151. $sourceFolderContent[$key]['path'] = $dir . $c['name'];
  152. $sourceFolderContent[$key]['uid_owner'] = $parent['uid_owner'];
  153. $sourceFolderContent[$key]['displayname_owner'] = $parent['uid_owner'];
  154. $sourceFolderContent[$key]['permissions'] = $sourceFolderContent[$key]['permissions'] & $this->storage->getPermissions($dir . $c['name']);
  155. }
  156. return $sourceFolderContent;
  157. }
  158. return false;
  159. }
  160. /**
  161. * store meta data for a file or folder
  162. *
  163. * @param string $file
  164. * @param array $data
  165. *
  166. * @return int file id
  167. */
  168. public function put($file, array $data) {
  169. $file = ($file === false) ? '' : $file;
  170. if ($cache = $this->getSourceCache($file)) {
  171. return $cache->put($this->files[$file], $data);
  172. }
  173. return false;
  174. }
  175. /**
  176. * get the file id for a file
  177. *
  178. * @param string $file
  179. * @return int
  180. */
  181. public function getId($file) {
  182. if ($file === false) {
  183. return $this->storage->getSourceId();
  184. }
  185. $cache = $this->getSourceCache($file);
  186. if ($cache) {
  187. return $cache->getId($this->files[$file]);
  188. }
  189. return -1;
  190. }
  191. /**
  192. * check if a file is available in the cache
  193. *
  194. * @param string $file
  195. * @return bool
  196. */
  197. public function inCache($file) {
  198. if ($file == '') {
  199. return true;
  200. }
  201. return parent::inCache($file);
  202. }
  203. /**
  204. * remove a file or folder from the cache
  205. *
  206. * @param string $file
  207. */
  208. public function remove($file) {
  209. $file = ($file === false) ? '' : $file;
  210. if ($cache = $this->getSourceCache($file)) {
  211. $cache->remove($this->files[$file]);
  212. }
  213. }
  214. /**
  215. * Move a file or folder in the cache
  216. *
  217. * @param string $source
  218. * @param string $target
  219. */
  220. public function move($source, $target) {
  221. if ($cache = $this->getSourceCache($source)) {
  222. $file = \OC_Share_Backend_File::getSource($target, $this->storage->getMountPoint(), $this->storage->getItemType());
  223. if ($file && isset($file['path'])) {
  224. $cache->move($this->files[$source], $file['path']);
  225. }
  226. }
  227. }
  228. /**
  229. * remove all entries for files that are stored on the storage from the cache
  230. */
  231. public function clear() {
  232. // Not a valid action for Shared Cache
  233. }
  234. /**
  235. * @param string $file
  236. *
  237. * @return int, Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE
  238. */
  239. public function getStatus($file) {
  240. if ($file == '') {
  241. return self::COMPLETE;
  242. }
  243. if ($cache = $this->getSourceCache($file)) {
  244. return $cache->getStatus($this->files[$file]);
  245. }
  246. return self::NOT_FOUND;
  247. }
  248. /**
  249. * search for files matching $pattern
  250. *
  251. * @param string $pattern
  252. * @return array of file data
  253. */
  254. public function search($pattern) {
  255. $where = '`name` LIKE ? AND ';
  256. // normalize pattern
  257. $value = $this->normalize($pattern);
  258. return $this->searchWithWhere($where, $value);
  259. }
  260. /**
  261. * search for files by mimetype
  262. *
  263. * @param string $mimetype
  264. * @return array
  265. */
  266. public function searchByMime($mimetype) {
  267. $mimepart = null;
  268. if (strpos($mimetype, '/') === false) {
  269. $mimepart = $mimetype;
  270. $mimetype = null;
  271. }
  272. // note: searchWithWhere is currently broken as it doesn't
  273. // recurse into subdirs nor returns the correct
  274. // file paths, so using getFolderContents() for now
  275. $result = array();
  276. $exploreDirs = array('');
  277. while (count($exploreDirs) > 0) {
  278. $dir = array_pop($exploreDirs);
  279. $files = $this->getFolderContents($dir);
  280. // no results?
  281. if (!$files) {
  282. // maybe it's a single shared file
  283. $file = $this->get('');
  284. if (($mimepart && $file['mimepart'] === $mimepart) || ($mimetype && $file['mimetype'] === $mimetype)) {
  285. $result[] = $file;
  286. }
  287. continue;
  288. }
  289. foreach ($files as $file) {
  290. if ($file['mimetype'] === 'httpd/unix-directory') {
  291. $exploreDirs[] = ltrim($dir . '/' . $file['name'], '/');
  292. } else if (($mimepart && $file['mimepart'] === $mimepart) || ($mimetype && $file['mimetype'] === $mimetype)) {
  293. $result[] = $file;
  294. }
  295. }
  296. }
  297. return $result;
  298. }
  299. /**
  300. * The maximum number of placeholders that can be used in an SQL query.
  301. * Value MUST be <= 1000 for oracle:
  302. * see ORA-01795 maximum number of expressions in a list is 1000
  303. * FIXME we should get this from doctrine as other DBs allow a lot more placeholders
  304. */
  305. const MAX_SQL_CHUNK_SIZE = 1000;
  306. /**
  307. * search for files with a custom where clause and value
  308. * the $wherevalue will be array_merge()d with the file id chunks
  309. *
  310. * @param string $sqlwhere
  311. * @param string $wherevalue
  312. * @return array
  313. */
  314. private function searchWithWhere($sqlwhere, $wherevalue, $chunksize = self::MAX_SQL_CHUNK_SIZE) {
  315. $ids = $this->getAll();
  316. $files = array();
  317. // divide into chunks
  318. $chunks = array_chunk($ids, $chunksize);
  319. foreach ($chunks as $chunk) {
  320. $placeholders = join(',', array_fill(0, count($chunk), '?'));
  321. $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`,
  322. `encrypted`, `unencrypted_size`, `etag`
  323. FROM `*PREFIX*filecache` WHERE ' . $sqlwhere . ' `fileid` IN (' . $placeholders . ')';
  324. $stmt = \OC_DB::prepare($sql);
  325. $result = $stmt->execute(array_merge(array($wherevalue), $chunk));
  326. while ($row = $result->fetchRow()) {
  327. if (substr($row['path'], 0, 6) === 'files/') {
  328. $row['path'] = substr($row['path'], 6); // remove 'files/' from path as it's relative to '/Shared'
  329. }
  330. $row['mimetype'] = $this->getMimetype($row['mimetype']);
  331. $row['mimepart'] = $this->getMimetype($row['mimepart']);
  332. if ($row['encrypted'] or ($row['unencrypted_size'] > 0 and $row['mimetype'] === 'httpd/unix-directory')) {
  333. $row['encrypted_size'] = $row['size'];
  334. $row['size'] = $row['unencrypted_size'];
  335. }
  336. $files[] = $row;
  337. }
  338. }
  339. return $files;
  340. }
  341. /**
  342. * get the size of a folder and set it in the cache
  343. *
  344. * @param string $path
  345. * @param array $entry (optional) meta data of the folder
  346. * @return int
  347. */
  348. public function calculateFolderSize($path, $entry = null) {
  349. $path = ($path === false) ? '' : $path;
  350. if ($cache = $this->getSourceCache($path)) {
  351. return $cache->calculateFolderSize($this->files[$path]);
  352. }
  353. return 0;
  354. }
  355. /**
  356. * get all file ids on the files on the storage
  357. *
  358. * @return int[]
  359. */
  360. public function getAll() {
  361. $ids = \OCP\Share::getItemsSharedWith('file', \OC_Share_Backend_File::FORMAT_GET_ALL);
  362. $folderBackend = \OCP\Share::getBackend('folder');
  363. if ($folderBackend instanceof Share_Backend_Collection) {
  364. foreach ($ids as $file) {
  365. /** @var $folderBackend Share_Backend_Collection */
  366. $children = $folderBackend->getChildren($file);
  367. foreach ($children as $child) {
  368. $ids[] = (int)$child['source'];
  369. }
  370. }
  371. }
  372. return $ids;
  373. }
  374. /**
  375. * find a folder in the cache which has not been fully scanned
  376. *
  377. * If multiply incomplete folders are in the cache, the one with the highest id will be returned,
  378. * use the one with the highest id gives the best result with the background scanner, since that is most
  379. * likely the folder where we stopped scanning previously
  380. *
  381. * @return boolean the path of the folder or false when no folder matched
  382. */
  383. public function getIncomplete() {
  384. return false;
  385. }
  386. /**
  387. * get the path of a file on this storage relative to the mount point by it's id
  388. *
  389. * @param int $id
  390. * @param string $pathEnd (optional) used internally for recursive calls
  391. * @return string|null
  392. */
  393. public function getPathById($id, $pathEnd = '') {
  394. // direct shares are easy
  395. $path = $this->getShareById($id);
  396. if (is_string($path)) {
  397. return ltrim($pathEnd, '/');
  398. } else {
  399. // if the item is a direct share we try and get the path of the parent and append the name of the item to it
  400. list($parent, $name) = $this->getParentInfo($id);
  401. if ($parent > 0) {
  402. return $this->getPathById($parent, '/' . $name . $pathEnd);
  403. } else {
  404. return null;
  405. }
  406. }
  407. }
  408. /**
  409. * @param integer $id
  410. */
  411. private function getShareById($id) {
  412. $item = \OCP\Share::getItemSharedWithBySource('file', $id);
  413. if ($item) {
  414. return trim($item['file_target'], '/');
  415. }
  416. $item = \OCP\Share::getItemSharedWithBySource('folder', $id);
  417. if ($item) {
  418. return trim($item['file_target'], '/');
  419. }
  420. return null;
  421. }
  422. /**
  423. * @param integer $id
  424. */
  425. private function getParentInfo($id) {
  426. $sql = 'SELECT `parent`, `name` FROM `*PREFIX*filecache` WHERE `fileid` = ?';
  427. $query = \OC_DB::prepare($sql);
  428. $result = $query->execute(array($id));
  429. if ($row = $result->fetchRow()) {
  430. return array($row['parent'], $row['name']);
  431. } else {
  432. return array(-1, '');
  433. }
  434. }
  435. }