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.

462 lines
15 KiB

9 years ago
10 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Robin McCorkell <robin@mccorkell.me.uk>
  9. * @author Vincent Petry <pvince81@owncloud.com>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\Files\Node;
  27. use OC\DB\QueryBuilder\Literal;
  28. use OCA\Files_Sharing\SharedStorage;
  29. use OCP\DB\QueryBuilder\IQueryBuilder;
  30. use OCP\Files\Config\ICachedMountInfo;
  31. use OCP\Files\FileInfo;
  32. use OCP\Files\Mount\IMountPoint;
  33. use OCP\Files\NotFoundException;
  34. use OCP\Files\NotPermittedException;
  35. use OCP\Files\Search\ISearchOperator;
  36. class Folder extends Node implements \OCP\Files\Folder {
  37. /**
  38. * Creates a Folder that represents a non-existing path
  39. *
  40. * @param string $path path
  41. * @return string non-existing node class
  42. */
  43. protected function createNonExistingNode($path) {
  44. return new NonExistingFolder($this->root, $this->view, $path);
  45. }
  46. /**
  47. * @param string $path path relative to the folder
  48. * @return string
  49. * @throws \OCP\Files\NotPermittedException
  50. */
  51. public function getFullPath($path) {
  52. if (!$this->isValidPath($path)) {
  53. throw new NotPermittedException('Invalid path');
  54. }
  55. return $this->path . $this->normalizePath($path);
  56. }
  57. /**
  58. * @param string $path
  59. * @return string
  60. */
  61. public function getRelativePath($path) {
  62. if ($this->path === '' or $this->path === '/') {
  63. return $this->normalizePath($path);
  64. }
  65. if ($path === $this->path) {
  66. return '/';
  67. } else if (strpos($path, $this->path . '/') !== 0) {
  68. return null;
  69. } else {
  70. $path = substr($path, strlen($this->path));
  71. return $this->normalizePath($path);
  72. }
  73. }
  74. /**
  75. * check if a node is a (grand-)child of the folder
  76. *
  77. * @param \OC\Files\Node\Node $node
  78. * @return bool
  79. */
  80. public function isSubNode($node) {
  81. return strpos($node->getPath(), $this->path . '/') === 0;
  82. }
  83. /**
  84. * get the content of this directory
  85. *
  86. * @throws \OCP\Files\NotFoundException
  87. * @return Node[]
  88. */
  89. public function getDirectoryListing() {
  90. $folderContent = $this->view->getDirectoryContent($this->path);
  91. return array_map(function (FileInfo $info) {
  92. if ($info->getMimetype() === 'httpd/unix-directory') {
  93. return new Folder($this->root, $this->view, $info->getPath(), $info);
  94. } else {
  95. return new File($this->root, $this->view, $info->getPath(), $info);
  96. }
  97. }, $folderContent);
  98. }
  99. /**
  100. * @param string $path
  101. * @param FileInfo $info
  102. * @return File|Folder
  103. */
  104. protected function createNode($path, FileInfo $info = null) {
  105. if (is_null($info)) {
  106. $isDir = $this->view->is_dir($path);
  107. } else {
  108. $isDir = $info->getType() === FileInfo::TYPE_FOLDER;
  109. }
  110. if ($isDir) {
  111. return new Folder($this->root, $this->view, $path, $info);
  112. } else {
  113. return new File($this->root, $this->view, $path, $info);
  114. }
  115. }
  116. /**
  117. * Get the node at $path
  118. *
  119. * @param string $path
  120. * @return \OC\Files\Node\Node
  121. * @throws \OCP\Files\NotFoundException
  122. */
  123. public function get($path) {
  124. return $this->root->get($this->getFullPath($path));
  125. }
  126. /**
  127. * @param string $path
  128. * @return bool
  129. */
  130. public function nodeExists($path) {
  131. try {
  132. $this->get($path);
  133. return true;
  134. } catch (NotFoundException $e) {
  135. return false;
  136. }
  137. }
  138. /**
  139. * @param string $path
  140. * @return \OC\Files\Node\Folder
  141. * @throws \OCP\Files\NotPermittedException
  142. */
  143. public function newFolder($path) {
  144. if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) {
  145. $fullPath = $this->getFullPath($path);
  146. $nonExisting = new NonExistingFolder($this->root, $this->view, $fullPath);
  147. $this->root->emit('\OC\Files', 'preWrite', array($nonExisting));
  148. $this->root->emit('\OC\Files', 'preCreate', array($nonExisting));
  149. $this->view->mkdir($fullPath);
  150. $node = new Folder($this->root, $this->view, $fullPath);
  151. $this->root->emit('\OC\Files', 'postWrite', array($node));
  152. $this->root->emit('\OC\Files', 'postCreate', array($node));
  153. return $node;
  154. } else {
  155. throw new NotPermittedException('No create permission for folder');
  156. }
  157. }
  158. /**
  159. * @param string $path
  160. * @return \OC\Files\Node\File
  161. * @throws \OCP\Files\NotPermittedException
  162. */
  163. public function newFile($path) {
  164. if ($this->checkPermissions(\OCP\Constants::PERMISSION_CREATE)) {
  165. $fullPath = $this->getFullPath($path);
  166. $nonExisting = new NonExistingFile($this->root, $this->view, $fullPath);
  167. $this->root->emit('\OC\Files', 'preWrite', array($nonExisting));
  168. $this->root->emit('\OC\Files', 'preCreate', array($nonExisting));
  169. $this->view->touch($fullPath);
  170. $node = new File($this->root, $this->view, $fullPath);
  171. $this->root->emit('\OC\Files', 'postWrite', array($node));
  172. $this->root->emit('\OC\Files', 'postCreate', array($node));
  173. return $node;
  174. } else {
  175. throw new NotPermittedException('No create permission for path');
  176. }
  177. }
  178. /**
  179. * search for files with the name matching $query
  180. *
  181. * @param string|ISearchOperator $query
  182. * @return \OC\Files\Node\Node[]
  183. */
  184. public function search($query) {
  185. if (is_string($query)) {
  186. return $this->searchCommon('search', array('%' . $query . '%'));
  187. } else {
  188. return $this->searchCommon('searchQuery', array($query));
  189. }
  190. }
  191. /**
  192. * search for files by mimetype
  193. *
  194. * @param string $mimetype
  195. * @return Node[]
  196. */
  197. public function searchByMime($mimetype) {
  198. return $this->searchCommon('searchByMime', array($mimetype));
  199. }
  200. /**
  201. * search for files by tag
  202. *
  203. * @param string|int $tag name or tag id
  204. * @param string $userId owner of the tags
  205. * @return Node[]
  206. */
  207. public function searchByTag($tag, $userId) {
  208. return $this->searchCommon('searchByTag', array($tag, $userId));
  209. }
  210. /**
  211. * @param string $method cache method
  212. * @param array $args call args
  213. * @return \OC\Files\Node\Node[]
  214. */
  215. private function searchCommon($method, $args) {
  216. $files = array();
  217. $rootLength = strlen($this->path);
  218. $mount = $this->root->getMount($this->path);
  219. $storage = $mount->getStorage();
  220. $internalPath = $mount->getInternalPath($this->path);
  221. $internalPath = rtrim($internalPath, '/');
  222. if ($internalPath !== '') {
  223. $internalPath = $internalPath . '/';
  224. }
  225. $internalRootLength = strlen($internalPath);
  226. $cache = $storage->getCache('');
  227. $results = call_user_func_array(array($cache, $method), $args);
  228. foreach ($results as $result) {
  229. if ($internalRootLength === 0 or substr($result['path'], 0, $internalRootLength) === $internalPath) {
  230. $result['internalPath'] = $result['path'];
  231. $result['path'] = substr($result['path'], $internalRootLength);
  232. $result['storage'] = $storage;
  233. $files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage, $result['internalPath'], $result, $mount);
  234. }
  235. }
  236. $mounts = $this->root->getMountsIn($this->path);
  237. foreach ($mounts as $mount) {
  238. $storage = $mount->getStorage();
  239. if ($storage) {
  240. $cache = $storage->getCache('');
  241. $relativeMountPoint = ltrim(substr($mount->getMountPoint(), $rootLength), '/');
  242. $results = call_user_func_array(array($cache, $method), $args);
  243. foreach ($results as $result) {
  244. $result['internalPath'] = $result['path'];
  245. $result['path'] = $relativeMountPoint . $result['path'];
  246. $result['storage'] = $storage;
  247. $files[] = new \OC\Files\FileInfo($this->path . '/' . $result['path'], $storage, $result['internalPath'], $result, $mount);
  248. }
  249. }
  250. }
  251. return array_map(function (FileInfo $file) {
  252. return $this->createNode($file->getPath(), $file);
  253. }, $files);
  254. }
  255. /**
  256. * @param int $id
  257. * @return \OC\Files\Node\Node[]
  258. */
  259. public function getById($id) {
  260. $mountCache = $this->root->getUserMountCache();
  261. if (strpos($this->getPath(), '/', 1) > 0) {
  262. list(, $user) = explode('/', $this->getPath());
  263. } else {
  264. $user = null;
  265. }
  266. $mountsContainingFile = $mountCache->getMountsForFileId((int)$id, $user);
  267. $mounts = $this->root->getMountsIn($this->path);
  268. $mounts[] = $this->root->getMount($this->path);
  269. /** @var IMountPoint[] $folderMounts */
  270. $folderMounts = array_combine(array_map(function (IMountPoint $mountPoint) {
  271. return $mountPoint->getMountPoint();
  272. }, $mounts), $mounts);
  273. /** @var ICachedMountInfo[] $mountsContainingFile */
  274. $mountsContainingFile = array_values(array_filter($mountsContainingFile, function (ICachedMountInfo $cachedMountInfo) use ($folderMounts) {
  275. return isset($folderMounts[$cachedMountInfo->getMountPoint()]);
  276. }));
  277. if (count($mountsContainingFile) === 0) {
  278. return [];
  279. }
  280. $nodes = array_map(function (ICachedMountInfo $cachedMountInfo) use ($folderMounts, $id) {
  281. $mount = $folderMounts[$cachedMountInfo->getMountPoint()];
  282. $cacheEntry = $mount->getStorage()->getCache()->get((int)$id);
  283. if (!$cacheEntry) {
  284. return null;
  285. }
  286. // cache jails will hide the "true" internal path
  287. $internalPath = ltrim($cachedMountInfo->getRootInternalPath() . '/' . $cacheEntry->getPath(), '/');
  288. $pathRelativeToMount = substr($internalPath, strlen($cachedMountInfo->getRootInternalPath()));
  289. $pathRelativeToMount = ltrim($pathRelativeToMount, '/');
  290. $absolutePath = rtrim($cachedMountInfo->getMountPoint() . $pathRelativeToMount, '/');
  291. return $this->root->createNode($absolutePath, new \OC\Files\FileInfo(
  292. $absolutePath, $mount->getStorage(), $cacheEntry->getPath(), $cacheEntry, $mount,
  293. \OC::$server->getUserManager()->get($mount->getStorage()->getOwner($pathRelativeToMount))
  294. ));
  295. }, $mountsContainingFile);
  296. $nodes = array_filter($nodes);
  297. return array_filter($nodes, function (Node $node) {
  298. return $this->getRelativePath($node->getPath());
  299. });
  300. }
  301. public function getFreeSpace() {
  302. return $this->view->free_space($this->path);
  303. }
  304. public function delete() {
  305. if ($this->checkPermissions(\OCP\Constants::PERMISSION_DELETE)) {
  306. $this->sendHooks(array('preDelete'));
  307. $fileInfo = $this->getFileInfo();
  308. $this->view->rmdir($this->path);
  309. $nonExisting = new NonExistingFolder($this->root, $this->view, $this->path, $fileInfo);
  310. $this->root->emit('\OC\Files', 'postDelete', array($nonExisting));
  311. $this->exists = false;
  312. } else {
  313. throw new NotPermittedException('No delete permission for path');
  314. }
  315. }
  316. /**
  317. * Add a suffix to the name in case the file exists
  318. *
  319. * @param string $name
  320. * @return string
  321. * @throws NotPermittedException
  322. */
  323. public function getNonExistingName($name) {
  324. $uniqueName = \OC_Helper::buildNotExistingFileNameForView($this->getPath(), $name, $this->view);
  325. return trim($this->getRelativePath($uniqueName), '/');
  326. }
  327. /**
  328. * @param int $limit
  329. * @param int $offset
  330. * @return \OCP\Files\Node[]
  331. */
  332. public function getRecent($limit, $offset = 0) {
  333. $mimetypeLoader = \OC::$server->getMimeTypeLoader();
  334. $mounts = $this->root->getMountsIn($this->path);
  335. $mounts[] = $this->getMountPoint();
  336. $mounts = array_filter($mounts, function (IMountPoint $mount) {
  337. return $mount->getStorage();
  338. });
  339. $storageIds = array_map(function (IMountPoint $mount) {
  340. return $mount->getStorage()->getCache()->getNumericStorageId();
  341. }, $mounts);
  342. /** @var IMountPoint[] $mountMap */
  343. $mountMap = array_combine($storageIds, $mounts);
  344. $folderMimetype = $mimetypeLoader->getId(FileInfo::MIMETYPE_FOLDER);
  345. // Search in batches of 500 entries
  346. $searchLimit = 500;
  347. $results = [];
  348. do {
  349. $searchResult = $this->recentSearch($searchLimit, $offset, $storageIds, $folderMimetype);
  350. // Exit condition if there are no more results
  351. if (count($searchResult) === 0) {
  352. break;
  353. }
  354. $parseResult = $this->recentParse($searchResult, $mountMap, $mimetypeLoader);
  355. foreach ($parseResult as $result) {
  356. $results[] = $result;
  357. }
  358. $offset += $searchLimit;
  359. } while (count($results) < $limit);
  360. return array_slice($results, 0, $limit);
  361. }
  362. private function recentSearch($limit, $offset, $storageIds, $folderMimetype) {
  363. $builder = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  364. $query = $builder
  365. ->select('f.*')
  366. ->from('filecache', 'f')
  367. ->andWhere($builder->expr()->in('f.storage', $builder->createNamedParameter($storageIds, IQueryBuilder::PARAM_INT_ARRAY)))
  368. ->andWhere($builder->expr()->orX(
  369. // handle non empty folders separate
  370. $builder->expr()->neq('f.mimetype', $builder->createNamedParameter($folderMimetype, IQueryBuilder::PARAM_INT)),
  371. $builder->expr()->eq('f.size', new Literal(0))
  372. ))
  373. ->andWhere($builder->expr()->notLike('f.path', $builder->createNamedParameter('files_versions/%')))
  374. ->andWhere($builder->expr()->notLike('f.path', $builder->createNamedParameter('files_trashbin/%')))
  375. ->orderBy('f.mtime', 'DESC')
  376. ->setMaxResults($limit)
  377. ->setFirstResult($offset);
  378. return $query->execute()->fetchAll();
  379. }
  380. private function recentParse($result, $mountMap, $mimetypeLoader) {
  381. $files = array_filter(array_map(function (array $entry) use ($mountMap, $mimetypeLoader) {
  382. $mount = $mountMap[$entry['storage']];
  383. $entry['internalPath'] = $entry['path'];
  384. $entry['mimetype'] = $mimetypeLoader->getMimetypeById($entry['mimetype']);
  385. $entry['mimepart'] = $mimetypeLoader->getMimetypeById($entry['mimepart']);
  386. $path = $this->getAbsolutePath($mount, $entry['path']);
  387. if (is_null($path)) {
  388. return null;
  389. }
  390. $fileInfo = new \OC\Files\FileInfo($path, $mount->getStorage(), $entry['internalPath'], $entry, $mount);
  391. return $this->root->createNode($fileInfo->getPath(), $fileInfo);
  392. }, $result));
  393. return array_values(array_filter($files, function (Node $node) {
  394. $relative = $this->getRelativePath($node->getPath());
  395. return $relative !== null && $relative !== '/';
  396. }));
  397. }
  398. private function getAbsolutePath(IMountPoint $mount, $path) {
  399. $storage = $mount->getStorage();
  400. if ($storage->instanceOfStorage('\OC\Files\Storage\Wrapper\Jail')) {
  401. if ($storage->instanceOfStorage(SharedStorage::class)) {
  402. $storage->getSourceStorage();
  403. }
  404. /** @var \OC\Files\Storage\Wrapper\Jail $storage */
  405. $jailRoot = $storage->getUnjailedPath('');
  406. $rootLength = strlen($jailRoot) + 1;
  407. if ($path === $jailRoot) {
  408. return $mount->getMountPoint();
  409. } else if (substr($path, 0, $rootLength) === $jailRoot . '/') {
  410. return $mount->getMountPoint() . substr($path, $rootLength);
  411. } else {
  412. return null;
  413. }
  414. } else {
  415. return $mount->getMountPoint() . $path;
  416. }
  417. }
  418. }