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