From 968de70bc57f6c5a88c2c688e1c466d419538709 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Wed, 4 Jan 2017 15:13:02 +0100 Subject: [PATCH] remove the need to register the quota streamwrapper globally Signed-off-by: Robin Appelman --- lib/base.php | 1 - lib/private/Files/Stream/Quota.php | 94 ++++++---------------------- tests/lib/Files/Stream/QuotaTest.php | 5 +- 3 files changed, 21 insertions(+), 79 deletions(-) diff --git a/lib/base.php b/lib/base.php index 3ab41f3759..38b07f1b30 100644 --- a/lib/base.php +++ b/lib/base.php @@ -669,7 +669,6 @@ class OC { // register the stream wrappers stream_wrapper_register('static', 'OC\Files\Stream\StaticStream'); stream_wrapper_register('close', 'OC\Files\Stream\Close'); - stream_wrapper_register('quota', 'OC\Files\Stream\Quota'); \OC::$server->getEventLogger()->start('init_session', 'Initialize session'); OC_App::loadApps(array('session')); diff --git a/lib/private/Files/Stream/Quota.php b/lib/private/Files/Stream/Quota.php index f064ca6c01..624a2021b9 100644 --- a/lib/private/Files/Stream/Quota.php +++ b/lib/private/Files/Stream/Quota.php @@ -25,61 +25,44 @@ namespace OC\Files\Stream; +use Icewind\Streams\Wrapper; + /** * stream wrapper limits the amount of data that can be written to a stream * - * usage: void \OC\Files\Stream\Quota::register($id, $stream, $limit) - * or: resource \OC\Files\Stream\Quota::wrap($stream, $limit) + * usage: resource \OC\Files\Stream\Quota::wrap($stream, $limit) */ -class Quota { - private static $streams = array(); - - /** - * @var resource $source - */ - private $source; - +class Quota extends Wrapper { /** * @var int $limit */ private $limit; - /** - * @param string $id - * @param resource $stream - * @param int $limit - */ - public static function register($id, $stream, $limit) { - self::$streams[$id] = array($stream, $limit); - } - - /** - * remove all registered streams - */ - public static function clear() { - self::$streams = array(); - } - /** * @param resource $stream * @param int $limit * @return resource */ static public function wrap($stream, $limit) { - $id = uniqid(); - self::register($id, $stream, $limit); - $meta = stream_get_meta_data($stream); - return fopen('quota://' . $id, $meta['mode']); + $context = stream_context_create(array( + 'quota' => array( + 'source' => $stream, + 'limit' => $limit + ) + )); + return Wrapper::wrapSource($stream, $context, 'quota', self::class); } public function stream_open($path, $mode, $options, &$opened_path) { - $id = substr($path, strlen('quota://')); - if (isset(self::$streams[$id])) { - list($this->source, $this->limit) = self::$streams[$id]; - return true; - } else { - return false; - } + $context = $this->loadContext('quota'); + $this->source = $context['source']; + $this->limit = $context['limit']; + + return true; + } + + public function dir_opendir($path, $options) { + return false; } public function stream_seek($offset, $whence = SEEK_SET) { @@ -103,10 +86,6 @@ class Quota { return fseek($this->source, $offset, $whence) === 0; } - public function stream_tell() { - return ftell($this->source); - } - public function stream_read($count) { $this->limit -= $count; return fread($this->source, $count); @@ -121,37 +100,4 @@ class Quota { $this->limit -= $size; return fwrite($this->source, $data); } - - public function stream_set_option($option, $arg1, $arg2) { - switch ($option) { - case STREAM_OPTION_BLOCKING: - stream_set_blocking($this->source, $arg1); - break; - case STREAM_OPTION_READ_TIMEOUT: - stream_set_timeout($this->source, $arg1, $arg2); - break; - case STREAM_OPTION_WRITE_BUFFER: - stream_set_write_buffer($this->source, $arg1, $arg2); - } - } - - public function stream_stat() { - return fstat($this->source); - } - - public function stream_lock($mode) { - return flock($this->source, $mode); - } - - public function stream_flush() { - return fflush($this->source); - } - - public function stream_eof() { - return feof($this->source); - } - - public function stream_close() { - fclose($this->source); - } } diff --git a/tests/lib/Files/Stream/QuotaTest.php b/tests/lib/Files/Stream/QuotaTest.php index d084f0c769..24b0e7f947 100644 --- a/tests/lib/Files/Stream/QuotaTest.php +++ b/tests/lib/Files/Stream/QuotaTest.php @@ -9,14 +9,11 @@ namespace Test\Files\Stream; class QuotaTest extends \Test\TestCase { - protected function tearDown() { - \OC\Files\Stream\Quota::clear(); - parent::tearDown(); - } /** * @param string $mode * @param integer $limit + * @return resource */ protected function getStream($mode, $limit) { $source = fopen('php://temp', $mode);