Skip to content

Commit ba57064

Browse files
committed
Fix UV writing in append mode
1 parent 25d8ef6 commit ba57064

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

lib/UvHandle.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,10 @@ private function push(string $data): Promise
172172
}
173173
} else {
174174
StatCache::clear($this->path);
175-
$newPosition = $this->position + $length;
176-
$delta = $newPosition - $this->position;
177-
$this->position = ($this->mode[0] === "a") ? $this->position : $newPosition;
178-
$this->size += $delta;
175+
$this->position += $length;
176+
if ($this->position > $this->size) {
177+
$this->size = $this->position;
178+
}
179179
$deferred->resolve($length);
180180
}
181181
};

test/HandleTest.php

+17
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,23 @@ public function testWriteAfterEnd()
7979
});
8080
}
8181

82+
public function testWriteInAppendMode()
83+
{
84+
$this->execute(function () {
85+
$path = Fixture::path() . "/write";
86+
/** @var \Amp\File\Handle $handle */
87+
$handle = yield File\open($path, "a+");
88+
$this->assertSame(0, $handle->tell());
89+
yield $handle->write("bar");
90+
yield $handle->write("foo");
91+
yield $handle->write("baz");
92+
$this->assertSame(9, $handle->tell());
93+
yield $handle->seek(0);
94+
$this->assertSame(0, $handle->tell());
95+
$this->assertSame("barfoobaz", yield $handle->read());
96+
});
97+
}
98+
8299
public function testReadingToEof()
83100
{
84101
$this->execute(function () {

0 commit comments

Comments
 (0)