2016-12-30 03:59:59 +01:00
|
|
|
<?php
|
2015-08-13 01:02:41 +02:00
|
|
|
|
|
|
|
namespace Amp\File;
|
|
|
|
|
2017-06-21 11:01:19 +02:00
|
|
|
use Amp\ByteStream\ClosedException;
|
2017-06-21 13:51:59 +02:00
|
|
|
use Amp\ByteStream\StreamException;
|
2017-06-17 23:41:57 +02:00
|
|
|
use Amp\Deferred;
|
2017-06-20 23:39:14 +02:00
|
|
|
use Amp\File\Internal\UvPoll;
|
2017-06-17 23:41:57 +02:00
|
|
|
use Amp\Loop;
|
|
|
|
use Amp\Promise;
|
|
|
|
use Amp\Success;
|
2017-06-20 07:06:12 +02:00
|
|
|
use function Amp\call;
|
2015-08-13 01:02:41 +02:00
|
|
|
|
2018-10-27 17:57:31 +02:00
|
|
|
class UvHandle implements Handle
|
|
|
|
{
|
2017-06-22 17:18:55 +02:00
|
|
|
/** @var UvPoll */
|
2017-06-20 23:39:14 +02:00
|
|
|
private $poll;
|
2017-06-22 17:18:55 +02:00
|
|
|
|
|
|
|
/** @var \UVLoop */
|
|
|
|
private $loop;
|
|
|
|
|
|
|
|
/** @var resource */
|
2015-08-13 01:02:41 +02:00
|
|
|
private $fh;
|
2017-06-22 17:18:55 +02:00
|
|
|
|
|
|
|
/** @var string */
|
2015-08-13 01:02:41 +02:00
|
|
|
private $path;
|
2017-06-22 17:18:55 +02:00
|
|
|
|
|
|
|
/** @var string */
|
2015-08-13 01:02:41 +02:00
|
|
|
private $mode;
|
2017-06-22 17:18:55 +02:00
|
|
|
|
|
|
|
/** @var int */
|
2015-08-13 01:02:41 +02:00
|
|
|
private $size;
|
2017-06-22 17:18:55 +02:00
|
|
|
|
|
|
|
/** @var int */
|
2015-08-13 01:02:41 +02:00
|
|
|
private $position;
|
2017-06-22 17:18:55 +02:00
|
|
|
|
|
|
|
/** @var \SplQueue */
|
2017-06-20 07:06:12 +02:00
|
|
|
private $queue;
|
2017-06-22 17:18:55 +02:00
|
|
|
|
|
|
|
/** @var bool */
|
2015-08-13 01:02:41 +02:00
|
|
|
private $isActive = false;
|
2017-06-22 17:18:55 +02:00
|
|
|
|
|
|
|
/** @var bool */
|
2017-06-20 07:06:12 +02:00
|
|
|
private $writable = true;
|
2015-08-13 01:02:41 +02:00
|
|
|
|
2017-06-22 17:18:55 +02:00
|
|
|
/** @var \Amp\Promise|null */
|
|
|
|
private $closing;
|
|
|
|
|
2017-05-17 02:55:24 +02:00
|
|
|
/**
|
|
|
|
* @param \Amp\Loop\UvDriver $driver
|
2017-06-20 23:39:14 +02:00
|
|
|
* @param UvPoll $poll Poll for keeping the loop active.
|
2017-05-17 02:55:24 +02:00
|
|
|
* @param resource $fh File handle.
|
|
|
|
* @param string $path
|
|
|
|
* @param string $mode
|
|
|
|
* @param int $size
|
|
|
|
*/
|
2018-10-27 17:57:31 +02:00
|
|
|
public function __construct(Loop\UvDriver $driver, UvPoll $poll, $fh, string $path, string $mode, int $size)
|
|
|
|
{
|
2017-06-20 23:39:14 +02:00
|
|
|
$this->poll = $poll;
|
2015-08-13 01:02:41 +02:00
|
|
|
$this->fh = $fh;
|
|
|
|
$this->path = $path;
|
|
|
|
$this->mode = $mode;
|
|
|
|
$this->size = $size;
|
2016-07-21 01:33:03 +02:00
|
|
|
$this->loop = $driver->getHandle();
|
2015-08-13 01:02:41 +02:00
|
|
|
$this->position = ($mode[0] === "a") ? $size : 0;
|
2017-06-20 07:06:12 +02:00
|
|
|
|
|
|
|
$this->queue = new \SplQueue;
|
2015-08-13 01:02:41 +02:00
|
|
|
}
|
|
|
|
|
2018-10-27 17:57:31 +02:00
|
|
|
public function read(int $length = self::DEFAULT_READ_LENGTH): Promise
|
|
|
|
{
|
2015-08-13 01:02:41 +02:00
|
|
|
if ($this->isActive) {
|
2017-06-20 07:06:12 +02:00
|
|
|
throw new PendingOperationError;
|
2015-08-13 01:02:41 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 07:06:12 +02:00
|
|
|
$deferred = new Deferred;
|
2017-06-20 23:39:14 +02:00
|
|
|
$this->poll->listen($deferred->promise());
|
|
|
|
|
2017-06-20 07:06:12 +02:00
|
|
|
$this->isActive = true;
|
|
|
|
|
|
|
|
$onRead = function ($fh, $result, $buffer) use ($deferred) {
|
|
|
|
$this->isActive = false;
|
2017-06-20 23:39:14 +02:00
|
|
|
|
2017-06-20 07:06:12 +02:00
|
|
|
if ($result < 0) {
|
2017-06-21 13:51:59 +02:00
|
|
|
$error = \uv_strerror($result);
|
|
|
|
if ($error === "bad file descriptor") {
|
|
|
|
$deferred->fail(new ClosedException("Reading from the file failed due to a closed handle"));
|
|
|
|
} else {
|
|
|
|
$deferred->fail(new StreamException("Reading from the file failed: " . $error));
|
|
|
|
}
|
2017-06-20 07:06:12 +02:00
|
|
|
} else {
|
2018-10-27 17:57:31 +02:00
|
|
|
$length = \strlen($buffer);
|
2017-06-20 07:06:12 +02:00
|
|
|
$this->position = $this->position + $length;
|
|
|
|
$deferred->resolve($length ? $buffer : null);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
\uv_fs_read($this->loop, $this->fh, $this->position, $length, $onRead);
|
2017-06-20 07:31:58 +02:00
|
|
|
|
2016-11-15 06:17:19 +01:00
|
|
|
return $deferred->promise();
|
2015-08-13 01:02:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2018-10-27 17:57:31 +02:00
|
|
|
public function write(string $data): Promise
|
|
|
|
{
|
2017-06-20 07:06:12 +02:00
|
|
|
if ($this->isActive && $this->queue->isEmpty()) {
|
|
|
|
throw new PendingOperationError;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->writable) {
|
2017-06-21 11:01:19 +02:00
|
|
|
throw new ClosedException("The file is no longer writable");
|
2017-06-20 07:06:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->isActive = true;
|
|
|
|
|
|
|
|
if ($this->queue->isEmpty()) {
|
|
|
|
$promise = $this->push($data);
|
2015-08-13 01:02:41 +02:00
|
|
|
} else {
|
2017-06-20 07:06:12 +02:00
|
|
|
$promise = $this->queue->top();
|
|
|
|
$promise = call(function () use ($promise, $data) {
|
|
|
|
yield $promise;
|
|
|
|
return yield $this->push($data);
|
|
|
|
});
|
2015-08-13 01:02:41 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 07:06:12 +02:00
|
|
|
$this->queue->push($promise);
|
|
|
|
|
|
|
|
return $promise;
|
2015-08-13 01:02:41 +02:00
|
|
|
}
|
|
|
|
|
2017-05-12 22:43:23 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2018-10-27 17:57:31 +02:00
|
|
|
public function end(string $data = ""): Promise
|
|
|
|
{
|
2017-06-21 11:01:19 +02:00
|
|
|
return call(function () use ($data) {
|
|
|
|
$promise = $this->write($data);
|
|
|
|
$this->writable = false;
|
|
|
|
|
|
|
|
// ignore any errors
|
|
|
|
yield Promise\any([$this->close()]);
|
|
|
|
|
|
|
|
return $promise;
|
|
|
|
});
|
2017-05-12 22:43:23 +02:00
|
|
|
}
|
|
|
|
|
2018-10-27 17:57:31 +02:00
|
|
|
private function push(string $data): Promise
|
|
|
|
{
|
2017-06-20 07:06:12 +02:00
|
|
|
$length = \strlen($data);
|
2017-06-20 23:39:14 +02:00
|
|
|
|
2019-01-07 18:32:42 +01:00
|
|
|
if ($length === 0) {
|
|
|
|
return new Success(0);
|
|
|
|
}
|
|
|
|
|
2017-06-20 07:06:12 +02:00
|
|
|
$deferred = new Deferred;
|
2017-06-20 23:39:14 +02:00
|
|
|
$this->poll->listen($deferred->promise());
|
2017-06-20 07:06:12 +02:00
|
|
|
|
|
|
|
$onWrite = function ($fh, $result) use ($deferred, $length) {
|
|
|
|
if ($this->queue->isEmpty()) {
|
2017-06-21 11:01:19 +02:00
|
|
|
$deferred->fail(new ClosedException('No pending write, the file may have been closed'));
|
2015-08-13 01:02:41 +02:00
|
|
|
}
|
2017-06-20 07:06:12 +02:00
|
|
|
|
|
|
|
$this->queue->shift();
|
|
|
|
if ($this->queue->isEmpty()) {
|
|
|
|
$this->isActive = false;
|
2015-08-13 01:02:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($result < 0) {
|
2017-06-21 13:51:59 +02:00
|
|
|
$error = \uv_strerror($result);
|
|
|
|
if ($error === "bad file descriptor") {
|
|
|
|
$deferred->fail(new ClosedException("Writing to the file failed due to a closed handle"));
|
|
|
|
} else {
|
|
|
|
$deferred->fail(new StreamException("Writing to the file failed: " . $error));
|
|
|
|
}
|
2015-08-13 01:02:41 +02:00
|
|
|
} else {
|
|
|
|
StatCache::clear($this->path);
|
2019-03-01 17:38:04 +01:00
|
|
|
$this->position += $length;
|
|
|
|
if ($this->position > $this->size) {
|
|
|
|
$this->size = $this->position;
|
|
|
|
}
|
2017-06-20 07:06:12 +02:00
|
|
|
$deferred->resolve($length);
|
2015-08-13 01:02:41 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-06-20 07:06:12 +02:00
|
|
|
\uv_fs_write($this->loop, $this->fh, $data, $this->position, $onWrite);
|
|
|
|
|
|
|
|
return $deferred->promise();
|
2015-08-13 01:02:41 +02:00
|
|
|
}
|
|
|
|
|
2018-10-29 05:55:11 +01:00
|
|
|
public function truncate(int $size): Promise
|
|
|
|
{
|
|
|
|
if ($this->isActive && $this->queue->isEmpty()) {
|
|
|
|
throw new PendingOperationError;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->writable) {
|
|
|
|
throw new ClosedException("The file is no longer writable");
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->isActive = true;
|
|
|
|
|
|
|
|
if ($this->queue->isEmpty()) {
|
|
|
|
$promise = $this->trim($size);
|
|
|
|
} else {
|
|
|
|
$promise = $this->queue->top();
|
|
|
|
$promise = call(function () use ($promise, $size) {
|
|
|
|
yield $promise;
|
|
|
|
return yield $this->trim($size);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->queue->push($promise);
|
|
|
|
|
|
|
|
return $promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function trim(int $size): Promise
|
|
|
|
{
|
|
|
|
$deferred = new Deferred;
|
|
|
|
$this->poll->listen($deferred->promise());
|
|
|
|
|
|
|
|
$onTruncate = function ($fh) use ($deferred, $size) {
|
|
|
|
if ($this->queue->isEmpty()) {
|
|
|
|
$deferred->fail(new ClosedException('No pending write, the file may have been closed'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->queue->shift();
|
|
|
|
if ($this->queue->isEmpty()) {
|
|
|
|
$this->isActive = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
StatCache::clear($this->path);
|
|
|
|
$this->size = $size;
|
|
|
|
$deferred->resolve();
|
|
|
|
};
|
|
|
|
|
|
|
|
\uv_fs_ftruncate(
|
|
|
|
$this->loop,
|
|
|
|
$this->fh,
|
|
|
|
$size,
|
|
|
|
$onTruncate
|
|
|
|
);
|
|
|
|
|
|
|
|
return $deferred->promise();
|
|
|
|
}
|
|
|
|
|
2015-08-13 01:02:41 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2018-10-27 17:57:31 +02:00
|
|
|
public function seek(int $offset, int $whence = \SEEK_SET): Promise
|
|
|
|
{
|
2017-06-20 07:06:12 +02:00
|
|
|
if ($this->isActive) {
|
|
|
|
throw new PendingOperationError;
|
|
|
|
}
|
|
|
|
|
2015-08-13 01:02:41 +02:00
|
|
|
$offset = (int) $offset;
|
|
|
|
switch ($whence) {
|
|
|
|
case \SEEK_SET:
|
|
|
|
$this->position = $offset;
|
|
|
|
break;
|
|
|
|
case \SEEK_CUR:
|
|
|
|
$this->position = $this->position + $offset;
|
|
|
|
break;
|
|
|
|
case \SEEK_END:
|
|
|
|
$this->position = $this->size + $offset;
|
|
|
|
break;
|
|
|
|
default:
|
2016-08-30 21:05:14 +02:00
|
|
|
throw new \Error(
|
2015-08-13 01:02:41 +02:00
|
|
|
"Invalid whence parameter; SEEK_SET, SEEK_CUR or SEEK_END expected"
|
|
|
|
);
|
|
|
|
}
|
2017-01-11 14:22:06 +01:00
|
|
|
|
2016-08-30 21:05:14 +02:00
|
|
|
return new Success($this->position);
|
2015-08-13 01:02:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2018-10-27 17:57:31 +02:00
|
|
|
public function tell(): int
|
|
|
|
{
|
2015-08-13 01:02:41 +02:00
|
|
|
return $this->position;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2018-10-27 17:57:31 +02:00
|
|
|
public function eof(): bool
|
|
|
|
{
|
2017-06-20 07:06:12 +02:00
|
|
|
return !$this->queue->isEmpty() ? false : ($this->size <= $this->position);
|
2015-08-13 01:02:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2018-10-27 17:57:31 +02:00
|
|
|
public function path(): string
|
|
|
|
{
|
2015-08-13 01:02:41 +02:00
|
|
|
return $this->path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2018-10-27 17:57:31 +02:00
|
|
|
public function mode(): string
|
|
|
|
{
|
2015-08-13 01:02:41 +02:00
|
|
|
return $this->mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2018-10-27 17:57:31 +02:00
|
|
|
public function close(): Promise
|
|
|
|
{
|
2017-06-22 17:18:55 +02:00
|
|
|
if ($this->closing) {
|
|
|
|
return $this->closing;
|
|
|
|
}
|
|
|
|
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred = new Deferred;
|
2017-06-22 17:18:55 +02:00
|
|
|
$this->poll->listen($this->closing = $deferred->promise());
|
2017-06-20 23:39:14 +02:00
|
|
|
|
2017-06-17 23:41:57 +02:00
|
|
|
\uv_fs_close($this->loop, $this->fh, function ($fh) use ($deferred) {
|
2017-06-24 00:47:22 +02:00
|
|
|
// Ignore errors when closing file, as the handle will become invalid anyway.
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred->resolve();
|
2015-08-13 01:02:41 +02:00
|
|
|
});
|
|
|
|
|
2016-11-15 06:17:19 +01:00
|
|
|
return $deferred->promise();
|
2015-08-13 01:02:41 +02:00
|
|
|
}
|
|
|
|
}
|