mirror of
https://codeberg.org/icewind/SMB.git
synced 2026-06-03 17:24:07 +02:00
Also add Share::getWrite to non native shares
This commit is contained in:
parent
7c2393afb0
commit
89d73346f2
7 changed files with 112 additions and 35 deletions
|
|
@ -75,14 +75,10 @@ class Connection extends RawConnection {
|
|||
}
|
||||
}
|
||||
|
||||
public function close() {
|
||||
public function close($terminate = true) {
|
||||
if (is_resource($this->getInputStream())) {
|
||||
$this->write('close' . PHP_EOL);
|
||||
}
|
||||
}
|
||||
|
||||
public function __destruct() {
|
||||
$this->close();
|
||||
parent::__destruct();
|
||||
parent::close($terminate);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -93,4 +93,12 @@ interface IShare {
|
|||
* @return resource a read only stream with the contents of the remote file
|
||||
*/
|
||||
public function read($source);
|
||||
|
||||
/**
|
||||
* Open a writable stream to a remote file
|
||||
*
|
||||
* @param string $target
|
||||
* @return resource a write only stream to upload a remote file
|
||||
*/
|
||||
public function write($target);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ class RawConnection {
|
|||
2 => array('pipe', 'w'), // child writes to stderr
|
||||
3 => array('pipe', 'r'), // child reads from fd#3
|
||||
4 => array('pipe', 'r'), // child reads from fd#4
|
||||
5 => array('pipe', 'w') // child writes to fd#5
|
||||
5 => array('pipe', 'w') // child writes to fd#5
|
||||
);
|
||||
setlocale(LC_ALL, Server::LOCALE);
|
||||
$env = array_merge($env, array(
|
||||
|
|
@ -62,8 +62,8 @@ class RawConnection {
|
|||
* @param string $input
|
||||
*/
|
||||
public function write($input) {
|
||||
fwrite($this->pipes[0], $input);
|
||||
fflush($this->pipes[0]);
|
||||
fwrite($this->getInputStream(), $input);
|
||||
fflush($this->getInputStream());
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -72,7 +72,7 @@ class RawConnection {
|
|||
* @return string
|
||||
*/
|
||||
public function read() {
|
||||
return trim(fgets($this->pipes[1]));
|
||||
return trim(fgets($this->getOutputStream()));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -88,12 +88,28 @@ class RawConnection {
|
|||
return $output;
|
||||
}
|
||||
|
||||
public function getInputStream() {
|
||||
return $this->pipes[0];
|
||||
}
|
||||
|
||||
public function getOutputStream() {
|
||||
return $this->pipes[1];
|
||||
}
|
||||
|
||||
public function getInputStream() {
|
||||
return $this->pipes[0];
|
||||
public function getErrorStream() {
|
||||
return $this->pipes[2];
|
||||
}
|
||||
|
||||
public function getAuthStream() {
|
||||
return $this->pipes[3];
|
||||
}
|
||||
|
||||
public function getFileInputStream() {
|
||||
return $this->pipes[4];
|
||||
}
|
||||
|
||||
public function getFileOutputStream() {
|
||||
return $this->pipes[5];
|
||||
}
|
||||
|
||||
public function writeAuthentication($user, $password) {
|
||||
|
|
@ -101,16 +117,25 @@ class RawConnection {
|
|||
? "username=$user"
|
||||
: "username=$user\npassword=$password";
|
||||
|
||||
if (fwrite($this->pipes[3], $auth) === false) {
|
||||
fclose($this->pipes[3]);
|
||||
if (fwrite($this->getAuthStream(), $auth) === false) {
|
||||
fclose($this->getAuthStream());
|
||||
return false;
|
||||
}
|
||||
fclose($this->pipes[3]);
|
||||
fclose($this->getAuthStream());
|
||||
return true;
|
||||
}
|
||||
|
||||
public function __destruct() {
|
||||
proc_terminate($this->process);
|
||||
public function close($terminate = true) {
|
||||
if (!is_resource($this->process)) {
|
||||
return;
|
||||
}
|
||||
if ($terminate) {
|
||||
proc_terminate($this->process);
|
||||
}
|
||||
proc_close($this->process);
|
||||
}
|
||||
|
||||
public function __destruct() {
|
||||
$this->close();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,8 @@
|
|||
|
||||
namespace Icewind\SMB;
|
||||
|
||||
use Icewind\Streams\CallbackWrapper;
|
||||
|
||||
class Share implements IShare {
|
||||
/**
|
||||
* @var Server $server
|
||||
|
|
@ -22,7 +24,7 @@ class Share implements IShare {
|
|||
/**
|
||||
* @var Connection $connection
|
||||
*/
|
||||
private $connection;
|
||||
public $connection;
|
||||
|
||||
private $serverTimezone;
|
||||
|
||||
|
|
@ -203,25 +205,68 @@ class Share implements IShare {
|
|||
}
|
||||
|
||||
/**
|
||||
* Open a readable stream top a remote file
|
||||
* Open a readable stream to a remote file
|
||||
*
|
||||
* @param string $source
|
||||
* @return resource a read only stream with the contents of the remote file
|
||||
*/
|
||||
public function read($source) {
|
||||
$source = $this->escapePath($source);
|
||||
// since we do binary transfer over STDOUT we create a new connection
|
||||
// since returned stream is closed by the caller we need to create a new instance
|
||||
// since we can't re-use the same file descriptor over multiple calls
|
||||
$command = Server::CLIENT . ' --authentication-file=/proc/self/fd/3' .
|
||||
' //' . $this->server->getHost() . '/' . $this->name
|
||||
. ' -c \'get ' . $source . ' -\'';
|
||||
. ' -c \'get ' . $source . ' /proc/self/fd/5\'';
|
||||
$connection = new Connection($command);
|
||||
$connection->writeAuthentication($this->server->getUser(), $this->server->getPassword());
|
||||
$fh = $connection->getOutputStream();
|
||||
$fh = $connection->getFileOutputStream();
|
||||
//save the connection as context of the stream to prevent it going out of scope and cleaning up the resource
|
||||
stream_context_set_option($fh, 'file', 'connection', $connection);
|
||||
return $fh;
|
||||
}
|
||||
|
||||
/**
|
||||
* Open a writable stream to a remote file
|
||||
*
|
||||
* @param string $target
|
||||
* @return resource a write only stream to upload a remote file
|
||||
*/
|
||||
public function write($target) {
|
||||
$target = $this->escapePath($target);
|
||||
// since returned stream is closed by the caller we need to create a new instance
|
||||
// since we can't re-use the same file descriptor over multiple calls
|
||||
$command = Server::CLIENT . ' --authentication-file=/proc/self/fd/3' .
|
||||
' //' . $this->server->getHost() . '/' . $this->name
|
||||
. ' -c \'put /proc/self/fd/4 ' . $target . '\'';
|
||||
$connection = new RawConnection($command);
|
||||
$connection->writeAuthentication($this->server->getUser(), $this->server->getPassword());
|
||||
$fh = $connection->getFileInputStream();
|
||||
|
||||
// use a close callback to ensure the upload is finished before continuing
|
||||
// this also serves as a way to keep the connection in scope
|
||||
return CallbackWrapper::wrap($fh, null, null, function () use ($connection) {
|
||||
$connection->close(false); // dont terminate, give the upload some time
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param resource $source
|
||||
* @param callable $callback
|
||||
* @return resource
|
||||
*/
|
||||
protected function addCloseCallback($source, $callback) {
|
||||
$context = stream_context_create(array(
|
||||
'callback' => array(
|
||||
'source' => $source,
|
||||
'close' => $callback
|
||||
)
|
||||
));
|
||||
stream_wrapper_register('callback', '\Icewind\Streams\CallbackWrapper');
|
||||
$stream = fopen('callback://', 'r+', false, $context);
|
||||
stream_wrapper_unregister('callback');
|
||||
return $stream;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $command
|
||||
* @return array
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue