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

fix using FSEEK_END with SeekableHttpStream to get file size #33718

Merged
merged 1 commit into from
Sep 15, 2022
Merged
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
81 changes: 60 additions & 21 deletions lib/private/Files/Stream/SeekableHttpStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
class SeekableHttpStream implements File {
private const PROTOCOL = 'httpseek';

private static $registered = false;
private static bool $registered = false;

/**
* Registers the stream wrapper using the `httpseek://` url scheme
Expand Down Expand Up @@ -73,24 +73,26 @@ public static function open(callable $callback) {
/** @var callable */
private $openCallback;

/** @var resource */
/** @var ?resource|closed-resource */
private $current;
/** @var int */
private $offset = 0;
/** @var int */
private $length = 0;
private int $offset = 0;
private int $length = 0;
private bool $needReconnect = false;

private function reconnect(int $start) {
private function reconnect(int $start): bool {
$this->needReconnect = false;
$range = $start . '-';
if ($this->current != null) {
if ($this->hasOpenStream()) {
fclose($this->current);
}

$this->current = ($this->openCallback)($range);
$stream = ($this->openCallback)($range);

if ($this->current === false) {
if ($stream === false) {
$this->current = null;
return false;
}
$this->current = $stream;

$responseHead = stream_get_meta_data($this->current)['wrapper_data'];

Expand All @@ -109,6 +111,7 @@ private function reconnect(int $start) {
return preg_match('#^content-range:#i', $v) === 1;
}));
if (!$rangeHeaders) {
$this->current = null;
return false;
}
$contentRange = $rangeHeaders[0];
Expand All @@ -119,6 +122,7 @@ private function reconnect(int $start) {
$length = intval(explode('/', $range)[1]);

if ($begin !== $start) {
$this->current = null;
return false;
}

Expand All @@ -128,6 +132,28 @@ private function reconnect(int $start) {
return true;
}

/**
* @return ?resource
*/
private function getCurrent() {
if ($this->needReconnect) {
$this->reconnect($this->offset);
}
if (is_resource($this->current)) {
return $this->current;
} else {
return null;
}
}

/**
* @return bool
* @psalm-assert-if-true resource $this->current
*/
private function hasOpenStream(): bool {
return is_resource($this->current);
}

public function stream_open($path, $mode, $options, &$opened_path) {
$options = stream_context_get_options($this->context)[self::PROTOCOL];
$this->openCallback = $options['callback'];
Expand All @@ -136,10 +162,10 @@ public function stream_open($path, $mode, $options, &$opened_path) {
}

public function stream_read($count) {
if (!$this->current) {
if (!$this->getCurrent()) {
return false;
}
$ret = fread($this->current, $count);
$ret = fread($this->getCurrent(), $count);
$this->offset += strlen($ret);
return $ret;
}
Expand All @@ -149,48 +175,61 @@ public function stream_seek($offset, $whence = SEEK_SET) {
case SEEK_SET:
if ($offset === $this->offset) {
return true;
} else {
$this->offset = $offset;
}
return $this->reconnect($offset);
break;
case SEEK_CUR:
if ($offset === 0) {
return true;
} else {
$this->offset += $offset;
}
return $this->reconnect($this->offset + $offset);
break;
case SEEK_END:
if ($this->length === 0) {
return false;
} elseif ($this->length + $offset === $this->offset) {
return true;
} else {
$this->offset = $this->length + $offset;
}
return $this->reconnect($this->length + $offset);
break;
}
return false;

if ($this->hasOpenStream()) {
fclose($this->current);
}
$this->current = null;
$this->needReconnect = true;
return true;
}

public function stream_tell() {
return $this->offset;
}

public function stream_stat() {
if (is_resource($this->current)) {
return fstat($this->current);
if ($this->getCurrent()) {
return fstat($this->getCurrent());
} else {
return false;
}
}

public function stream_eof() {
if (is_resource($this->current)) {
return feof($this->current);
if ($this->getCurrent()) {
return feof($this->getCurrent());
} else {
return true;
}
}

public function stream_close() {
if (is_resource($this->current)) {
if ($this->hasOpenStream()) {
fclose($this->current);
}
$this->current = null;
}

public function stream_write($data) {
Expand Down
4 changes: 4 additions & 0 deletions tests/lib/Files/ObjectStore/AzureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@ protected function getInstance() {

return new Azure($config['arguments']);
}

public function testFseekSize() {
$this->markTestSkipped('azure does not support seeking at the moment');
}
}
15 changes: 15 additions & 0 deletions tests/lib/Files/ObjectStore/ObjectStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,19 @@ public function testCopy() {

$this->assertEquals('foobar', stream_get_contents($instance->readObject('target')));
}

public function testFseekSize() {
$instance = $this->getInstance();

$textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
$size = filesize($textFile);
$instance->writeObject('source', fopen($textFile, 'r'));

$fh = $instance->readObject('source');

fseek($fh, 0, SEEK_END);
$pos = ftell($fh);

$this->assertEquals($size, $pos);
}
}
14 changes: 14 additions & 0 deletions tests/lib/Files/Storage/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -664,4 +664,18 @@ public function testWriteStream() {
$this->assertStringEqualsFile($textFile, $storage->file_get_contents('test.txt'));
$this->assertEquals('resource (closed)', gettype($source));
}

public function testFseekSize() {
$textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
$this->instance->file_put_contents('bar.txt', file_get_contents($textFile));

$size = $this->instance->filesize('bar.txt');
$this->assertEquals(filesize($textFile), $size);
$fh = $this->instance->fopen('bar.txt', 'r');

fseek($fh, 0, SEEK_END);
$pos = ftell($fh);

$this->assertEquals($size, $pos);
}
}