diff options
author | Robin Appelman <robin@icewind.nl> | 2015-03-19 16:20:38 +0100 |
---|---|---|
committer | Robin Appelman <robin@icewind.nl> | 2015-03-19 16:20:38 +0100 |
commit | 73874ca27f102b40f40df70367e01a4045a17b3e (patch) | |
tree | 7f92975f05831748d873a5dec4c259100e51fbf0 | |
parent | be6edd465a5a77ea859661feaedbeacd5a66f9db (diff) | |
parent | 7ab919256b066970a0a7139ab19bcdae5773c036 (diff) | |
download | nextcloud-server-73874ca27f102b40f40df70367e01a4045a17b3e.tar.gz nextcloud-server-73874ca27f102b40f40df70367e01a4045a17b3e.zip |
Merge pull request #14704 from owncloud/storage-wrapper-mount
pass mountpoint to storage wrapper callback
-rw-r--r-- | lib/private/files/cache/watcher.php | 7 | ||||
-rw-r--r-- | lib/private/files/mount/mountpoint.php | 17 | ||||
-rw-r--r-- | lib/private/files/storage/common.php | 21 | ||||
-rw-r--r-- | lib/private/files/storage/storagefactory.php | 11 | ||||
-rw-r--r-- | lib/private/util.php | 8 | ||||
-rw-r--r-- | lib/public/files/mount/imountpoint.php | 7 | ||||
-rw-r--r-- | lib/public/files/storage/istoragefactory.php | 5 | ||||
-rw-r--r-- | tests/lib/files/storage/storagefactory.php | 45 | ||||
-rw-r--r-- | tests/lib/files/view.php | 18 |
9 files changed, 127 insertions, 12 deletions
diff --git a/lib/private/files/cache/watcher.php b/lib/private/files/cache/watcher.php index f4572895b09..22c3fb202c3 100644 --- a/lib/private/files/cache/watcher.php +++ b/lib/private/files/cache/watcher.php @@ -52,6 +52,13 @@ class Watcher { } /** + * @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 * * @param string $path diff --git a/lib/private/files/mount/mountpoint.php b/lib/private/files/mount/mountpoint.php index 85edb7cb570..a187f4db109 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 === '/') { @@ -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); } } @@ -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/private/files/storage/common.php b/lib/private/files/storage/common.php index 5de243e177a..11cf3405fd9 100644 --- a/lib/private/files/storage/common.php +++ b/lib/private/files/storage/common.php @@ -35,6 +35,8 @@ abstract class Common implements \OC\Files\Storage\Storage { protected $watcher; protected $storageCache; + protected $mountOptions = []; + /** * @var string[] */ @@ -330,7 +332,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; } @@ -517,4 +520,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/files/storage/storagefactory.php b/lib/private/files/storage/storagefactory.php index fa6dea2537c..51972791290 100644 --- a/lib/private/files/storage/storagefactory.php +++ b/lib/private/files/storage/storagefactory.php @@ -8,6 +8,7 @@ namespace OC\Files\Storage; +use OCP\Files\Mount\IMountPoint; use OCP\Files\Storage\IStorageFactory; class StorageFactory implements IStorageFactory { @@ -55,23 +56,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/private/util.php b/lib/private/util.php index a2024fddc3d..a048996da6e 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/lib/public/files/mount/imountpoint.php b/lib/public/files/mount/imountpoint.php index af7819ae160..2ec0cca1dce 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(); } diff --git a/lib/public/files/storage/istoragefactory.php b/lib/public/files/storage/istoragefactory.php index 50c844af2e6..7d4fa55e418 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); } diff --git a/tests/lib/files/storage/storagefactory.php b/tests/lib/files/storage/storagefactory.php new file mode 100644 index 00000000000..15519ef83fb --- /dev/null +++ b/tests/lib/files/storage/storagefactory.php @@ -0,0 +1,45 @@ +<?php +/** + * Copyright (c) 2015 Robin Appelman <icewind@owncloud.com> + * 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 as IStorage; +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, IStorage $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, IStorage $storage) { + return new DummyWrapper(['storage' => $storage]); + }); + $instance->removeStorageWrapper('dummy'); + $wrapped = $mount->getStorage(); + $this->assertInstanceOf('\OC\Files\Storage\Temporary', $wrapped); + } +} diff --git a/tests/lib/files/view.php b/tests/lib/files/view.php index db39df7d16b..cd9f2d4afd1 100644 --- a/tests/lib/files/view.php +++ b/tests/lib/files/view.php @@ -8,6 +8,7 @@ namespace Test\Files; use OC\Files\Cache\Watcher; +use OC\Files\Mount\MountPoint; use OC\Files\Storage\Temporary; class TemporaryNoTouch extends \OC\Files\Storage\Temporary { @@ -975,4 +976,21 @@ 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/', [[]], \OC\Files\Filesystem::getLoader(), ['foo' => 'bar']); + \OC\Files\Filesystem::getMountManager()->addMount($mount); + /** @var \OC\Files\Storage\Common $storage */ + $storage = $mount->getStorage(); + $this->assertEquals($storage->getMountOption('foo'), 'bar'); + } + + public function testSetMountOptionsWatcherPolicy() { + $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(); + $watcher = $storage->getWatcher(); + $this->assertEquals(Watcher::CHECK_NEVER, $watcher->getPolicy()); + } } |