view.php 30 KB

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