PageRenderTime 50ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/files/view.php

https://github.com/sezuan/core
PHP | 1048 lines | 782 code | 83 blank | 183 comment | 165 complexity | 834dd9eab85db00741e4f3285687831b MD5 | raw file
Possible License(s): AGPL-3.0, AGPL-1.0, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. /**
  9. * Class to provide access to ownCloud filesystem via a "view", and methods for
  10. * working with files within that view (e.g. read, write, delete, etc.). Each
  11. * view is restricted to a set of directories via a virtual root. The default view
  12. * uses the currently logged in user's data directory as root (parts of
  13. * OC_Filesystem are merely a wrapper for OC_FilesystemView).
  14. *
  15. * Apps that need to access files outside of the user data folders (to modify files
  16. * belonging to a user other than the one currently logged in, for example) should
  17. * use this class directly rather than using OC_Filesystem, or making use of PHP's
  18. * built-in file manipulation functions. This will ensure all hooks and proxies
  19. * are triggered correctly.
  20. *
  21. * Filesystem functions are not called directly; they are passed to the correct
  22. * \OC\Files\Storage\Storage object
  23. */
  24. namespace OC\Files;
  25. class View {
  26. private $fakeRoot = '';
  27. private $internal_path_cache = array();
  28. private $storage_cache = array();
  29. public function __construct($root) {
  30. $this->fakeRoot = $root;
  31. }
  32. public function getAbsolutePath($path = '/') {
  33. if (!$path) {
  34. $path = '/';
  35. }
  36. if ($path[0] !== '/') {
  37. $path = '/' . $path;
  38. }
  39. return $this->fakeRoot . $path;
  40. }
  41. /**
  42. * change the root to a fake root
  43. *
  44. * @param string $fakeRoot
  45. * @return bool
  46. */
  47. public function chroot($fakeRoot) {
  48. if (!$fakeRoot == '') {
  49. if ($fakeRoot[0] !== '/') {
  50. $fakeRoot = '/' . $fakeRoot;
  51. }
  52. }
  53. $this->fakeRoot = $fakeRoot;
  54. }
  55. /**
  56. * get the fake root
  57. *
  58. * @return string
  59. */
  60. public function getRoot() {
  61. return $this->fakeRoot;
  62. }
  63. /**
  64. * get path relative to the root of the view
  65. *
  66. * @param string $path
  67. * @return string
  68. */
  69. public function getRelativePath($path) {
  70. if ($this->fakeRoot == '') {
  71. return $path;
  72. }
  73. if (strpos($path, $this->fakeRoot) !== 0) {
  74. return null;
  75. } else {
  76. $path = substr($path, strlen($this->fakeRoot));
  77. if (strlen($path) === 0) {
  78. return '/';
  79. } else {
  80. return $path;
  81. }
  82. }
  83. }
  84. /**
  85. * get the mountpoint of the storage object for a path
  86. * ( note: because a storage is not always mounted inside the fakeroot, the
  87. * returned mountpoint is relative to the absolute root of the filesystem
  88. * and doesn't take the chroot into account )
  89. *
  90. * @param string $path
  91. * @return string
  92. */
  93. public function getMountPoint($path) {
  94. return Filesystem::getMountPoint($this->getAbsolutePath($path));
  95. }
  96. /**
  97. * resolve a path to a storage and internal path
  98. *
  99. * @param string $path
  100. * @return array consisting of the storage and the internal path
  101. */
  102. public function resolvePath($path) {
  103. return Filesystem::resolvePath($this->getAbsolutePath($path));
  104. }
  105. /**
  106. * return the path to a local version of the file
  107. * we need this because we can't know if a file is stored local or not from
  108. * outside the filestorage and for some purposes a local file is needed
  109. *
  110. * @param string $path
  111. * @return string
  112. */
  113. public function getLocalFile($path) {
  114. $parent = substr($path, 0, strrpos($path, '/'));
  115. $path = $this->getAbsolutePath($path);
  116. list($storage, $internalPath) = Filesystem::resolvePath($path);
  117. if (Filesystem::isValidPath($parent) and $storage) {
  118. return $storage->getLocalFile($internalPath);
  119. } else {
  120. return null;
  121. }
  122. }
  123. /**
  124. * @param string $path
  125. * @return string
  126. */
  127. public function getLocalFolder($path) {
  128. $parent = substr($path, 0, strrpos($path, '/'));
  129. $path = $this->getAbsolutePath($path);
  130. list($storage, $internalPath) = Filesystem::resolvePath($path);
  131. if (Filesystem::isValidPath($parent) and $storage) {
  132. return $storage->getLocalFolder($internalPath);
  133. } else {
  134. return null;
  135. }
  136. }
  137. /**
  138. * the following functions operate with arguments and return values identical
  139. * to those of their PHP built-in equivalents. Mostly they are merely wrappers
  140. * for \OC\Files\Storage\Storage via basicOperation().
  141. */
  142. public function mkdir($path) {
  143. return $this->basicOperation('mkdir', $path, array('create', 'write'));
  144. }
  145. public function rmdir($path) {
  146. return $this->basicOperation('rmdir', $path, array('delete'));
  147. }
  148. public function opendir($path) {
  149. return $this->basicOperation('opendir', $path, array('read'));
  150. }
  151. public function readdir($handle) {
  152. $fsLocal = new Storage\Local(array('datadir' => '/'));
  153. return $fsLocal->readdir($handle);
  154. }
  155. public function is_dir($path) {
  156. if ($path == '/') {
  157. return true;
  158. }
  159. return $this->basicOperation('is_dir', $path);
  160. }
  161. public function is_file($path) {
  162. if ($path == '/') {
  163. return false;
  164. }
  165. return $this->basicOperation('is_file', $path);
  166. }
  167. public function stat($path) {
  168. return $this->basicOperation('stat', $path);
  169. }
  170. public function filetype($path) {
  171. return $this->basicOperation('filetype', $path);
  172. }
  173. public function filesize($path) {
  174. return $this->basicOperation('filesize', $path);
  175. }
  176. public function readfile($path) {
  177. @ob_end_clean();
  178. $handle = $this->fopen($path, 'rb');
  179. if ($handle) {
  180. $chunkSize = 8192; // 8 kB chunks
  181. while (!feof($handle)) {
  182. echo fread($handle, $chunkSize);
  183. flush();
  184. }
  185. $size = $this->filesize($path);
  186. return $size;
  187. }
  188. return false;
  189. }
  190. public function isCreatable($path) {
  191. return $this->basicOperation('isCreatable', $path);
  192. }
  193. public function isReadable($path) {
  194. return $this->basicOperation('isReadable', $path);
  195. }
  196. public function isUpdatable($path) {
  197. return $this->basicOperation('isUpdatable', $path);
  198. }
  199. public function isDeletable($path) {
  200. return $this->basicOperation('isDeletable', $path);
  201. }
  202. public function isSharable($path) {
  203. return $this->basicOperation('isSharable', $path);
  204. }
  205. public function file_exists($path) {
  206. if ($path == '/') {
  207. return true;
  208. }
  209. return $this->basicOperation('file_exists', $path);
  210. }
  211. public function filemtime($path) {
  212. return $this->basicOperation('filemtime', $path);
  213. }
  214. public function touch($path, $mtime = null) {
  215. if (!is_null($mtime) and !is_numeric($mtime)) {
  216. $mtime = strtotime($mtime);
  217. }
  218. $hooks = array('touch');
  219. if (!$this->file_exists($path)) {
  220. $hooks[] = 'write';
  221. }
  222. $result = $this->basicOperation('touch', $path, $hooks, $mtime);
  223. if (!$result) { //if native touch fails, we emulate it by changing the mtime in the cache
  224. $this->putFileInfo($path, array('mtime' => $mtime));
  225. }
  226. return true;
  227. }
  228. public function file_get_contents($path) {
  229. return $this->basicOperation('file_get_contents', $path, array('read'));
  230. }
  231. public function file_put_contents($path, $data) {
  232. if (is_resource($data)) { //not having to deal with streams in file_put_contents makes life easier
  233. $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path));
  234. if (\OC_FileProxy::runPreProxies('file_put_contents', $absolutePath, $data)
  235. and Filesystem::isValidPath($path)
  236. and !Filesystem::isFileBlacklisted($path)
  237. ) {
  238. $path = $this->getRelativePath($absolutePath);
  239. $exists = $this->file_exists($path);
  240. $run = true;
  241. if ($this->fakeRoot == Filesystem::getRoot() && !Cache\Scanner::isPartialFile($path)) {
  242. if (!$exists) {
  243. \OC_Hook::emit(
  244. Filesystem::CLASSNAME,
  245. Filesystem::signal_create,
  246. array(
  247. Filesystem::signal_param_path => $path,
  248. Filesystem::signal_param_run => &$run
  249. )
  250. );
  251. }
  252. \OC_Hook::emit(
  253. Filesystem::CLASSNAME,
  254. Filesystem::signal_write,
  255. array(
  256. Filesystem::signal_param_path => $path,
  257. Filesystem::signal_param_run => &$run
  258. )
  259. );
  260. }
  261. if (!$run) {
  262. return false;
  263. }
  264. $target = $this->fopen($path, 'w');
  265. if ($target) {
  266. list ($count, $result) = \OC_Helper::streamCopy($data, $target);
  267. fclose($target);
  268. fclose($data);
  269. if ($this->fakeRoot == Filesystem::getRoot() && !Cache\Scanner::isPartialFile($path) && $result !== false) {
  270. if (!$exists) {
  271. \OC_Hook::emit(
  272. Filesystem::CLASSNAME,
  273. Filesystem::signal_post_create,
  274. array(Filesystem::signal_param_path => $path)
  275. );
  276. }
  277. \OC_Hook::emit(
  278. Filesystem::CLASSNAME,
  279. Filesystem::signal_post_write,
  280. array(Filesystem::signal_param_path => $path)
  281. );
  282. }
  283. \OC_FileProxy::runPostProxies('file_put_contents', $absolutePath, $count);
  284. return $result;
  285. } else {
  286. return false;
  287. }
  288. } else {
  289. return false;
  290. }
  291. } else {
  292. return $this->basicOperation('file_put_contents', $path, array('create', 'write'), $data);
  293. }
  294. }
  295. public function unlink($path) {
  296. return $this->basicOperation('unlink', $path, array('delete'));
  297. }
  298. public function deleteAll($directory, $empty = false) {
  299. return $this->basicOperation('deleteAll', $directory, array('delete'), $empty);
  300. }
  301. public function rename($path1, $path2) {
  302. $postFix1 = (substr($path1, -1, 1) === '/') ? '/' : '';
  303. $postFix2 = (substr($path2, -1, 1) === '/') ? '/' : '';
  304. $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1));
  305. $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2));
  306. if (
  307. \OC_FileProxy::runPreProxies('rename', $absolutePath1, $absolutePath2)
  308. and Filesystem::isValidPath($path2)
  309. and Filesystem::isValidPath($path1)
  310. and !Filesystem::isFileBlacklisted($path2)
  311. ) {
  312. $path1 = $this->getRelativePath($absolutePath1);
  313. $path2 = $this->getRelativePath($absolutePath2);
  314. if ($path1 == null or $path2 == null) {
  315. return false;
  316. }
  317. $run = true;
  318. if ($this->fakeRoot == Filesystem::getRoot() && (Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2))) {
  319. // if it was a rename from a part file to a regular file it was a write and not a rename operation
  320. \OC_Hook::emit(
  321. Filesystem::CLASSNAME, Filesystem::signal_write,
  322. array(
  323. Filesystem::signal_param_path => $path2,
  324. Filesystem::signal_param_run => &$run
  325. )
  326. );
  327. } elseif ($this->fakeRoot == Filesystem::getRoot()) {
  328. \OC_Hook::emit(
  329. Filesystem::CLASSNAME, Filesystem::signal_rename,
  330. array(
  331. Filesystem::signal_param_oldpath => $path1,
  332. Filesystem::signal_param_newpath => $path2,
  333. Filesystem::signal_param_run => &$run
  334. )
  335. );
  336. }
  337. if ($run) {
  338. $mp1 = $this->getMountPoint($path1 . $postFix1);
  339. $mp2 = $this->getMountPoint($path2 . $postFix2);
  340. if ($mp1 == $mp2) {
  341. list($storage, $internalPath1) = Filesystem::resolvePath($absolutePath1 . $postFix1);
  342. list(, $internalPath2) = Filesystem::resolvePath($absolutePath2 . $postFix2);
  343. if ($storage) {
  344. $result = $storage->rename($internalPath1, $internalPath2);
  345. \OC_FileProxy::runPostProxies('rename', $absolutePath1, $absolutePath2);
  346. } else {
  347. $result = false;
  348. }
  349. } else {
  350. if ($this->is_dir($path1)) {
  351. $result = $this->copy($path1, $path2);
  352. if ($result === true) {
  353. list($storage1, $internalPath1) = Filesystem::resolvePath($absolutePath1 . $postFix1);
  354. $result = $storage1->deleteAll($internalPath1);
  355. }
  356. } else {
  357. $source = $this->fopen($path1 . $postFix1, 'r');
  358. $target = $this->fopen($path2 . $postFix2, 'w');
  359. list($count, $result) = \OC_Helper::streamCopy($source, $target);
  360. // close open handle - especially $source is necessary because unlink below will
  361. // throw an exception on windows because the file is locked
  362. fclose($source);
  363. fclose($target);
  364. if ($result !== false) {
  365. list($storage1, $internalPath1) = Filesystem::resolvePath($absolutePath1 . $postFix1);
  366. $storage1->unlink($internalPath1);
  367. }
  368. }
  369. }
  370. if ($this->fakeRoot == Filesystem::getRoot() && (Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) {
  371. // if it was a rename from a part file to a regular file it was a write and not a rename operation
  372. \OC_Hook::emit(
  373. Filesystem::CLASSNAME,
  374. Filesystem::signal_post_write,
  375. array(
  376. Filesystem::signal_param_path => $path2,
  377. )
  378. );
  379. } elseif ($this->fakeRoot == Filesystem::getRoot() && $result !== false) {
  380. \OC_Hook::emit(
  381. Filesystem::CLASSNAME,
  382. Filesystem::signal_post_rename,
  383. array(
  384. Filesystem::signal_param_oldpath => $path1,
  385. Filesystem::signal_param_newpath => $path2
  386. )
  387. );
  388. }
  389. return $result;
  390. } else {
  391. return false;
  392. }
  393. } else {
  394. return false;
  395. }
  396. }
  397. public function copy($path1, $path2) {
  398. $postFix1 = (substr($path1, -1, 1) === '/') ? '/' : '';
  399. $postFix2 = (substr($path2, -1, 1) === '/') ? '/' : '';
  400. $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1));
  401. $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2));
  402. if (
  403. \OC_FileProxy::runPreProxies('copy', $absolutePath1, $absolutePath2)
  404. and Filesystem::isValidPath($path2)
  405. and Filesystem::isValidPath($path1)
  406. and !Filesystem::isFileBlacklisted($path2)
  407. ) {
  408. $path1 = $this->getRelativePath($absolutePath1);
  409. $path2 = $this->getRelativePath($absolutePath2);
  410. if ($path1 == null or $path2 == null) {
  411. return false;
  412. }
  413. $run = true;
  414. $exists = $this->file_exists($path2);
  415. if ($this->fakeRoot == Filesystem::getRoot()) {
  416. \OC_Hook::emit(
  417. Filesystem::CLASSNAME,
  418. Filesystem::signal_copy,
  419. array(
  420. Filesystem::signal_param_oldpath => $path1,
  421. Filesystem::signal_param_newpath => $path2,
  422. Filesystem::signal_param_run => &$run
  423. )
  424. );
  425. if ($run and !$exists) {
  426. \OC_Hook::emit(
  427. Filesystem::CLASSNAME,
  428. Filesystem::signal_create,
  429. array(
  430. Filesystem::signal_param_path => $path2,
  431. Filesystem::signal_param_run => &$run
  432. )
  433. );
  434. }
  435. if ($run) {
  436. \OC_Hook::emit(
  437. Filesystem::CLASSNAME,
  438. Filesystem::signal_write,
  439. array(
  440. Filesystem::signal_param_path => $path2,
  441. Filesystem::signal_param_run => &$run
  442. )
  443. );
  444. }
  445. }
  446. if ($run) {
  447. $mp1 = $this->getMountPoint($path1 . $postFix1);
  448. $mp2 = $this->getMountPoint($path2 . $postFix2);
  449. if ($mp1 == $mp2) {
  450. list($storage, $internalPath1) = Filesystem::resolvePath($absolutePath1 . $postFix1);
  451. list(, $internalPath2) = Filesystem::resolvePath($absolutePath2 . $postFix2);
  452. if ($storage) {
  453. $result = $storage->copy($internalPath1, $internalPath2);
  454. } else {
  455. $result = false;
  456. }
  457. } else {
  458. if ($this->is_dir($path1) && ($dh = $this->opendir($path1))) {
  459. $result = $this->mkdir($path2);
  460. while ($file = readdir($dh)) {
  461. if (!Filesystem::isIgnoredDir($file)) {
  462. $result = $this->copy($path1 . '/' . $file, $path2 . '/' . $file);
  463. }
  464. }
  465. } else {
  466. $source = $this->fopen($path1 . $postFix1, 'r');
  467. $target = $this->fopen($path2 . $postFix2, 'w');
  468. list($count, $result) = \OC_Helper::streamCopy($source, $target);
  469. }
  470. }
  471. if ($this->fakeRoot == Filesystem::getRoot() && $result !== false) {
  472. \OC_Hook::emit(
  473. Filesystem::CLASSNAME,
  474. Filesystem::signal_post_copy,
  475. array(
  476. Filesystem::signal_param_oldpath => $path1,
  477. Filesystem::signal_param_newpath => $path2
  478. )
  479. );
  480. if (!$exists) {
  481. \OC_Hook::emit(
  482. Filesystem::CLASSNAME,
  483. Filesystem::signal_post_create,
  484. array(Filesystem::signal_param_path => $path2)
  485. );
  486. }
  487. \OC_Hook::emit(
  488. Filesystem::CLASSNAME,
  489. Filesystem::signal_post_write,
  490. array(Filesystem::signal_param_path => $path2)
  491. );
  492. }
  493. return $result;
  494. } else {
  495. return false;
  496. }
  497. } else {
  498. return false;
  499. }
  500. }
  501. public function fopen($path, $mode) {
  502. $hooks = array();
  503. switch ($mode) {
  504. case 'r':
  505. case 'rb':
  506. $hooks[] = 'read';
  507. break;
  508. case 'r+':
  509. case 'rb+':
  510. case 'w+':
  511. case 'wb+':
  512. case 'x+':
  513. case 'xb+':
  514. case 'a+':
  515. case 'ab+':
  516. $hooks[] = 'read';
  517. $hooks[] = 'write';
  518. break;
  519. case 'w':
  520. case 'wb':
  521. case 'x':
  522. case 'xb':
  523. case 'a':
  524. case 'ab':
  525. $hooks[] = 'write';
  526. break;
  527. default:
  528. \OC_Log::write('core', 'invalid mode (' . $mode . ') for ' . $path, \OC_Log::ERROR);
  529. }
  530. return $this->basicOperation('fopen', $path, $hooks, $mode);
  531. }
  532. public function toTmpFile($path) {
  533. if (Filesystem::isValidPath($path)) {
  534. $source = $this->fopen($path, 'r');
  535. if ($source) {
  536. $extension = pathinfo($path, PATHINFO_EXTENSION);
  537. $tmpFile = \OC_Helper::tmpFile($extension);
  538. file_put_contents($tmpFile, $source);
  539. return $tmpFile;
  540. } else {
  541. return false;
  542. }
  543. } else {
  544. return false;
  545. }
  546. }
  547. public function fromTmpFile($tmpFile, $path) {
  548. if (Filesystem::isValidPath($path)) {
  549. if (!$tmpFile) {
  550. debug_print_backtrace();
  551. }
  552. $source = fopen($tmpFile, 'r');
  553. if ($source) {
  554. $this->file_put_contents($path, $source);
  555. unlink($tmpFile);
  556. return true;
  557. } else {
  558. return false;
  559. }
  560. } else {
  561. return false;
  562. }
  563. }
  564. public function getMimeType($path) {
  565. return $this->basicOperation('getMimeType', $path);
  566. }
  567. public function hash($type, $path, $raw = false) {
  568. $postFix = (substr($path, -1, 1) === '/') ? '/' : '';
  569. $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path));
  570. if (\OC_FileProxy::runPreProxies('hash', $absolutePath) && Filesystem::isValidPath($path)) {
  571. $path = $this->getRelativePath($absolutePath);
  572. if ($path == null) {
  573. return false;
  574. }
  575. if (Filesystem::$loaded && $this->fakeRoot == Filesystem::getRoot()) {
  576. \OC_Hook::emit(
  577. Filesystem::CLASSNAME,
  578. Filesystem::signal_read,
  579. array(Filesystem::signal_param_path => $path)
  580. );
  581. }
  582. list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix);
  583. if ($storage) {
  584. $result = $storage->hash($type, $internalPath, $raw);
  585. $result = \OC_FileProxy::runPostProxies('hash', $absolutePath, $result);
  586. return $result;
  587. }
  588. }
  589. return null;
  590. }
  591. public function free_space($path = '/') {
  592. return $this->basicOperation('free_space', $path);
  593. }
  594. /**
  595. * @brief abstraction layer for basic filesystem functions: wrapper for \OC\Files\Storage\Storage
  596. * @param string $operation
  597. * @param string $path
  598. * @param array $hooks (optional)
  599. * @param mixed $extraParam (optional)
  600. * @return mixed
  601. *
  602. * This method takes requests for basic filesystem functions (e.g. reading & writing
  603. * files), processes hooks and proxies, sanitises paths, and finally passes them on to
  604. * \OC\Files\Storage\Storage for delegation to a storage backend for execution
  605. */
  606. private function basicOperation($operation, $path, $hooks = array(), $extraParam = null) {
  607. $postFix = (substr($path, -1, 1) === '/') ? '/' : '';
  608. $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path));
  609. if (\OC_FileProxy::runPreProxies($operation, $absolutePath, $extraParam)
  610. and Filesystem::isValidPath($path)
  611. and !Filesystem::isFileBlacklisted($path)
  612. ) {
  613. $path = $this->getRelativePath($absolutePath);
  614. if ($path == null) {
  615. return false;
  616. }
  617. $run = $this->runHooks($hooks, $path);
  618. list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix);
  619. if ($run and $storage) {
  620. if (!is_null($extraParam)) {
  621. $result = $storage->$operation($internalPath, $extraParam);
  622. } else {
  623. $result = $storage->$operation($internalPath);
  624. }
  625. $result = \OC_FileProxy::runPostProxies($operation, $this->getAbsolutePath($path), $result);
  626. if (Filesystem::$loaded and $this->fakeRoot == Filesystem::getRoot() && $result !== false) {
  627. if ($operation != 'fopen') { //no post hooks for fopen, the file stream is still open
  628. $this->runHooks($hooks, $path, true);
  629. }
  630. }
  631. return $result;
  632. }
  633. }
  634. return null;
  635. }
  636. private function runHooks($hooks, $path, $post = false) {
  637. $prefix = ($post) ? 'post_' : '';
  638. $run = true;
  639. if (Filesystem::$loaded and $this->fakeRoot == Filesystem::getRoot() && !Cache\Scanner::isPartialFile($path)) {
  640. foreach ($hooks as $hook) {
  641. if ($hook != 'read') {
  642. \OC_Hook::emit(
  643. Filesystem::CLASSNAME,
  644. $prefix . $hook,
  645. array(
  646. Filesystem::signal_param_run => &$run,
  647. Filesystem::signal_param_path => $path
  648. )
  649. );
  650. } elseif (!$post) {
  651. \OC_Hook::emit(
  652. Filesystem::CLASSNAME,
  653. $prefix . $hook,
  654. array(
  655. Filesystem::signal_param_path => $path
  656. )
  657. );
  658. }
  659. }
  660. }
  661. return $run;
  662. }
  663. /**
  664. * check if a file or folder has been updated since $time
  665. *
  666. * @param string $path
  667. * @param int $time
  668. * @return bool
  669. */
  670. public function hasUpdated($path, $time) {
  671. return $this->basicOperation('hasUpdated', $path, array(), $time);
  672. }
  673. /**
  674. * get the filesystem info
  675. *
  676. * @param string $path
  677. * @return array
  678. *
  679. * returns an associative array with the following keys:
  680. * - size
  681. * - mtime
  682. * - mimetype
  683. * - encrypted
  684. * - versioned
  685. */
  686. public function getFileInfo($path) {
  687. $data = array();
  688. if (!Filesystem::isValidPath($path)) {
  689. return $data;
  690. }
  691. $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path);
  692. /**
  693. * @var \OC\Files\Storage\Storage $storage
  694. * @var string $internalPath
  695. */
  696. list($storage, $internalPath) = Filesystem::resolvePath($path);
  697. if ($storage) {
  698. $cache = $storage->getCache($internalPath);
  699. $permissionsCache = $storage->getPermissionsCache($internalPath);
  700. $user = \OC_User::getUser();
  701. if (!$cache->inCache($internalPath)) {
  702. $scanner = $storage->getScanner($internalPath);
  703. $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW);
  704. } else {
  705. $watcher = $storage->getWatcher($internalPath);
  706. $watcher->checkUpdate($internalPath);
  707. }
  708. $data = $cache->get($internalPath);
  709. if ($data and $data['fileid']) {
  710. if ($data['mimetype'] === 'httpd/unix-directory') {
  711. //add the sizes of other mountpoints to the folder
  712. $mountPoints = Filesystem::getMountPoints($path);
  713. foreach ($mountPoints as $mountPoint) {
  714. $subStorage = Filesystem::getStorage($mountPoint);
  715. if ($subStorage) {
  716. $subCache = $subStorage->getCache('');
  717. $rootEntry = $subCache->get('');
  718. $data['size'] += isset($rootEntry['size']) ? $rootEntry['size'] : 0;
  719. }
  720. }
  721. }
  722. $permissions = $permissionsCache->get($data['fileid'], $user);
  723. if ($permissions === -1) {
  724. $permissions = $storage->getPermissions($internalPath);
  725. $permissionsCache->set($data['fileid'], $user, $permissions);
  726. }
  727. $data['permissions'] = $permissions;
  728. }
  729. }
  730. $data = \OC_FileProxy::runPostProxies('getFileInfo', $path, $data);
  731. return $data;
  732. }
  733. /**
  734. * get the content of a directory
  735. *
  736. * @param string $directory path under datadirectory
  737. * @param string $mimetype_filter limit returned content to this mimetype or mimepart
  738. * @return array
  739. */
  740. public function getDirectoryContent($directory, $mimetype_filter = '') {
  741. $result = array();
  742. if (!Filesystem::isValidPath($directory)) {
  743. return $result;
  744. }
  745. $path = Filesystem::normalizePath($this->fakeRoot . '/' . $directory);
  746. /**
  747. * @var \OC\Files\Storage\Storage $storage
  748. * @var string $internalPath
  749. */
  750. list($storage, $internalPath) = Filesystem::resolvePath($path);
  751. if ($storage) {
  752. $cache = $storage->getCache($internalPath);
  753. $permissionsCache = $storage->getPermissionsCache($internalPath);
  754. $user = \OC_User::getUser();
  755. if ($cache->getStatus($internalPath) < Cache\Cache::COMPLETE) {
  756. $scanner = $storage->getScanner($internalPath);
  757. $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW);
  758. } else {
  759. $watcher = $storage->getWatcher($internalPath);
  760. $watcher->checkUpdate($internalPath);
  761. }
  762. $files = $cache->getFolderContents($internalPath); //TODO: mimetype_filter
  763. $permissions = $permissionsCache->getDirectoryPermissions($cache->getId($internalPath), $user);
  764. $ids = array();
  765. foreach ($files as $i => $file) {
  766. $files[$i]['type'] = $file['mimetype'] === 'httpd/unix-directory' ? 'dir' : 'file';
  767. $ids[] = $file['fileid'];
  768. if (!isset($permissions[$file['fileid']])) {
  769. $permissions[$file['fileid']] = $storage->getPermissions($file['path']);
  770. $permissionsCache->set($file['fileid'], $user, $permissions[$file['fileid']]);
  771. }
  772. $files[$i]['permissions'] = $permissions[$file['fileid']];
  773. }
  774. //add a folder for any mountpoint in this directory and add the sizes of other mountpoints to the folders
  775. $mountPoints = Filesystem::getMountPoints($path);
  776. $dirLength = strlen($path);
  777. foreach ($mountPoints as $mountPoint) {
  778. $subStorage = Filesystem::getStorage($mountPoint);
  779. if ($subStorage) {
  780. $subCache = $subStorage->getCache('');
  781. if ($subCache->getStatus('') === Cache\Cache::NOT_FOUND) {
  782. $subScanner = $subStorage->getScanner('');
  783. $subScanner->scanFile('');
  784. }
  785. $rootEntry = $subCache->get('');
  786. if ($rootEntry) {
  787. $relativePath = trim(substr($mountPoint, $dirLength), '/');
  788. if ($pos = strpos($relativePath, '/')) {
  789. //mountpoint inside subfolder add size to the correct folder
  790. $entryName = substr($relativePath, 0, $pos);
  791. foreach ($files as &$entry) {
  792. if ($entry['name'] === $entryName) {
  793. $entry['size'] += $rootEntry['size'];
  794. }
  795. }
  796. } else { //mountpoint in this folder, add an entry for it
  797. $rootEntry['name'] = $relativePath;
  798. $rootEntry['type'] = $rootEntry['mimetype'] === 'httpd/unix-directory' ? 'dir' : 'file';
  799. $subPermissionsCache = $subStorage->getPermissionsCache('');
  800. $permissions = $subPermissionsCache->get($rootEntry['fileid'], $user);
  801. if ($permissions === -1) {
  802. $permissions = $subStorage->getPermissions($rootEntry['path']);
  803. $subPermissionsCache->set($rootEntry['fileid'], $user, $permissions);
  804. }
  805. $rootEntry['permissions'] = $permissions;
  806. //remove any existing entry with the same name
  807. foreach ($files as $i => $file) {
  808. if ($file['name'] === $rootEntry['name']) {
  809. unset($files[$i]);
  810. break;
  811. }
  812. }
  813. $files[] = $rootEntry;
  814. }
  815. }
  816. }
  817. }
  818. if ($mimetype_filter) {
  819. foreach ($files as $file) {
  820. if (strpos($mimetype_filter, '/')) {
  821. if ($file['mimetype'] === $mimetype_filter) {
  822. $result[] = $file;
  823. }
  824. } else {
  825. if ($file['mimepart'] === $mimetype_filter) {
  826. $result[] = $file;
  827. }
  828. }
  829. }
  830. } else {
  831. $result = $files;
  832. }
  833. }
  834. return $result;
  835. }
  836. /**
  837. * change file metadata
  838. *
  839. * @param string $path
  840. * @param array $data
  841. * @return int
  842. *
  843. * returns the fileid of the updated file
  844. */
  845. public function putFileInfo($path, $data) {
  846. $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path);
  847. /**
  848. * @var \OC\Files\Storage\Storage $storage
  849. * @var string $internalPath
  850. */
  851. list($storage, $internalPath) = Filesystem::resolvePath($path);
  852. if ($storage) {
  853. $cache = $storage->getCache($path);
  854. if (!$cache->inCache($internalPath)) {
  855. $scanner = $storage->getScanner($internalPath);
  856. $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW);
  857. }
  858. return $cache->put($internalPath, $data);
  859. } else {
  860. return -1;
  861. }
  862. }
  863. /**
  864. * search for files with the name matching $query
  865. *
  866. * @param string $query
  867. * @return array
  868. */
  869. public function search($query) {
  870. return $this->searchCommon('%' . $query . '%', 'search');
  871. }
  872. /**
  873. * search for files by mimetype
  874. *
  875. * @param string $query
  876. * @return array
  877. */
  878. public function searchByMime($mimetype) {
  879. return $this->searchCommon($mimetype, 'searchByMime');
  880. }
  881. /**
  882. * @param string $query
  883. * @param string $method
  884. * @return array
  885. */
  886. private function searchCommon($query, $method) {
  887. $files = array();
  888. $rootLength = strlen($this->fakeRoot);
  889. $mountPoint = Filesystem::getMountPoint($this->fakeRoot);
  890. $storage = Filesystem::getStorage($mountPoint);
  891. if ($storage) {
  892. $cache = $storage->getCache('');
  893. $results = $cache->$method($query);
  894. foreach ($results as $result) {
  895. if (substr($mountPoint . $result['path'], 0, $rootLength) === $this->fakeRoot) {
  896. $result['path'] = substr($mountPoint . $result['path'], $rootLength);
  897. $files[] = $result;
  898. }
  899. }
  900. $mountPoints = Filesystem::getMountPoints($this->fakeRoot);
  901. foreach ($mountPoints as $mountPoint) {
  902. $storage = Filesystem::getStorage($mountPoint);
  903. if ($storage) {
  904. $cache = $storage->getCache('');
  905. $relativeMountPoint = substr($mountPoint, $rootLength);
  906. $results = $cache->$method($query);
  907. foreach ($results as $result) {
  908. $result['path'] = $relativeMountPoint . $result['path'];
  909. $files[] = $result;
  910. }
  911. }
  912. }
  913. }
  914. return $files;
  915. }
  916. /**
  917. * Get the owner for a file or folder
  918. *
  919. * @param string $path
  920. * @return string
  921. */
  922. public function getOwner($path) {
  923. return $this->basicOperation('getOwner', $path);
  924. }
  925. /**
  926. * get the ETag for a file or folder
  927. *
  928. * @param string $path
  929. * @return string
  930. */
  931. public function getETag($path) {
  932. /**
  933. * @var Storage\Storage $storage
  934. * @var string $internalPath
  935. */
  936. list($storage, $internalPath) = $this->resolvePath($path);
  937. if ($storage) {
  938. return $storage->getETag($internalPath);
  939. } else {
  940. return null;
  941. }
  942. }
  943. /**
  944. * Get the path of a file by id, relative to the view
  945. *
  946. * Note that the resulting path is not guarantied to be unique for the id, multiple paths can point to the same file
  947. *
  948. * @param int $id
  949. * @return string
  950. */
  951. public function getPath($id) {
  952. list($storage, $internalPath) = Cache\Cache::getById($id);
  953. $mounts = Filesystem::getMountByStorageId($storage);
  954. foreach ($mounts as $mount) {
  955. /**
  956. * @var \OC\Files\Mount $mount
  957. */
  958. $fullPath = $mount->getMountPoint() . $internalPath;
  959. if (!is_null($path = $this->getRelativePath($fullPath))) {
  960. return $path;
  961. }
  962. }
  963. return null;
  964. }
  965. }