1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
4
|
|
|
* |
5
|
|
|
* @author Arthur Schiwon <[email protected]> |
6
|
|
|
* @author Bart Visscher <[email protected]> |
7
|
|
|
* @author Christopher Schäpers <[email protected]> |
8
|
|
|
* @author Florin Peter <[email protected]> |
9
|
|
|
* @author Joas Schilling <[email protected]> |
10
|
|
|
* @author Jörn Friedrich Dreyer <[email protected]> |
11
|
|
|
* @author Lukas Reschke <[email protected]> |
12
|
|
|
* @author Michael Gapczynski <[email protected]> |
13
|
|
|
* @author Morris Jobke <[email protected]> |
14
|
|
|
* @author Robin Appelman <[email protected]> |
15
|
|
|
* @author Robin McCorkell <[email protected]> |
16
|
|
|
* @author Roeland Jago Douma <[email protected]> |
17
|
|
|
* @author Sam Tuke <[email protected]> |
18
|
|
|
* @author Stephan Peijnik <[email protected]> |
19
|
|
|
* @author Vincent Petry <[email protected]> |
20
|
|
|
* |
21
|
|
|
* @license AGPL-3.0 |
22
|
|
|
* |
23
|
|
|
* This code is free software: you can redistribute it and/or modify |
24
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
25
|
|
|
* as published by the Free Software Foundation. |
26
|
|
|
* |
27
|
|
|
* This program is distributed in the hope that it will be useful, |
28
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
29
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
30
|
|
|
* GNU Affero General Public License for more details. |
31
|
|
|
* |
32
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
33
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
34
|
|
|
* |
35
|
|
|
*/ |
36
|
|
|
|
37
|
|
|
/** |
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\CappedMemoryCache; |
62
|
|
|
use OC\Files\Config\MountProviderCollection; |
63
|
|
|
use OC\Files\Mount\MountPoint; |
64
|
|
|
use OC\Files\Storage\StorageFactory; |
65
|
|
|
use OC\Lockdown\Filesystem\NullStorage; |
66
|
|
|
use OCP\Files\Config\IMountProvider; |
67
|
|
|
use OCP\Files\NotFoundException; |
68
|
|
|
use OCP\ILogger; |
69
|
|
|
use OCP\IUserManager; |
70
|
|
|
|
71
|
|
|
class Filesystem { |
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, ILogger::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, ILogger::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 is_dir($path) { |
651
|
|
|
return self::$defaultInstance->is_dir($path); |
652
|
|
|
} |
653
|
|
|
|
654
|
|
|
static public function is_file($path) { |
655
|
|
|
return self::$defaultInstance->is_file($path); |
656
|
|
|
} |
657
|
|
|
|
658
|
|
|
static public function stat($path) { |
659
|
|
|
return self::$defaultInstance->stat($path); |
660
|
|
|
} |
661
|
|
|
|
662
|
|
|
static public function filetype($path) { |
663
|
|
|
return self::$defaultInstance->filetype($path); |
664
|
|
|
} |
665
|
|
|
|
666
|
|
|
static public function filesize($path) { |
667
|
|
|
return self::$defaultInstance->filesize($path); |
668
|
|
|
} |
669
|
|
|
|
670
|
|
|
static public function readfile($path) { |
671
|
|
|
return self::$defaultInstance->readfile($path); |
672
|
|
|
} |
673
|
|
|
|
674
|
|
|
static public function isCreatable($path) { |
675
|
|
|
return self::$defaultInstance->isCreatable($path); |
676
|
|
|
} |
677
|
|
|
|
678
|
|
|
static public function isReadable($path) { |
679
|
|
|
return self::$defaultInstance->isReadable($path); |
680
|
|
|
} |
681
|
|
|
|
682
|
|
|
static public function isUpdatable($path) { |
683
|
|
|
return self::$defaultInstance->isUpdatable($path); |
684
|
|
|
} |
685
|
|
|
|
686
|
|
|
static public function isDeletable($path) { |
687
|
|
|
return self::$defaultInstance->isDeletable($path); |
688
|
|
|
} |
689
|
|
|
|
690
|
|
|
static public function isSharable($path) { |
691
|
|
|
return self::$defaultInstance->isSharable($path); |
692
|
|
|
} |
693
|
|
|
|
694
|
|
|
static public function file_exists($path) { |
695
|
|
|
return self::$defaultInstance->file_exists($path); |
696
|
|
|
} |
697
|
|
|
|
698
|
|
|
static public function filemtime($path) { |
699
|
|
|
return self::$defaultInstance->filemtime($path); |
700
|
|
|
} |
701
|
|
|
|
702
|
|
|
static public function touch($path, $mtime = null) { |
703
|
|
|
return self::$defaultInstance->touch($path, $mtime); |
704
|
|
|
} |
705
|
|
|
|
706
|
|
|
/** |
707
|
|
|
* @return string |
708
|
|
|
*/ |
709
|
|
|
static public function file_get_contents($path) { |
710
|
|
|
return self::$defaultInstance->file_get_contents($path); |
711
|
|
|
} |
712
|
|
|
|
713
|
|
|
static public function file_put_contents($path, $data) { |
714
|
|
|
return self::$defaultInstance->file_put_contents($path, $data); |
715
|
|
|
} |
716
|
|
|
|
717
|
|
|
static public function unlink($path) { |
718
|
|
|
return self::$defaultInstance->unlink($path); |
719
|
|
|
} |
720
|
|
|
|
721
|
|
|
static public function rename($path1, $path2) { |
722
|
|
|
return self::$defaultInstance->rename($path1, $path2); |
723
|
|
|
} |
724
|
|
|
|
725
|
|
|
static public function copy($path1, $path2) { |
726
|
|
|
return self::$defaultInstance->copy($path1, $path2); |
727
|
|
|
} |
728
|
|
|
|
729
|
|
|
static public function fopen($path, $mode) { |
730
|
|
|
return self::$defaultInstance->fopen($path, $mode); |
731
|
|
|
} |
732
|
|
|
|
733
|
|
|
/** |
734
|
|
|
* @return string |
735
|
|
|
*/ |
736
|
|
|
static public function toTmpFile($path) { |
737
|
|
|
return self::$defaultInstance->toTmpFile($path); |
738
|
|
|
} |
739
|
|
|
|
740
|
|
|
static public function fromTmpFile($tmpFile, $path) { |
741
|
|
|
return self::$defaultInstance->fromTmpFile($tmpFile, $path); |
742
|
|
|
} |
743
|
|
|
|
744
|
|
|
static public function getMimeType($path) { |
745
|
|
|
return self::$defaultInstance->getMimeType($path); |
746
|
|
|
} |
747
|
|
|
|
748
|
|
|
static public function hash($type, $path, $raw = false) { |
749
|
|
|
return self::$defaultInstance->hash($type, $path, $raw); |
750
|
|
|
} |
751
|
|
|
|
752
|
|
|
static public function free_space($path = '/') { |
753
|
|
|
return self::$defaultInstance->free_space($path); |
754
|
|
|
} |
755
|
|
|
|
756
|
|
|
static public function search($query) { |
757
|
|
|
return self::$defaultInstance->search($query); |
758
|
|
|
} |
759
|
|
|
|
760
|
|
|
/** |
761
|
|
|
* @param string $query |
762
|
|
|
*/ |
763
|
|
|
static public function searchByMime($query) { |
764
|
|
|
return self::$defaultInstance->searchByMime($query); |
765
|
|
|
} |
766
|
|
|
|
767
|
|
|
/** |
768
|
|
|
* @param string|int $tag name or tag id |
769
|
|
|
* @param string $userId owner of the tags |
770
|
|
|
* @return FileInfo[] array or file info |
771
|
|
|
*/ |
772
|
|
|
static public function searchByTag($tag, $userId) { |
773
|
|
|
return self::$defaultInstance->searchByTag($tag, $userId); |
774
|
|
|
} |
775
|
|
|
|
776
|
|
|
/** |
777
|
|
|
* check if a file or folder has been updated since $time |
778
|
|
|
* |
779
|
|
|
* @param string $path |
780
|
|
|
* @param int $time |
781
|
|
|
* @return bool |
782
|
|
|
*/ |
783
|
|
|
static public function hasUpdated($path, $time) { |
784
|
|
|
return self::$defaultInstance->hasUpdated($path, $time); |
785
|
|
|
} |
786
|
|
|
|
787
|
|
|
/** |
788
|
|
|
* Fix common problems with a file path |
789
|
|
|
* |
790
|
|
|
* @param string $path |
791
|
|
|
* @param bool $stripTrailingSlash whether to strip the trailing slash |
792
|
|
|
* @param bool $isAbsolutePath whether the given path is absolute |
793
|
|
|
* @param bool $keepUnicode true to disable unicode normalization |
794
|
|
|
* @return string |
795
|
|
|
*/ |
796
|
|
|
public static function normalizePath($path, $stripTrailingSlash = true, $isAbsolutePath = false, $keepUnicode = false) { |
797
|
|
|
if (is_null(self::$normalizedPathCache)) { |
798
|
|
|
self::$normalizedPathCache = new CappedMemoryCache(2048); |
799
|
|
|
} |
800
|
|
|
|
801
|
|
|
/** |
802
|
|
|
* FIXME: This is a workaround for existing classes and files which call |
803
|
|
|
* this function with another type than a valid string. This |
804
|
|
|
* conversion should get removed as soon as all existing |
805
|
|
|
* function calls have been fixed. |
806
|
|
|
*/ |
807
|
|
|
$path = (string)$path; |
808
|
|
|
|
809
|
|
|
$cacheKey = json_encode([$path, $stripTrailingSlash, $isAbsolutePath, $keepUnicode]); |
810
|
|
|
|
811
|
|
|
if (isset(self::$normalizedPathCache[$cacheKey])) { |
812
|
|
|
return self::$normalizedPathCache[$cacheKey]; |
813
|
|
|
} |
814
|
|
|
|
815
|
|
|
if ($path === '') { |
816
|
|
|
return '/'; |
817
|
|
|
} |
818
|
|
|
|
819
|
|
|
//normalize unicode if possible |
820
|
|
|
if (!$keepUnicode) { |
821
|
|
|
$path = \OC_Util::normalizeUnicode($path); |
822
|
|
|
} |
823
|
|
|
|
824
|
|
|
//add leading slash, if it is already there we strip it anyway |
825
|
|
|
$path = '/' . $path; |
826
|
|
|
|
827
|
|
|
$patterns = [ |
828
|
|
|
'/\\\\/s', // no windows style slashes |
829
|
|
|
'/\/\.(\/\.)?\//s', // remove '/./' |
830
|
|
|
'/\/{2,}/s', // remove squence of slashes |
831
|
|
|
'/\/\.$/s', // remove trailing /. |
832
|
|
|
]; |
833
|
|
|
|
834
|
|
|
do { |
835
|
|
|
$count = 0; |
836
|
|
|
$path = preg_replace($patterns, '/', $path, -1, $count); |
837
|
|
|
} while ($count > 0); |
838
|
|
|
|
839
|
|
|
//remove trailing slash |
840
|
|
|
if ($stripTrailingSlash && strlen($path) > 1) { |
841
|
|
|
$path = rtrim($path, '/'); |
842
|
|
|
} |
843
|
|
|
|
844
|
|
|
self::$normalizedPathCache[$cacheKey] = $path; |
845
|
|
|
|
846
|
|
|
return $path; |
847
|
|
|
} |
848
|
|
|
|
849
|
|
|
/** |
850
|
|
|
* get the filesystem info |
851
|
|
|
* |
852
|
|
|
* @param string $path |
853
|
|
|
* @param boolean $includeMountPoints whether to add mountpoint sizes, |
854
|
|
|
* defaults to true |
855
|
|
|
* @return \OC\Files\FileInfo|bool False if file does not exist |
856
|
|
|
*/ |
857
|
|
|
public static function getFileInfo($path, $includeMountPoints = true) { |
858
|
|
|
return self::$defaultInstance->getFileInfo($path, $includeMountPoints); |
859
|
|
|
} |
860
|
|
|
|
861
|
|
|
/** |
862
|
|
|
* change file metadata |
863
|
|
|
* |
864
|
|
|
* @param string $path |
865
|
|
|
* @param array $data |
866
|
|
|
* @return int |
867
|
|
|
* |
868
|
|
|
* returns the fileid of the updated file |
869
|
|
|
*/ |
870
|
|
|
public static function putFileInfo($path, $data) { |
871
|
|
|
return self::$defaultInstance->putFileInfo($path, $data); |
872
|
|
|
} |
873
|
|
|
|
874
|
|
|
/** |
875
|
|
|
* get the content of a directory |
876
|
|
|
* |
877
|
|
|
* @param string $directory path under datadirectory |
878
|
|
|
* @param string $mimetype_filter limit returned content to this mimetype or mimepart |
879
|
|
|
* @return \OC\Files\FileInfo[] |
880
|
|
|
*/ |
881
|
|
|
public static function getDirectoryContent($directory, $mimetype_filter = '') { |
882
|
|
|
return self::$defaultInstance->getDirectoryContent($directory, $mimetype_filter); |
883
|
|
|
} |
884
|
|
|
|
885
|
|
|
/** |
886
|
|
|
* Get the path of a file by id |
887
|
|
|
* |
888
|
|
|
* Note that the resulting path is not guaranteed to be unique for the id, multiple paths can point to the same file |
889
|
|
|
* |
890
|
|
|
* @param int $id |
891
|
|
|
* @throws NotFoundException |
892
|
|
|
* @return string |
893
|
|
|
*/ |
894
|
|
|
public static function getPath($id) { |
895
|
|
|
return self::$defaultInstance->getPath($id); |
896
|
|
|
} |
897
|
|
|
|
898
|
|
|
/** |
899
|
|
|
* Get the owner for a file or folder |
900
|
|
|
* |
901
|
|
|
* @param string $path |
902
|
|
|
* @return string |
903
|
|
|
*/ |
904
|
|
|
public static function getOwner($path) { |
905
|
|
|
return self::$defaultInstance->getOwner($path); |
906
|
|
|
} |
907
|
|
|
|
908
|
|
|
/** |
909
|
|
|
* get the ETag for a file or folder |
910
|
|
|
* |
911
|
|
|
* @param string $path |
912
|
|
|
* @return string |
913
|
|
|
*/ |
914
|
|
|
static public function getETag($path) { |
915
|
|
|
return self::$defaultInstance->getETag($path); |
916
|
|
|
} |
917
|
|
|
} |
918
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
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.