Filesystem.php 23 KB

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