implement feof for native stream

This commit is contained in:
Robin Appelman 2014-08-26 19:36:16 +02:00
commit fcb7aa6a55
2 changed files with 27 additions and 1 deletions

View file

@ -25,6 +25,11 @@ class NativeStream implements File {
*/ */
private $handle; private $handle;
/**
* @var bool
*/
private $eof = false;
/** /**
* Wrap a stream from libsmbclient-php into a regular php stream * Wrap a stream from libsmbclient-php into a regular php stream
* *
@ -51,6 +56,7 @@ class NativeStream implements File {
} }
public function stream_eof() { public function stream_eof() {
return $this->eof;
} }
public function stream_flush() { public function stream_flush() {
@ -65,10 +71,15 @@ class NativeStream implements File {
} }
public function stream_read($count) { public function stream_read($count) {
return $this->state->read($this->handle, $count); $result = $this->state->read($this->handle, $count);
if (strlen($result) < $count) {
$this->eof = true;
}
return $result;
} }
public function stream_seek($offset, $whence = SEEK_SET) { public function stream_seek($offset, $whence = SEEK_SET) {
$this->eof = false;
return $this->state->lseek($this->handle, $offset, $whence); return $this->state->lseek($this->handle, $offset, $whence);
} }

View file

@ -97,6 +97,21 @@ class NativeStream extends \PHPUnit_Framework_TestCase {
$this->assertEquals('foo', stream_get_contents($fh)); $this->assertEquals('foo', stream_get_contents($fh));
} }
public function testEOF() {
if (version_compare(phpversion(), '5.4.0', '<')) {
$this->markTestSkipped('php <5.4 doesn\'t support truncate for stream wrappers');
}
$fh = $this->share->write($this->root . '/foobar');
fwrite($fh, 'foobar');
fclose($fh);
$fh = $this->share->read($this->root . '/foobar');
fread($fh, 3);
$this->assertFalse(feof($fh));
fread($fh, 5);
$this->assertTrue(feof($fh));
}
public function testLockUnsupported() { public function testLockUnsupported() {
$fh = $this->share->write($this->root . '/foobar'); $fh = $this->share->write($this->root . '/foobar');
$this->assertFalse(flock($fh, LOCK_SH)); $this->assertFalse(flock($fh, LOCK_SH));