mirror of
https://codeberg.org/icewind/SMB.git
synced 2026-06-03 17:24:07 +02:00
Clean native stream wrapping
This commit is contained in:
parent
5c48e22929
commit
8d41d7a97b
3 changed files with 26 additions and 59 deletions
|
|
@ -25,11 +25,6 @@ class NativeShare implements IShare {
|
|||
*/
|
||||
private $state;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private static $registed = false;
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param string $name
|
||||
|
|
@ -38,15 +33,6 @@ class NativeShare implements IShare {
|
|||
public function __construct($server, $name) {
|
||||
$this->server = $server;
|
||||
$this->name = $name;
|
||||
self::registerHandlers();
|
||||
}
|
||||
|
||||
private static function registerHandlers() {
|
||||
if (self::$registed) {
|
||||
return;
|
||||
}
|
||||
self::$registed = true;
|
||||
stream_wrapper_register('nativesmb', '\Icewind\SMB\NativeStream');
|
||||
}
|
||||
|
||||
public static function registerErrorHandler() {
|
||||
|
|
@ -316,13 +302,7 @@ class NativeShare implements IShare {
|
|||
*/
|
||||
public function read($source) {
|
||||
$handle = $this->fopen($source, 'r');
|
||||
$context = stream_context_create(array(
|
||||
'nativesmb' => array(
|
||||
'state' => $this->state,
|
||||
'handle' => $handle
|
||||
)
|
||||
));
|
||||
return fopen('nativesmb://dummy', 'r', false, $context);
|
||||
return NativeStream::wrap($this->state, $handle, 'r');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -336,12 +316,6 @@ class NativeShare implements IShare {
|
|||
*/
|
||||
public function write($source) {
|
||||
$handle = $this->create($source);
|
||||
$context = stream_context_create(array(
|
||||
'nativesmb' => array(
|
||||
'state' => $this->state,
|
||||
'handle' => $handle
|
||||
)
|
||||
));
|
||||
return fopen('nativesmb://dummy', 'w', false, $context);
|
||||
return NativeStream::wrap($this->state, $handle, 'w');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,6 +14,27 @@ class NativeStream {
|
|||
|
||||
private $handle;
|
||||
|
||||
/**
|
||||
* Wrap a stream from libsmbclient-php into a regular php stream
|
||||
*
|
||||
* @param resource $state
|
||||
* @param resource $smbStream
|
||||
* @param string $mode
|
||||
* @return resource
|
||||
*/
|
||||
public static function wrap($state, $smbStream, $mode) {
|
||||
stream_wrapper_register('nativesmb', '\Icewind\SMB\NativeStream');
|
||||
$context = stream_context_create(array(
|
||||
'nativesmb' => array(
|
||||
'state' => $state,
|
||||
'handle' => $smbStream
|
||||
)
|
||||
));
|
||||
$fh = fopen('nativesmb://', $mode, false, $context);
|
||||
stream_wrapper_unregister('nativesmb');
|
||||
return $fh;
|
||||
}
|
||||
|
||||
public function stream_close() {
|
||||
return smbclient_close($this->state, $this->handle);
|
||||
}
|
||||
|
|
@ -27,18 +48,9 @@ class NativeStream {
|
|||
|
||||
public function stream_open() {
|
||||
$context = stream_context_get_options($this->context);
|
||||
if (isset($context['nativesmb'])) {
|
||||
$context = $context['nativesmb'];
|
||||
} else {
|
||||
throw new Exception('Invalid context');
|
||||
}
|
||||
if (isset($context['state']) and isset($context['handle'])) {
|
||||
$this->state = $context['state'];
|
||||
$this->handle = $context['handle'];
|
||||
$this->state = $context['nativesmb']['state'];
|
||||
$this->handle = $context['nativesmb']['handle'];
|
||||
return true;
|
||||
} else {
|
||||
throw new Exception('Invalid context');
|
||||
}
|
||||
}
|
||||
|
||||
public function stream_read($count) {
|
||||
|
|
|
|||
|
|
@ -102,23 +102,4 @@ class NativeStream extends \PHPUnit_Framework_TestCase {
|
|||
}
|
||||
$this->share->rmdir($dir);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Icewind\SMB\Exception
|
||||
*/
|
||||
public function testNoContext() {
|
||||
return fopen('nativesmb://dummy', 'r');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Icewind\SMB\Exception
|
||||
*/
|
||||
public function testInvalidContext() {
|
||||
$context = stream_context_create(array(
|
||||
'nativesmb' => array(
|
||||
'foo' => 'bar'
|
||||
)
|
||||
));
|
||||
return fopen('nativesmb://dummy', 'r', false, $context);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue