Skip to content

Commit

Permalink
Fix for Issue bovigo#222: handle EOF and offset correctly on read
Browse files Browse the repository at this point in the history
The EOF flag on native file streams is only set after reading *past*
the end of the buffer, never on open or seek.

I believe all the failing tests here are asserting behaviour that
doesn't match the native file handler.
  • Loading branch information
IMSoP committed Feb 26, 2020
1 parent ef000db commit ad9449b
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/internal/OpenedFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,22 @@ class OpenedFile implements FileHandle
private $content;
/** @var int */
private $offset = 0;

/**
* has the stream reached EOF; this happens on the first read *after* seeking to the end of the file
*
* @var bool
*/
private $eof=false;

/**
* mode the file was opened with
*
* @var int
*/
private $mode;

public function __construct(vfsFile $file, FileContent $content, int $mode)
public function __construct(vfsFile $file, FileContent $content, int $mode)
{
$this->file = $file;
$this->content = $content;
Expand Down Expand Up @@ -105,8 +113,14 @@ public function read(int $count): string
}

$this->file->lastAccessed(time());

if ( $this->offset >= $this->size() ) {
$this->eof = true;
return '';
}

$data = $this->content->read($this->offset, $count);
$this->offset += $count;
$this->offset += strlen($data);

return $data;
}
Expand Down Expand Up @@ -169,7 +183,7 @@ public function truncate(int $size): bool
*/
public function eof(): bool
{
return $this->content->size() <= $this->offset;
return $this->eof;
}

/**
Expand Down Expand Up @@ -210,6 +224,7 @@ public function seek(int $offset, int $whence): bool
}

$this->offset = $newOffset;
$this->eof = false;

return true;
}
Expand Down

0 comments on commit ad9449b

Please sign in to comment.