cache.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. *
  41. * @param string $target Shared target file path
  42. * @return \OC\Files\Cache\Cache
  43. */
  44. private function getSourceCache($target) {
  45. if ($target === false || $target === $this->storage->getMountPoint()) {
  46. $target = '';
  47. }
  48. $source = \OC_Share_Backend_File::getSource($target, $this->storage->getMountPoint(), $this->storage->getItemType());
  49. if (isset($source['path']) && isset($source['fileOwner'])) {
  50. \OC\Files\Filesystem::initMountPoints($source['fileOwner']);
  51. $mounts = \OC\Files\Filesystem::getMountByNumericId($source['storage']);
  52. if (is_array($mounts) and !empty($mounts)) {
  53. $fullPath = $mounts[0]->getMountPoint() . $source['path'];
  54. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($fullPath);
  55. if ($storage) {
  56. $this->files[$target] = $internalPath;
  57. $cache = $storage->getCache();
  58. $this->storageId = $storage->getId();
  59. $this->numericId = $cache->getNumericStorageId();
  60. return $cache;
  61. }
  62. }
  63. }
  64. return false;
  65. }
  66. public function getNumericStorageId() {
  67. if (isset($this->numericId)) {
  68. return $this->numericId;
  69. } else {
  70. return false;
  71. }
  72. }
  73. /**
  74. * get the stored metadata of a file or folder
  75. *
  76. * @param string|int $file
  77. * @return array
  78. */
  79. public function get($file) {
  80. if (is_string($file)) {
  81. $cache = $this->getSourceCache($file);
  82. if ($cache) {
  83. $data = $cache->get($this->files[$file]);
  84. $data['displayname_owner'] = \OC_User::getDisplayName($this->storage->getSharedFrom());
  85. $data['path'] = $file;
  86. if ($file === '') {
  87. $data['is_share_mount_point'] = true;
  88. }
  89. $data['uid_owner'] = $this->storage->getOwner($file);
  90. if (isset($data['permissions'])) {
  91. $data['permissions'] &= $this->storage->getPermissions($file);
  92. } else {
  93. $data['permissions'] = $this->storage->getPermissions($file);
  94. }
  95. return $data;
  96. }
  97. } else {
  98. $sourceId = $file;
  99. // if we are at the root of the mount point we want to return the
  100. // cache information for the source item
  101. if (!is_int($sourceId) || $sourceId === 0) {
  102. $sourceId = $this->storage->getSourceId();
  103. }
  104. $query = \OC_DB::prepare(
  105. 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`,'
  106. . ' `size`, `mtime`, `encrypted`, `unencrypted_size`, `storage_mtime`, `etag`, `permissions`'
  107. . ' FROM `*PREFIX*filecache` WHERE `fileid` = ?');
  108. $result = $query->execute(array($sourceId));
  109. $data = $result->fetchRow();
  110. $data['fileid'] = (int)$data['fileid'];
  111. $data['mtime'] = (int)$data['mtime'];
  112. $data['storage_mtime'] = (int)$data['storage_mtime'];
  113. $data['encrypted'] = (bool)$data['encrypted'];
  114. $data['mimetype'] = $this->getMimetype($data['mimetype']);
  115. $data['mimepart'] = $this->getMimetype($data['mimepart']);
  116. if ($data['storage_mtime'] === 0) {
  117. $data['storage_mtime'] = $data['mtime'];
  118. }
  119. if ($data['encrypted'] or ($data['unencrypted_size'] > 0 and $data['mimetype'] === 'httpd/unix-directory')) {
  120. $data['encrypted_size'] = (int)$data['size'];
  121. $data['size'] = (int)$data['unencrypted_size'];
  122. } else {
  123. $data['size'] = (int)$data['size'];
  124. }
  125. $data['permissions'] = (int)$data['permissions'];
  126. if (!is_int($file) || $file === 0) {
  127. $data['path'] = '';
  128. $data['name'] = basename($this->storage->getMountPoint());
  129. $data['is_share_mount_point'] = true;
  130. }
  131. $data['permissions'] &= $this->storage->getPermissions('');
  132. return $data;
  133. }
  134. return false;
  135. }
  136. /**
  137. * get the metadata of all files stored in $folder
  138. *
  139. * @param string $folderId
  140. * @return array
  141. */
  142. public function getFolderContentsById($folderId) {
  143. $cache = $this->getSourceCache('');
  144. if ($cache) {
  145. $owner = $this->storage->getSharedFrom();
  146. $parentPath = $this->getPathById($folderId);
  147. if ($parentPath !== '') {
  148. $parentPath .= '/';
  149. }
  150. $sourceFolderContent = $cache->getFolderContentsById($folderId);
  151. foreach ($sourceFolderContent as &$c) {
  152. $c['path'] = ltrim($parentPath . $c['name'], '/');
  153. $c['uid_owner'] = $owner;
  154. $c['displayname_owner'] = \OC_User::getDisplayName($owner);
  155. $c['permissions'] = $c['permissions'] & $this->storage->getPermissions(false);
  156. }
  157. return $sourceFolderContent;
  158. }
  159. return false;
  160. }
  161. /**
  162. * store meta data for a file or folder
  163. *
  164. * @param string $file
  165. * @param array $data
  166. *
  167. * @return int file id
  168. */
  169. public function put($file, array $data) {
  170. $file = ($file === false) ? '' : $file;
  171. if ($cache = $this->getSourceCache($file)) {
  172. return $cache->put($this->files[$file], $data);
  173. }
  174. return false;
  175. }
  176. /**
  177. * get the file id for a file
  178. *
  179. * @param string $file
  180. * @return int
  181. */
  182. public function getId($file) {
  183. if ($file === false) {
  184. return $this->storage->getSourceId();
  185. }
  186. $cache = $this->getSourceCache($file);
  187. if ($cache) {
  188. return $cache->getId($this->files[$file]);
  189. }
  190. return -1;
  191. }
  192. /**
  193. * check if a file is available in the cache
  194. *
  195. * @param string $file
  196. * @return bool
  197. */
  198. public function inCache($file) {
  199. if ($file == '') {
  200. return true;
  201. }
  202. return parent::inCache($file);
  203. }
  204. /**
  205. * remove a file or folder from the cache
  206. *
  207. * @param string $file
  208. */
  209. public function remove($file) {
  210. $file = ($file === false) ? '' : $file;
  211. if ($cache = $this->getSourceCache($file)) {
  212. $cache->remove($this->files[$file]);
  213. }
  214. }
  215. /**
  216. * Move a file or folder in the cache
  217. *
  218. * @param string $source
  219. * @param string $target
  220. */
  221. public function move($source, $target) {
  222. if ($cache = $this->getSourceCache($source)) {
  223. $file = \OC_Share_Backend_File::getSource($target, $this->storage->getMountPoint(), $this->storage->getItemType());
  224. if ($file && isset($file['path'])) {
  225. $cache->move($this->files[$source], $file['path']);
  226. }
  227. }
  228. }
  229. /**
  230. * remove all entries for files that are stored on the storage from the cache
  231. */
  232. public function clear() {
  233. // Not a valid action for Shared Cache
  234. }
  235. /**
  236. * @param string $file
  237. *
  238. * @return int, Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE
  239. */
  240. public function getStatus($file) {
  241. if ($file == '') {
  242. return self::COMPLETE;
  243. }
  244. if ($cache = $this->getSourceCache($file)) {
  245. return $cache->getStatus($this->files[$file]);
  246. }
  247. return self::NOT_FOUND;
  248. }
  249. /**
  250. * search for files matching $pattern
  251. *
  252. * @param string $pattern
  253. * @return array of file data
  254. */
  255. public function search($pattern) {
  256. $pattern = trim($pattern, '%');
  257. $normalizedPattern = $this->normalize($pattern);
  258. $result = array();
  259. $exploreDirs = array('');
  260. while (count($exploreDirs) > 0) {
  261. $dir = array_pop($exploreDirs);
  262. $files = $this->getFolderContents($dir);
  263. // no results?
  264. if (!$files) {
  265. // maybe it's a single shared file
  266. $file = $this->get('');
  267. if ($normalizedPattern === '' || stristr($file['name'], $normalizedPattern) !== false) {
  268. $result[] = $file;
  269. }
  270. continue;
  271. }
  272. foreach ($files as $file) {
  273. if ($normalizedPattern === '' || stristr($file['name'], $normalizedPattern) !== false) {
  274. $result[] = $file;
  275. }
  276. if ($file['mimetype'] === 'httpd/unix-directory') {
  277. $exploreDirs[] = ltrim($dir . '/' . $file['name'], '/');
  278. }
  279. }
  280. }
  281. return $result;
  282. }
  283. /**
  284. * search for files by mimetype
  285. *
  286. * @param string $mimetype
  287. * @return array
  288. */
  289. public function searchByMime($mimetype) {
  290. $mimepart = null;
  291. if (strpos($mimetype, '/') === false) {
  292. $mimepart = $mimetype;
  293. $mimetype = null;
  294. }
  295. $result = array();
  296. $exploreDirs = array('');
  297. while (count($exploreDirs) > 0) {
  298. $dir = array_pop($exploreDirs);
  299. $files = $this->getFolderContents($dir);
  300. // no results?
  301. if (!$files) {
  302. // maybe it's a single shared file
  303. $file = $this->get('');
  304. if (($mimepart && $file['mimepart'] === $mimepart) || ($mimetype && $file['mimetype'] === $mimetype)) {
  305. $result[] = $file;
  306. }
  307. continue;
  308. }
  309. foreach ($files as $file) {
  310. if ($file['mimetype'] === 'httpd/unix-directory') {
  311. $exploreDirs[] = ltrim($dir . '/' . $file['name'], '/');
  312. } else if (($mimepart && $file['mimepart'] === $mimepart) || ($mimetype && $file['mimetype'] === $mimetype)) {
  313. $result[] = $file;
  314. }
  315. }
  316. }
  317. return $result;
  318. }
  319. /**
  320. * get the size of a folder and set it in the cache
  321. *
  322. * @param string $path
  323. * @param array $entry (optional) meta data of the folder
  324. * @return int
  325. */
  326. public function calculateFolderSize($path, $entry = null) {
  327. $path = ($path === false) ? '' : $path;
  328. if ($cache = $this->getSourceCache($path)) {
  329. return $cache->calculateFolderSize($this->files[$path]);
  330. }
  331. return 0;
  332. }
  333. /**
  334. * get all file ids on the files on the storage
  335. *
  336. * @return int[]
  337. */
  338. public function getAll() {
  339. $ids = \OCP\Share::getItemsSharedWith('file', \OC_Share_Backend_File::FORMAT_GET_ALL);
  340. $folderBackend = \OCP\Share::getBackend('folder');
  341. if ($folderBackend instanceof Share_Backend_Collection) {
  342. foreach ($ids as $file) {
  343. /** @var $folderBackend Share_Backend_Collection */
  344. $children = $folderBackend->getChildren($file);
  345. foreach ($children as $child) {
  346. $ids[] = (int)$child['source'];
  347. }
  348. }
  349. }
  350. return $ids;
  351. }
  352. /**
  353. * find a folder in the cache which has not been fully scanned
  354. *
  355. * If multiply incomplete folders are in the cache, the one with the highest id will be returned,
  356. * use the one with the highest id gives the best result with the background scanner, since that is most
  357. * likely the folder where we stopped scanning previously
  358. *
  359. * @return boolean the path of the folder or false when no folder matched
  360. */
  361. public function getIncomplete() {
  362. return false;
  363. }
  364. /**
  365. * get the path of a file on this storage relative to the mount point by it's id
  366. *
  367. * @param int $id
  368. * @param string $pathEnd (optional) used internally for recursive calls
  369. * @return string|null
  370. */
  371. public function getPathById($id, $pathEnd = '') {
  372. // direct shares are easy
  373. if ($id === $this->storage->getSourceId()) {
  374. return ltrim($pathEnd, '/');
  375. } else {
  376. // 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
  377. list($parent, $name) = $this->getParentInfo($id);
  378. if ($parent > 0) {
  379. return $this->getPathById($parent, '/' . $name . $pathEnd);
  380. } else {
  381. return null;
  382. }
  383. }
  384. }
  385. /**
  386. * @param integer $id
  387. * @return array
  388. */
  389. private function getParentInfo($id) {
  390. $sql = 'SELECT `parent`, `name` FROM `*PREFIX*filecache` WHERE `fileid` = ?';
  391. $query = \OC_DB::prepare($sql);
  392. $result = $query->execute(array($id));
  393. if ($row = $result->fetchRow()) {
  394. return array((int)$row['parent'], $row['name']);
  395. } else {
  396. return array(-1, '');
  397. }
  398. }
  399. }