Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 4 additions & 24 deletions apps/files_external/lib/Lib/Storage/SFTPReadStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ class SFTPReadStream implements File {
private $eof = false;

private $buffer = '';
private bool $pendingRead = false;
private int $size = 0;

public static function register($protocol = 'sftpread') {
Expand Down Expand Up @@ -100,8 +99,6 @@ public function stream_open($path, $mode, $options, &$opened_path) {
return false;
}

$this->request_chunk(256 * 1024);

return true;
}

Expand All @@ -127,7 +124,6 @@ private function seekTo(int $offset): void {
$this->buffer = '';
// a previous read may have reached the end of the file, reading from the new position has to work again
$this->eof = false;
$this->request_chunk(256 * 1024);
}

#[\Override]
Expand All @@ -138,11 +134,10 @@ public function stream_tell() {
#[\Override]
public function stream_read($count) {
if (!$this->eof && strlen($this->buffer) < $count) {
// Complete the read before returning control to other users of the shared connection.
$this->request_chunk(256 * 1024);
$chunk = $this->read_chunk();
$this->buffer .= $chunk;
if (!$this->eof) {
$this->request_chunk(256 * 1024);
}
}

$data = substr($this->buffer, 0, $count);
Expand All @@ -152,23 +147,12 @@ public function stream_read($count) {
return $data;
}

private function request_chunk(int $size) {
if ($this->pendingRead) {
$this->invokeSftp($this->sftp, 'get_sftp_packet');
}

private function request_chunk(int $size): void {
$packet = pack('Na*N3', strlen($this->handle), $this->handle, $this->internalPosition / 4294967296, $this->internalPosition, $size);
$this->pendingRead = true;
try {
$this->invokeSftp($this->sftp, 'send_sftp_packet', [NET_SFTP_READ, $packet]);
return true;
} catch (\Throwable) {
return false;
}
$this->invokeSftp($this->sftp, 'send_sftp_packet', [NET_SFTP_READ, $packet]);
}

private function read_chunk() {
$this->pendingRead = false;
$response = $this->invokeSftp($this->sftp, 'get_sftp_packet');

switch ($this->getSftpProperty($this->sftp, 'packet_type')) {
Expand Down Expand Up @@ -225,10 +209,6 @@ public function stream_eof() {

#[\Override]
public function stream_close() {
// we still have a read request incoming that needs to be handled before we can close
if ($this->pendingRead) {
$this->invokeSftp($this->sftp, 'get_sftp_packet');
}
if (!$this->invokeSftp($this->sftp, 'close_handle', [$this->handle])) {
return false;
}
Expand Down
62 changes: 60 additions & 2 deletions apps/files_external/tests/Storage/SftpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

/**
* Class SftpTest
*
*
* @package OCA\Files_External\Tests\Storage
*/
#[\PHPUnit\Framework\Attributes\Group(name: 'DB')]
Expand Down Expand Up @@ -130,4 +128,64 @@ public static function configProvider(): array {
],
];
}

public static function interleavedReadProvider(): array {
return [
'after opening' => [false],
'after reading' => [true],
];
}

/**
* A read stream must not leave SFTP responses pending on the shared client:
* another operation could consume a READ response as its own.
*
* Exercise both opening and reading, since either can initiate read-ahead.
* Clear the stat cache before opening so statting another file requires a
* network request. Use the client directly so packet errors are not swallowed
* by the storage layer.
*
* On a sequential-processing server, the unfixed implementation makes STAT
* consume the pending DATA response. Servers that reorder responses may mask
* the bug.
*/
#[\PHPUnit\Framework\Attributes\DataProvider('interleavedReadProvider')]
public function testReadStreamAllowsOtherSftpOperations(bool $readFirst): void {
$content = str_repeat('0123456789abcdef', 40000);
$this->assertSame(
strlen($content),
$this->instance->file_put_contents('source.bin', $content),
);
$this->assertSame(
5,
$this->instance->file_put_contents('other.txt', 'other'),
);

$connection = $this->instance->getConnection();
$connection->clearStatCache();

$stream = $this->instance->fopen('source.bin', 'r');
$this->assertIsResource($stream);

try {
$prefix = '';
if ($readFirst) {
$prefix = fread($stream, 8192);
$this->assertSame(substr($content, 0, 8192), $prefix);
}

$stat = $connection->stat(
$this->instance->getRoot() . 'other.txt',
);
$this->assertIsArray($stat);
$this->assertSame(5, $stat['size']);

$this->assertSame(
$content,
$prefix . stream_get_contents($stream),
);
} finally {
fclose($stream);
}
}
}
Loading