Filesystem.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bart Visscher <bartv@thisnet.nl>
  7. * @author Christopher Schäpers <kondou@ts.unde.re>
  8. * @author Florin Peter <github@florin-peter.de>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  11. * @author Lukas Reschke <lukas@statuscode.ch>
  12. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  13. * @author Morris Jobke <hey@morrisjobke.de>
  14. * @author Robin Appelman <robin@icewind.nl>
  15. * @author Robin McCorkell <robin@mccorkell.me.uk>
  16. * @author Roeland Jago Douma <roeland@famdouma.nl>
  17. * @author Sam Tuke <mail@samtuke.com>
  18. * @author Stephan Peijnik <speijnik@anexia-it.com>
  19. * @author Vincent Petry <pvince81@owncloud.com>
  20. *
  21. * @license AGPL-3.0
  22. *
  23. * This code is free software: you can redistribute it and/or modify
  24. * it under the terms of the GNU Affero General Public License, version 3,
  25. * as published by the Free Software Foundation.
  26. *
  27. * This program is distributed in the hope that it will be useful,
  28. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30. * GNU Affero General Public License for more details.
  31. *
  32. * You should have received a copy of the GNU Affero General Public License, version 3,
  33. * along with this program. If not, see <http://www.gnu.org/licenses/>
  34. *
  35. */
  36. /**
  37. * Class for abstraction of filesystem functions
  38. * This class won't call any filesystem functions for itself but will pass them to the correct OC_Filestorage object
  39. * this class should also handle all the file permission related stuff
  40. *
  41. * Hooks provided:
  42. * read(path)
  43. * write(path, &run)
  44. * post_write(path)
  45. * create(path, &run) (when a file is created, both create and write will be emitted in that order)
  46. * post_create(path)
  47. * delete(path, &run)
  48. * post_delete(path)
  49. * rename(oldpath,newpath, &run)
  50. * post_rename(oldpath,newpath)
  51. * copy(oldpath,newpath, &run) (if the newpath doesn't exists yes, copy, create and write will be emitted in that order)
  52. * post_rename(oldpath,newpath)
  53. * post_initMountPoints(user, user_dir)
  54. *
  55. * the &run parameter can be set to false to prevent the operation from occurring
  56. */
  57. namespace OC\Files;
  58. use OC\Cache\CappedMemoryCache;
  59. use OC\Files\Config\MountProviderCollection;
  60. use OC\Files\Mount\MountPoint;
  61. use OC\Files\Storage\StorageFactory;
  62. use OC\Lockdown\Filesystem\NullStorage;
  63. use OCP\Files\Config\IMountProvider;
  64. use OCP\Files\NotFoundException;
  65. use OCP\IUserManager;
  66. class Filesystem {
  67. /**
  68. * @var Mount\Manager $mounts
  69. */
  70. private static $mounts;
  71. public static $loaded = false;
  72. /**
  73. * @var \OC\Files\View $defaultInstance
  74. */
  75. static private $defaultInstance;
  76. static private $usersSetup = array();
  77. static private $normalizedPathCache = null;
  78. static private $listeningForProviders = false;
  79. /**
  80. * classname which used for hooks handling
  81. * used as signalclass in OC_Hooks::emit()
  82. */
  83. const CLASSNAME = 'OC_Filesystem';
  84. /**
  85. * signalname emitted before file renaming
  86. *
  87. * @param string $oldpath
  88. * @param string $newpath
  89. */
  90. const signal_rename = 'rename';
  91. /**
  92. * signal emitted after file renaming
  93. *
  94. * @param string $oldpath
  95. * @param string $newpath
  96. */
  97. const signal_post_rename = 'post_rename';
  98. /**
  99. * signal emitted before file/dir creation
  100. *
  101. * @param string $path
  102. * @param bool $run changing this flag to false in hook handler will cancel event
  103. */
  104. const signal_create = 'create';
  105. /**
  106. * signal emitted after file/dir creation
  107. *
  108. * @param string $path
  109. * @param bool $run changing this flag to false in hook handler will cancel event
  110. */
  111. const signal_post_create = 'post_create';
  112. /**
  113. * signal emits before file/dir copy
  114. *
  115. * @param string $oldpath
  116. * @param string $newpath
  117. * @param bool $run changing this flag to false in hook handler will cancel event
  118. */
  119. const signal_copy = 'copy';
  120. /**
  121. * signal emits after file/dir copy
  122. *
  123. * @param string $oldpath
  124. * @param string $newpath
  125. */
  126. const signal_post_copy = 'post_copy';
  127. /**
  128. * signal emits before file/dir save
  129. *
  130. * @param string $path
  131. * @param bool $run changing this flag to false in hook handler will cancel event
  132. */
  133. const signal_write = 'write';
  134. /**
  135. * signal emits after file/dir save
  136. *
  137. * @param string $path
  138. */
  139. const signal_post_write = 'post_write';
  140. /**
  141. * signal emitted before file/dir update
  142. *
  143. * @param string $path
  144. * @param bool $run changing this flag to false in hook handler will cancel event
  145. */
  146. const signal_update = 'update';
  147. /**
  148. * signal emitted after file/dir update
  149. *
  150. * @param string $path
  151. * @param bool $run changing this flag to false in hook handler will cancel event
  152. */
  153. const signal_post_update = 'post_update';
  154. /**
  155. * signal emits when reading file/dir
  156. *
  157. * @param string $path
  158. */
  159. const signal_read = 'read';
  160. /**
  161. * signal emits when removing file/dir
  162. *
  163. * @param string $path
  164. */
  165. const signal_delete = 'delete';
  166. /**
  167. * parameters definitions for signals
  168. */
  169. const signal_param_path = 'path';
  170. const signal_param_oldpath = 'oldpath';
  171. const signal_param_newpath = 'newpath';
  172. /**
  173. * run - changing this flag to false in hook handler will cancel event
  174. */
  175. const signal_param_run = 'run';
  176. const signal_create_mount = 'create_mount';
  177. const signal_delete_mount = 'delete_mount';
  178. const signal_param_mount_type = 'mounttype';
  179. const signal_param_users = 'users';
  180. /**
  181. * @var \OC\Files\Storage\StorageFactory $loader
  182. */
  183. private static $loader;
  184. /** @var bool */
  185. private static $logWarningWhenAddingStorageWrapper = true;
  186. /**
  187. * @param bool $shouldLog
  188. * @return bool previous value
  189. * @internal
  190. */
  191. public static function logWarningWhenAddingStorageWrapper($shouldLog) {
  192. $previousValue = self::$logWarningWhenAddingStorageWrapper;
  193. self::$logWarningWhenAddingStorageWrapper = (bool) $shouldLog;
  194. return $previousValue;
  195. }
  196. /**
  197. * @param string $wrapperName
  198. * @param callable $wrapper
  199. * @param int $priority
  200. */
  201. public static function addStorageWrapper($wrapperName, $wrapper, $priority = 50) {
  202. if (self::$logWarningWhenAddingStorageWrapper) {
  203. \OC::$server->getLogger()->warning("Storage wrapper '{wrapper}' was not registered via the 'OC_Filesystem - preSetup' hook which could cause potential problems.", [
  204. 'wrapper' => $wrapperName,
  205. 'app' => 'filesystem',
  206. ]);
  207. }
  208. $mounts = self::getMountManager()->getAll();
  209. if (!self::getLoader()->addStorageWrapper($wrapperName, $wrapper, $priority, $mounts)) {
  210. // do not re-wrap if storage with this name already existed
  211. return;
  212. }
  213. }
  214. /**
  215. * Returns the storage factory
  216. *
  217. * @return \OCP\Files\Storage\IStorageFactory
  218. */
  219. public static function getLoader() {
  220. if (!self::$loader) {
  221. self::$loader = new StorageFactory();
  222. }
  223. return self::$loader;
  224. }
  225. /**
  226. * Returns the mount manager
  227. *
  228. * @return \OC\Files\Mount\Manager
  229. */
  230. public static function getMountManager($user = '') {
  231. if (!self::$mounts) {
  232. \OC_Util::setupFS($user);
  233. }
  234. return self::$mounts;
  235. }
  236. /**
  237. * get the mountpoint of the storage object for a path
  238. * ( note: because a storage is not always mounted inside the fakeroot, the
  239. * returned mountpoint is relative to the absolute root of the filesystem
  240. * and doesn't take the chroot into account )
  241. *
  242. * @param string $path
  243. * @return string
  244. */
  245. static public function getMountPoint($path) {
  246. if (!self::$mounts) {
  247. \OC_Util::setupFS();
  248. }
  249. $mount = self::$mounts->find($path);
  250. if ($mount) {
  251. return $mount->getMountPoint();
  252. } else {
  253. return '';
  254. }
  255. }
  256. /**
  257. * get a list of all mount points in a directory
  258. *
  259. * @param string $path
  260. * @return string[]
  261. */
  262. static public function getMountPoints($path) {
  263. if (!self::$mounts) {
  264. \OC_Util::setupFS();
  265. }
  266. $result = array();
  267. $mounts = self::$mounts->findIn($path);
  268. foreach ($mounts as $mount) {
  269. $result[] = $mount->getMountPoint();
  270. }
  271. return $result;
  272. }
  273. /**
  274. * get the storage mounted at $mountPoint
  275. *
  276. * @param string $mountPoint
  277. * @return \OC\Files\Storage\Storage
  278. */
  279. public static function getStorage($mountPoint) {
  280. if (!self::$mounts) {
  281. \OC_Util::setupFS();
  282. }
  283. $mount = self::$mounts->find($mountPoint);
  284. return $mount->getStorage();
  285. }
  286. /**
  287. * @param string $id
  288. * @return Mount\MountPoint[]
  289. */
  290. public static function getMountByStorageId($id) {
  291. if (!self::$mounts) {
  292. \OC_Util::setupFS();
  293. }
  294. return self::$mounts->findByStorageId($id);
  295. }
  296. /**
  297. * @param int $id
  298. * @return Mount\MountPoint[]
  299. */
  300. public static function getMountByNumericId($id) {
  301. if (!self::$mounts) {
  302. \OC_Util::setupFS();
  303. }
  304. return self::$mounts->findByNumericId($id);
  305. }
  306. /**
  307. * resolve a path to a storage and internal path
  308. *
  309. * @param string $path
  310. * @return array an array consisting of the storage and the internal path
  311. */
  312. static public function resolvePath($path) {
  313. if (!self::$mounts) {
  314. \OC_Util::setupFS();
  315. }
  316. $mount = self::$mounts->find($path);
  317. if ($mount) {
  318. return array($mount->getStorage(), rtrim($mount->getInternalPath($path), '/'));
  319. } else {
  320. return array(null, null);
  321. }
  322. }
  323. static public function init($user, $root) {
  324. if (self::$defaultInstance) {
  325. return false;
  326. }
  327. self::getLoader();
  328. self::$defaultInstance = new View($root);
  329. if (!self::$mounts) {
  330. self::$mounts = \OC::$server->getMountManager();
  331. }
  332. //load custom mount config
  333. self::initMountPoints($user);
  334. self::$loaded = true;
  335. return true;
  336. }
  337. static public function initMountManager() {
  338. if (!self::$mounts) {
  339. self::$mounts = \OC::$server->getMountManager();
  340. }
  341. }
  342. /**
  343. * Initialize system and personal mount points for a user
  344. *
  345. * @param string $user
  346. * @throws \OC\User\NoUserException if the user is not available
  347. */
  348. public static function initMountPoints($user = '') {
  349. if ($user == '') {
  350. $user = \OC_User::getUser();
  351. }
  352. if ($user === null || $user === false || $user === '') {
  353. throw new \OC\User\NoUserException('Attempted to initialize mount points for null user and no user in session');
  354. }
  355. if (isset(self::$usersSetup[$user])) {
  356. return;
  357. }
  358. self::$usersSetup[$user] = true;
  359. $userManager = \OC::$server->getUserManager();
  360. $userObject = $userManager->get($user);
  361. if (is_null($userObject)) {
  362. \OCP\Util::writeLog('files', ' Backends provided no user object for ' . $user, \OCP\Util::ERROR);
  363. // reset flag, this will make it possible to rethrow the exception if called again
  364. unset(self::$usersSetup[$user]);
  365. throw new \OC\User\NoUserException('Backends provided no user object for ' . $user);
  366. }
  367. $realUid = $userObject->getUID();
  368. // workaround in case of different casings
  369. if ($user !== $realUid) {
  370. $stack = json_encode(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 50));
  371. \OCP\Util::writeLog('files', 'initMountPoints() called with wrong user casing. This could be a bug. Expected: "' . $realUid . '" got "' . $user . '". Stack: ' . $stack, \OCP\Util::WARN);
  372. $user = $realUid;
  373. // again with the correct casing
  374. if (isset(self::$usersSetup[$user])) {
  375. return;
  376. }
  377. self::$usersSetup[$user] = true;
  378. }
  379. if (\OC::$server->getLockdownManager()->canAccessFilesystem()) {
  380. /** @var \OC\Files\Config\MountProviderCollection $mountConfigManager */
  381. $mountConfigManager = \OC::$server->getMountProviderCollection();
  382. // home mounts are handled seperate since we need to ensure this is mounted before we call the other mount providers
  383. $homeMount = $mountConfigManager->getHomeMountForUser($userObject);
  384. self::getMountManager()->addMount($homeMount);
  385. \OC\Files\Filesystem::getStorage($user);
  386. // Chance to mount for other storages
  387. if ($userObject) {
  388. $mounts = $mountConfigManager->addMountForUser($userObject, self::getMountManager());
  389. $mounts[] = $homeMount;
  390. $mountConfigManager->registerMounts($userObject, $mounts);
  391. }
  392. self::listenForNewMountProviders($mountConfigManager, $userManager);
  393. } else {
  394. self::getMountManager()->addMount(new MountPoint(
  395. new NullStorage([]),
  396. '/' . $user
  397. ));
  398. self::getMountManager()->addMount(new MountPoint(
  399. new NullStorage([]),
  400. '/' . $user . '/files'
  401. ));
  402. }
  403. \OC_Hook::emit('OC_Filesystem', 'post_initMountPoints', array('user' => $user));
  404. }
  405. /**
  406. * Get mounts from mount providers that are registered after setup
  407. *
  408. * @param MountProviderCollection $mountConfigManager
  409. * @param IUserManager $userManager
  410. */
  411. private static function listenForNewMountProviders(MountProviderCollection $mountConfigManager, IUserManager $userManager) {
  412. if (!self::$listeningForProviders) {
  413. self::$listeningForProviders = true;
  414. $mountConfigManager->listen('\OC\Files\Config', 'registerMountProvider', function (IMountProvider $provider) use ($userManager) {
  415. foreach (Filesystem::$usersSetup as $user => $setup) {
  416. $userObject = $userManager->get($user);
  417. if ($userObject) {
  418. $mounts = $provider->getMountsForUser($userObject, Filesystem::getLoader());
  419. array_walk($mounts, array(self::$mounts, 'addMount'));
  420. }
  421. }
  422. });
  423. }
  424. }
  425. /**
  426. * get the default filesystem view
  427. *
  428. * @return View
  429. */
  430. static public function getView() {
  431. return self::$defaultInstance;
  432. }
  433. /**
  434. * tear down the filesystem, removing all storage providers
  435. */
  436. static public function tearDown() {
  437. self::clearMounts();
  438. self::$defaultInstance = null;
  439. }
  440. /**
  441. * get the relative path of the root data directory for the current user
  442. *
  443. * @return string
  444. *
  445. * Returns path like /admin/files
  446. */
  447. static public function getRoot() {
  448. if (!self::$defaultInstance) {
  449. return null;
  450. }
  451. return self::$defaultInstance->getRoot();
  452. }
  453. /**
  454. * clear all mounts and storage backends
  455. */
  456. public static function clearMounts() {
  457. if (self::$mounts) {
  458. self::$usersSetup = array();
  459. self::$mounts->clear();
  460. }
  461. }
  462. /**
  463. * mount an \OC\Files\Storage\Storage in our virtual filesystem
  464. *
  465. * @param \OC\Files\Storage\Storage|string $class
  466. * @param array $arguments
  467. * @param string $mountpoint
  468. */
  469. static public function mount($class, $arguments, $mountpoint) {
  470. if (!self::$mounts) {
  471. \OC_Util::setupFS();
  472. }
  473. $mount = new Mount\MountPoint($class, $mountpoint, $arguments, self::getLoader());
  474. self::$mounts->addMount($mount);
  475. }
  476. /**
  477. * return the path to a local version of the file
  478. * we need this because we can't know if a file is stored local or not from
  479. * outside the filestorage and for some purposes a local file is needed
  480. *
  481. * @param string $path
  482. * @return string
  483. */
  484. static public function getLocalFile($path) {
  485. return self::$defaultInstance->getLocalFile($path);
  486. }
  487. /**
  488. * @param string $path
  489. * @return string
  490. */
  491. static public function getLocalFolder($path) {
  492. return self::$defaultInstance->getLocalFolder($path);
  493. }
  494. /**
  495. * return path to file which reflects one visible in browser
  496. *
  497. * @param string $path
  498. * @return string
  499. */
  500. static public function getLocalPath($path) {
  501. $datadir = \OC_User::getHome(\OC_User::getUser()) . '/files';
  502. $newpath = $path;
  503. if (strncmp($newpath, $datadir, strlen($datadir)) == 0) {
  504. $newpath = substr($path, strlen($datadir));
  505. }
  506. return $newpath;
  507. }
  508. /**
  509. * check if the requested path is valid
  510. *
  511. * @param string $path
  512. * @return bool
  513. */
  514. static public function isValidPath($path) {
  515. $path = self::normalizePath($path);
  516. if (!$path || $path[0] !== '/') {
  517. $path = '/' . $path;
  518. }
  519. if (strpos($path, '/../') !== false || strrchr($path, '/') === '/..') {
  520. return false;
  521. }
  522. return true;
  523. }
  524. /**
  525. * checks if a file is blacklisted for storage in the filesystem
  526. * Listens to write and rename hooks
  527. *
  528. * @param array $data from hook
  529. */
  530. static public function isBlacklisted($data) {
  531. if (isset($data['path'])) {
  532. $path = $data['path'];
  533. } else if (isset($data['newpath'])) {
  534. $path = $data['newpath'];
  535. }
  536. if (isset($path)) {
  537. if (self::isFileBlacklisted($path)) {
  538. $data['run'] = false;
  539. }
  540. }
  541. }
  542. /**
  543. * @param string $filename
  544. * @return bool
  545. */
  546. static public function isFileBlacklisted($filename) {
  547. $filename = self::normalizePath($filename);
  548. $blacklist = \OC::$server->getConfig()->getSystemValue('blacklisted_files', array('.htaccess'));
  549. $filename = strtolower(basename($filename));
  550. return in_array($filename, $blacklist);
  551. }
  552. /**
  553. * check if the directory should be ignored when scanning
  554. * NOTE: the special directories . and .. would cause never ending recursion
  555. *
  556. * @param String $dir
  557. * @return boolean
  558. */
  559. static public function isIgnoredDir($dir) {
  560. if ($dir === '.' || $dir === '..') {
  561. return true;
  562. }
  563. return false;
  564. }
  565. /**
  566. * following functions are equivalent to their php builtin equivalents for arguments/return values.
  567. */
  568. static public function mkdir($path) {
  569. return self::$defaultInstance->mkdir($path);
  570. }
  571. static public function rmdir($path) {
  572. return self::$defaultInstance->rmdir($path);
  573. }
  574. static public function is_dir($path) {
  575. return self::$defaultInstance->is_dir($path);
  576. }
  577. static public function is_file($path) {
  578. return self::$defaultInstance->is_file($path);
  579. }
  580. static public function stat($path) {
  581. return self::$defaultInstance->stat($path);
  582. }
  583. static public function filetype($path) {
  584. return self::$defaultInstance->filetype($path);
  585. }
  586. static public function filesize($path) {
  587. return self::$defaultInstance->filesize($path);
  588. }
  589. static public function readfile($path) {
  590. return self::$defaultInstance->readfile($path);
  591. }
  592. static public function isCreatable($path) {
  593. return self::$defaultInstance->isCreatable($path);
  594. }
  595. static public function isReadable($path) {
  596. return self::$defaultInstance->isReadable($path);
  597. }
  598. static public function isUpdatable($path) {
  599. return self::$defaultInstance->isUpdatable($path);
  600. }
  601. static public function isDeletable($path) {
  602. return self::$defaultInstance->isDeletable($path);
  603. }
  604. static public function isSharable($path) {
  605. return self::$defaultInstance->isSharable($path);
  606. }
  607. static public function file_exists($path) {
  608. return self::$defaultInstance->file_exists($path);
  609. }
  610. static public function filemtime($path) {
  611. return self::$defaultInstance->filemtime($path);
  612. }
  613. static public function touch($path, $mtime = null) {
  614. return self::$defaultInstance->touch($path, $mtime);
  615. }
  616. /**
  617. * @return string
  618. */
  619. static public function file_get_contents($path) {
  620. return self::$defaultInstance->file_get_contents($path);
  621. }
  622. static public function file_put_contents($path, $data) {
  623. return self::$defaultInstance->file_put_contents($path, $data);
  624. }
  625. static public function unlink($path) {
  626. return self::$defaultInstance->unlink($path);
  627. }
  628. static public function rename($path1, $path2) {
  629. return self::$defaultInstance->rename($path1, $path2);
  630. }
  631. static public function copy($path1, $path2) {
  632. return self::$defaultInstance->copy($path1, $path2);
  633. }
  634. static public function fopen($path, $mode) {
  635. return self::$defaultInstance->fopen($path, $mode);
  636. }
  637. /**
  638. * @return string
  639. */
  640. static public function toTmpFile($path) {
  641. return self::$defaultInstance->toTmpFile($path);
  642. }
  643. static public function fromTmpFile($tmpFile, $path) {
  644. return self::$defaultInstance->fromTmpFile($tmpFile, $path);
  645. }
  646. static public function getMimeType($path) {
  647. return self::$defaultInstance->getMimeType($path);
  648. }
  649. static public function hash($type, $path, $raw = false) {
  650. return self::$defaultInstance->hash($type, $path, $raw);
  651. }
  652. static public function free_space($path = '/') {
  653. return self::$defaultInstance->free_space($path);
  654. }
  655. static public function search($query) {
  656. return self::$defaultInstance->search($query);
  657. }
  658. /**
  659. * @param string $query
  660. */
  661. static public function searchByMime($query) {
  662. return self::$defaultInstance->searchByMime($query);
  663. }
  664. /**
  665. * @param string|int $tag name or tag id
  666. * @param string $userId owner of the tags
  667. * @return FileInfo[] array or file info
  668. */
  669. static public function searchByTag($tag, $userId) {
  670. return self::$defaultInstance->searchByTag($tag, $userId);
  671. }
  672. /**
  673. * check if a file or folder has been updated since $time
  674. *
  675. * @param string $path
  676. * @param int $time
  677. * @return bool
  678. */
  679. static public function hasUpdated($path, $time) {
  680. return self::$defaultInstance->hasUpdated($path, $time);
  681. }
  682. /**
  683. * Fix common problems with a file path
  684. *
  685. * @param string $path
  686. * @param bool $stripTrailingSlash whether to strip the trailing slash
  687. * @param bool $isAbsolutePath whether the given path is absolute
  688. * @param bool $keepUnicode true to disable unicode normalization
  689. * @return string
  690. */
  691. public static function normalizePath($path, $stripTrailingSlash = true, $isAbsolutePath = false, $keepUnicode = false) {
  692. if (is_null(self::$normalizedPathCache)) {
  693. self::$normalizedPathCache = new CappedMemoryCache();
  694. }
  695. /**
  696. * FIXME: This is a workaround for existing classes and files which call
  697. * this function with another type than a valid string. This
  698. * conversion should get removed as soon as all existing
  699. * function calls have been fixed.
  700. */
  701. $path = (string)$path;
  702. $cacheKey = json_encode([$path, $stripTrailingSlash, $isAbsolutePath, $keepUnicode]);
  703. if (isset(self::$normalizedPathCache[$cacheKey])) {
  704. return self::$normalizedPathCache[$cacheKey];
  705. }
  706. if ($path == '') {
  707. return '/';
  708. }
  709. //normalize unicode if possible
  710. if (!$keepUnicode) {
  711. $path = \OC_Util::normalizeUnicode($path);
  712. }
  713. //no windows style slashes
  714. $path = str_replace('\\', '/', $path);
  715. //add leading slash
  716. if ($path[0] !== '/') {
  717. $path = '/' . $path;
  718. }
  719. // remove '/./'
  720. // ugly, but str_replace() can't replace them all in one go
  721. // as the replacement itself is part of the search string
  722. // which will only be found during the next iteration
  723. while (strpos($path, '/./') !== false) {
  724. $path = str_replace('/./', '/', $path);
  725. }
  726. // remove sequences of slashes
  727. $path = preg_replace('#/{2,}#', '/', $path);
  728. //remove trailing slash
  729. if ($stripTrailingSlash and strlen($path) > 1) {
  730. $path = rtrim($path, '/');
  731. }
  732. // remove trailing '/.'
  733. if (substr($path, -2) == '/.') {
  734. $path = substr($path, 0, -2);
  735. }
  736. $normalizedPath = $path;
  737. self::$normalizedPathCache[$cacheKey] = $normalizedPath;
  738. return $normalizedPath;
  739. }
  740. /**
  741. * get the filesystem info
  742. *
  743. * @param string $path
  744. * @param boolean $includeMountPoints whether to add mountpoint sizes,
  745. * defaults to true
  746. * @return \OC\Files\FileInfo|bool False if file does not exist
  747. */
  748. public static function getFileInfo($path, $includeMountPoints = true) {
  749. return self::$defaultInstance->getFileInfo($path, $includeMountPoints);
  750. }
  751. /**
  752. * change file metadata
  753. *
  754. * @param string $path
  755. * @param array $data
  756. * @return int
  757. *
  758. * returns the fileid of the updated file
  759. */
  760. public static function putFileInfo($path, $data) {
  761. return self::$defaultInstance->putFileInfo($path, $data);
  762. }
  763. /**
  764. * get the content of a directory
  765. *
  766. * @param string $directory path under datadirectory
  767. * @param string $mimetype_filter limit returned content to this mimetype or mimepart
  768. * @return \OC\Files\FileInfo[]
  769. */
  770. public static function getDirectoryContent($directory, $mimetype_filter = '') {
  771. return self::$defaultInstance->getDirectoryContent($directory, $mimetype_filter);
  772. }
  773. /**
  774. * Get the path of a file by id
  775. *
  776. * Note that the resulting path is not guaranteed to be unique for the id, multiple paths can point to the same file
  777. *
  778. * @param int $id
  779. * @throws NotFoundException
  780. * @return string
  781. */
  782. public static function getPath($id) {
  783. return self::$defaultInstance->getPath($id);
  784. }
  785. /**
  786. * Get the owner for a file or folder
  787. *
  788. * @param string $path
  789. * @return string
  790. */
  791. public static function getOwner($path) {
  792. return self::$defaultInstance->getOwner($path);
  793. }
  794. /**
  795. * get the ETag for a file or folder
  796. *
  797. * @param string $path
  798. * @return string
  799. */
  800. static public function getETag($path) {
  801. return self::$defaultInstance->getETag($path);
  802. }
  803. }