Skip to content

Commit

Permalink
Merge PR #157
Browse files Browse the repository at this point in the history
  • Loading branch information
mikey179 committed Aug 1, 2017
2 parents 03c1427 + 0c05416 commit 1205449
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
1.6.5 (2017-08-01)
------------------

* fixed #157 seeking before beginning of file should fail, reported and fixed by @merijnvdk


1.6.4 (2016-07-18)
------------------

Expand Down
21 changes: 13 additions & 8 deletions src/main/php/org/bovigo/vfs/content/SeekableFileContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,29 @@ protected abstract function doRead($offset, $count);
*/
public function seek($offset, $whence)
{
$newOffset = $this->offset;
switch ($whence) {
case SEEK_CUR:
$this->offset += $offset;
return true;
$newOffset += $offset;
break;

case SEEK_END:
$this->offset = $this->size() + $offset;
return true;
$newOffset = $this->size() + $offset;
break;

case SEEK_SET:
$this->offset = $offset;
return true;
$newOffset = $offset;
break;

default:
return false;
}

return false;

if ($newOffset<0) {
return false;
}
$this->offset = $newOffset;
return true;
}

/**
Expand Down
31 changes: 31 additions & 0 deletions src/test/php/org/bovigo/vfs/vfsStreamFileTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@ public function seekEmptyFile()
$this->assertEquals(2, $this->file->getBytesRead());
}

/**
* @test
* @since 1.6.5
*/
public function seekEmptyFileBeforeBeginningDoesNotChangeOffset()
{
$this->assertFalse($this->file->seek(-5, SEEK_SET), 'Seek before beginning of file');
$this->assertEquals(0, $this->file->getBytesRead());
}

/**
* test seeking to offset
*
Expand Down Expand Up @@ -160,6 +170,27 @@ public function seekRead()
$this->assertEquals(11, $this->file->getBytesRead());
}

/**
* @test
* @since 1.6.5
*/
public function seekFileBeforeBeginningDoesNotChangeOffset()
{
$this->file->setContent('foobarbaz');
$this->assertFalse($this->file->seek(-5, SEEK_SET), 'Seek before beginning of file');
$this->assertEquals(0, $this->file->getBytesRead());
$this->assertTrue($this->file->seek(2, SEEK_CUR));
$this->assertFalse($this->file->seek(-5, SEEK_SET), 'Seek before beginning of file');
$this->assertEquals(2, $this->file->getBytesRead());
$this->assertEquals('obarbaz', $this->file->readUntilEnd());
$this->assertFalse($this->file->seek(-5, SEEK_CUR), 'Seek before beginning of file');
$this->assertEquals(2, $this->file->getBytesRead());
$this->assertEquals('obarbaz', $this->file->readUntilEnd());
$this->assertFalse($this->file->seek(-20, SEEK_END), 'Seek before beginning of file');
$this->assertEquals(2, $this->file->getBytesRead());
$this->assertEquals('obarbaz', $this->file->readUntilEnd());
}

/**
* test writing data into the file
*
Expand Down

0 comments on commit 1205449

Please sign in to comment.