Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read larger chunks from sftp in stream wrapper #1452

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 38 additions & 7 deletions phpseclib/Net/SFTP/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace phpseclib3\Net\SFTP;

use phpseclib3\Common\Functions\Strings;
use phpseclib3\Crypt\RSA;
use phpseclib3\Net\SFTP;
use phpseclib3\Net\SSH2;
Expand Down Expand Up @@ -113,6 +114,16 @@ class Stream
*/
private $notification;

private $readAheadLength = 81920;

/**
* Buffer of data being read
*
* @var string
* @access public
*/
private $buffer = '';
Comment on lines +123 to +125

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this say @access private?


/**
* Registers this class as a URL wrapper.
*
Expand Down Expand Up @@ -207,6 +218,9 @@ protected function parse_path($path)
if (isset($context[$scheme]['privkey']) && $context[$scheme]['privkey'] instanceof RSA) {
$pass = $context[$scheme]['privkey'];
}
if (isset($context[$scheme]['readahead_length'])) {
$this->readAheadLength = $context[$scheme]['readahead_length'];
}

if (!isset($user) || !isset($pass)) {
return false;
Expand Down Expand Up @@ -315,16 +329,24 @@ private function _stream_read($count)
// return false;
//}

$result = $this->sftp->get($this->path, false, $this->pos, $count);
if (isset($this->notification) && is_callable($this->notification)) {
if ($result === false) {
call_user_func($this->notification, STREAM_NOTIFY_FAILURE, STREAM_NOTIFY_SEVERITY_ERR, $this->sftp->getLastSFTPError(), NET_SFTP_OPEN, 0, 0);
return 0;
if (strlen($this->buffer) < $count) {
// read 80k chunks (configurable by the readahead_length context option) from sftp even though php will only request 8k chunk
// this allows us to minimize the overhead of making sftp requests
$chunk = $this->sftp->get($this->path, false, $this->pos, $this->readAheadLength);
if (isset($this->notification) && is_callable($this->notification)) {
if ($chunk === false) {
call_user_func($this->notification, STREAM_NOTIFY_FAILURE, STREAM_NOTIFY_SEVERITY_ERR, $this->sftp->getLastSFTPError(), NET_SFTP_OPEN, 0, 0);
return 0;
}
// seems that PHP calls stream_read in 8k chunks
call_user_func($this->notification, STREAM_NOTIFY_PROGRESS, STREAM_NOTIFY_SEVERITY_INFO, '', 0, strlen($chunk), $this->size);
}
// seems that PHP calls stream_read in 8k chunks
call_user_func($this->notification, STREAM_NOTIFY_PROGRESS, STREAM_NOTIFY_SEVERITY_INFO, '', 0, strlen($result), $this->size);

$this->buffer .= $chunk;
}

$result = Strings::shift($this->buffer, $count);

if (empty($result)) { // ie. false or empty string
$this->eof = true;
return false;
Expand All @@ -343,6 +365,9 @@ private function _stream_read($count)
*/
private function _stream_write($data)
{
// invalidate any readahead buffer on write
$this->buffer = '';

switch ($this->mode) {
case 'r':
return false;
Expand Down Expand Up @@ -408,6 +433,9 @@ private function _stream_eof()
*/
private function _stream_seek($offset, $whence)
{
// invalidate any readahead buffer on seek
$this->buffer = '';

switch ($whence) {
case SEEK_SET:
if ($offset >= $this->size || $offset < 0) {
Expand Down Expand Up @@ -721,6 +749,9 @@ private function _url_stat($path, $flags)
*/
private function _stream_truncate($new_size)
{
// invalidate any readahead buffer on truncate
$this->buffer = '';

if (!$this->sftp->truncate($this->path, $new_size)) {
return false;
}
Expand Down