Skip to content

Commit e80e0d5

Browse files
authored
Merge pull request #30942 from nextcloud/appdata-lite-filesystem
only setup part of the filesystem for appdata requests
2 parents b22f680 + de26000 commit e80e0d5

File tree

5 files changed

+86
-69
lines changed

5 files changed

+86
-69
lines changed

lib/private/Files/Filesystem.php

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
use OCP\Files\NotFoundException;
4646
use OCP\Files\Storage\IStorageFactory;
4747
use OCP\ILogger;
48+
use OCP\IUser;
4849
use OCP\IUserManager;
4950

5051
class Filesystem {
@@ -240,9 +241,7 @@ public static function getLoader() {
240241
* @return \OC\Files\Mount\Manager
241242
*/
242243
public static function getMountManager($user = '') {
243-
if (!self::$mounts) {
244-
\OC_Util::setupFS($user);
245-
}
244+
self::initMountManager();
246245
return self::$mounts;
247246
}
248247

@@ -292,10 +291,7 @@ public static function getMountPoints($path) {
292291
* @return \OC\Files\Storage\Storage|null
293292
*/
294293
public static function getStorage($mountPoint) {
295-
if (!self::$mounts) {
296-
\OC_Util::setupFS();
297-
}
298-
$mount = self::$mounts->find($mountPoint);
294+
$mount = self::getMountManager()->find($mountPoint);
299295
return $mount->getStorage();
300296
}
301297

@@ -304,21 +300,15 @@ public static function getStorage($mountPoint) {
304300
* @return Mount\MountPoint[]
305301
*/
306302
public static function getMountByStorageId($id) {
307-
if (!self::$mounts) {
308-
\OC_Util::setupFS();
309-
}
310-
return self::$mounts->findByStorageId($id);
303+
return self::getMountManager()->findByStorageId($id);
311304
}
312305

313306
/**
314307
* @param int $id
315308
* @return Mount\MountPoint[]
316309
*/
317310
public static function getMountByNumericId($id) {
318-
if (!self::$mounts) {
319-
\OC_Util::setupFS();
320-
}
321-
return self::$mounts->findByNumericId($id);
311+
return self::getMountManager()->findByNumericId($id);
322312
}
323313

324314
/**
@@ -328,10 +318,7 @@ public static function getMountByNumericId($id) {
328318
* @return array an array consisting of the storage and the internal path
329319
*/
330320
public static function resolvePath($path) {
331-
if (!self::$mounts) {
332-
\OC_Util::setupFS();
333-
}
334-
$mount = self::$mounts->find($path);
321+
$mount = self::getMountManager()->find($path);
335322
if ($mount) {
336323
return [$mount->getStorage(), rtrim($mount->getInternalPath($path), '/')];
337324
} else {
@@ -367,14 +354,25 @@ public static function initMountManager() {
367354
/**
368355
* Initialize system and personal mount points for a user
369356
*
370-
* @param string $user
357+
* @param string|IUser|null $user
371358
* @throws \OC\User\NoUserException if the user is not available
372359
*/
373360
public static function initMountPoints($user = '') {
374-
if ($user == '') {
375-
$user = \OC_User::getUser();
361+
$userManager = \OC::$server->getUserManager();
362+
if (is_string($user)) {
363+
if ($user === '') {
364+
$user = \OC_User::getUser();
365+
}
366+
367+
$userObject = $userManager->get($user);
368+
} elseif ($user instanceof IUser) {
369+
$userObject = $user;
370+
$user = $userObject->getUID();
371+
} else {
372+
$userObject = null;
376373
}
377-
if ($user === null || $user === false || $user === '') {
374+
375+
if ($userObject === null || $user === false || $user === '') {
378376
throw new \OC\User\NoUserException('Attempted to initialize mount points for null user and no user in session');
379377
}
380378

@@ -384,9 +382,6 @@ public static function initMountPoints($user = '') {
384382

385383
self::$usersSetup[$user] = true;
386384

387-
$userManager = \OC::$server->getUserManager();
388-
$userObject = $userManager->get($user);
389-
390385
if (is_null($userObject)) {
391386
\OCP\Util::writeLog('files', ' Backends provided no user object for ' . $user, ILogger::ERROR);
392387
// reset flag, this will make it possible to rethrow the exception if called again

lib/private/Files/Mount/Manager.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,23 @@ public function moveMount(string $mountPoint, string $target) {
8181
$this->inPathCache->clear();
8282
}
8383

84+
private function setupForFind(string $path) {
85+
if (strpos($path, '/appdata_' . \OC_Util::getInstanceId()) === 0) {
86+
// for appdata, we only setup the root bits, not the user bits
87+
\OC_Util::setupRootFS();
88+
} else {
89+
\OC_Util::setupFS();
90+
}
91+
}
92+
8493
/**
8594
* Find the mount for $path
8695
*
8796
* @param string $path
8897
* @return MountPoint|null
8998
*/
9099
public function find(string $path) {
91-
\OC_Util::setupFS();
100+
$this->setupForFind($path);
92101
$path = Filesystem::normalizePath($path);
93102

94103
if (isset($this->pathCache[$path])) {
@@ -121,7 +130,7 @@ public function find(string $path) {
121130
* @return MountPoint[]
122131
*/
123132
public function findIn(string $path): array {
124-
\OC_Util::setupFS();
133+
$this->setupForFind($path);
125134
$path = $this->formatPath($path);
126135

127136
if (isset($this->inPathCache[$path])) {

lib/private/legacy/OC_User.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ public static function isAdminUser($uid) {
323323
/**
324324
* get the user id of the user currently logged in.
325325
*
326-
* @return string|bool uid or false
326+
* @return string|false uid or false
327327
*/
328328
public static function getUser() {
329329
$uid = \OC::$server->getSession() ? \OC::$server->getSession()->get('user_id') : null;

lib/private/legacy/OC_Util.php

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class OC_Util {
8181
public static $styles = [];
8282
public static $headers = [];
8383
private static $rootMounted = false;
84+
private static $rootFsSetup = false;
8485
private static $fsSetup = false;
8586

8687
/** @var array Local cache of version.php */
@@ -186,30 +187,18 @@ private static function initObjectStoreMultibucketRootFS($config) {
186187
* @suppress PhanDeprecatedFunction
187188
* @suppress PhanAccessMethodInternal
188189
*/
189-
public static function setupFS($user = '') {
190+
public static function setupRootFS(string $user = '') {
190191
//setting up the filesystem twice can only lead to trouble
191-
if (self::$fsSetup) {
192+
if (self::$rootFsSetup) {
192193
return false;
193194
}
194195

195-
\OC::$server->getEventLogger()->start('setup_fs', 'Setup filesystem');
196-
197-
// If we are not forced to load a specific user we load the one that is logged in
198-
if ($user === null) {
199-
$user = '';
200-
} elseif ($user == "" && \OC::$server->getUserSession()->isLoggedIn()) {
201-
$user = OC_User::getUser();
202-
}
196+
\OC::$server->getEventLogger()->start('setup_root_fs', 'Setup root filesystem');
203197

204198
// load all filesystem apps before, so no setup-hook gets lost
205199
OC_App::loadApps(['filesystem']);
206200

207-
// the filesystem will finish when $user is not empty,
208-
// mark fs setup here to avoid doing the setup from loading
209-
// OC_Filesystem
210-
if ($user != '') {
211-
self::$fsSetup = true;
212-
}
201+
self::$rootFsSetup = true;
213202

214203
\OC\Files\Filesystem::initMountManager();
215204

@@ -277,10 +266,10 @@ public static function setupFS($user = '') {
277266
return new \OC\Files\Storage\Wrapper\PermissionsMask([
278267
'storage' => $storage,
279268
'mask' => \OCP\Constants::PERMISSION_ALL & ~(
280-
\OCP\Constants::PERMISSION_UPDATE |
281-
\OCP\Constants::PERMISSION_CREATE |
282-
\OCP\Constants::PERMISSION_DELETE
283-
),
269+
\OCP\Constants::PERMISSION_UPDATE |
270+
\OCP\Constants::PERMISSION_CREATE |
271+
\OCP\Constants::PERMISSION_DELETE
272+
),
284273
]);
285274
}
286275
return $storage;
@@ -313,19 +302,46 @@ public static function setupFS($user = '') {
313302
$mountManager->addMount($rootMountProvider);
314303
}
315304

316-
if ($user != '' && !\OC::$server->getUserManager()->userExists($user)) {
317-
\OC::$server->getEventLogger()->end('setup_fs');
305+
\OC::$server->getEventLogger()->end('setup_root_fs');
306+
307+
return true;
308+
}
309+
310+
/**
311+
* Setup the file system
312+
*
313+
* @param string|null $user
314+
* @return boolean
315+
* @description configure the initial filesystem based on the configuration
316+
* @suppress PhanDeprecatedFunction
317+
* @suppress PhanAccessMethodInternal
318+
*/
319+
public static function setupFS(?string $user = '') {
320+
self::setupRootFS($user ?? '');
321+
322+
if (self::$fsSetup) {
318323
return false;
319324
}
320325

321-
//if we aren't logged in, there is no use to set up the filesystem
322-
if ($user != "") {
323-
$userDir = '/' . $user . '/files';
326+
\OC::$server->getEventLogger()->start('setup_fs', 'Setup filesystem');
327+
328+
// If we are not forced to load a specific user we load the one that is logged in
329+
if ($user === '') {
330+
$userObject = \OC::$server->get(\OCP\IUserSession::class)->getUser();
331+
} else {
332+
$userObject = \OC::$server->get(\OCP\IUserManager::class)->get($user);
333+
}
334+
335+
//if we aren't logged in, or the user doesn't exist, there is no use to set up the filesystem
336+
if ($userObject) {
337+
self::$fsSetup = true;
338+
339+
$userDir = '/' . $userObject->getUID() . '/files';
324340

325341
//jail the user into his "home" directory
326-
\OC\Files\Filesystem::init($user, $userDir);
342+
\OC\Files\Filesystem::init($userObject, $userDir);
327343

328-
OC_Hook::emit('OC_Filesystem', 'setup', ['user' => $user, 'user_dir' => $userDir]);
344+
OC_Hook::emit('OC_Filesystem', 'setup', ['user' => $userObject->getUID(), 'user_dir' => $userDir]);
329345
}
330346
\OC::$server->getEventLogger()->end('setup_fs');
331347
return true;
@@ -484,6 +500,7 @@ public static function tearDownFS() {
484500
\OC\Files\Filesystem::tearDown();
485501
\OC::$server->getRootFolder()->clearCache();
486502
self::$fsSetup = false;
503+
self::$rootFsSetup = false;
487504
self::$rootMounted = false;
488505
}
489506

tests/lib/Cache/FileCacheTest.php

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
namespace Test\Cache;
2424

2525
use OC\Files\Storage\Local;
26+
use Test\Traits\UserTrait;
2627

2728
/**
2829
* Class FileCacheTest
@@ -32,6 +33,8 @@
3233
* @package Test\Cache
3334
*/
3435
class FileCacheTest extends TestCache {
36+
use UserTrait;
37+
3538
/**
3639
* @var string
3740
* */
@@ -56,6 +59,12 @@ public function skip() {
5659
protected function setUp(): void {
5760
parent::setUp();
5861

62+
//login
63+
$this->createUser('test', 'test');
64+
65+
$this->user = \OC_User::getUser();
66+
\OC_User::setUserId('test');
67+
5968
//clear all proxies and hooks so we can do clean testing
6069
\OC_Hook::clear('OC_Filesystem');
6170

@@ -69,15 +78,6 @@ protected function setUp(): void {
6978
$this->datadir = $config->getSystemValue('cachedirectory', \OC::$SERVERROOT.'/data/cache');
7079
$config->setSystemValue('cachedirectory', $datadir);
7180

72-
\OC_User::clearBackends();
73-
\OC_User::useBackend(new \Test\Util\User\Dummy());
74-
75-
//login
76-
\OC::$server->getUserManager()->createUser('test', 'test');
77-
78-
$this->user = \OC_User::getUser();
79-
\OC_User::setUserId('test');
80-
8181
//set up the users dir
8282
$this->rootView = new \OC\Files\View('');
8383
$this->rootView->mkdir('/test');
@@ -101,10 +101,6 @@ protected function tearDown(): void {
101101
$this->instance = null;
102102
}
103103

104-
//tear down the users dir aswell
105-
$user = \OC::$server->getUserManager()->get('test');
106-
$user->delete();
107-
108104
// Restore the original mount point
109105
\OC\Files\Filesystem::clearMounts();
110106
\OC\Files\Filesystem::mount($this->storage, [], '/');

0 commit comments

Comments
 (0)