From e5c8fd37df9a5727bdfdf3664390b911a67a617a Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Thu, 5 Mar 2015 12:28:17 +0100 Subject: [PATCH 1/7] pass mountpoint to storage wrapper callback --- lib/private/files/mount/mountpoint.php | 6 +++--- lib/private/files/storage/storagefactory.php | 12 +++++++----- lib/public/files/storage/istoragefactory.php | 5 +++-- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/lib/private/files/mount/mountpoint.php b/lib/private/files/mount/mountpoint.php index 85edb7cb57..0442a36903 100644 --- a/lib/private/files/mount/mountpoint.php +++ b/lib/private/files/mount/mountpoint.php @@ -71,9 +71,10 @@ class MountPoint implements IMountPoint { } $mountpoint = $this->formatPath($mountpoint); + $this->mountPoint = $mountpoint; if ($storage instanceof Storage) { $this->class = get_class($storage); - $this->storage = $this->loader->wrap($mountpoint, $storage); + $this->storage = $this->loader->wrap($this, $storage); } else { // Update old classes to new namespace if (strpos($storage, 'OC_Filestorage_') !== false) { @@ -82,7 +83,6 @@ class MountPoint implements IMountPoint { $this->class = $storage; $this->arguments = $arguments; } - $this->mountPoint = $mountpoint; } /** @@ -113,7 +113,7 @@ class MountPoint implements IMountPoint { if (class_exists($this->class)) { try { - return $this->loader->getInstance($this->mountPoint, $this->class, $this->arguments); + return $this->loader->getInstance($this, $this->class, $this->arguments); } catch (\Exception $exception) { $this->invalidStorage = true; if ($this->mountPoint === '/') { diff --git a/lib/private/files/storage/storagefactory.php b/lib/private/files/storage/storagefactory.php index fa6dea2537..b43a6f737c 100644 --- a/lib/private/files/storage/storagefactory.php +++ b/lib/private/files/storage/storagefactory.php @@ -8,6 +8,8 @@ namespace OC\Files\Storage; +use OCP\Files\Config\IMountProviderCollection; +use OCP\Files\Mount\IMountPoint; use OCP\Files\Storage\IStorageFactory; class StorageFactory implements IStorageFactory { @@ -55,23 +57,23 @@ class StorageFactory implements IStorageFactory { /** * Create an instance of a storage and apply the registered storage wrappers * - * @param string|boolean $mountPoint + * @param \OCP\Files\Mount\IMountPoint $mountPoint * @param string $class * @param array $arguments * @return \OCP\Files\Storage */ - public function getInstance($mountPoint, $class, $arguments) { + public function getInstance(IMountPoint $mountPoint, $class, $arguments) { return $this->wrap($mountPoint, new $class($arguments)); } /** - * @param string|boolean $mountPoint + * @param \OCP\Files\Mount\IMountPoint $mountPoint * @param \OCP\Files\Storage $storage * @return \OCP\Files\Storage */ - public function wrap($mountPoint, $storage) { + public function wrap(IMountPoint $mountPoint, $storage) { foreach ($this->storageWrappers as $wrapper) { - $storage = $wrapper($mountPoint, $storage); + $storage = $wrapper($mountPoint->getMountPoint(), $storage, $mountPoint); } return $storage; } diff --git a/lib/public/files/storage/istoragefactory.php b/lib/public/files/storage/istoragefactory.php index 50c844af2e..7d4fa55e41 100644 --- a/lib/public/files/storage/istoragefactory.php +++ b/lib/public/files/storage/istoragefactory.php @@ -7,6 +7,7 @@ */ namespace OCP\Files\Storage; +use OCP\Files\Mount\IMountPoint; /** * Creates storage instances and manages and applies storage wrappers @@ -25,10 +26,10 @@ interface IStorageFactory { public function addStorageWrapper($wrapperName, $callback); /** - * @param string|boolean $mountPoint + * @param \OCP\Files\Mount\IMountPoint $mountPoint * @param string $class * @param array $arguments * @return \OCP\Files\Storage */ - public function getInstance($mountPoint, $class, $arguments); + public function getInstance(IMountPoint $mountPoint, $class, $arguments); } From 82a62fd24905c3072e19257171838bcf9c6d48ab Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Thu, 5 Mar 2015 17:10:32 +0100 Subject: [PATCH 2/7] Add test for storage factory --- tests/lib/files/storage/storagefactory.php | 45 ++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 tests/lib/files/storage/storagefactory.php diff --git a/tests/lib/files/storage/storagefactory.php b/tests/lib/files/storage/storagefactory.php new file mode 100644 index 0000000000..83cae447a4 --- /dev/null +++ b/tests/lib/files/storage/storagefactory.php @@ -0,0 +1,45 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test\Files\Storage; + +use OC\Files\Mount\MountPoint; +use OCP\Files\Mount\IMountPoint; +use OCP\Files\Storage; +use Test\TestCase; +use OC\Files\Storage\Wrapper\Wrapper; + +class DummyWrapper extends Wrapper { + +} + +class StorageFactory extends TestCase { + public function testSimpleWrapper() { + $instance = new \OC\Files\Storage\StorageFactory(); + $mount = new MountPoint('\OC\Files\Storage\Temporary', '/foo', [[]], $instance); + $instance->addStorageWrapper('dummy', function ($mountPoint, Storage $storage, IMountPoint $mount) { + $this->assertInstanceOf('\OC\Files\Storage\Temporary', $storage); + $this->assertEquals('/foo/', $mount->getMountPoint()); + $this->assertEquals('/foo/', $mountPoint); + return new DummyWrapper(['storage' => $storage]); + }); + $wrapped = $mount->getStorage(); + $this->assertInstanceOf('\Test\Files\Storage\DummyWrapper', $wrapped); + } + + public function testRemoveWrapper() { + $instance = new \OC\Files\Storage\StorageFactory(); + $mount = new MountPoint('\OC\Files\Storage\Temporary', '/foo', [[]], $instance); + $instance->addStorageWrapper('dummy', function ($mountPoint, Storage $storage) { + return new DummyWrapper(['storage' => $storage]); + }); + $instance->removeStorageWrapper('dummy'); + $wrapped = $mount->getStorage(); + $this->assertInstanceOf('\OC\Files\Storage\Temporary', $wrapped); + } +} From 4f0f175f8b1e8719e4b5858322be3d2e30280add Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Thu, 5 Mar 2015 17:10:52 +0100 Subject: [PATCH 3/7] Allow getting all mount options as array --- lib/private/files/mount/mountpoint.php | 9 +++++++++ lib/public/files/mount/imountpoint.php | 7 +++++++ 2 files changed, 16 insertions(+) diff --git a/lib/private/files/mount/mountpoint.php b/lib/private/files/mount/mountpoint.php index 0442a36903..02bd8eaa70 100644 --- a/lib/private/files/mount/mountpoint.php +++ b/lib/private/files/mount/mountpoint.php @@ -209,4 +209,13 @@ class MountPoint implements IMountPoint { public function getOption($name, $default) { return isset($this->mountOptions[$name]) ? $this->mountOptions[$name] : $default; } + + /** + * Get all options for the mount + * + * @return array + */ + public function getOptions() { + return $this->mountOptions; + } } diff --git a/lib/public/files/mount/imountpoint.php b/lib/public/files/mount/imountpoint.php index af7819ae16..2ec0cca1dc 100644 --- a/lib/public/files/mount/imountpoint.php +++ b/lib/public/files/mount/imountpoint.php @@ -64,4 +64,11 @@ interface IMountPoint { * @return mixed */ public function getOption($name, $default); + + /** + * Get all options for the mount + * + * @return array + */ + public function getOptions(); } From 7adda887865d43ea66e4854826cbb64f942af42c Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Thu, 5 Mar 2015 17:22:48 +0100 Subject: [PATCH 4/7] Copy mount options to the storage --- lib/private/files/mount/mountpoint.php | 2 +- lib/private/files/storage/common.php | 18 ++++++++++++++++++ lib/private/util.php | 8 ++++++++ tests/lib/files/view.php | 10 ++++++++++ 4 files changed, 37 insertions(+), 1 deletion(-) diff --git a/lib/private/files/mount/mountpoint.php b/lib/private/files/mount/mountpoint.php index 02bd8eaa70..a187f4db10 100644 --- a/lib/private/files/mount/mountpoint.php +++ b/lib/private/files/mount/mountpoint.php @@ -195,7 +195,7 @@ class MountPoint implements IMountPoint { $storage = $this->getStorage(); // storage can be null if it couldn't be initialized if ($storage != null) { - $this->storage = $wrapper($this->mountPoint, $storage); + $this->storage = $wrapper($this->mountPoint, $storage, $this); } } diff --git a/lib/private/files/storage/common.php b/lib/private/files/storage/common.php index 8549d5a1fa..db66feb460 100644 --- a/lib/private/files/storage/common.php +++ b/lib/private/files/storage/common.php @@ -34,6 +34,8 @@ abstract class Common implements \OC\Files\Storage\Storage { protected $watcher; protected $storageCache; + protected $mountOptions = []; + /** * @var string[] */ @@ -512,4 +514,20 @@ abstract class Common implements \OC\Files\Storage\Storage { throw new InvalidCharacterInPathException(); } } + + /** + * @param array $options + */ + public function setMountOptions(array $options) { + $this->mountOptions = $options; + } + + /** + * @param string $name + * @param mixed $default + * @return mixed + */ + public function getMountOption($name, $default = null) { + return isset($this->mountOptions[$name]) ? $this->mountOptions[$name] : $default; + } } diff --git a/lib/private/util.php b/lib/private/util.php index 72802409da..cf76ff5c08 100644 --- a/lib/private/util.php +++ b/lib/private/util.php @@ -98,6 +98,14 @@ class OC_Util { return false; } + \OC\Files\Filesystem::addStorageWrapper('mount_options', function($mountPoint, \OCP\Files\Storage $storage, \OCP\Files\Mount\IMountPoint $mount) { + if($storage->instanceOfStorage('\OC\Files\Storage\Common')) { + /** @var \OC\Files\Storage\Common $storage */ + $storage->setMountOptions($mount->getOptions()); + } + return $storage; + }); + //if we aren't logged in, there is no use to set up the filesystem if ($user != "") { \OC\Files\Filesystem::addStorageWrapper('oc_quota', function ($mountPoint, $storage) { diff --git a/tests/lib/files/view.php b/tests/lib/files/view.php index db39df7d16..4ac014a92b 100644 --- a/tests/lib/files/view.php +++ b/tests/lib/files/view.php @@ -8,6 +8,8 @@ namespace Test\Files; use OC\Files\Cache\Watcher; +use OC\Files\Filesystem; +use OC\Files\Mount\MountPoint; use OC\Files\Storage\Temporary; class TemporaryNoTouch extends \OC\Files\Storage\Temporary { @@ -975,4 +977,12 @@ class View extends \Test\TestCase { $view = new \OC\Files\View(''); $this->assertTrue($view->rename('/test/foo.txt', '/test/foo/bar.txt')); } + + public function testSetMountOptionsInStorage() { + $mount = new MountPoint('\OC\Files\Storage\Temporary', '/asd/', [[]], Filesystem::getLoader(), ['foo' => 'bar']); + Filesystem::getMountManager()->addMount($mount); + /** @var \OC\Files\Storage\Common $storage */ + $storage = $mount->getStorage(); + $this->assertEquals($storage->getMountOption('foo'), 'bar'); + } } From e1f2a6df94af29864778a3ac286887ba51f19b04 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Thu, 5 Mar 2015 17:27:10 +0100 Subject: [PATCH 5/7] Allow setting the watcher policy as mount option --- lib/private/files/cache/watcher.php | 7 +++++++ lib/private/files/storage/common.php | 3 ++- tests/lib/files/view.php | 9 +++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/private/files/cache/watcher.php b/lib/private/files/cache/watcher.php index f4572895b0..22c3fb202c 100644 --- a/lib/private/files/cache/watcher.php +++ b/lib/private/files/cache/watcher.php @@ -51,6 +51,13 @@ class Watcher { $this->watchPolicy = $policy; } + /** + * @return int either \OC\Files\Cache\Watcher::CHECK_NEVER, \OC\Files\Cache\Watcher::CHECK_ONCE, \OC\Files\Cache\Watcher::CHECK_ALWAYS + */ + public function getPolicy() { + return $this->watchPolicy; + } + /** * check $path for updates * diff --git a/lib/private/files/storage/common.php b/lib/private/files/storage/common.php index db66feb460..d7fff6e6e1 100644 --- a/lib/private/files/storage/common.php +++ b/lib/private/files/storage/common.php @@ -331,7 +331,8 @@ abstract class Common implements \OC\Files\Storage\Storage { } if (!isset($this->watcher)) { $this->watcher = new Watcher($storage); - $this->watcher->setPolicy(\OC::$server->getConfig()->getSystemValue('filesystem_check_changes', Watcher::CHECK_ONCE)); + $globalPolicy = \OC::$server->getConfig()->getSystemValue('filesystem_check_changes', Watcher::CHECK_ONCE); + $this->watcher->setPolicy($this->getMountOption('filesystem_check_changes', $globalPolicy)); } return $this->watcher; } diff --git a/tests/lib/files/view.php b/tests/lib/files/view.php index 4ac014a92b..475383b5fe 100644 --- a/tests/lib/files/view.php +++ b/tests/lib/files/view.php @@ -985,4 +985,13 @@ class View extends \Test\TestCase { $storage = $mount->getStorage(); $this->assertEquals($storage->getMountOption('foo'), 'bar'); } + + public function testSetMountOptionsWatcherPolicy() { + $mount = new MountPoint('\OC\Files\Storage\Temporary', '/asd/', [[]], Filesystem::getLoader(), ['filesystem_check_changes' => Watcher::CHECK_NEVER]); + Filesystem::getMountManager()->addMount($mount); + /** @var \OC\Files\Storage\Common $storage */ + $storage = $mount->getStorage(); + $watcher = $storage->getWatcher(); + $this->assertEquals(Watcher::CHECK_NEVER, $watcher->getPolicy()); + } } From 169031d1c5f0fbd3249e5a569b035a39be5a779d Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Thu, 5 Mar 2015 17:31:35 +0100 Subject: [PATCH 6/7] fix factory test --- lib/private/files/storage/storagefactory.php | 1 - tests/lib/files/storage/storagefactory.php | 6 +++--- tests/lib/files/view.php | 5 ++--- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/lib/private/files/storage/storagefactory.php b/lib/private/files/storage/storagefactory.php index b43a6f737c..5197279129 100644 --- a/lib/private/files/storage/storagefactory.php +++ b/lib/private/files/storage/storagefactory.php @@ -8,7 +8,6 @@ namespace OC\Files\Storage; -use OCP\Files\Config\IMountProviderCollection; use OCP\Files\Mount\IMountPoint; use OCP\Files\Storage\IStorageFactory; diff --git a/tests/lib/files/storage/storagefactory.php b/tests/lib/files/storage/storagefactory.php index 83cae447a4..15519ef83f 100644 --- a/tests/lib/files/storage/storagefactory.php +++ b/tests/lib/files/storage/storagefactory.php @@ -10,7 +10,7 @@ namespace Test\Files\Storage; use OC\Files\Mount\MountPoint; use OCP\Files\Mount\IMountPoint; -use OCP\Files\Storage; +use OCP\Files\Storage as IStorage; use Test\TestCase; use OC\Files\Storage\Wrapper\Wrapper; @@ -22,7 +22,7 @@ class StorageFactory extends TestCase { public function testSimpleWrapper() { $instance = new \OC\Files\Storage\StorageFactory(); $mount = new MountPoint('\OC\Files\Storage\Temporary', '/foo', [[]], $instance); - $instance->addStorageWrapper('dummy', function ($mountPoint, Storage $storage, IMountPoint $mount) { + $instance->addStorageWrapper('dummy', function ($mountPoint, IStorage $storage, IMountPoint $mount) { $this->assertInstanceOf('\OC\Files\Storage\Temporary', $storage); $this->assertEquals('/foo/', $mount->getMountPoint()); $this->assertEquals('/foo/', $mountPoint); @@ -35,7 +35,7 @@ class StorageFactory extends TestCase { public function testRemoveWrapper() { $instance = new \OC\Files\Storage\StorageFactory(); $mount = new MountPoint('\OC\Files\Storage\Temporary', '/foo', [[]], $instance); - $instance->addStorageWrapper('dummy', function ($mountPoint, Storage $storage) { + $instance->addStorageWrapper('dummy', function ($mountPoint, IStorage $storage) { return new DummyWrapper(['storage' => $storage]); }); $instance->removeStorageWrapper('dummy'); diff --git a/tests/lib/files/view.php b/tests/lib/files/view.php index 475383b5fe..9ff46705b8 100644 --- a/tests/lib/files/view.php +++ b/tests/lib/files/view.php @@ -8,7 +8,6 @@ namespace Test\Files; use OC\Files\Cache\Watcher; -use OC\Files\Filesystem; use OC\Files\Mount\MountPoint; use OC\Files\Storage\Temporary; @@ -980,7 +979,7 @@ class View extends \Test\TestCase { public function testSetMountOptionsInStorage() { $mount = new MountPoint('\OC\Files\Storage\Temporary', '/asd/', [[]], Filesystem::getLoader(), ['foo' => 'bar']); - Filesystem::getMountManager()->addMount($mount); + \OC\Files\Filesystem::getMountManager()->addMount($mount); /** @var \OC\Files\Storage\Common $storage */ $storage = $mount->getStorage(); $this->assertEquals($storage->getMountOption('foo'), 'bar'); @@ -988,7 +987,7 @@ class View extends \Test\TestCase { public function testSetMountOptionsWatcherPolicy() { $mount = new MountPoint('\OC\Files\Storage\Temporary', '/asd/', [[]], Filesystem::getLoader(), ['filesystem_check_changes' => Watcher::CHECK_NEVER]); - Filesystem::getMountManager()->addMount($mount); + \OC\Files\Filesystem::getMountManager()->addMount($mount); /** @var \OC\Files\Storage\Common $storage */ $storage = $mount->getStorage(); $watcher = $storage->getWatcher(); From 7ab919256b066970a0a7139ab19bcdae5773c036 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Mon, 16 Mar 2015 14:13:56 +0100 Subject: [PATCH 7/7] fix test --- tests/lib/files/view.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/lib/files/view.php b/tests/lib/files/view.php index 9ff46705b8..cd9f2d4afd 100644 --- a/tests/lib/files/view.php +++ b/tests/lib/files/view.php @@ -978,7 +978,7 @@ class View extends \Test\TestCase { } public function testSetMountOptionsInStorage() { - $mount = new MountPoint('\OC\Files\Storage\Temporary', '/asd/', [[]], Filesystem::getLoader(), ['foo' => 'bar']); + $mount = new MountPoint('\OC\Files\Storage\Temporary', '/asd/', [[]], \OC\Files\Filesystem::getLoader(), ['foo' => 'bar']); \OC\Files\Filesystem::getMountManager()->addMount($mount); /** @var \OC\Files\Storage\Common $storage */ $storage = $mount->getStorage(); @@ -986,7 +986,7 @@ class View extends \Test\TestCase { } public function testSetMountOptionsWatcherPolicy() { - $mount = new MountPoint('\OC\Files\Storage\Temporary', '/asd/', [[]], Filesystem::getLoader(), ['filesystem_check_changes' => Watcher::CHECK_NEVER]); + $mount = new MountPoint('\OC\Files\Storage\Temporary', '/asd/', [[]], \OC\Files\Filesystem::getLoader(), ['filesystem_check_changes' => Watcher::CHECK_NEVER]); \OC\Files\Filesystem::getMountManager()->addMount($mount); /** @var \OC\Files\Storage\Common $storage */ $storage = $mount->getStorage();