view.php 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937
  1. <?php
  2. /**
  3. * @author Arthur Schiwon <blizzz@owncloud.com>
  4. * @author Bart Visscher <bartv@thisnet.nl>
  5. * @author Björn Schießle <schiessle@owncloud.com>
  6. * @author cmeh <cmeh@users.noreply.github.com>
  7. * @author Florin Peter <github@florin-peter.de>
  8. * @author Jesus Macias <jmacias@full-on-net.com>
  9. * @author Joas Schilling <nickvergessen@owncloud.com>
  10. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  11. * @author Klaas Freitag <freitag@owncloud.com>
  12. * @author Lukas Reschke <lukas@owncloud.com>
  13. * @author Luke Policinski <lpolicinski@gmail.com>
  14. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  15. * @author Morris Jobke <hey@morrisjobke.de>
  16. * @author Robin Appelman <icewind@owncloud.com>
  17. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  18. * @author Roman Geber <rgeber@owncloudapps.com>
  19. * @author Sam Tuke <mail@samtuke.com>
  20. * @author Thomas Müller <thomas.mueller@tmit.eu>
  21. * @author Thomas Tanghus <thomas@tanghus.net>
  22. * @author Vincent Petry <pvince81@owncloud.com>
  23. *
  24. * @copyright Copyright (c) 2015, ownCloud, Inc.
  25. * @license AGPL-3.0
  26. *
  27. * This code is free software: you can redistribute it and/or modify
  28. * it under the terms of the GNU Affero General Public License, version 3,
  29. * as published by the Free Software Foundation.
  30. *
  31. * This program is distributed in the hope that it will be useful,
  32. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  33. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  34. * GNU Affero General Public License for more details.
  35. *
  36. * You should have received a copy of the GNU Affero General Public License, version 3,
  37. * along with this program. If not, see <http://www.gnu.org/licenses/>
  38. *
  39. */
  40. namespace OC\Files;
  41. use Icewind\Streams\CallbackWrapper;
  42. use OC\Files\Cache\Updater;
  43. use OC\Files\Mount\MoveableMount;
  44. use OCP\Files\FileNameTooLongException;
  45. use OCP\Files\InvalidCharacterInPathException;
  46. use OCP\Files\InvalidPathException;
  47. use OCP\Files\ReservedWordException;
  48. use OCP\Lock\ILockingProvider;
  49. use OCP\Lock\LockedException;
  50. /**
  51. * Class to provide access to ownCloud filesystem via a "view", and methods for
  52. * working with files within that view (e.g. read, write, delete, etc.). Each
  53. * view is restricted to a set of directories via a virtual root. The default view
  54. * uses the currently logged in user's data directory as root (parts of
  55. * OC_Filesystem are merely a wrapper for OC\Files\View).
  56. *
  57. * Apps that need to access files outside of the user data folders (to modify files
  58. * belonging to a user other than the one currently logged in, for example) should
  59. * use this class directly rather than using OC_Filesystem, or making use of PHP's
  60. * built-in file manipulation functions. This will ensure all hooks and proxies
  61. * are triggered correctly.
  62. *
  63. * Filesystem functions are not called directly; they are passed to the correct
  64. * \OC\Files\Storage\Storage object
  65. */
  66. class View {
  67. /** @var string */
  68. private $fakeRoot = '';
  69. /** @var \OC\Files\Cache\Updater */
  70. protected $updater;
  71. /**
  72. * @var \OCP\Lock\ILockingProvider
  73. */
  74. private $lockingProvider;
  75. private $lockingEnabled;
  76. /**
  77. * @param string $root
  78. * @throws \Exception If $root contains an invalid path
  79. */
  80. public function __construct($root = '') {
  81. if (is_null($root)) {
  82. throw new \InvalidArgumentException('Root can\'t be null');
  83. }
  84. if (!Filesystem::isValidPath($root)) {
  85. throw new \Exception();
  86. }
  87. $this->fakeRoot = $root;
  88. $this->updater = new Updater($this);
  89. $this->lockingProvider = \OC::$server->getLockingProvider();
  90. $this->lockingEnabled = !($this->lockingProvider instanceof \OC\Lock\NoopLockingProvider);
  91. }
  92. public function getAbsolutePath($path = '/') {
  93. if ($path === null) {
  94. return null;
  95. }
  96. $this->assertPathLength($path);
  97. if ($path === '') {
  98. $path = '/';
  99. }
  100. if ($path[0] !== '/') {
  101. $path = '/' . $path;
  102. }
  103. return $this->fakeRoot . $path;
  104. }
  105. /**
  106. * change the root to a fake root
  107. *
  108. * @param string $fakeRoot
  109. * @return boolean|null
  110. */
  111. public function chroot($fakeRoot) {
  112. if (!$fakeRoot == '') {
  113. if ($fakeRoot[0] !== '/') {
  114. $fakeRoot = '/' . $fakeRoot;
  115. }
  116. }
  117. $this->fakeRoot = $fakeRoot;
  118. }
  119. /**
  120. * get the fake root
  121. *
  122. * @return string
  123. */
  124. public function getRoot() {
  125. return $this->fakeRoot;
  126. }
  127. /**
  128. * get path relative to the root of the view
  129. *
  130. * @param string $path
  131. * @return string
  132. */
  133. public function getRelativePath($path) {
  134. $this->assertPathLength($path);
  135. if ($this->fakeRoot == '') {
  136. return $path;
  137. }
  138. if (rtrim($path, '/') === rtrim($this->fakeRoot, '/')) {
  139. return '/';
  140. }
  141. if (strpos($path, $this->fakeRoot) !== 0) {
  142. return null;
  143. } else {
  144. $path = substr($path, strlen($this->fakeRoot));
  145. if (strlen($path) === 0) {
  146. return '/';
  147. } else {
  148. return $path;
  149. }
  150. }
  151. }
  152. /**
  153. * get the mountpoint of the storage object for a path
  154. * ( note: because a storage is not always mounted inside the fakeroot, the
  155. * returned mountpoint is relative to the absolute root of the filesystem
  156. * and does not take the chroot into account )
  157. *
  158. * @param string $path
  159. * @return string
  160. */
  161. public function getMountPoint($path) {
  162. return Filesystem::getMountPoint($this->getAbsolutePath($path));
  163. }
  164. /**
  165. * get the mountpoint of the storage object for a path
  166. * ( note: because a storage is not always mounted inside the fakeroot, the
  167. * returned mountpoint is relative to the absolute root of the filesystem
  168. * and does not take the chroot into account )
  169. *
  170. * @param string $path
  171. * @return \OCP\Files\Mount\IMountPoint
  172. */
  173. public function getMount($path) {
  174. return Filesystem::getMountManager()->find($this->getAbsolutePath($path));
  175. }
  176. /**
  177. * resolve a path to a storage and internal path
  178. *
  179. * @param string $path
  180. * @return array an array consisting of the storage and the internal path
  181. */
  182. public function resolvePath($path) {
  183. $a = $this->getAbsolutePath($path);
  184. $p = Filesystem::normalizePath($a);
  185. return Filesystem::resolvePath($p);
  186. }
  187. /**
  188. * return the path to a local version of the file
  189. * we need this because we can't know if a file is stored local or not from
  190. * outside the filestorage and for some purposes a local file is needed
  191. *
  192. * @param string $path
  193. * @return string
  194. */
  195. public function getLocalFile($path) {
  196. $parent = substr($path, 0, strrpos($path, '/'));
  197. $path = $this->getAbsolutePath($path);
  198. list($storage, $internalPath) = Filesystem::resolvePath($path);
  199. if (Filesystem::isValidPath($parent) and $storage) {
  200. return $storage->getLocalFile($internalPath);
  201. } else {
  202. return null;
  203. }
  204. }
  205. /**
  206. * @param string $path
  207. * @return string
  208. */
  209. public function getLocalFolder($path) {
  210. $parent = substr($path, 0, strrpos($path, '/'));
  211. $path = $this->getAbsolutePath($path);
  212. list($storage, $internalPath) = Filesystem::resolvePath($path);
  213. if (Filesystem::isValidPath($parent) and $storage) {
  214. return $storage->getLocalFolder($internalPath);
  215. } else {
  216. return null;
  217. }
  218. }
  219. /**
  220. * the following functions operate with arguments and return values identical
  221. * to those of their PHP built-in equivalents. Mostly they are merely wrappers
  222. * for \OC\Files\Storage\Storage via basicOperation().
  223. */
  224. public function mkdir($path) {
  225. return $this->basicOperation('mkdir', $path, array('create', 'write'));
  226. }
  227. /**
  228. * remove mount point
  229. *
  230. * @param \OC\Files\Mount\MoveableMount $mount
  231. * @param string $path relative to data/
  232. * @return boolean
  233. */
  234. protected function removeMount($mount, $path) {
  235. if ($mount instanceof MoveableMount) {
  236. // cut of /user/files to get the relative path to data/user/files
  237. $pathParts = explode('/', $path, 4);
  238. $relPath = '/' . $pathParts[3];
  239. \OC_Hook::emit(
  240. Filesystem::CLASSNAME, "umount",
  241. array(Filesystem::signal_param_path => $relPath)
  242. );
  243. $result = $mount->removeMount();
  244. if ($result) {
  245. \OC_Hook::emit(
  246. Filesystem::CLASSNAME, "post_umount",
  247. array(Filesystem::signal_param_path => $relPath)
  248. );
  249. }
  250. return $result;
  251. } else {
  252. // do not allow deleting the storage's root / the mount point
  253. // because for some storages it might delete the whole contents
  254. // but isn't supposed to work that way
  255. return false;
  256. }
  257. }
  258. /**
  259. * @param string $path
  260. * @return bool|mixed
  261. */
  262. public function rmdir($path) {
  263. $absolutePath = $this->getAbsolutePath($path);
  264. $mount = Filesystem::getMountManager()->find($absolutePath);
  265. if ($mount->getInternalPath($absolutePath) === '') {
  266. return $this->removeMount($mount, $path);
  267. }
  268. if ($this->is_dir($path)) {
  269. return $this->basicOperation('rmdir', $path, array('delete'));
  270. } else {
  271. return false;
  272. }
  273. }
  274. /**
  275. * @param string $path
  276. * @return resource
  277. */
  278. public function opendir($path) {
  279. return $this->basicOperation('opendir', $path, array('read'));
  280. }
  281. /**
  282. * @param $handle
  283. * @return mixed
  284. */
  285. public function readdir($handle) {
  286. $fsLocal = new Storage\Local(array('datadir' => '/'));
  287. return $fsLocal->readdir($handle);
  288. }
  289. /**
  290. * @param string $path
  291. * @return bool|mixed
  292. */
  293. public function is_dir($path) {
  294. if ($path == '/') {
  295. return true;
  296. }
  297. return $this->basicOperation('is_dir', $path);
  298. }
  299. /**
  300. * @param string $path
  301. * @return bool|mixed
  302. */
  303. public function is_file($path) {
  304. if ($path == '/') {
  305. return false;
  306. }
  307. return $this->basicOperation('is_file', $path);
  308. }
  309. /**
  310. * @param string $path
  311. * @return mixed
  312. */
  313. public function stat($path) {
  314. return $this->basicOperation('stat', $path);
  315. }
  316. /**
  317. * @param string $path
  318. * @return mixed
  319. */
  320. public function filetype($path) {
  321. return $this->basicOperation('filetype', $path);
  322. }
  323. /**
  324. * @param string $path
  325. * @return mixed
  326. */
  327. public function filesize($path) {
  328. return $this->basicOperation('filesize', $path);
  329. }
  330. /**
  331. * @param string $path
  332. * @return bool|mixed
  333. * @throws \OCP\Files\InvalidPathException
  334. */
  335. public function readfile($path) {
  336. $this->assertPathLength($path);
  337. @ob_end_clean();
  338. $handle = $this->fopen($path, 'rb');
  339. if ($handle) {
  340. $chunkSize = 8192; // 8 kB chunks
  341. while (!feof($handle)) {
  342. echo fread($handle, $chunkSize);
  343. flush();
  344. }
  345. $size = $this->filesize($path);
  346. return $size;
  347. }
  348. return false;
  349. }
  350. /**
  351. * @param string $path
  352. * @return mixed
  353. */
  354. public function isCreatable($path) {
  355. return $this->basicOperation('isCreatable', $path);
  356. }
  357. /**
  358. * @param string $path
  359. * @return mixed
  360. */
  361. public function isReadable($path) {
  362. return $this->basicOperation('isReadable', $path);
  363. }
  364. /**
  365. * @param string $path
  366. * @return mixed
  367. */
  368. public function isUpdatable($path) {
  369. return $this->basicOperation('isUpdatable', $path);
  370. }
  371. /**
  372. * @param string $path
  373. * @return bool|mixed
  374. */
  375. public function isDeletable($path) {
  376. $absolutePath = $this->getAbsolutePath($path);
  377. $mount = Filesystem::getMountManager()->find($absolutePath);
  378. if ($mount->getInternalPath($absolutePath) === '') {
  379. return $mount instanceof MoveableMount;
  380. }
  381. return $this->basicOperation('isDeletable', $path);
  382. }
  383. /**
  384. * @param string $path
  385. * @return mixed
  386. */
  387. public function isSharable($path) {
  388. return $this->basicOperation('isSharable', $path);
  389. }
  390. /**
  391. * @param string $path
  392. * @return bool|mixed
  393. */
  394. public function file_exists($path) {
  395. if ($path == '/') {
  396. return true;
  397. }
  398. return $this->basicOperation('file_exists', $path);
  399. }
  400. /**
  401. * @param string $path
  402. * @return mixed
  403. */
  404. public function filemtime($path) {
  405. return $this->basicOperation('filemtime', $path);
  406. }
  407. /**
  408. * @param string $path
  409. * @param int|string $mtime
  410. * @return bool
  411. */
  412. public function touch($path, $mtime = null) {
  413. if (!is_null($mtime) and !is_numeric($mtime)) {
  414. $mtime = strtotime($mtime);
  415. }
  416. $hooks = array('touch');
  417. if (!$this->file_exists($path)) {
  418. $hooks[] = 'create';
  419. $hooks[] = 'write';
  420. }
  421. $result = $this->basicOperation('touch', $path, $hooks, $mtime);
  422. if (!$result) {
  423. // If create file fails because of permissions on external storage like SMB folders,
  424. // check file exists and return false if not.
  425. if (!$this->file_exists($path)) {
  426. return false;
  427. }
  428. if (is_null($mtime)) {
  429. $mtime = time();
  430. }
  431. //if native touch fails, we emulate it by changing the mtime in the cache
  432. $this->putFileInfo($path, array('mtime' => $mtime));
  433. }
  434. return true;
  435. }
  436. /**
  437. * @param string $path
  438. * @return mixed
  439. */
  440. public function file_get_contents($path) {
  441. return $this->basicOperation('file_get_contents', $path, array('read'));
  442. }
  443. /**
  444. * @param bool $exists
  445. * @param string $path
  446. * @param bool $run
  447. */
  448. protected function emit_file_hooks_pre($exists, $path, &$run) {
  449. if (!$exists) {
  450. \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_create, array(
  451. Filesystem::signal_param_path => $this->getHookPath($path),
  452. Filesystem::signal_param_run => &$run,
  453. ));
  454. } else {
  455. \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_update, array(
  456. Filesystem::signal_param_path => $this->getHookPath($path),
  457. Filesystem::signal_param_run => &$run,
  458. ));
  459. }
  460. \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_write, array(
  461. Filesystem::signal_param_path => $this->getHookPath($path),
  462. Filesystem::signal_param_run => &$run,
  463. ));
  464. }
  465. /**
  466. * @param bool $exists
  467. * @param string $path
  468. */
  469. protected function emit_file_hooks_post($exists, $path) {
  470. if (!$exists) {
  471. \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_create, array(
  472. Filesystem::signal_param_path => $this->getHookPath($path),
  473. ));
  474. } else {
  475. \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_update, array(
  476. Filesystem::signal_param_path => $this->getHookPath($path),
  477. ));
  478. }
  479. \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_write, array(
  480. Filesystem::signal_param_path => $this->getHookPath($path),
  481. ));
  482. }
  483. /**
  484. * @param string $path
  485. * @param mixed $data
  486. * @return bool|mixed
  487. */
  488. public function file_put_contents($path, $data) {
  489. if (is_resource($data)) { //not having to deal with streams in file_put_contents makes life easier
  490. $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path));
  491. if (Filesystem::isValidPath($path)
  492. and !Filesystem::isFileBlacklisted($path)
  493. ) {
  494. $path = $this->getRelativePath($absolutePath);
  495. $this->lockFile($path, ILockingProvider::LOCK_SHARED);
  496. $exists = $this->file_exists($path);
  497. $run = true;
  498. if ($this->shouldEmitHooks($path)) {
  499. $this->emit_file_hooks_pre($exists, $path, $run);
  500. }
  501. if (!$run) {
  502. $this->unlockFile($path, ILockingProvider::LOCK_SHARED);
  503. return false;
  504. }
  505. $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE);
  506. /** @var \OC\Files\Storage\Storage $storage */
  507. list($storage, $internalPath) = $this->resolvePath($path);
  508. $target = $storage->fopen($internalPath, 'w');
  509. if ($target) {
  510. list (, $result) = \OC_Helper::streamCopy($data, $target);
  511. fclose($target);
  512. fclose($data);
  513. $this->updater->update($path);
  514. $this->changeLock($path, ILockingProvider::LOCK_SHARED);
  515. if ($this->shouldEmitHooks($path) && $result !== false) {
  516. $this->emit_file_hooks_post($exists, $path);
  517. }
  518. $this->unlockFile($path, ILockingProvider::LOCK_SHARED);
  519. return $result;
  520. } else {
  521. $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE);
  522. return false;
  523. }
  524. } else {
  525. return false;
  526. }
  527. } else {
  528. $hooks = ($this->file_exists($path)) ? array('update', 'write') : array('create', 'write');
  529. return $this->basicOperation('file_put_contents', $path, $hooks, $data);
  530. }
  531. }
  532. /**
  533. * @param string $path
  534. * @return bool|mixed
  535. */
  536. public function unlink($path) {
  537. if ($path === '' || $path === '/') {
  538. // do not allow deleting the root
  539. return false;
  540. }
  541. $postFix = (substr($path, -1, 1) === '/') ? '/' : '';
  542. $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path));
  543. $mount = Filesystem::getMountManager()->find($absolutePath . $postFix);
  544. if ($mount and $mount->getInternalPath($absolutePath) === '') {
  545. return $this->removeMount($mount, $absolutePath);
  546. }
  547. return $this->basicOperation('unlink', $path, array('delete'));
  548. }
  549. /**
  550. * @param string $directory
  551. * @return bool|mixed
  552. */
  553. public function deleteAll($directory) {
  554. return $this->rmdir($directory);
  555. }
  556. /**
  557. * Rename/move a file or folder from the source path to target path.
  558. *
  559. * @param string $path1 source path
  560. * @param string $path2 target path
  561. *
  562. * @return bool|mixed
  563. */
  564. public function rename($path1, $path2) {
  565. $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1));
  566. $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2));
  567. $result = false;
  568. if (
  569. Filesystem::isValidPath($path2)
  570. and Filesystem::isValidPath($path1)
  571. and !Filesystem::isFileBlacklisted($path2)
  572. ) {
  573. $path1 = $this->getRelativePath($absolutePath1);
  574. $path2 = $this->getRelativePath($absolutePath2);
  575. $exists = $this->file_exists($path2);
  576. if ($path1 == null or $path2 == null) {
  577. return false;
  578. }
  579. $this->lockFile($path1, ILockingProvider::LOCK_SHARED, true);
  580. try {
  581. $this->lockFile($path2, ILockingProvider::LOCK_SHARED, true);
  582. } catch (LockedException $e) {
  583. $this->unlockFile($path1, ILockingProvider::LOCK_SHARED);
  584. throw $e;
  585. }
  586. $run = true;
  587. if ($this->shouldEmitHooks($path1) && (Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2))) {
  588. // if it was a rename from a part file to a regular file it was a write and not a rename operation
  589. $this->emit_file_hooks_pre($exists, $path2, $run);
  590. } elseif ($this->shouldEmitHooks($path1)) {
  591. \OC_Hook::emit(
  592. Filesystem::CLASSNAME, Filesystem::signal_rename,
  593. array(
  594. Filesystem::signal_param_oldpath => $this->getHookPath($path1),
  595. Filesystem::signal_param_newpath => $this->getHookPath($path2),
  596. Filesystem::signal_param_run => &$run
  597. )
  598. );
  599. }
  600. if ($run) {
  601. $this->verifyPath(dirname($path2), basename($path2));
  602. $manager = Filesystem::getMountManager();
  603. $mount1 = $this->getMount($path1);
  604. $mount2 = $this->getMount($path2);
  605. $storage1 = $mount1->getStorage();
  606. $storage2 = $mount2->getStorage();
  607. $internalPath1 = $mount1->getInternalPath($absolutePath1);
  608. $internalPath2 = $mount2->getInternalPath($absolutePath2);
  609. $this->changeLock($path1, ILockingProvider::LOCK_EXCLUSIVE, true);
  610. $this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE, true);
  611. if ($internalPath1 === '' and $mount1 instanceof MoveableMount) {
  612. if ($this->isTargetAllowed($absolutePath2)) {
  613. /**
  614. * @var \OC\Files\Mount\MountPoint | \OC\Files\Mount\MoveableMount $mount1
  615. */
  616. $sourceMountPoint = $mount1->getMountPoint();
  617. $result = $mount1->moveMount($absolutePath2);
  618. $manager->moveMount($sourceMountPoint, $mount1->getMountPoint());
  619. } else {
  620. $result = false;
  621. }
  622. // moving a file/folder within the same mount point
  623. } elseif ($storage1 == $storage2) {
  624. if ($storage1) {
  625. $result = $storage1->rename($internalPath1, $internalPath2);
  626. } else {
  627. $result = false;
  628. }
  629. // moving a file/folder between storages (from $storage1 to $storage2)
  630. } else {
  631. $result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2);
  632. }
  633. if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) {
  634. // if it was a rename from a part file to a regular file it was a write and not a rename operation
  635. $this->updater->update($path2);
  636. } else if ($result) {
  637. if ($internalPath1 !== '') { // dont do a cache update for moved mounts
  638. $this->updater->rename($path1, $path2);
  639. } else { // only do etag propagation
  640. $this->getUpdater()->getPropagator()->addChange($path1);
  641. $this->getUpdater()->getPropagator()->addChange($path2);
  642. $this->getUpdater()->getPropagator()->propagateChanges();
  643. }
  644. }
  645. $this->changeLock($path1, ILockingProvider::LOCK_SHARED, true);
  646. $this->changeLock($path2, ILockingProvider::LOCK_SHARED, true);
  647. if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) {
  648. if ($this->shouldEmitHooks()) {
  649. $this->emit_file_hooks_post($exists, $path2);
  650. }
  651. } elseif ($result) {
  652. if ($this->shouldEmitHooks($path1) and $this->shouldEmitHooks($path2)) {
  653. \OC_Hook::emit(
  654. Filesystem::CLASSNAME,
  655. Filesystem::signal_post_rename,
  656. array(
  657. Filesystem::signal_param_oldpath => $this->getHookPath($path1),
  658. Filesystem::signal_param_newpath => $this->getHookPath($path2)
  659. )
  660. );
  661. }
  662. }
  663. }
  664. $this->unlockFile($path1, ILockingProvider::LOCK_SHARED, true);
  665. $this->unlockFile($path2, ILockingProvider::LOCK_SHARED, true);
  666. }
  667. return $result;
  668. }
  669. /**
  670. * Copy a file/folder from the source path to target path
  671. *
  672. * @param string $path1 source path
  673. * @param string $path2 target path
  674. * @param bool $preserveMtime whether to preserve mtime on the copy
  675. *
  676. * @return bool|mixed
  677. */
  678. public function copy($path1, $path2, $preserveMtime = false) {
  679. $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1));
  680. $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2));
  681. $result = false;
  682. if (
  683. Filesystem::isValidPath($path2)
  684. and Filesystem::isValidPath($path1)
  685. and !Filesystem::isFileBlacklisted($path2)
  686. ) {
  687. $path1 = $this->getRelativePath($absolutePath1);
  688. $path2 = $this->getRelativePath($absolutePath2);
  689. if ($path1 == null or $path2 == null) {
  690. return false;
  691. }
  692. $run = true;
  693. $this->lockFile($path2, ILockingProvider::LOCK_SHARED);
  694. $this->lockFile($path1, ILockingProvider::LOCK_SHARED);
  695. $exists = $this->file_exists($path2);
  696. if ($this->shouldEmitHooks()) {
  697. \OC_Hook::emit(
  698. Filesystem::CLASSNAME,
  699. Filesystem::signal_copy,
  700. array(
  701. Filesystem::signal_param_oldpath => $this->getHookPath($path1),
  702. Filesystem::signal_param_newpath => $this->getHookPath($path2),
  703. Filesystem::signal_param_run => &$run
  704. )
  705. );
  706. $this->emit_file_hooks_pre($exists, $path2, $run);
  707. }
  708. if ($run) {
  709. $mount1 = $this->getMount($path1);
  710. $mount2 = $this->getMount($path2);
  711. $storage1 = $mount1->getStorage();
  712. $internalPath1 = $mount1->getInternalPath($absolutePath1);
  713. $storage2 = $mount2->getStorage();
  714. $internalPath2 = $mount2->getInternalPath($absolutePath2);
  715. $this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE);
  716. if ($mount1->getMountPoint() == $mount2->getMountPoint()) {
  717. if ($storage1) {
  718. $result = $storage1->copy($internalPath1, $internalPath2);
  719. } else {
  720. $result = false;
  721. }
  722. } else {
  723. $result = $storage2->copyFromStorage($storage1, $internalPath1, $internalPath2);
  724. }
  725. $this->updater->update($path2);
  726. $this->changeLock($path2, ILockingProvider::LOCK_SHARED);
  727. if ($this->shouldEmitHooks() && $result !== false) {
  728. \OC_Hook::emit(
  729. Filesystem::CLASSNAME,
  730. Filesystem::signal_post_copy,
  731. array(
  732. Filesystem::signal_param_oldpath => $this->getHookPath($path1),
  733. Filesystem::signal_param_newpath => $this->getHookPath($path2)
  734. )
  735. );
  736. $this->emit_file_hooks_post($exists, $path2);
  737. }
  738. $this->unlockFile($path2, ILockingProvider::LOCK_SHARED);
  739. $this->unlockFile($path1, ILockingProvider::LOCK_SHARED);
  740. }
  741. }
  742. return $result;
  743. }
  744. /**
  745. * @param string $path
  746. * @param string $mode
  747. * @return resource
  748. */
  749. public function fopen($path, $mode) {
  750. $hooks = array();
  751. switch ($mode) {
  752. case 'r':
  753. case 'rb':
  754. $hooks[] = 'read';
  755. break;
  756. case 'r+':
  757. case 'rb+':
  758. case 'w+':
  759. case 'wb+':
  760. case 'x+':
  761. case 'xb+':
  762. case 'a+':
  763. case 'ab+':
  764. $hooks[] = 'read';
  765. $hooks[] = 'write';
  766. break;
  767. case 'w':
  768. case 'wb':
  769. case 'x':
  770. case 'xb':
  771. case 'a':
  772. case 'ab':
  773. $hooks[] = 'write';
  774. break;
  775. default:
  776. \OCP\Util::writeLog('core', 'invalid mode (' . $mode . ') for ' . $path, \OCP\Util::ERROR);
  777. }
  778. return $this->basicOperation('fopen', $path, $hooks, $mode);
  779. }
  780. /**
  781. * @param string $path
  782. * @return bool|string
  783. * @throws \OCP\Files\InvalidPathException
  784. */
  785. public function toTmpFile($path) {
  786. $this->assertPathLength($path);
  787. if (Filesystem::isValidPath($path)) {
  788. $source = $this->fopen($path, 'r');
  789. if ($source) {
  790. $extension = pathinfo($path, PATHINFO_EXTENSION);
  791. $tmpFile = \OC_Helper::tmpFile($extension);
  792. file_put_contents($tmpFile, $source);
  793. return $tmpFile;
  794. } else {
  795. return false;
  796. }
  797. } else {
  798. return false;
  799. }
  800. }
  801. /**
  802. * @param string $tmpFile
  803. * @param string $path
  804. * @return bool|mixed
  805. * @throws \OCP\Files\InvalidPathException
  806. */
  807. public function fromTmpFile($tmpFile, $path) {
  808. $this->assertPathLength($path);
  809. if (Filesystem::isValidPath($path)) {
  810. // Get directory that the file is going into
  811. $filePath = dirname($path);
  812. // Create the directories if any
  813. if (!$this->file_exists($filePath)) {
  814. $this->mkdir($filePath);
  815. }
  816. $source = fopen($tmpFile, 'r');
  817. if ($source) {
  818. $result = $this->file_put_contents($path, $source);
  819. // $this->file_put_contents() might have already closed
  820. // the resource, so we check it, before trying to close it
  821. // to avoid messages in the error log.
  822. if (is_resource($source)) {
  823. fclose($source);
  824. }
  825. unlink($tmpFile);
  826. return $result;
  827. } else {
  828. return false;
  829. }
  830. } else {
  831. return false;
  832. }
  833. }
  834. /**
  835. * @param string $path
  836. * @return mixed
  837. * @throws \OCP\Files\InvalidPathException
  838. */
  839. public function getMimeType($path) {
  840. $this->assertPathLength($path);
  841. return $this->basicOperation('getMimeType', $path);
  842. }
  843. /**
  844. * @param string $type
  845. * @param string $path
  846. * @param bool $raw
  847. * @return bool|null|string
  848. */
  849. public function hash($type, $path, $raw = false) {
  850. $postFix = (substr($path, -1, 1) === '/') ? '/' : '';
  851. $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path));
  852. if (Filesystem::isValidPath($path)) {
  853. $path = $this->getRelativePath($absolutePath);
  854. if ($path == null) {
  855. return false;
  856. }
  857. if ($this->shouldEmitHooks($path)) {
  858. \OC_Hook::emit(
  859. Filesystem::CLASSNAME,
  860. Filesystem::signal_read,
  861. array(Filesystem::signal_param_path => $this->getHookPath($path))
  862. );
  863. }
  864. list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix);
  865. if ($storage) {
  866. $result = $storage->hash($type, $internalPath, $raw);
  867. return $result;
  868. }
  869. }
  870. return null;
  871. }
  872. /**
  873. * @param string $path
  874. * @return mixed
  875. * @throws \OCP\Files\InvalidPathException
  876. */
  877. public function free_space($path = '/') {
  878. $this->assertPathLength($path);
  879. return $this->basicOperation('free_space', $path);
  880. }
  881. /**
  882. * abstraction layer for basic filesystem functions: wrapper for \OC\Files\Storage\Storage
  883. *
  884. * @param string $operation
  885. * @param string $path
  886. * @param array $hooks (optional)
  887. * @param mixed $extraParam (optional)
  888. * @return mixed
  889. *
  890. * This method takes requests for basic filesystem functions (e.g. reading & writing
  891. * files), processes hooks and proxies, sanitises paths, and finally passes them on to
  892. * \OC\Files\Storage\Storage for delegation to a storage backend for execution
  893. */
  894. private function basicOperation($operation, $path, $hooks = array(), $extraParam = null) {
  895. $postFix = (substr($path, -1, 1) === '/') ? '/' : '';
  896. $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path));
  897. if (Filesystem::isValidPath($path)
  898. and !Filesystem::isFileBlacklisted($path)
  899. ) {
  900. $path = $this->getRelativePath($absolutePath);
  901. if ($path == null) {
  902. return false;
  903. }
  904. if (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) {
  905. // always a shared lock during pre-hooks so the hook can read the file
  906. $this->lockFile($path, ILockingProvider::LOCK_SHARED);
  907. }
  908. $run = $this->runHooks($hooks, $path);
  909. list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix);
  910. if ($run and $storage) {
  911. if (in_array('write', $hooks) || in_array('delete', $hooks)) {
  912. $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE);
  913. }
  914. try {
  915. if (!is_null($extraParam)) {
  916. $result = $storage->$operation($internalPath, $extraParam);
  917. } else {
  918. $result = $storage->$operation($internalPath);
  919. }
  920. } catch (\Exception $e) {
  921. if (in_array('write', $hooks) || in_array('delete', $hooks)) {
  922. $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE);
  923. } else if (in_array('read', $hooks)) {
  924. $this->unlockFile($path, ILockingProvider::LOCK_SHARED);
  925. }
  926. throw $e;
  927. }
  928. if (in_array('delete', $hooks) and $result) {
  929. $this->updater->remove($path);
  930. }
  931. if (in_array('write', $hooks) and $operation !== 'fopen') {
  932. $this->updater->update($path);
  933. }
  934. if (in_array('touch', $hooks)) {
  935. $this->updater->update($path, $extraParam);
  936. }
  937. if ((in_array('write', $hooks) || in_array('delete', $hooks)) && ($operation !== 'fopen' || $result === false)) {
  938. $this->changeLock($path, ILockingProvider::LOCK_SHARED);
  939. }
  940. $unlockLater = false;
  941. if ($this->lockingEnabled && $operation === 'fopen' && is_resource($result)) {
  942. $unlockLater = true;
  943. $result = CallbackWrapper::wrap($result, null, null, function () use ($hooks, $path) {
  944. if (in_array('write', $hooks)) {
  945. $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE);
  946. } else if (in_array('read', $hooks)) {
  947. $this->unlockFile($path, ILockingProvider::LOCK_SHARED);
  948. }
  949. });
  950. }
  951. if ($this->shouldEmitHooks($path) && $result !== false) {
  952. if ($operation != 'fopen') { //no post hooks for fopen, the file stream is still open
  953. $this->runHooks($hooks, $path, true);
  954. }
  955. }
  956. if (!$unlockLater
  957. && (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks))
  958. ) {
  959. $this->unlockFile($path, ILockingProvider::LOCK_SHARED);
  960. }
  961. return $result;
  962. } else {
  963. $this->unlockFile($path, ILockingProvider::LOCK_SHARED);
  964. }
  965. }
  966. return null;
  967. }
  968. /**
  969. * get the path relative to the default root for hook usage
  970. *
  971. * @param string $path
  972. * @return string
  973. */
  974. private function getHookPath($path) {
  975. if (!Filesystem::getView()) {
  976. return $path;
  977. }
  978. return Filesystem::getView()->getRelativePath($this->getAbsolutePath($path));
  979. }
  980. private function shouldEmitHooks($path = '') {
  981. if ($path && Cache\Scanner::isPartialFile($path)) {
  982. return false;
  983. }
  984. if (!Filesystem::$loaded) {
  985. return false;
  986. }
  987. $defaultRoot = Filesystem::getRoot();
  988. if ($defaultRoot === null) {
  989. return false;
  990. }
  991. if ($this->fakeRoot === $defaultRoot) {
  992. return true;
  993. }
  994. $fullPath = $this->getAbsolutePath($path);
  995. if ($fullPath === $defaultRoot) {
  996. return true;
  997. }
  998. return (strlen($fullPath) > strlen($defaultRoot)) && (substr($fullPath, 0, strlen($defaultRoot) + 1) === $defaultRoot . '/');
  999. }
  1000. /**
  1001. * @param string[] $hooks
  1002. * @param string $path
  1003. * @param bool $post
  1004. * @return bool
  1005. */
  1006. private function runHooks($hooks, $path, $post = false) {
  1007. $relativePath = $path;
  1008. $path = $this->getHookPath($path);
  1009. $prefix = ($post) ? 'post_' : '';
  1010. $run = true;
  1011. if ($this->shouldEmitHooks($relativePath)) {
  1012. foreach ($hooks as $hook) {
  1013. if ($hook != 'read') {
  1014. \OC_Hook::emit(
  1015. Filesystem::CLASSNAME,
  1016. $prefix . $hook,
  1017. array(
  1018. Filesystem::signal_param_run => &$run,
  1019. Filesystem::signal_param_path => $path
  1020. )
  1021. );
  1022. } elseif (!$post) {
  1023. \OC_Hook::emit(
  1024. Filesystem::CLASSNAME,
  1025. $prefix . $hook,
  1026. array(
  1027. Filesystem::signal_param_path => $path
  1028. )
  1029. );
  1030. }
  1031. }
  1032. }
  1033. return $run;
  1034. }
  1035. /**
  1036. * check if a file or folder has been updated since $time
  1037. *
  1038. * @param string $path
  1039. * @param int $time
  1040. * @return bool
  1041. */
  1042. public function hasUpdated($path, $time) {
  1043. return $this->basicOperation('hasUpdated', $path, array(), $time);
  1044. }
  1045. /**
  1046. * get the filesystem info
  1047. *
  1048. * @param string $path
  1049. * @param boolean|string $includeMountPoints true to add mountpoint sizes,
  1050. * 'ext' to add only ext storage mount point sizes. Defaults to true.
  1051. * defaults to true
  1052. * @return \OC\Files\FileInfo|false
  1053. */
  1054. public function getFileInfo($path, $includeMountPoints = true) {
  1055. $this->assertPathLength($path);
  1056. $data = array();
  1057. if (!Filesystem::isValidPath($path)) {
  1058. return $data;
  1059. }
  1060. if (Cache\Scanner::isPartialFile($path)) {
  1061. return $this->getPartFileInfo($path);
  1062. }
  1063. $relativePath = $path;
  1064. $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path);
  1065. $mount = Filesystem::getMountManager()->find($path);
  1066. $storage = $mount->getStorage();
  1067. $internalPath = $mount->getInternalPath($path);
  1068. $data = null;
  1069. if ($storage) {
  1070. $cache = $storage->getCache($internalPath);
  1071. try {
  1072. $this->lockFile($relativePath, ILockingProvider::LOCK_SHARED);
  1073. $data = $cache->get($internalPath);
  1074. $watcher = $storage->getWatcher($internalPath);
  1075. // if the file is not in the cache or needs to be updated, trigger the scanner and reload the data
  1076. if (!$data) {
  1077. if (!$storage->file_exists($internalPath)) {
  1078. $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED);
  1079. return false;
  1080. }
  1081. $scanner = $storage->getScanner($internalPath);
  1082. $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW);
  1083. $data = $cache->get($internalPath);
  1084. } else if (!Cache\Scanner::isPartialFile($internalPath) && $watcher->checkUpdate($internalPath, $data)) {
  1085. $this->updater->propagate($path);
  1086. $data = $cache->get($internalPath);
  1087. }
  1088. $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED);
  1089. } catch (LockedException $e) {
  1090. // dont try to update the cache when the file is locked
  1091. $data = $cache->get($internalPath);
  1092. }
  1093. if ($data and isset($data['fileid'])) {
  1094. // upgrades from oc6 or lower might not have the permissions set in the file cache
  1095. if ($data['permissions'] === 0) {
  1096. $data['permissions'] = $storage->getPermissions($data['path']);
  1097. $cache->update($data['fileid'], array('permissions' => $data['permissions']));
  1098. }
  1099. if ($includeMountPoints and $data['mimetype'] === 'httpd/unix-directory') {
  1100. //add the sizes of other mount points to the folder
  1101. $extOnly = ($includeMountPoints === 'ext');
  1102. $mountPoints = Filesystem::getMountPoints($path);
  1103. foreach ($mountPoints as $mountPoint) {
  1104. $subStorage = Filesystem::getStorage($mountPoint);
  1105. if ($subStorage) {
  1106. // exclude shared storage ?
  1107. if ($extOnly && $subStorage instanceof \OC\Files\Storage\Shared) {
  1108. continue;
  1109. }
  1110. $subCache = $subStorage->getCache('');
  1111. $rootEntry = $subCache->get('');
  1112. $data['size'] += isset($rootEntry['size']) ? $rootEntry['size'] : 0;
  1113. }
  1114. }
  1115. }
  1116. }
  1117. }
  1118. if (!$data) {
  1119. return false;
  1120. }
  1121. if ($mount instanceof MoveableMount && $internalPath === '') {
  1122. $data['permissions'] |= \OCP\Constants::PERMISSION_DELETE;
  1123. }
  1124. return new FileInfo($path, $storage, $internalPath, $data, $mount);
  1125. }
  1126. /**
  1127. * get the content of a directory
  1128. *
  1129. * @param string $directory path under datadirectory
  1130. * @param string $mimetype_filter limit returned content to this mimetype or mimepart
  1131. * @return FileInfo[]
  1132. */
  1133. public function getDirectoryContent($directory, $mimetype_filter = '') {
  1134. $this->assertPathLength($directory);
  1135. $result = array();
  1136. if (!Filesystem::isValidPath($directory)) {
  1137. return $result;
  1138. }
  1139. $path = $this->getAbsolutePath($directory);
  1140. $path = Filesystem::normalizePath($path);
  1141. $mount = $this->getMount($directory);
  1142. $storage = $mount->getStorage();
  1143. $internalPath = $mount->getInternalPath($path);
  1144. if ($storage) {
  1145. $cache = $storage->getCache($internalPath);
  1146. $user = \OC_User::getUser();
  1147. /**
  1148. * @var \OC\Files\FileInfo[] $files
  1149. */
  1150. $files = array();
  1151. try {
  1152. $this->lockFile($directory, ILockingProvider::LOCK_SHARED);
  1153. $data = $cache->get($internalPath);
  1154. $watcher = $storage->getWatcher($internalPath);
  1155. if (!$data or $data['size'] === -1) {
  1156. if (!$storage->file_exists($internalPath)) {
  1157. $this->unlockFile($directory, ILockingProvider::LOCK_SHARED);
  1158. return array();
  1159. }
  1160. $scanner = $storage->getScanner($internalPath);
  1161. $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW);
  1162. $data = $cache->get($internalPath);
  1163. } else if ($watcher->checkUpdate($internalPath, $data)) {
  1164. $this->updater->propagate($path);
  1165. $data = $cache->get($internalPath);
  1166. }
  1167. $folderId = $data['fileid'];
  1168. $contents = $cache->getFolderContentsById($folderId); //TODO: mimetype_filter
  1169. $this->unlockFile($directory, ILockingProvider::LOCK_SHARED);
  1170. } catch (LockedException $e) {
  1171. // dont try to update the cache when the file is locked
  1172. $contents = $cache->getFolderContents($internalPath);
  1173. }
  1174. foreach ($contents as $content) {
  1175. if ($content['permissions'] === 0) {
  1176. $content['permissions'] = $storage->getPermissions($content['path']);
  1177. $cache->update($content['fileid'], array('permissions' => $content['permissions']));
  1178. }
  1179. // if sharing was disabled for the user we remove the share permissions
  1180. if (\OCP\Util::isSharingDisabledForUser()) {
  1181. $content['permissions'] = $content['permissions'] & ~\OCP\Constants::PERMISSION_SHARE;
  1182. }
  1183. $files[] = new FileInfo($path . '/' . $content['name'], $storage, $content['path'], $content, $mount);
  1184. }
  1185. //add a folder for any mountpoint in this directory and add the sizes of other mountpoints to the folders
  1186. $mounts = Filesystem::getMountManager()->findIn($path);
  1187. $dirLength = strlen($path);
  1188. foreach ($mounts as $mount) {
  1189. $mountPoint = $mount->getMountPoint();
  1190. $subStorage = $mount->getStorage();
  1191. if ($subStorage) {
  1192. $subCache = $subStorage->getCache('');
  1193. if ($subCache->getStatus('') === Cache\Cache::NOT_FOUND) {
  1194. $subScanner = $subStorage->getScanner('');
  1195. try {
  1196. $subScanner->scanFile('');
  1197. } catch (\OCP\Files\StorageNotAvailableException $e) {
  1198. continue;
  1199. } catch (\OCP\Files\StorageInvalidException $e) {
  1200. continue;
  1201. } catch (\Exception $e) {
  1202. // sometimes when the storage is not available it can be any exception
  1203. \OCP\Util::writeLog(
  1204. 'core',
  1205. 'Exception while scanning storage "' . $subStorage->getId() . '": ' .
  1206. get_class($e) . ': ' . $e->getMessage(),
  1207. \OCP\Util::ERROR
  1208. );
  1209. continue;
  1210. }
  1211. }
  1212. $rootEntry = $subCache->get('');
  1213. if ($rootEntry) {
  1214. $relativePath = trim(substr($mountPoint, $dirLength), '/');
  1215. if ($pos = strpos($relativePath, '/')) {
  1216. //mountpoint inside subfolder add size to the correct folder
  1217. $entryName = substr($relativePath, 0, $pos);
  1218. foreach ($files as &$entry) {
  1219. if ($entry['name'] === $entryName) {
  1220. $entry['size'] += $rootEntry['size'];
  1221. }
  1222. }
  1223. } else { //mountpoint in this folder, add an entry for it
  1224. $rootEntry['name'] = $relativePath;
  1225. $rootEntry['type'] = $rootEntry['mimetype'] === 'httpd/unix-directory' ? 'dir' : 'file';
  1226. $permissions = $rootEntry['permissions'];
  1227. // do not allow renaming/deleting the mount point if they are not shared files/folders
  1228. // for shared files/folders we use the permissions given by the owner
  1229. if ($mount instanceof MoveableMount) {
  1230. $rootEntry['permissions'] = $permissions | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE;
  1231. } else {
  1232. $rootEntry['permissions'] = $permissions & (\OCP\Constants::PERMISSION_ALL - (\OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE));
  1233. }
  1234. //remove any existing entry with the same name
  1235. foreach ($files as $i => $file) {
  1236. if ($file['name'] === $rootEntry['name']) {
  1237. unset($files[$i]);
  1238. break;
  1239. }
  1240. }
  1241. $rootEntry['path'] = substr(Filesystem::normalizePath($path . '/' . $rootEntry['name']), strlen($user) + 2); // full path without /$user/
  1242. // if sharing was disabled for the user we remove the share permissions
  1243. if (\OCP\Util::isSharingDisabledForUser()) {
  1244. $rootEntry['permissions'] = $rootEntry['permissions'] & ~\OCP\Constants::PERMISSION_SHARE;
  1245. }
  1246. $files[] = new FileInfo($path . '/' . $rootEntry['name'], $subStorage, '', $rootEntry, $mount);
  1247. }
  1248. }
  1249. }
  1250. }
  1251. if ($mimetype_filter) {
  1252. foreach ($files as $file) {
  1253. if (strpos($mimetype_filter, '/')) {
  1254. if ($file['mimetype'] === $mimetype_filter) {
  1255. $result[] = $file;
  1256. }
  1257. } else {
  1258. if ($file['mimepart'] === $mimetype_filter) {
  1259. $result[] = $file;
  1260. }
  1261. }
  1262. }
  1263. } else {
  1264. $result = $files;
  1265. }
  1266. }
  1267. return $result;
  1268. }
  1269. /**
  1270. * change file metadata
  1271. *
  1272. * @param string $path
  1273. * @param array|\OCP\Files\FileInfo $data
  1274. * @return int
  1275. *
  1276. * returns the fileid of the updated file
  1277. */
  1278. public function putFileInfo($path, $data) {
  1279. $this->assertPathLength($path);
  1280. if ($data instanceof FileInfo) {
  1281. $data = $data->getData();
  1282. }
  1283. $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path);
  1284. /**
  1285. * @var \OC\Files\Storage\Storage $storage
  1286. * @var string $internalPath
  1287. */
  1288. list($storage, $internalPath) = Filesystem::resolvePath($path);
  1289. if ($storage) {
  1290. $cache = $storage->getCache($path);
  1291. if (!$cache->inCache($internalPath)) {
  1292. $scanner = $storage->getScanner($internalPath);
  1293. $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW);
  1294. }
  1295. return $cache->put($internalPath, $data);
  1296. } else {
  1297. return -1;
  1298. }
  1299. }
  1300. /**
  1301. * search for files with the name matching $query
  1302. *
  1303. * @param string $query
  1304. * @return FileInfo[]
  1305. */
  1306. public function search($query) {
  1307. return $this->searchCommon('search', array('%' . $query . '%'));
  1308. }
  1309. /**
  1310. * search for files with the name matching $query
  1311. *
  1312. * @param string $query
  1313. * @return FileInfo[]
  1314. */
  1315. public function searchRaw($query) {
  1316. return $this->searchCommon('search', array($query));
  1317. }
  1318. /**
  1319. * search for files by mimetype
  1320. *
  1321. * @param string $mimetype
  1322. * @return FileInfo[]
  1323. */
  1324. public function searchByMime($mimetype) {
  1325. return $this->searchCommon('searchByMime', array($mimetype));
  1326. }
  1327. /**
  1328. * search for files by tag
  1329. *
  1330. * @param string|int $tag name or tag id
  1331. * @param string $userId owner of the tags
  1332. * @return FileInfo[]
  1333. */
  1334. public function searchByTag($tag, $userId) {
  1335. return $this->searchCommon('searchByTag', array($tag, $userId));
  1336. }
  1337. /**
  1338. * @param string $method cache method
  1339. * @param array $args
  1340. * @return FileInfo[]
  1341. */
  1342. private function searchCommon($method, $args) {
  1343. $files = array();
  1344. $rootLength = strlen($this->fakeRoot);
  1345. $mount = $this->getMount('');
  1346. $mountPoint = $mount->getMountPoint();
  1347. $storage = $mount->getStorage();
  1348. if ($storage) {
  1349. $cache = $storage->getCache('');
  1350. $results = call_user_func_array(array($cache, $method), $args);
  1351. foreach ($results as $result) {
  1352. if (substr($mountPoint . $result['path'], 0, $rootLength + 1) === $this->fakeRoot . '/') {
  1353. $internalPath = $result['path'];
  1354. $path = $mountPoint . $result['path'];
  1355. $result['path'] = substr($mountPoint . $result['path'], $rootLength);
  1356. $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount);
  1357. }
  1358. }
  1359. $mounts = Filesystem::getMountManager()->findIn($this->fakeRoot);
  1360. foreach ($mounts as $mount) {
  1361. $mountPoint = $mount->getMountPoint();
  1362. $storage = $mount->getStorage();
  1363. if ($storage) {
  1364. $cache = $storage->getCache('');
  1365. $relativeMountPoint = substr($mountPoint, $rootLength);
  1366. $results = call_user_func_array(array($cache, $method), $args);
  1367. if ($results) {
  1368. foreach ($results as $result) {
  1369. $internalPath = $result['path'];
  1370. $result['path'] = rtrim($relativeMountPoint . $result['path'], '/');
  1371. $path = rtrim($mountPoint . $internalPath, '/');
  1372. $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount);
  1373. }
  1374. }
  1375. }
  1376. }
  1377. }
  1378. return $files;
  1379. }
  1380. /**
  1381. * Get the owner for a file or folder
  1382. *
  1383. * @param string $path
  1384. * @return string
  1385. */
  1386. public function getOwner($path) {
  1387. return $this->basicOperation('getOwner', $path);
  1388. }
  1389. /**
  1390. * get the ETag for a file or folder
  1391. *
  1392. * @param string $path
  1393. * @return string
  1394. */
  1395. public function getETag($path) {
  1396. /**
  1397. * @var Storage\Storage $storage
  1398. * @var string $internalPath
  1399. */
  1400. list($storage, $internalPath) = $this->resolvePath($path);
  1401. if ($storage) {
  1402. return $storage->getETag($internalPath);
  1403. } else {
  1404. return null;
  1405. }
  1406. }
  1407. /**
  1408. * Get the path of a file by id, relative to the view
  1409. *
  1410. * Note that the resulting path is not guarantied to be unique for the id, multiple paths can point to the same file
  1411. *
  1412. * @param int $id
  1413. * @return string|null
  1414. */
  1415. public function getPath($id) {
  1416. $id = (int)$id;
  1417. $manager = Filesystem::getMountManager();
  1418. $mounts = $manager->findIn($this->fakeRoot);
  1419. $mounts[] = $manager->find($this->fakeRoot);
  1420. // reverse the array so we start with the storage this view is in
  1421. // which is the most likely to contain the file we're looking for
  1422. $mounts = array_reverse($mounts);
  1423. foreach ($mounts as $mount) {
  1424. /**
  1425. * @var \OC\Files\Mount\MountPoint $mount
  1426. */
  1427. if ($mount->getStorage()) {
  1428. $cache = $mount->getStorage()->getCache();
  1429. $internalPath = $cache->getPathById($id);
  1430. if (is_string($internalPath)) {
  1431. $fullPath = $mount->getMountPoint() . $internalPath;
  1432. if (!is_null($path = $this->getRelativePath($fullPath))) {
  1433. return $path;
  1434. }
  1435. }
  1436. }
  1437. }
  1438. return null;
  1439. }
  1440. private function assertPathLength($path) {
  1441. $maxLen = min(PHP_MAXPATHLEN, 4000);
  1442. // Check for the string length - performed using isset() instead of strlen()
  1443. // because isset() is about 5x-40x faster.
  1444. if (isset($path[$maxLen])) {
  1445. $pathLen = strlen($path);
  1446. throw new \OCP\Files\InvalidPathException("Path length($pathLen) exceeds max path length($maxLen): $path");
  1447. }
  1448. }
  1449. /**
  1450. * check if it is allowed to move a mount point to a given target.
  1451. * It is not allowed to move a mount point into a different mount point
  1452. *
  1453. * @param string $target path
  1454. * @return boolean
  1455. */
  1456. private function isTargetAllowed($target) {
  1457. $result = false;
  1458. list($targetStorage,) = \OC\Files\Filesystem::resolvePath($target);
  1459. if ($targetStorage->instanceOfStorage('\OCP\Files\IHomeStorage')) {
  1460. $result = true;
  1461. } else {
  1462. \OCP\Util::writeLog('files',
  1463. 'It is not allowed to move one mount point into another one',
  1464. \OCP\Util::DEBUG);
  1465. }
  1466. return $result;
  1467. }
  1468. /**
  1469. * Get a fileinfo object for files that are ignored in the cache (part files)
  1470. *
  1471. * @param string $path
  1472. * @return \OCP\Files\FileInfo
  1473. */
  1474. private function getPartFileInfo($path) {
  1475. $mount = $this->getMount($path);
  1476. $storage = $mount->getStorage();
  1477. $internalPath = $mount->getInternalPath($this->getAbsolutePath($path));
  1478. return new FileInfo(
  1479. $this->getAbsolutePath($path),
  1480. $storage,
  1481. $internalPath,
  1482. [
  1483. 'fileid' => null,
  1484. 'mimetype' => $storage->getMimeType($internalPath),
  1485. 'name' => basename($path),
  1486. 'etag' => null,
  1487. 'size' => $storage->filesize($internalPath),
  1488. 'mtime' => $storage->filemtime($internalPath),
  1489. 'encrypted' => false,
  1490. 'permissions' => \OCP\Constants::PERMISSION_ALL
  1491. ],
  1492. $mount
  1493. );
  1494. }
  1495. /**
  1496. * @return Updater
  1497. */
  1498. public function getUpdater() {
  1499. return $this->updater;
  1500. }
  1501. /**
  1502. * @param string $path
  1503. * @param string $fileName
  1504. * @throws InvalidPathException
  1505. */
  1506. public function verifyPath($path, $fileName) {
  1507. $l10n = \OC::$server->getL10N('lib');
  1508. // verify empty and dot files
  1509. $trimmed = trim($fileName);
  1510. if ($trimmed === '') {
  1511. throw new InvalidPathException($l10n->t('Empty filename is not allowed'));
  1512. }
  1513. if ($trimmed === '.' || $trimmed === '..') {
  1514. throw new InvalidPathException($l10n->t('Dot files are not allowed'));
  1515. }
  1516. // verify database - e.g. mysql only 3-byte chars
  1517. if (preg_match('%(?:
  1518. \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
  1519. | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
  1520. | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
  1521. )%xs', $fileName)) {
  1522. throw new InvalidPathException($l10n->t('4-byte characters are not supported in file names'));
  1523. }
  1524. try {
  1525. /** @type \OCP\Files\Storage $storage */
  1526. list($storage, $internalPath) = $this->resolvePath($path);
  1527. $storage->verifyPath($internalPath, $fileName);
  1528. } catch (ReservedWordException $ex) {
  1529. throw new InvalidPathException($l10n->t('File name is a reserved word'));
  1530. } catch (InvalidCharacterInPathException $ex) {
  1531. throw new InvalidPathException($l10n->t('File name contains at least one invalid character'));
  1532. } catch (FileNameTooLongException $ex) {
  1533. throw new InvalidPathException($l10n->t('File name is too long'));
  1534. }
  1535. }
  1536. /**
  1537. * get all parent folders of $path
  1538. *
  1539. * @param string $path
  1540. * @return string[]
  1541. */
  1542. private function getParents($path) {
  1543. $path = trim($path, '/');
  1544. if (!$path) {
  1545. return [];
  1546. }
  1547. $parts = explode('/', $path);
  1548. // remove the single file
  1549. array_pop($parts);
  1550. $result = array('/');
  1551. $resultPath = '';
  1552. foreach ($parts as $part) {
  1553. if ($part) {
  1554. $resultPath .= '/' . $part;
  1555. $result[] = $resultPath;
  1556. }
  1557. }
  1558. return $result;
  1559. }
  1560. /**
  1561. * Returns the mount point for which to lock
  1562. *
  1563. * @param string $absolutePath absolute path
  1564. * @param bool $useParentMount true to return parent mount instead of whatever
  1565. * is mounted directly on the given path, false otherwise
  1566. * @return \OC\Files\Mount\MountPoint mount point for which to apply locks
  1567. */
  1568. private function getMountForLock($absolutePath, $useParentMount = false) {
  1569. $results = [];
  1570. $mount = Filesystem::getMountManager()->find($absolutePath);
  1571. if (!$mount) {
  1572. return $results;
  1573. }
  1574. if ($useParentMount) {
  1575. // find out if something is mounted directly on the path
  1576. $internalPath = $mount->getInternalPath($absolutePath);
  1577. if ($internalPath === '') {
  1578. // resolve the parent mount instead
  1579. $mount = Filesystem::getMountManager()->find(dirname($absolutePath));
  1580. }
  1581. }
  1582. return $mount;
  1583. }
  1584. /**
  1585. * Lock the given path
  1586. *
  1587. * @param string $path the path of the file to lock, relative to the view
  1588. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  1589. * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage
  1590. *
  1591. * @return bool False if the path is excluded from locking, true otherwise
  1592. * @throws \OCP\Lock\LockedException if the path is already locked
  1593. */
  1594. private function lockPath($path, $type, $lockMountPoint = false) {
  1595. $absolutePath = $this->getAbsolutePath($path);
  1596. $absolutePath = Filesystem::normalizePath($absolutePath);
  1597. if (!$this->shouldLockFile($absolutePath)) {
  1598. return false;
  1599. }
  1600. $mount = $this->getMountForLock($absolutePath, $lockMountPoint);
  1601. if ($mount) {
  1602. try {
  1603. $mount->getStorage()->acquireLock(
  1604. $mount->getInternalPath($absolutePath),
  1605. $type,
  1606. $this->lockingProvider
  1607. );
  1608. } catch (\OCP\Lock\LockedException $e) {
  1609. // rethrow with the a human-readable path
  1610. throw new \OCP\Lock\LockedException(
  1611. $this->getPathRelativeToFiles($absolutePath),
  1612. $e
  1613. );
  1614. }
  1615. }
  1616. return true;
  1617. }
  1618. /**
  1619. * Change the lock type
  1620. *
  1621. * @param string $path the path of the file to lock, relative to the view
  1622. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  1623. * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage
  1624. *
  1625. * @return bool False if the path is excluded from locking, true otherwise
  1626. * @throws \OCP\Lock\LockedException if the path is already locked
  1627. */
  1628. public function changeLock($path, $type, $lockMountPoint = false) {
  1629. $path = Filesystem::normalizePath($path);
  1630. $absolutePath = $this->getAbsolutePath($path);
  1631. $absolutePath = Filesystem::normalizePath($absolutePath);
  1632. if (!$this->shouldLockFile($absolutePath)) {
  1633. return false;
  1634. }
  1635. $mount = $this->getMountForLock($absolutePath, $lockMountPoint);
  1636. if ($mount) {
  1637. try {
  1638. $mount->getStorage()->changeLock(
  1639. $mount->getInternalPath($absolutePath),
  1640. $type,
  1641. $this->lockingProvider
  1642. );
  1643. } catch (\OCP\Lock\LockedException $e) {
  1644. // rethrow with the a human-readable path
  1645. throw new \OCP\Lock\LockedException(
  1646. $this->getPathRelativeToFiles($absolutePath),
  1647. $e
  1648. );
  1649. }
  1650. }
  1651. return true;
  1652. }
  1653. /**
  1654. * Unlock the given path
  1655. *
  1656. * @param string $path the path of the file to unlock, relative to the view
  1657. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  1658. * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage
  1659. *
  1660. * @return bool False if the path is excluded from locking, true otherwise
  1661. */
  1662. private function unlockPath($path, $type, $lockMountPoint = false) {
  1663. $absolutePath = $this->getAbsolutePath($path);
  1664. $absolutePath = Filesystem::normalizePath($absolutePath);
  1665. if (!$this->shouldLockFile($absolutePath)) {
  1666. return false;
  1667. }
  1668. $mount = $this->getMountForLock($absolutePath, $lockMountPoint);
  1669. if ($mount) {
  1670. $mount->getStorage()->releaseLock(
  1671. $mount->getInternalPath($absolutePath),
  1672. $type,
  1673. $this->lockingProvider
  1674. );
  1675. }
  1676. return true;
  1677. }
  1678. /**
  1679. * Lock a path and all its parents up to the root of the view
  1680. *
  1681. * @param string $path the path of the file to lock relative to the view
  1682. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  1683. * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage
  1684. *
  1685. * @return bool False if the path is excluded from locking, true otherwise
  1686. */
  1687. public function lockFile($path, $type, $lockMountPoint = false) {
  1688. $absolutePath = $this->getAbsolutePath($path);
  1689. $absolutePath = Filesystem::normalizePath($absolutePath);
  1690. if (!$this->shouldLockFile($absolutePath)) {
  1691. return false;
  1692. }
  1693. $this->lockPath($path, $type, $lockMountPoint);
  1694. $parents = $this->getParents($path);
  1695. foreach ($parents as $parent) {
  1696. $this->lockPath($parent, ILockingProvider::LOCK_SHARED);
  1697. }
  1698. return true;
  1699. }
  1700. /**
  1701. * Unlock a path and all its parents up to the root of the view
  1702. *
  1703. * @param string $path the path of the file to lock relative to the view
  1704. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  1705. * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage
  1706. *
  1707. * @return bool False if the path is excluded from locking, true otherwise
  1708. */
  1709. public function unlockFile($path, $type, $lockMountPoint = false) {
  1710. $absolutePath = $this->getAbsolutePath($path);
  1711. $absolutePath = Filesystem::normalizePath($absolutePath);
  1712. if (!$this->shouldLockFile($absolutePath)) {
  1713. return false;
  1714. }
  1715. $this->unlockPath($path, $type, $lockMountPoint);
  1716. $parents = $this->getParents($path);
  1717. foreach ($parents as $parent) {
  1718. $this->unlockPath($parent, ILockingProvider::LOCK_SHARED);
  1719. }
  1720. return true;
  1721. }
  1722. /**
  1723. * Only lock files in data/user/files/
  1724. *
  1725. * @param string $path Absolute path to the file/folder we try to (un)lock
  1726. * @return bool
  1727. */
  1728. protected function shouldLockFile($path) {
  1729. $path = Filesystem::normalizePath($path);
  1730. $pathSegments = explode('/', $path);
  1731. if (isset($pathSegments[2])) {
  1732. // E.g.: /username/files/path-to-file
  1733. return $pathSegments[2] === 'files';
  1734. }
  1735. return true;
  1736. }
  1737. /**
  1738. * Shortens the given absolute path to be relative to
  1739. * "$user/files".
  1740. *
  1741. * @param string $absolutePath absolute path which is under "files"
  1742. *
  1743. * @return string path relative to "files" with trimmed slashes or null
  1744. * if the path was NOT relative to files
  1745. *
  1746. * @throws \InvalidArgumentException if the given path was not under "files"
  1747. * @since 8.1.0
  1748. */
  1749. public function getPathRelativeToFiles($absolutePath) {
  1750. $path = Filesystem::normalizePath($absolutePath);
  1751. $parts = explode('/', trim($path, '/'), 3);
  1752. // "$user", "files", "path/to/dir"
  1753. if (!isset($parts[1]) || $parts[1] !== 'files') {
  1754. throw new \InvalidArgumentException('$absolutePath must be relative to "files"');
  1755. }
  1756. if (isset($parts[2])) {
  1757. return $parts[2];
  1758. }
  1759. return '';
  1760. }
  1761. }