From 654596edeb2255277d21049c852c3eed886659c2 Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Fri, 1 Mar 2019 10:38:04 -0600 Subject: [PATCH] Fix UV writing in append mode --- lib/UvHandle.php | 8 ++++---- test/HandleTest.php | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 4 deletions(-) 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 () {