Completed
Push — master ( 8931ba...9baa96 )
by Roeland
47:53
created

Filesystem::getStorage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2
Metric Value
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.4286
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * @author Arthur Schiwon <[email protected]>
4
 * @author Bart Visscher <[email protected]>
5
 * @author Christopher Schäpers <[email protected]>
6
 * @author Florin Peter <[email protected]>
7
 * @author Georg Ehrke <[email protected]>
8
 * @author Joas Schilling <[email protected]>
9
 * @author Jörn Friedrich Dreyer <[email protected]>
10
 * @author Lukas Reschke <[email protected]>
11
 * @author Michael Gapczynski <[email protected]>
12
 * @author Morris Jobke <[email protected]>
13
 * @author Robin Appelman <[email protected]>
14
 * @author Robin McCorkell <[email protected]>
15
 * @author Sam Tuke <[email protected]>
16
 * @author Scrutinizer Auto-Fixer <[email protected]>
17
 * @author Stephan Peijnik <[email protected]>
18
 * @author Vincent Petry <[email protected]>
19
 *
20
 * @copyright Copyright (c) 2015, ownCloud, Inc.
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
/**
38
 * Class for abstraction of filesystem functions
39
 * This class won't call any filesystem functions for itself but will pass them to the correct OC_Filestorage object
40
 * this class should also handle all the file permission related stuff
41
 *
42
 * Hooks provided:
43
 *   read(path)
44
 *   write(path, &run)
45
 *   post_write(path)
46
 *   create(path, &run) (when a file is created, both create and write will be emitted in that order)
47
 *   post_create(path)
48
 *   delete(path, &run)
49
 *   post_delete(path)
50
 *   rename(oldpath,newpath, &run)
51
 *   post_rename(oldpath,newpath)
52
 *   copy(oldpath,newpath, &run) (if the newpath doesn't exists yes, copy, create and write will be emitted in that order)
53
 *   post_rename(oldpath,newpath)
54
 *   post_initMountPoints(user, user_dir)
55
 *
56
 *   the &run parameter can be set to false to prevent the operation from occurring
57
 */
