[Pkg-owncloud-commits] [owncloud] 256/457: call mount providers that are registered after the filesystem is setup

David Prévot taffit at moszumanska.debian.org
Sun Jun 28 20:06:18 UTC 2015


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch stable8
in repository owncloud.

commit 24131586d74836a0f08f57511f5ec40a3d3ee156
Author: Robin Appelman <icewind at owncloud.com>
Date:   Thu May 7 14:07:02 2015 +0200

    call mount providers that are registered after the filesystem is setup
---
 .../files/config/mountprovidercollection.php       |  7 +++-
 lib/private/files/filesystem.php                   | 41 ++++++++++++++++++----
 tests/lib/files/filesystem.php                     | 35 ++++++++++++++++++
 3 files changed, 75 insertions(+), 8 deletions(-)

diff --git a/lib/private/files/config/mountprovidercollection.php b/lib/private/files/config/mountprovidercollection.php
index 326d720..a14a6ef 100644
--- a/lib/private/files/config/mountprovidercollection.php
+++ b/lib/private/files/config/mountprovidercollection.php
@@ -22,12 +22,16 @@
 
 namespace OC\Files\Config;
 
+use OC\Hooks\Emitter;
+use OC\Hooks\EmitterTrait;
 use OCP\Files\Config\IMountProviderCollection;
 use OCP\Files\Config\IMountProvider;
 use OCP\Files\Storage\IStorageFactory;
 use OCP\IUser;
 
