Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 21 additions & 26 deletions lib/private/Files/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
use OCP\Files\NotFoundException;
use OCP\Files\Storage\IStorageFactory;
use OCP\ILogger;
use OCP\IUser;
use OCP\IUserManager;

class Filesystem {
Expand Down Expand Up @@ -240,9 +241,7 @@ public static function getLoader() {
* @return \OC\Files\Mount\Manager
*/
public static function getMountManager($user = '') {
if (!self::$mounts) {
\OC_Util::setupFS($user);
}
self::initMountManager();
return self::$mounts;
}

Expand Down Expand Up @@ -292,10 +291,7 @@ public static function getMountPoints($path) {
* @return \OC\Files\Storage\Storage|null
*/
public static function getStorage($mountPoint) {
if (!self::$mounts) {
\OC_Util::setupFS();
}
$mount = self::$mounts->find($mountPoint);
$mount = self::getMountManager()->find($mountPoint);
return $mount->getStorage();
}

Expand All @@ -304,21 +300,15 @@ public static function getStorage($mountPoint) {
* @return Mount\MountPoint[]
*/
public static function getMountByStorageId($id) {
if (!self::$mounts) {
\OC_Util::setupFS();
}
return self::$mounts->findByStorageId($id);
return self::getMountManager()->findByStorageId($id);
}

/**
* @param int $id
* @return Mount\MountPoint[]
*/
public static function getMountByNumericId($id) {
if (!self::$mounts) {
\OC_Util::setupFS();
}
return self::$mounts->findByNumericId($id);
return self::getMountManager()->findByNumericId($id);
}

/**
Expand All @@ -328,10 +318,7 @@ public static function getMountByNumericId($id) {
* @return array an array consisting of the storage and the internal path
*/
public static function resolvePath($path) {
if (!self::$mounts) {
\OC_Util::setupFS();
}
$mount = self::$mounts->find($path);
$mount = self::getMountManager()->find($path);
if ($mount) {
return [$mount->getStorage(), rtrim($mount->getInternalPath($path), '/')];
} else {
Expand Down Expand Up @@ -367,14 +354,25 @@ public static function initMountManager() {
/**
* Initialize system and personal mount points for a user
*
* @param string $user
* @param string|IUser|null $user
* @throws \OC\User\NoUserException if the user is not available
*/
public static function initMountPoints($user = '') {
if ($user == '') {
$user = \OC_User::getUser();
$userManager = \OC::$server->getUserManager();
if (is_string($user)) {
if ($user === '') {
$user = \OC_User::getUser();
}

$userObject = $userManager->get($user);
} elseif ($user instanceof IUser) {
$userObject = $user;
$user = $userObject->getUID();
} else {
$userObject = null;
}
if ($user === null || $user === false || $user === '') {

if ($userObject === null || $user === false || $user === '') {
throw new \OC\User\NoUserException('Attempted to initialize mount points for null user and no user in session');
}

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

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

$userManager = \OC::$server->getUserManager();
$userObject = $userManager->get($user);

if (is_null($userObject)) {
\OCP\Util::writeLog('files', ' Backends provided no user object for ' . $user, ILogger::ERROR);
// reset flag, this will make it possible to rethrow the exception if called again
Expand Down
13 changes: 11 additions & 2 deletions lib/private/Files/Mount/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,23 @@ public function moveMount(string $mountPoint, string $target) {
$this->inPathCache->clear();
}

private function setupForFind(string $path) {
if (strpos($path, '/appdata_' . \OC_Util::getInstanceId()) === 0) {
// for appdata, we only setup the root bits, not the user bits
\OC_Util::setupRootFS();
} else {
\OC_Util::setupFS();
}
}

/**
* Find the mount for $path
*
* @param string $path
* @return MountPoint|null
*/
public function find(string $path) {
\OC_Util::setupFS();
$this->setupForFind($path);
$path = Filesystem::normalizePath($path);

if (isset($this->pathCache[$path])) {
Expand Down Expand Up @@ -121,7 +130,7 @@ public function find(string $path) {
* @return MountPoint[]
*/
public function findIn(string $path): array {
\OC_Util::setupFS();
$this->setupForFind($path);
$path = $this->formatPath($path);

if (isset($this->inPathCache[$path])) {
Expand Down
2 changes: 1 addition & 1 deletion lib/private/legacy/OC_User.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ public static function isAdminUser($uid) {
/**
* get the user id of the user currently logged in.
*
* @return string|bool uid or false
* @return string|false uid or false
*/
public static function getUser() {
$uid = \OC::$server->getSession() ? \OC::$server->getSession()->get('user_id') : null;
Expand Down
71 changes: 44 additions & 27 deletions lib/private/legacy/OC_Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class OC_Util {
public static $styles = [];
public static $headers = [];
private static $rootMounted = false;
private static $rootFsSetup = false;
private static $fsSetup = false;

/** @var array Local cache of version.php */
Expand Down Expand Up @@ -185,30 +186,18 @@ private static function initObjectStoreMultibucketRootFS($config) {
* @suppress PhanDeprecatedFunction
* @suppress PhanAccessMethodInternal
*/
public static function setupFS($user = '') {
public static function setupRootFS(string $user = '') {
//setting up the filesystem twice can only lead to trouble
if (self::$fsSetup) {
if (self::$rootFsSetup) {
return false;
}

\OC::$server->getEventLogger()->start('setup_fs', 'Setup filesystem');

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

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

// the filesystem will finish when $user is not empty,
// mark fs setup here to avoid doing the setup from loading
// OC_Filesystem
if ($user != '') {
self::$fsSetup = true;
}
self::$rootFsSetup = true;

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

Expand Down Expand Up @@ -276,10 +265,10 @@ public static function setupFS($user = '') {
return new \OC\Files\Storage\Wrapper\PermissionsMask([
'storage' => $storage,
'mask' => \OCP\Constants::PERMISSION_ALL & ~(
\OCP\Constants::PERMISSION_UPDATE |
\OCP\Constants::PERMISSION_CREATE |
\OCP\Constants::PERMISSION_DELETE
),
\OCP\Constants::PERMISSION_UPDATE |
\OCP\Constants::PERMISSION_CREATE |
\OCP\Constants::PERMISSION_DELETE
),
]);
}
return $storage;
Expand Down Expand Up @@ -312,19 +301,46 @@ public static function setupFS($user = '') {
$mountManager->addMount($rootMountProvider);
}

if ($user != '' && !\OC::$server->getUserManager()->userExists($user)) {
\OC::$server->getEventLogger()->end('setup_fs');
\OC::$server->getEventLogger()->end('setup_root_fs');

return true;
}

/**
* Setup the file system
*
* @param string|null $user
* @return boolean
* @description configure the initial filesystem based on the configuration
* @suppress PhanDeprecatedFunction
* @suppress PhanAccessMethodInternal
*/
public static function setupFS(?string $user = '') {
self::setupRootFS($user ?? '');

if (self::$fsSetup) {
return false;
}

//if we aren't logged in, there is no use to set up the filesystem
if ($user != "") {
$userDir = '/' . $user . '/files';
\OC::$server->getEventLogger()->start('setup_fs', 'Setup filesystem');

// If we are not forced to load a specific user we load the one that is logged in
if ($user === '') {
$userObject = \OC::$server->get(\OCP\IUserSession::class)->getUser();
} else {
$userObject = \OC::$server->get(\OCP\IUserManager::class)->get($user);
}

//if we aren't logged in, or the user doesn't exist, there is no use to set up the filesystem
if ($userObject) {
self::$fsSetup = true;

$userDir = '/' . $userObject->getUID() . '/files';

//jail the user into his "home" directory
\OC\Files\Filesystem::init($user, $userDir);
\OC\Files\Filesystem::init($userObject, $userDir);

OC_Hook::emit('OC_Filesystem', 'setup', ['user' => $user, 'user_dir' => $userDir]);
OC_Hook::emit('OC_Filesystem', 'setup', ['user' => $userObject->getUID(), 'user_dir' => $userDir]);
}
\OC::$server->getEventLogger()->end('setup_fs');
return true;
Expand Down Expand Up @@ -483,6 +499,7 @@ public static function tearDownFS() {
\OC\Files\Filesystem::tearDown();
\OC::$server->getRootFolder()->clearCache();
self::$fsSetup = false;
self::$rootFsSetup = false;
self::$rootMounted = false;
}

Expand Down
22 changes: 9 additions & 13 deletions tests/lib/Cache/FileCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
namespace Test\Cache;

use OC\Files\Storage\Local;
use Test\Traits\UserTrait;

/**
* Class FileCacheTest
Expand All @@ -32,6 +33,8 @@
* @package Test\Cache
*/
class FileCacheTest extends TestCache {
use UserTrait;

/**
* @var string
* */
Expand All @@ -56,6 +59,12 @@ public function skip() {
protected function setUp(): void {
parent::setUp();

//login
$this->createUser('test', 'test');

$this->user = \OC_User::getUser();
\OC_User::setUserId('test');

//clear all proxies and hooks so we can do clean testing
\OC_Hook::clear('OC_Filesystem');

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

\OC_User::clearBackends();
\OC_User::useBackend(new \Test\Util\User\Dummy());

//login
\OC::$server->getUserManager()->createUser('test', 'test');

$this->user = \OC_User::getUser();
\OC_User::setUserId('test');

//set up the users dir
$this->rootView = new \OC\Files\View('');
$this->rootView->mkdir('/test');
Expand All @@ -101,10 +101,6 @@ protected function tearDown(): void {
$this->instance = null;
}

//tear down the users dir aswell
$user = \OC::$server->getUserManager()->get('test');
$user->delete();

// Restore the original mount point
\OC\Files\Filesystem::clearMounts();
\OC\Files\Filesystem::mount($this->storage, [], '/');
Expand Down