58
59
namespace OC\Files;
60
61
use OC\Cache\File;
62
use OC\Files\Config\MountProviderCollection;
63
use OC\Files\Storage\StorageFactory;
64
use OCP\Files\Config\IMountProvider;
65
use OCP\Files\NotFoundException;
66
use OCP\IUserManager;
67
68
class Filesystem {
69
70
	/**
71
	 * @var Mount\Manager $mounts
72
	 */
73
	private static $mounts;
74
75
	public static $loaded = false;
76
	/**
77
	 * @var \OC\Files\View $defaultInstance
78
	 */
79
	static private $defaultInstance;
80
81
	static private $usersSetup = array();
82
83
	static private $normalizedPathCache = array();
84
85
	static private $listeningForProviders = false;
86
87
	/**
88
	 * classname which used for hooks handling
89
	 * used as signalclass in OC_Hooks::emit()
90
	 */
91
	const CLASSNAME = 'OC_Filesystem';
92
93
	/**
94
	 * signalname emitted before file renaming
95
	 *
96
	 * @param string $oldpath
97
	 * @param string $newpath
98
	 */
99
	const signal_rename = 'rename';
100
101
	/**
102
	 * signal emitted after file renaming
103
	 *
104
	 * @param string $oldpath
105
	 * @param string $newpath
106
	 */
107
	const signal_post_rename = 'post_rename';
108
109
	/**
110
	 * signal emitted before file/dir creation
111
	 *
112
	 * @param string $path
113
	 * @param bool $run changing this flag to false in hook handler will cancel event
114
	 */
115
	const signal_create = 'create';
116
117
	/**
118
	 * signal emitted after file/dir creation
119
	 *
120
	 * @param string $path
121
	 * @param bool $run changing this flag to false in hook handler will cancel event
122
	 */
123
	const signal_post_create = 'post_create';
124
125
	/**
126
	 * signal emits before file/dir copy
127
	 *
128
	 * @param string $oldpath
129
	 * @param string $newpath
130
	 * @param bool $run changing this flag to false in hook handler will cancel event
131
	 */
132
	const signal_copy = 'copy';
133
134
	/**
135
	 * signal emits after file/dir copy
136
	 *
137
	 * @param string $oldpath
138
	 * @param string $newpath
139
	 */
140
	const signal_post_copy = 'post_copy';
141
142
	/**
143
	 * signal emits before file/dir save
144
	 *
145
	 * @param string $path
146
	 * @param bool $run changing this flag to false in hook handler will cancel event
147
	 */
148
	const signal_write = 'write';
149
150
	/**
151
	 * signal emits after file/dir save
152
	 *
153
	 * @param string $path
154
	 */
155
	const signal_post_write = 'post_write';
156
157
	/**
158
	 * signal emitted before file/dir update
159
	 *
160
	 * @param string $path
161
	 * @param bool $run changing this flag to false in hook handler will cancel event
162
	 */
163
	const signal_update = 'update';
164
165
	/**
166
	 * signal emitted after file/dir update
167
	 *
168
	 * @param string $path
169
	 * @param bool $run changing this flag to false in hook handler will cancel event
170
	 */
171
	const signal_post_update = 'post_update';
172
173
	/**
174
	 * signal emits when reading file/dir
175
	 *
176
	 * @param string $path
177
	 */
178
	const signal_read = 'read';
179
180
	/**
181
	 * signal emits when removing file/dir
182
	 *
183
	 * @param string $path
184
	 */
185
	const signal_delete = 'delete';
186
187
	/**
188
	 * parameters definitions for signals
189
	 */
190
	const signal_param_path = 'path';
191
	const signal_param_oldpath = 'oldpath';
192
	const signal_param_newpath = 'newpath';
193
194
	/**
195
	 * run - changing this flag to false in hook handler will cancel event
196
	 */
197
	const signal_param_run = 'run';
198
199
	const signal_create_mount = 'create_mount';
200
	const signal_delete_mount = 'delete_mount';
201
	const signal_param_mount_type = 'mounttype';
202
	const signal_param_users = 'users';
203
204
	/**
205
	 * @var \OC\Files\Storage\StorageFactory $loader
206
	 */
207
	private static $loader;
208
209
	/**
210
	 * @param string $wrapperName
211
	 * @param callable $wrapper
212
	 * @param int $priority
213
	 */
214 1071
	public static function addStorageWrapper($wrapperName, $wrapper, $priority = 50) {
215 1071
		$mounts = self::getMountManager()->getAll();
216 1071
		if (!self::getLoader()->addStorageWrapper($wrapperName, $wrapper, $priority, $mounts)) {
217
			// do not re-wrap if storage with this name already existed
218 1071
			return;
219
		}
220 30
	}
221
222
	/**
223
	 * Returns the storage factory
224
	 *
225
	 * @return \OCP\Files\Storage\IStorageFactory
226
	 */
227 1086
	public static function getLoader() {
228 1086
		if (!self::$loader) {
229
			self::$loader = new StorageFactory();
230
		}
231 1086
		return self::$loader;
232
	}
233
234
	/**
235
	 * Returns the mount manager
236
	 *
237
	 * @return \OC\Files\Mount\Manager
238
	 */
239 1099
	public static function getMountManager() {
240 1099
		if (!self::$mounts) {
241
			\OC_Util::setupFS();
242
		}
243 1099
		return self::$mounts;
244
	}
245
246
	/**
247
	 * get the mountpoint of the storage object for a path
248
	 * ( note: because a storage is not always mounted inside the fakeroot, the
249
	 * returned mountpoint is relative to the absolute root of the filesystem
250
	 * and doesn't take the chroot into account )
251
	 *
252
	 * @param string $path
253
	 * @return string
254
	 */
255 4
	static public function getMountPoint($path) {
256 4
		if (!self::$mounts) {
257
			\OC_Util::setupFS();
258
		}
259 4
		$mount = self::$mounts->find($path);
260 4
		if ($mount) {
261 4
			return $mount->getMountPoint();
262
		} else {
263
			return '';
264
		}
265
	}
266
267
	/**
268
	 * get a list of all mount points in a directory
269
	 *
270
	 * @param string $path
271
	 * @return string[]
272
	 */
273
	static public function getMountPoints($path) {
274
		if (!self::$mounts) {
275
			\OC_Util::setupFS();
276
		}
277
		$result = array();
278
		$mounts = self::$mounts->findIn($path);
279
		foreach ($mounts as $mount) {
280
			$result[] = $mount->getMountPoint();
281
		}
282
		return $result;
283
	}
284
285
	/**
286
	 * get the storage mounted at $mountPoint
287
	 *
288
	 * @param string $mountPoint
289
	 * @return \OC\Files\Storage\Storage
290
	 */
291 959
	public static function getStorage($mountPoint) {
292 959
		if (!self::$mounts) {
293 1
			\OC_Util::setupFS();
294 1
		}
295 959
		$mount = self::$mounts->find($mountPoint);
296 959
		return $mount->getStorage();
297
	}
298
299
	/**
300
	 * @param string $id
301
	 * @return Mount\MountPoint[]
302
	 */
303
	public static function getMountByStorageId($id) {
304
		if (!self::$mounts) {
305
			\OC_Util::setupFS();
306
		}
307
		return self::$mounts->findByStorageId($id);
308
	}
309
310
	/**
311
	 * @param int $id
312
	 * @return Mount\MountPoint[]
313
	 */
314 108
	public static function getMountByNumericId($id) {
315 108
		if (!self::$mounts) {
316
			\OC_Util::setupFS();
317
		}
318 108
		return self::$mounts->findByNumericId($id);
319
	}
320
321
	/**
322
	 * resolve a path to a storage and internal path
323
	 *
324
	 * @param string $path
325
	 * @return array an array consisting of the storage and the internal path
326
	 */
327 951
	static public function resolvePath($path) {
328 951
		if (!self::$mounts) {
329
			\OC_Util::setupFS();
330
		}
331 951
		$mount = self::$mounts->find($path);
332 951
		if ($mount) {
333 951
			return array($mount->getStorage(), rtrim($mount->getInternalPath($path), '/'));
334
		} else {
335
			return array(null, null);
336
		}
337
	}
338
339 948
	static public function init($user, $root) {
340 948
		if (self::$defaultInstance) {
341 10
			return false;
342
		}
343 948
		self::getLoader();
344 948
		self::$defaultInstance = new View($root);
345
346 948
		if (!self::$mounts) {
347
			self::$mounts = \OC::$server->getMountManager();
348
		}
349
350
		//load custom mount config
351 948
		self::initMountPoints($user);
352
353 948
		self::$loaded = true;
354
355 948
		return true;
356
	}
357
358 1071
	static public function initMountManager() {
359 1071
		if (!self::$mounts) {
360 1
			self::$mounts = \OC::$server->getMountManager();
361 1
		}
362 1071
	}
363
364
	/**
365
	 * Initialize system and personal mount points for a user
366
	 *
367
	 * @param string $user
368
	 * @throws \OC\User\NoUserException if the user is not available
369
	 */
370 959
	public static function initMountPoints($user = '') {
371 959
		if ($user == '') {
372 2
			$user = \OC_User::getUser();
373 2
		}
374 959
		if (isset(self::$usersSetup[$user])) {
375 910
			return;
376
		}
377 959
		self::$usersSetup[$user] = true;
378
379 959
		$root = \OC_User::getHome($user);
380
381 959
		$userManager = \OC::$server->getUserManager();
382 959
		$userObject = $userManager->get($user);
383
384 959
		if (is_null($userObject)) {
385 1
			\OCP\Util::writeLog('files', ' Backends provided no user object for ' . $user, \OCP\Util::ERROR);
386 1
			throw new \OC\User\NoUserException('Backends provided no user object for ' . $user);
387
		}
388
389 958
		$homeStorage = \OC_Config::getValue('objectstore');
390 958
		if (!empty($homeStorage)) {
391
			// sanity checks
392
			if (empty($homeStorage['class'])) {
393
				\OCP\Util::writeLog('files', 'No class given for objectstore', \OCP\Util::ERROR);
394
			}
395
			if (!isset($homeStorage['arguments'])) {
396
				$homeStorage['arguments'] = array();
397
			}
398
			// instantiate object store implementation
399
			$homeStorage['arguments']['objectstore'] = new $homeStorage['class']($homeStorage['arguments']);
400
			// mount with home object store implementation
401
			$homeStorage['class'] = '\OC\Files\ObjectStore\HomeObjectStoreStorage';
402
		} else {
403
			$homeStorage = array(
404
				//default home storage configuration:
405 958
				'class' => '\OC\Files\Storage\Home',
406 958
				'arguments' => array()
407 958
			);
408
		}
409 958
		$homeStorage['arguments']['user'] = $userObject;
410
411
		// check for legacy home id (<= 5.0.12)
412 958
		if (\OC\Files\Cache\Storage::exists('local::' . $root . '/')) {
413 1
			$homeStorage['arguments']['legacy'] = true;
414 1
		}
415
416 958
		self::mount($homeStorage['class'], $homeStorage['arguments'], $user);
417
418 958
		$home = \OC\Files\Filesystem::getStorage($user);
0 ignored issues
show
Unused Code introduced by
$home is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
419
420 958
		self::mountCacheDir($user);
421
422
		// Chance to mount for other storages
423
		/** @var \OC\Files\Config\MountProviderCollection $mountConfigManager */
424 958
		$mountConfigManager = \OC::$server->getMountProviderCollection();
425 958
		if ($userObject) {
426 958
			$mounts = $mountConfigManager->getMountsForUser($userObject);
427 958
			array_walk($mounts, array(self::$mounts, 'addMount'));
428 958
		}
429
430 958
		self::listenForNewMountProviders($mountConfigManager, $userManager);
431 958
		\OC_Hook::emit('OC_Filesystem', 'post_initMountPoints', array('user' => $user, 'user_dir' => $root));
432 958
	}
433
434
	/**
435
	 * Get mounts from mount providers that are registered after setup
436
	 *
437
	 * @param MountProviderCollection $mountConfigManager
438
	 * @param IUserManager $userManager
439
	 */
440 958
	private static function listenForNewMountProviders(MountProviderCollection $mountConfigManager, IUserManager $userManager) {
441 958
		if (!self::$listeningForProviders) {
442 1
			self::$listeningForProviders = true;
443 216
			$mountConfigManager->listen('\OC\Files\Config', 'registerMountProvider', function (IMountProvider $provider) use ($userManager) {
444 215
				foreach (Filesystem::$usersSetup as $user => $setup) {
445 26
					$userObject = $userManager->get($user);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $userObject is correct as $userManager->get($user) (which targets OCP\IUserManager::get()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
446 26
					if ($userObject) {
447 5
						$mounts = $provider->getMountsForUser($userObject, Filesystem::getLoader());
448 5
						array_walk($mounts, array(self::$mounts, 'addMount'));
449 5
					}
450 215
				}
451 216
			});
452 1
		}
453 958
	}
454
455
	/**
456
	 * Mounts the cache directory
457
	 *
458
	 * @param string $user user name
459
	 */
460 958
	private static function mountCacheDir($user) {
461 958
		$cacheBaseDir = \OC_Config::getValue('cache_path', '');
462 958
		if ($cacheBaseDir !== '') {
463 1
			$cacheDir = rtrim($cacheBaseDir, '/') . '/' . $user;
464 1
			if (!file_exists($cacheDir)) {
465 1
				mkdir($cacheDir, 0770, true);
466 1
			}
467
			// mount external cache dir to "/$user/cache" mount point
468 1
			self::mount('\OC\Files\Storage\Local', array('datadir' => $cacheDir), '/' . $user . '/cache');
469 1
		}
470 958
	}
471
472
	/**
473
	 * get the default filesystem view
474
	 *
475
	 * @return View
476
	 */
477 944
	static public function getView() {
478 944
		return self::$defaultInstance;
479
	}
480
481
	/**
482
	 * tear down the filesystem, removing all storage providers
483
	 */
484 1121
	static public function tearDown() {
485 1121
		self::clearMounts();
486 1121
		self::$defaultInstance = null;
487 1121
	}
488
489
	/**
490
	 * get the relative path of the root data directory for the current user
491
	 *
492
	 * @return string
493
	 *
494
	 * Returns path like /admin/files
495
	 */
496 944
	static public function getRoot() {
497 944
		if (!self::$defaultInstance) {
498 7
			return null;
499
		}
500 938
		return self::$defaultInstance->getRoot();
501
	}
502
503
	/**
504
	 * clear all mounts and storage backends
505
	 */
506 1128
	public static function clearMounts() {
507 1128
		if (self::$mounts) {
508 1128
			self::$usersSetup = array();
509 1128
			self::$mounts->clear();
510 1128
		}
511 1128
	}
512
513
	/**
514
	 * mount an \OC\Files\Storage\Storage in our virtual filesystem
515
	 *
516
	 * @param \OC\Files\Storage\Storage|string $class
517
	 * @param array $arguments
518
	 * @param string $mountpoint
519
	 */
520 1083
	static public function mount($class, $arguments, $mountpoint) {
521 1083
		if (!self::$mounts) {
522
			\OC_Util::setupFS();
523
		}
524 1083
		$mount = new Mount\MountPoint($class, $mountpoint, $arguments, self::getLoader());
525 1083
		self::$mounts->addMount($mount);
526 1083
	}
527
528
	/**
529
	 * return the path to a local version of the file
530
	 * we need this because we can't know if a file is stored local or not from
531
	 * outside the filestorage and for some purposes a local file is needed
532
	 *
533
	 * @param string $path
534
	 * @return string
535
	 */
536
	static public function getLocalFile($path) {
537
		return self::$defaultInstance->getLocalFile($path);
538
	}
539
540
	/**
541
	 * @param string $path
542
	 * @return string
543
	 */
544
	static public function getLocalFolder($path) {
545
		return self::$defaultInstance->getLocalFolder($path);
546
	}
547
548
	/**
549
	 * return path to file which reflects one visible in browser
550
	 *
551
	 * @param string $path
552
	 * @return string
553
	 */
554
	static public function getLocalPath($path) {
555
		$datadir = \OC_User::getHome(\OC_User::getUser()) . '/files';
556
		$newpath = $path;
557
		if (strncmp($newpath, $datadir, strlen($datadir)) == 0) {
558
			$newpath = substr($path, strlen($datadir));
559
		}
560
		return $newpath;
561
	}
562
563
	/**
564
	 * check if the requested path is valid
565
	 *
566
	 * @param string $path
567
	 * @return bool
568
	 */
569 1413
	static public function isValidPath($path) {
570 1413
		$path = self::normalizePath($path);
571 1413
		if (!$path || $path[0] !== '/') {
572 2
			$path = '/' . $path;
573 2
		}
574 1413
		if (strpos($path, '/../') !== false || strrchr($path, '/') === '/..') {
575 21
			return false;
576
		}
577 1404
		return true;
578
	}
579
580
	/**
581
	 * checks if a file is blacklisted for storage in the filesystem
582
	 * Listens to write and rename hooks
583
	 *
584
	 * @param array $data from hook
585
	 */
586
	static public function isBlacklisted($data) {
587
		if (isset($data['path'])) {
588
			$path = $data['path'];
589
		} else if (isset($data['newpath'])) {
590
			$path = $data['newpath'];
591
		}
592
		if (isset($path)) {
593
			if (self::isFileBlacklisted($path)) {
594
				$data['run'] = false;
595
			}
596
		}
597
	}
598
599
	/**
600
	 * @param string $filename
601
	 * @return bool
602
	 */
603 1017
	static public function isFileBlacklisted($filename) {
604 1017
		$filename = self::normalizePath($filename);
605
606 1017
		$blacklist = \OC_Config::getValue('blacklisted_files', array('.htaccess'));
607 1017
		$filename = strtolower(basename($filename));
608 1017
		return in_array($filename, $blacklist);
609
	}
610
611
	/**
612
	 * check if the directory should be ignored when scanning
613
	 * NOTE: the special directories . and .. would cause never ending recursion
614
	 *
615
	 * @param String $dir
616
	 * @return boolean
617
	 */
618 1001
	static public function isIgnoredDir($dir) {
619 1001
		if ($dir === '.' || $dir === '..') {
620 967
			return true;
621
		}
622 744
		return false;
623
	}
624
625
	/**
626
	 * following functions are equivalent to their php builtin equivalents for arguments/return values.
627
	 */
628 24
	static public function mkdir($path) {
629 24
		return self::$defaultInstance->mkdir($path);
630
	}
631
632 3
	static public function rmdir($path) {
633 3
		return self::$defaultInstance->rmdir($path);
634
	}
635
636
	static public function opendir($path) {
637
		return self::$defaultInstance->opendir($path);
638
	}
639
640
	static public function readdir($path) {
641
		return self::$defaultInstance->readdir($path);
642
	}
643
644 25
	static public function is_dir($path) {
645 25
		return self::$defaultInstance->is_dir($path);
646
	}
647
648
	static public function is_file($path) {
649
		return self::$defaultInstance->is_file($path);
650
	}
651
652
	static public function stat($path) {
653
		return self::$defaultInstance->stat($path);
654
	}
655
656
	static public function filetype($path) {
657
		return self::$defaultInstance->filetype($path);
658
	}
659
660 2
	static public function filesize($path) {
661 2
		return self::$defaultInstance->filesize($path);
662
	}
663
664
	static public function readfile($path) {
665
		return self::$defaultInstance->readfile($path);
666
	}
667
668
	static public function isCreatable($path) {
669
		return self::$defaultInstance->isCreatable($path);
670
	}
671
672 1
	static public function isReadable($path) {
673 1
		return self::$defaultInstance->isReadable($path);
674
	}
675
676
	static public function isUpdatable($path) {
677
		return self::$defaultInstance->isUpdatable($path);
678
	}
679
680
	static public function isDeletable($path) {
681
		return self::$defaultInstance->isDeletable($path);
682
	}
683
684 149
	static public function isSharable($path) {
685 149
		return self::$defaultInstance->isSharable($path);
686
	}
687
688 162
	static public function file_exists($path) {
689 162
		return self::$defaultInstance->file_exists($path);
690
	}
691
692
	static public function filemtime($path) {
693
		return self::$defaultInstance->filemtime($path);
694
	}
695
696 7
	static public function touch($path, $mtime = null) {
697 7
		return self::$defaultInstance->touch($path, $mtime);
698
	}
699
700
	/**
701
	 * @return string
702
	 */
703
	static public function file_get_contents($path) {
704
		return self::$defaultInstance->file_get_contents($path);
705
	}
706
707 40
	static public function file_put_contents($path, $data) {
708 40
		return self::$defaultInstance->file_put_contents($path, $data);
709
	}
710
711 15
	static public function unlink($path) {
712 15
		return self::$defaultInstance->unlink($path);
713
	}
714
715 27
	static public function rename($path1, $path2) {
716 27
		return self::$defaultInstance->rename($path1, $path2);
717
	}
718
719 3
	static public function copy($path1, $path2) {
720 3
		return self::$defaultInstance->copy($path1, $path2);
721
	}
722
723
	static public function fopen($path, $mode) {
724
		return self::$defaultInstance->fopen($path, $mode);
725
	}
726
727
	/**
728
	 * @return string
729
	 */
730
	static public function toTmpFile($path) {
731
		return self::$defaultInstance->toTmpFile($path);
732
	}
733
734
	static public function fromTmpFile($tmpFile, $path) {
735
		return self::$defaultInstance->fromTmpFile($tmpFile, $path);
736
	}
737
738 1
	static public function getMimeType($path) {
739 1
		return self::$defaultInstance->getMimeType($path);
740
	}
741
742
	static public function hash($type, $path, $raw = false) {
743
		return self::$defaultInstance->hash($type, $path, $raw);
744
	}
745
746 47
	static public function free_space($path = '/') {
747 47
		return self::$defaultInstance->free_space($path);
748
	}
749
750
	static public function search($query) {
751
		return self::$defaultInstance->search($query);
752
	}
753
754
	/**
755
	 * @param string $query
756
	 */
757
	static public function searchByMime($query) {
758
		return self::$defaultInstance->searchByMime($query);
759
	}
760
761
	/**
762
	 * @param string|int $tag name or tag id
763
	 * @param string $userId owner of the tags
764
	 * @return FileInfo[] array or file info
765
	 */
766
	static public function searchByTag($tag, $userId) {
767
		return self::$defaultInstance->searchByTag($tag, $userId);
768
	}
769
770
	/**
771
	 * check if a file or folder has been updated since $time
772
	 *
773
	 * @param string $path
774
	 * @param int $time
775
	 * @return bool
776
	 */
777
	static public function hasUpdated($path, $time) {
778
		return self::$defaultInstance->hasUpdated($path, $time);
779
	}
780
781
	/**
782
	 * Fix common problems with a file path
783
	 *
784
	 * @param string $path
785
	 * @param bool $stripTrailingSlash
786
	 * @param bool $isAbsolutePath
787
	 * @return string
788
	 */
789 1648
	public static function normalizePath($path, $stripTrailingSlash = true, $isAbsolutePath = false) {
790
		/**
791
		 * FIXME: This is a workaround for existing classes and files which call
792
		 *        this function with another type than a valid string. This
793
		 *        conversion should get removed as soon as all existing
794
		 *        function calls have been fixed.
795
		 */
796 1648
		$path = (string)$path;
797
798 1648
		$cacheKey = json_encode([$path, $stripTrailingSlash, $isAbsolutePath]);
799
800 1648
		if (isset(self::$normalizedPathCache[$cacheKey])) {
801 1314
			return self::$normalizedPathCache[$cacheKey];
802
		}
803
804 1552
		if ($path == '') {
805 1417
			return '/';
806
		}
807
808
		//normalize unicode if possible
809 747
		$path = \OC_Util::normalizeUnicode($path);
810
811
		//no windows style slashes
812 747
		$path = str_replace('\\', '/', $path);
813
814
		// When normalizing an absolute path, we need to ensure that the drive-letter
815
		// is still at the beginning on windows
816 747
		$windows_drive_letter = '';
817 747
		if ($isAbsolutePath && \OC_Util::runningOnWindows() && preg_match('#^([a-zA-Z])$#', $path[0]) && $path[1] == ':' && $path[2] == '/') {
818
			$windows_drive_letter = substr($path, 0, 2);
819
			$path = substr($path, 2);
820
		}
821
822
		//add leading slash
823 747
		if ($path[0] !== '/') {
824 521
			$path = '/' . $path;
825 521
		}
826
827
		// remove '/./'
828
		// ugly, but str_replace() can't replace them all in one go
829
		// as the replacement itself is part of the search string
830
		// which will only be found during the next iteration
831 747
		while (strpos($path, '/./') !== false) {
832 24
			$path = str_replace('/./', '/', $path);
833 24
		}
834
		// remove sequences of slashes
835 747
		$path = preg_replace('#/{2,}#', '/', $path);
836
837
		//remove trailing slash
838 747
		if ($stripTrailingSlash and strlen($path) > 1 and substr($path, -1, 1) === '/') {
839 420
			$path = substr($path, 0, -1);
840 420
		}
841
842
		// remove trailing '/.'
843 747
		if (substr($path, -2) == '/.') {
844 18
			$path = substr($path, 0, -2);
845 18
		}
846
847 747
		$normalizedPath = $windows_drive_letter . $path;
848 747
		self::$normalizedPathCache[$cacheKey] = $normalizedPath;
849
850 747
		return $normalizedPath;
851
	}
852
853
	/**
854
	 * get the filesystem info
855
	 *
856
	 * @param string $path
857
	 * @param boolean $includeMountPoints whether to add mountpoint sizes,
858
	 * defaults to true
859
	 * @return \OC\Files\FileInfo|bool False if file does not exist
860
	 */
861 44
	public static function getFileInfo($path, $includeMountPoints = true) {
862 44
		return self::$defaultInstance->getFileInfo($path, $includeMountPoints);
863
	}
864
865
	/**
866
	 * change file metadata
867
	 *
868
	 * @param string $path
869
	 * @param array $data
870
	 * @return int
871
	 *
872
	 * returns the fileid of the updated file
873
	 */
874
	public static function putFileInfo($path, $data) {
875
		return self::$defaultInstance->putFileInfo($path, $data);
876
	}
877
878
	/**
879
	 * get the content of a directory
880
	 *
881
	 * @param string $directory path under datadirectory
882
	 * @param string $mimetype_filter limit returned content to this mimetype or mimepart
883
	 * @return \OC\Files\FileInfo[]
884
	 */
885 1
	public static function getDirectoryContent($directory, $mimetype_filter = '') {
886 1
		return self::$defaultInstance->getDirectoryContent($directory, $mimetype_filter);
887
	}
888
889
	/**
890
	 * Get the path of a file by id
891
	 *
892
	 * Note that the resulting path is not guaranteed to be unique for the id, multiple paths can point to the same file
893
	 *
894
	 * @param int $id
895
	 * @throws NotFoundException
896
	 * @return string
897
	 */
898 152
	public static function getPath($id) {
899 152
		return self::$defaultInstance->getPath($id);
900
	}
901
902
	/**
903
	 * Get the owner for a file or folder
904
	 *
905
	 * @param string $path
906
	 * @return string
907
	 */
908 76
	public static function getOwner($path) {
909 76
		return self::$defaultInstance->getOwner($path);
910
	}
911
912
	/**
913
	 * get the ETag for a file or folder
914
	 *
915
	 * @param string $path
916
	 * @return string
917
	 */
918
	static public function getETag($path) {
919
		return self::$defaultInstance->getETag($path);
920
	}
921
}
922