diff --git a/lib/UvHandle.php b/lib/UvHandle.php index d693a76..1095e3f 100644 --- a/lib/UvHandle.php +++ b/lib/UvHandle.php @@ -176,10 +176,10 @@ class UvHandle implements Handle } } else { StatCache::clear($this->path); - $newPosition = $this->position + $length; - $delta = $newPosition - $this->position; - $this->position = ($this->mode[0] === "a") ? $this->position : $newPosition; - $this->size += $delta; + $this->position += $length; + if ($this->position > $this->size) { + $this->size = $this->position; + } $deferred->resolve($length); } }; diff --git a/test/HandleTest.php b/test/HandleTest.php index 8aedf87..13cea7d 100644 --- a/test/HandleTest.php +++ b/test/HandleTest.php @@ -94,6 +94,23 @@ abstract class HandleTest extends TestCase }); } + public function testWriteInAppendMode() + { + $this->execute(function () { + $path = Fixture::path() . "/write"; + /** @var \Amp\File\Handle $handle */ + $handle = yield File\open($path, "a+"); + $this->assertSame(0, $handle->tell()); + yield $handle->write("bar"); + yield $handle->write("foo"); + yield $handle->write("baz"); + $this->assertSame(9, $handle->tell()); + yield $handle->seek(0); + $this->assertSame(0, $handle->tell()); + $this->assertSame("barfoobaz", yield $handle->read()); + }); + } + public function testReadingToEof() { $this->execute(function () {