diff options
Diffstat (limited to 'apps/files_external/3rdparty/icewind/smb/src')
5 files changed, 215 insertions, 11 deletions
diff --git a/apps/files_external/3rdparty/icewind/smb/src/NativeReadStream.php b/apps/files_external/3rdparty/icewind/smb/src/NativeReadStream.php new file mode 100644 index 00000000000..6fd4f5b7f35 --- /dev/null +++ b/apps/files_external/3rdparty/icewind/smb/src/NativeReadStream.php @@ -0,0 +1,101 @@ +<?php +/** + * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com> + * This file is licensed under the Licensed under the MIT license: + * http://opensource.org/licenses/MIT + */ + +namespace Icewind\SMB; + +/** + * Stream optimized for read only usage + */ +class NativeReadStream extends NativeStream { + const CHUNK_SIZE = 1048576; // 1MB chunks + /** + * @var resource + */ + private $readBuffer = null; + + private $bufferSize = 0; + + private $pos = 0; + + public function stream_open($path, $mode, $options, &$opened_path) { + $this->readBuffer = fopen('php://memory', 'r+'); + + return parent::stream_open($path, $mode, $options, $opened_path); + + } + + /** + * Wrap a stream from libsmbclient-php into a regular php stream + * + * @param \Icewind\SMB\NativeState $state + * @param resource $smbStream + * @param string $mode + * @param string $url + * @return resource + */ + public static function wrap($state, $smbStream, $mode, $url) { + stream_wrapper_register('nativesmb', '\Icewind\SMB\NativeReadStream'); + $context = stream_context_create(array( + 'nativesmb' => array( + 'state' => $state, + 'handle' => $smbStream, + 'url' => $url + ) + )); + $fh = fopen('nativesmb://', $mode, false, $context); + stream_wrapper_unregister('nativesmb'); + return $fh; + } + + public function stream_read($count) { + // php reads 8192 bytes at once + // however due to network latency etc, it's faster to read in larger chunks + // and buffer the result + if (!parent::stream_eof() && $this->bufferSize < $count) { + $remaining = $this->readBuffer; + $this->readBuffer = fopen('php://memory', 'r+'); + $this->bufferSize = 0; + stream_copy_to_stream($remaining, $this->readBuffer); + $this->bufferSize += fwrite($this->readBuffer, parent::stream_read(self::CHUNK_SIZE)); + fseek($this->readBuffer, 0); + } + + $result = fread($this->readBuffer, $count); + $this->bufferSize -= $count; + + $read = strlen($result); + $this->pos += $read; + + return $result; + } + + public function stream_seek($offset, $whence = SEEK_SET) { + $result = parent::stream_seek($offset, $whence); + if ($result) { + $this->readBuffer = fopen('php://memory', 'r+'); + $this->bufferSize = 0; + $this->pos = parent::stream_tell(); + } + return $result; + } + + public function stream_eof() { + return $this->bufferSize <= 0 && parent::stream_eof(); + } + + public function stream_tell() { + return $this->pos; + } + + public function stream_write($data) { + return false; + } + + public function stream_truncate($size) { + return false; + } +} diff --git a/apps/files_external/3rdparty/icewind/smb/src/NativeShare.php b/apps/files_external/3rdparty/icewind/smb/src/NativeShare.php index 7efa34df370..228d9cd7d2e 100644 --- a/apps/files_external/3rdparty/icewind/smb/src/NativeShare.php +++ b/apps/files_external/3rdparty/icewind/smb/src/NativeShare.php @@ -174,7 +174,7 @@ class NativeShare extends AbstractShare { $sourceHandle = fopen($source, 'rb'); $targetHandle = $this->state->create($this->buildUrl($target)); - while ($data = fread($sourceHandle, 4096)) { + while ($data = fread($sourceHandle, NativeReadStream::CHUNK_SIZE)) { $this->state->write($targetHandle, $data); } $this->state->close($targetHandle); @@ -214,7 +214,7 @@ class NativeShare extends AbstractShare { throw new InvalidResourceException('Failed opening remote file "' . $source . '" for reading'); } - while ($data = $this->state->read($sourceHandle, 4096)) { + while ($data = $this->state->read($sourceHandle, NativeReadStream::CHUNK_SIZE)) { fwrite($targetHandle, $data); } $this->state->close($sourceHandle); @@ -233,7 +233,7 @@ class NativeShare extends AbstractShare { public function read($source) { $url = $this->buildUrl($source); $handle = $this->state->open($url, 'r'); - return NativeStream::wrap($this->state, $handle, 'r', $url); + return NativeReadStream::wrap($this->state, $handle, 'r', $url); } /** @@ -248,7 +248,7 @@ class NativeShare extends AbstractShare { public function write($source) { $url = $this->buildUrl($source); $handle = $this->state->create($url); - return NativeStream::wrap($this->state, $handle, 'w', $url); + return NativeWriteStream::wrap($this->state, $handle, 'w', $url); } /** diff --git a/apps/files_external/3rdparty/icewind/smb/src/NativeStream.php b/apps/files_external/3rdparty/icewind/smb/src/NativeStream.php index 481395b025a..bce72001b5a 100644 --- a/apps/files_external/3rdparty/icewind/smb/src/NativeStream.php +++ b/apps/files_external/3rdparty/icewind/smb/src/NativeStream.php @@ -20,22 +20,22 @@ class NativeStream implements File { /** * @var \Icewind\SMB\NativeState */ - private $state; + protected $state; /** * @var resource */ - private $handle; + protected $handle; /** * @var bool */ - private $eof = false; + protected $eof = false; /** * @var string */ - private $url; + protected $url; /** * Wrap a stream from libsmbclient-php into a regular php stream @@ -50,9 +50,9 @@ class NativeStream implements File { stream_wrapper_register('nativesmb', '\Icewind\SMB\NativeStream'); $context = stream_context_create(array( 'nativesmb' => array( - 'state' => $state, + 'state' => $state, 'handle' => $smbStream, - 'url' => $url + 'url' => $url ) )); $fh = fopen('nativesmb://', $mode, false, $context); diff --git a/apps/files_external/3rdparty/icewind/smb/src/NativeWriteStream.php b/apps/files_external/3rdparty/icewind/smb/src/NativeWriteStream.php new file mode 100644 index 00000000000..69a270f160f --- /dev/null +++ b/apps/files_external/3rdparty/icewind/smb/src/NativeWriteStream.php @@ -0,0 +1,100 @@ +<?php +/** + * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com> + * This file is licensed under the Licensed under the MIT license: + * http://opensource.org/licenses/MIT + */ + +namespace Icewind\SMB; + +/** + * Stream optimized for write only usage + */ +class NativeWriteStream extends NativeStream { + const CHUNK_SIZE = 1048576; // 1MB chunks + /** + * @var resource + */ + private $writeBuffer = null; + + private $bufferSize = 0; + + private $pos = 0; + + public function stream_open($path, $mode, $options, &$opened_path) { + $this->writeBuffer = fopen('php://memory', 'r+'); + + return parent::stream_open($path, $mode, $options, $opened_path); + + } + + /** + * Wrap a stream from libsmbclient-php into a regular php stream + * + * @param \Icewind\SMB\NativeState $state + * @param resource $smbStream + * @param string $mode + * @param string $url + * @return resource + */ + public static function wrap($state, $smbStream, $mode, $url) { + stream_wrapper_register('nativesmb', '\Icewind\SMB\NativeWriteStream'); + $context = stream_context_create(array( + 'nativesmb' => array( + 'state' => $state, + 'handle' => $smbStream, + 'url' => $url + ) + )); + $fh = fopen('nativesmb://', $mode, false, $context); + stream_wrapper_unregister('nativesmb'); + return $fh; + } + + public function stream_seek($offset, $whence = SEEK_SET) { + $this->flushWrite(); + $result = parent::stream_seek($offset, $whence); + if ($result) { + $this->pos = parent::stream_tell(); + } + return $result; + } + + private function flushWrite() { + rewind($this->writeBuffer); + $this->state->write($this->handle, stream_get_contents($this->writeBuffer)); + $this->writeBuffer = fopen('php://memory', 'r+'); + $this->bufferSize = 0; + } + + public function stream_write($data) { + $written = fwrite($this->writeBuffer, $data); + $this->bufferSize += $written; + $this->pos += $written; + + if ($this->bufferSize >= self::CHUNK_SIZE) { + $this->flushWrite(); + } + + return $written; + } + + public function stream_close() { + $this->flushWrite(); + return parent::stream_close(); + } + + public function stream_tell() { + return $this->pos; + } + + public function stream_read($count) { + return false; + } + + public function stream_truncate($size) { + $this->flushWrite(); + $this->pos = $size; + return parent::stream_truncate($size); + } +} diff --git a/apps/files_external/3rdparty/icewind/smb/src/NotifyHandler.php b/apps/files_external/3rdparty/icewind/smb/src/NotifyHandler.php index 194e848502b..6ad565555bf 100644 --- a/apps/files_external/3rdparty/icewind/smb/src/NotifyHandler.php +++ b/apps/files_external/3rdparty/icewind/smb/src/NotifyHandler.php @@ -59,7 +59,10 @@ class NotifyHandler implements INotifyHandler { public function listen($callback) { if ($this->listening) { $this->connection->read(function ($line) use ($callback) { - return $callback($this->parseChangeLine($line)); + $change = $this->parseChangeLine($line); + if ($change) { + return $callback($change); + } }); } } |