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.

cache.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. * @brief 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. return $data;
  90. }
  91. } else {
  92. $sourceId = $file;
  93. // if we are at the root of the mount point we want to return the
  94. // cache information for the source item
  95. if (!is_int($sourceId) || $sourceId === 0) {
  96. $sourceId = $this->storage->getSourceId();
  97. }
  98. $query = \OC_DB::prepare(
  99. 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`,'
  100. . ' `size`, `mtime`, `encrypted`, `unencrypted_size`, `storage_mtime`, `etag`'
  101. . ' FROM `*PREFIX*filecache` WHERE `fileid` = ?');
  102. $result = $query->execute(array($sourceId));
  103. $data = $result->fetchRow();
  104. $data['fileid'] = (int)$data['fileid'];
  105. $data['mtime'] = (int)$data['mtime'];
  106. $data['storage_mtime'] = (int)$data['storage_mtime'];
  107. $data['encrypted'] = (bool)$data['encrypted'];
  108. $data['mimetype'] = $this->getMimetype($data['mimetype']);
  109. $data['mimepart'] = $this->getMimetype($data['mimepart']);
  110. if ($data['storage_mtime'] === 0) {
  111. $data['storage_mtime'] = $data['mtime'];
  112. }
  113. if ($data['encrypted'] or ($data['unencrypted_size'] > 0 and $data['mimetype'] === 'httpd/unix-directory')) {
  114. $data['encrypted_size'] = (int)$data['size'];
  115. $data['size'] = (int)$data['unencrypted_size'];
  116. } else {
  117. $data['size'] = (int)$data['size'];
  118. }
  119. if (!is_int($file) || $file === 0) {
  120. $data['path'] = '';
  121. $data['name'] = basename($this->storage->getMountPoint());
  122. $data['is_share_mount_point'] = true;
  123. }
  124. return $data;
  125. }
  126. return false;
  127. }
  128. /**
  129. * get the metadata of all files stored in $folder
  130. *
  131. * @param string $folder
  132. * @return array
  133. */
  134. public function getFolderContents($folder) {
  135. if ($folder === false) {
  136. $folder = '';
  137. }
  138. $dir = ($folder !== '') ? $folder . '/' : '';
  139. $cache = $this->getSourceCache($folder);
  140. if ($cache) {
  141. $parent = $this->storage->getFile($folder);
  142. $sourceFolderContent = $cache->getFolderContents($this->files[$folder]);
  143. foreach ($sourceFolderContent as $key => $c) {
  144. $sourceFolderContent[$key]['path'] = $dir . $c['name'];
  145. $sourceFolderContent[$key]['uid_owner'] = $parent['uid_owner'];
  146. $sourceFolderContent[$key]['displayname_owner'] = $parent['uid_owner'];
  147. }
  148. return $sourceFolderContent;
  149. }
  150. return false;
  151. }
  152. /**
  153. * store meta data for a file or folder
  154. *
  155. * @param string $file
  156. * @param array $data
  157. *
  158. * @return int file id
  159. */
  160. public function put($file, array $data) {
  161. $file = ($file === false) ? '' : $file;
  162. if ($cache = $this->getSourceCache($file)) {
  163. return $cache->put($this->files[$file], $data);
  164. }
  165. return false;
  166. }
  167. /**
  168. * get the file id for a file
  169. *
  170. * @param string $file
  171. * @return int
  172. */
  173. public function getId($file) {
  174. if ($file === false) {
  175. return $this->storage->getSourceId();
  176. }
  177. $cache = $this->getSourceCache($file);
  178. if ($cache) {
  179. return $cache->getId($this->files[$file]);
  180. }
  181. return -1;
  182. }
  183. /**
  184. * check if a file is available in the cache
  185. *
  186. * @param string $file
  187. * @return bool
  188. */
  189. public function inCache($file) {
  190. if ($file == '') {
  191. return true;
  192. }
  193. return parent::inCache($file);
  194. }
  195. /**
  196. * remove a file or folder from the cache
  197. *
  198. * @param string $file
  199. */
  200. public function remove($file) {
  201. $file = ($file === false) ? '' : $file;
  202. if ($cache = $this->getSourceCache($file)) {
  203. $cache->remove($this->files[$file]);
  204. }
  205. }
  206. /**
  207. * Move a file or folder in the cache
  208. *
  209. * @param string $source
  210. * @param string $target
  211. */
  212. public function move($source, $target) {
  213. if ($cache = $this->getSourceCache($source)) {
  214. $file = \OC_Share_Backend_File::getSource($target, $this->storage->getMountPoint(), $this->storage->getItemType());
  215. if ($file && isset($file['path'])) {
  216. $cache->move($this->files[$source], $file['path']);
  217. }
  218. }
  219. }
  220. /**
  221. * remove all entries for files that are stored on the storage from the cache
  222. */
  223. public function clear() {
  224. // Not a valid action for Shared Cache
  225. }
  226. /**
  227. * @param string $file
  228. *
  229. * @return int, Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE
  230. */
  231. public function getStatus($file) {
  232. if ($file == '') {
  233. return self::COMPLETE;
  234. }
  235. if ($cache = $this->getSourceCache($file)) {
  236. return $cache->getStatus($this->files[$file]);
  237. }
  238. return self::NOT_FOUND;
  239. }
  240. /**
  241. * search for files matching $pattern
  242. *
  243. * @param string $pattern
  244. * @return array of file data
  245. */
  246. public function search($pattern) {
  247. $where = '`name` LIKE ? AND ';
  248. // normalize pattern
  249. $value = $this->normalize($pattern);
  250. return $this->searchWithWhere($where, $value);
  251. }
  252. /**
  253. * search for files by mimetype
  254. *
  255. * @param string $mimetype
  256. * @return array
  257. */
  258. public function searchByMime($mimetype) {
  259. $mimepart = null;
  260. if (strpos($mimetype, '/') === false) {
  261. $mimepart = $mimetype;
  262. $mimetype = null;
  263. }
  264. // note: searchWithWhere is currently broken as it doesn't
  265. // recurse into subdirs nor returns the correct
  266. // file paths, so using getFolderContents() for now
  267. $result = array();
  268. $exploreDirs = array('');
  269. while (count($exploreDirs) > 0) {
  270. $dir = array_pop($exploreDirs);
  271. $files = $this->getFolderContents($dir);
  272. // no results?
  273. if (!$files) {
  274. // maybe it's a single shared file
  275. $file = $this->get('');
  276. if (($mimepart && $file['mimepart'] === $mimepart) || ($mimetype && $file['mimetype'] === $mimetype)) {
  277. $result[] = $file;
  278. }
  279. continue;
  280. }
  281. foreach ($files as $file) {
  282. if ($file['mimetype'] === 'httpd/unix-directory') {
  283. $exploreDirs[] = ltrim($dir . '/' . $file['name'], '/');
  284. } else if (($mimepart && $file['mimepart'] === $mimepart) || ($mimetype && $file['mimetype'] === $mimetype)) {
  285. $result[] = $file;
  286. }
  287. }
  288. }
  289. return $result;
  290. }
  291. /**
  292. * The maximum number of placeholders that can be used in an SQL query.
  293. * Value MUST be <= 1000 for oracle:
  294. * see ORA-01795 maximum number of expressions in a list is 1000
  295. * FIXME we should get this from doctrine as other DBs allow a lot more placeholders
  296. */
  297. const MAX_SQL_CHUNK_SIZE = 1000;
  298. /**
  299. * search for files with a custom where clause and value
  300. * the $wherevalue will be array_merge()d with the file id chunks
  301. *
  302. * @param string $sqlwhere
  303. * @param string $wherevalue
  304. * @return array
  305. */
  306. private function searchWithWhere($sqlwhere, $wherevalue, $chunksize = self::MAX_SQL_CHUNK_SIZE) {
  307. $ids = $this->getAll();
  308. $files = array();
  309. // divide into chunks
  310. $chunks = array_chunk($ids, $chunksize);
  311. foreach ($chunks as $chunk) {
  312. $placeholders = join(',', array_fill(0, count($chunk), '?'));
  313. $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`,
  314. `encrypted`, `unencrypted_size`, `etag`
  315. FROM `*PREFIX*filecache` WHERE ' . $sqlwhere . ' `fileid` IN (' . $placeholders . ')';
  316. $stmt = \OC_DB::prepare($sql);
  317. $result = $stmt->execute(array_merge(array($wherevalue), $chunk));
  318. while ($row = $result->fetchRow()) {
  319. if (substr($row['path'], 0, 6) === 'files/') {
  320. $row['path'] = substr($row['path'], 6); // remove 'files/' from path as it's relative to '/Shared'
  321. }
  322. $row['mimetype'] = $this->getMimetype($row['mimetype']);
  323. $row['mimepart'] = $this->getMimetype($row['mimepart']);
  324. if ($row['encrypted'] or ($row['unencrypted_size'] > 0 and $row['mimetype'] === 'httpd/unix-directory')) {
  325. $row['encrypted_size'] = $row['size'];
  326. $row['size'] = $row['unencrypted_size'];
  327. }
  328. $files[] = $row;
  329. }
  330. }
  331. return $files;
  332. }
  333. /**
  334. * get the size of a folder and set it in the cache
  335. *
  336. * @param string $path
  337. * @param array $entry (optional) meta data of the folder
  338. * @return int
  339. */
  340. public function calculateFolderSize($path, $entry = null) {
  341. $path = ($path === false) ? '' : $path;
  342. if ($cache = $this->getSourceCache($path)) {
  343. return $cache->calculateFolderSize($this->files[$path]);
  344. }
  345. return 0;
  346. }
  347. /**
  348. * get all file ids on the files on the storage
  349. *
  350. * @return int[]
  351. */
  352. public function getAll() {
  353. $ids = \OCP\Share::getItemsSharedWith('file', \OC_Share_Backend_File::FORMAT_GET_ALL);
  354. $folderBackend = \OCP\Share::getBackend('folder');
  355. if ($folderBackend instanceof Share_Backend_Collection) {
  356. foreach ($ids as $file) {
  357. /** @var $folderBackend Share_Backend_Collection */
  358. $children = $folderBackend->getChildren($file);
  359. foreach ($children as $child) {
  360. $ids[] = (int)$child['source'];
  361. }
  362. }
  363. }
  364. return $ids;
  365. }
  366. /**
  367. * find a folder in the cache which has not been fully scanned
  368. *
  369. * If multiply incomplete folders are in the cache, the one with the highest id will be returned,
  370. * use the one with the highest id gives the best result with the background scanner, since that is most
  371. * likely the folder where we stopped scanning previously
  372. *
  373. * @return boolean the path of the folder or false when no folder matched
  374. */
  375. public function getIncomplete() {
  376. return false;
  377. }
  378. /**
  379. * get the path of a file on this storage relative to the mount point by it's id
  380. *
  381. * @param int $id
  382. * @param string $pathEnd (optional) used internally for recursive calls
  383. * @return string | null
  384. */
  385. public function getPathById($id, $pathEnd = '') {
  386. // direct shares are easy
  387. $path = $this->getShareById($id);
  388. if (is_string($path)) {
  389. return ltrim($pathEnd, '/');
  390. } else {
  391. // 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
  392. list($parent, $name) = $this->getParentInfo($id);
  393. if ($parent > 0) {
  394. return $this->getPathById($parent, '/' . $name . $pathEnd);
  395. } else {
  396. return null;
  397. }
  398. }
  399. }
  400. /**
  401. * @param integer $id
  402. */
  403. private function getShareById($id) {
  404. $item = \OCP\Share::getItemSharedWithBySource('file', $id);
  405. if ($item) {
  406. return trim($item['file_target'], '/');
  407. }
  408. $item = \OCP\Share::getItemSharedWithBySource('folder', $id);
  409. if ($item) {
  410. return trim($item['file_target'], '/');
  411. }
  412. return null;
  413. }
  414. /**
  415. * @param integer $id
  416. */
  417. private function getParentInfo($id) {
  418. $sql = 'SELECT `parent`, `name` FROM `*PREFIX*filecache` WHERE `fileid` = ?';
  419. $query = \OC_DB::prepare($sql);
  420. $result = $query->execute(array($id));
  421. if ($row = $result->fetchRow()) {
  422. return array($row['parent'], $row['name']);
  423. } else {
  424. return array(-1, '');
  425. }
  426. }
  427. }