-class MountProviderCollection implements IMountProviderCollection {
+class MountProviderCollection implements IMountProviderCollection, Emitter {
+	use EmitterTrait;
+
 	/**
 	 * @var \OCP\Files\Config\IMountProvider[]
 	 */
@@ -65,5 +69,6 @@ class MountProviderCollection implements IMountProviderCollection {
 	 */
 	public function registerProvider(IMountProvider $provider) {
 		$this->providers[] = $provider;
+		$this->emit('\OC\Files\Config', 'registerMountProvider', [$provider]);
 	}
 }
diff --git a/lib/private/files/filesystem.php b/lib/private/files/filesystem.php
index 10c64e1..1893996 100644
--- a/lib/private/files/filesystem.php
+++ b/lib/private/files/filesystem.php
@@ -59,7 +59,11 @@
 
 namespace OC\Files;
 
+use OC\Cache\File;
+use OC\Files\Config\MountProviderCollection;
 use OC\Files\Storage\StorageFactory;
+use OCP\Files\Config\IMountProvider;
+use OCP\IUserManager;
 
 class Filesystem {
 
@@ -78,6 +82,8 @@ class Filesystem {
 
 	static private $normalizedPathCache = array();
 
+	static private $listeningForProviders = false;
+
 	/**
 	 * classname which used for hooks handling
 	 * used as signalclass in OC_Hooks::emit()
@@ -371,14 +377,15 @@ class Filesystem {
 
 		$root = \OC_User::getHome($user);
 
-		$userObject = \OC_User::getManager()->get($user);
+		$userManager = \OC::$server->getUserManager();
+		$userObject = $userManager->get($user);
 
 		if (is_null($userObject)) {
-			\OCP\Util::writeLog('files', ' Backends provided no user object for '.$user, \OCP\Util::ERROR);
+			\OCP\Util::writeLog('files', ' Backends provided no user object for ' . $user, \OCP\Util::ERROR);
 			throw new \OC\User\NoUserException();
 		}
 
-		$homeStorage = \OC_Config::getValue( 'objectstore' );
+		$homeStorage = \OC_Config::getValue('objectstore');
 		if (!empty($homeStorage)) {
 			// sanity checks
 			if (empty($homeStorage['class'])) {
@@ -412,16 +419,33 @@ class Filesystem {
 		self::mountCacheDir($user);
 
 		// Chance to mount for other storages
-		if($userObject) {
-			$mountConfigManager = \OC::$server->getMountProviderCollection();
+		/** @var \OC\Files\Config\MountProviderCollection $mountConfigManager */
+		$mountConfigManager = \OC::$server->getMountProviderCollection();
+		if ($userObject) {
 			$mounts = $mountConfigManager->getMountsForUser($userObject);
 			array_walk($mounts, array(self::$mounts, 'addMount'));
 		}
+
+		self::listenForNewMountProviders($mountConfigManager, $userManager);
 		\OC_Hook::emit('OC_Filesystem', 'post_initMountPoints', array('user' => $user, 'user_dir' => $root));
 	}
 
+	private static function listenForNewMountProviders(MountProviderCollection $mountConfigManager, IUserManager $userManager) {
+		if (!self::$listeningForProviders) {
+			self::$listeningForProviders = true;
+			$mountConfigManager->listen('\OC\Files\Config', 'registerMountProvider', function (IMountProvider $provider) use ($userManager) {
+				foreach (Filesystem::$usersSetup as $user => $setup) {
+					$userObject = $userManager->get($user);
+					$mounts = $provider->getMountsForUser($userObject, Filesystem::getLoader());
+					array_walk($mounts, array(self::$mounts, 'addMount'));
+				}
+			});
+		}
+	}
+
 	/**
 	 * Mounts the cache directory
+	 *
 	 * @param string $user user name
 	 */
 	private static function mountCacheDir($user) {
@@ -455,6 +479,7 @@ class Filesystem {
 
 	/**
 	 * get the relative path of the root data directory for the current user
+	 *
 	 * @return string
 	 *
 	 * Returns path like /admin/files
@@ -537,7 +562,7 @@ class Filesystem {
 		if (!$path || $path[0] !== '/') {
 			$path = '/' . $path;
 		}
-		if (strpos($path, '/../') !== FALSE || strrchr($path, '/') === '/..') {
+		if (strpos($path, '/../') !== false || strrchr($path, '/') === '/..') {
 			return false;
 		}
 		return true;
@@ -577,6 +602,7 @@ class Filesystem {
 	/**
 	 * check if the directory should be ignored when scanning
 	 * NOTE: the special directories . and .. would cause never ending recursion
+	 *
 	 * @param String $dir
 	 * @return boolean
 	 */
@@ -745,6 +771,7 @@ class Filesystem {
 
 	/**
 	 * Fix common problems with a file path
+	 *
 	 * @param string $path
 	 * @param bool $stripTrailingSlash
 	 * @param bool $isAbsolutePath
@@ -761,7 +788,7 @@ class Filesystem {
 
 		$cacheKey = json_encode([$path, $stripTrailingSlash, $isAbsolutePath]);
 
-		if(isset(self::$normalizedPathCache[$cacheKey])) {
+		if (isset(self::$normalizedPathCache[$cacheKey])) {
 			return self::$normalizedPathCache[$cacheKey];
 		}
 
diff --git a/tests/lib/files/filesystem.php b/tests/lib/files/filesystem.php
index 082d227..b5ee27f 100644
--- a/tests/lib/files/filesystem.php
+++ b/tests/lib/files/filesystem.php
@@ -22,11 +22,32 @@
 
 namespace Test\Files;
 
+use OC\Files\Mount\MountPoint;
+use OC\Files\Storage\Temporary;
 use OC\User\NoUserException;
+use OCP\Files\Config\IMountProvider;
+use OCP\Files\Storage\IStorageFactory;
+use OCP\IUser;
+
+class DummyMountProvider implements IMountProvider {
+	private $mounts = [];
+
+	/**
+	 * @param array $mounts
+	 */
+	public function __construct(array $mounts) {
+		$this->mounts = $mounts;
+	}
+
+	public function  getMountsForUser(IUser $user, IStorageFactory $loader) {
+		return isset($this->mounts[$user->getUID()]) ? $this->mounts[$user->getUID()] : [];
+	}
+}
 
 class Filesystem extends \Test\TestCase {
 
 	const TEST_FILESYSTEM_USER1 = "test-filesystem-user1";
+	const TEST_FILESYSTEM_USER2 = "test-filesystem-user1";
 
 	/**
 	 * @var array tmpDirs
@@ -44,6 +65,10 @@ class Filesystem extends \Test\TestCase {
 
 	protected function setUp() {
 		parent::setUp();
+		$userBackend = new \OC_User_Dummy();
+		$userBackend->createUser(self::TEST_FILESYSTEM_USER1, self::TEST_FILESYSTEM_USER1);
+		$userBackend->createUser(self::TEST_FILESYSTEM_USER2, self::TEST_FILESYSTEM_USER2);
+		\OC::$server->getUserManager()->registerBackend($userBackend);
 		$this->loginAsUser();
 	}
 
@@ -271,6 +296,7 @@ class Filesystem extends \Test\TestCase {
 
 	/**
 	 * Tests that an exception is thrown when passed user does not exist.
+	 *
 	 * @expectedException \OC\User\NoUserException
 	 */
 	public function testLocalMountWhenUserDoesNotExist() {
@@ -380,4 +406,13 @@ class Filesystem extends \Test\TestCase {
 
 		\OC_Config::setValue('cache_path', $oldCachePath);
 	}
+
+	public function testRegisterMountProviderAfterSetup() {
+		\OC\Files\Filesystem::initMountPoints(self::TEST_FILESYSTEM_USER2);
+		$this->assertEquals('/', \OC\Files\Filesystem::getMountPoint('/foo/bar'));
+		$mount = new MountPoint(new Temporary([]), '/foo/bar');
+		$mountProvider = new DummyMountProvider([self::TEST_FILESYSTEM_USER2 => [$mount]]);
+		\OC::$server->getMountProviderCollection()->registerProvider($mountProvider);
+		$this->assertEquals('/foo/bar/', \OC\Files\Filesystem::getMountPoint('/foo/bar'));
+	}
 }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/owncloud.git



More information about the Pkg-owncloud-commits mailing list