2016-12-30 03:59:59 +01:00
|
|
|
<?php
|
2015-08-13 01:02:41 +02:00
|
|
|
|
|
|
|
namespace Amp\File;
|
|
|
|
|
2017-06-17 23:41:57 +02:00
|
|
|
use Amp\Deferred;
|
|
|
|
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
|
|
|
|
|
|
|
class EioHandle implements Handle {
|
2017-06-20 05:58:11 +02:00
|
|
|
private $poll;
|
2015-08-13 01:02:41 +02:00
|
|
|
private $fh;
|
|
|
|
private $path;
|
|
|
|
private $mode;
|
|
|
|
private $size;
|
|
|
|
private $position;
|
2017-06-20 07:06:12 +02:00
|
|
|
private $queue;
|
2015-08-13 01:02:41 +02:00
|
|
|
private $isActive = false;
|
2017-06-20 07:06:12 +02:00
|
|
|
private $writable = true;
|
2015-08-13 01:02:41 +02:00
|
|
|
|
2017-06-20 05:58:11 +02:00
|
|
|
public function __construct(Internal\EioPoll $poll, $fh, string $path, string $mode, int $size) {
|
|
|
|
$this->poll = $poll;
|
2015-08-13 01:02:41 +02:00
|
|
|
$this->fh = $fh;
|
|
|
|
$this->path = $path;
|
|
|
|
$this->mode = $mode;
|
|
|
|
$this->size = $size;
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2017-05-12 22:43:23 +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
|
|
|
}
|
|
|
|
|
|
|
|
$this->isActive = true;
|
2017-06-20 07:06:12 +02:00
|
|
|
|
|
|
|
$remaining = $this->size - $this->position;
|
|
|
|
$length = $length > $remaining ? $remaining : $length;
|
|
|
|
|
|
|
|
$deferred = new Deferred;
|
|
|
|
$this->poll->listen();
|
|
|
|
|
|
|
|
\eio_read(
|
|
|
|
$this->fh,
|
|
|
|
$length,
|
|
|
|
$this->position,
|
|
|
|
\EIO_PRI_DEFAULT,
|
|
|
|
[$this, "onRead"],
|
|
|
|
$deferred
|
|
|
|
);
|
|
|
|
|
|
|
|
return $deferred->promise();
|
2015-08-13 01:02:41 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 07:06:12 +02:00
|
|
|
private function onRead(Deferred $deferred, $result, $req) {
|
2015-08-13 01:02:41 +02:00
|
|
|
$this->isActive = false;
|
2017-06-20 05:58:11 +02:00
|
|
|
$this->poll->done();
|
2015-08-13 01:02:41 +02:00
|
|
|
if ($result === -1) {
|
2017-06-20 07:06:12 +02:00
|
|
|
$deferred->fail(new FilesystemException(
|
|
|
|
sprintf('Reading from file failed: %s.', \eio_get_last_error($req))
|
2015-08-13 01:02:41 +02:00
|
|
|
));
|
|
|
|
} else {
|
2017-06-20 07:06:12 +02:00
|
|
|
$this->position += \strlen($result);
|
|
|
|
$deferred->resolve($result);
|
2015-08-13 01:02:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-11-15 06:17:19 +01: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) {
|
|
|
|
throw new \Error("The file is no longer writable");
|
|
|
|
}
|
|
|
|
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function push(string $data): Promise {
|
|
|
|
$length = \strlen($data);
|
|
|
|
$deferred = new Deferred;
|
|
|
|
$this->poll->listen();
|
|
|
|
|
|
|
|
\eio_write(
|
|
|
|
$this->fh,
|
|
|
|
$data,
|
|
|
|
$length,
|
|
|
|
$this->position,
|
|
|
|
\EIO_PRI_DEFAULT,
|
|
|
|
[$this, "onWrite"],
|
|
|
|
$deferred
|
|
|
|
);
|
|
|
|
|
2016-11-15 06:17:19 +01:00
|
|
|
return $deferred->promise();
|
2015-08-13 01:02:41 +02:00
|
|
|
}
|
|
|
|
|
2017-05-12 22:43:23 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function end(string $data = ""): Promise {
|
|
|
|
$promise = $this->write($data);
|
2017-06-20 07:06:12 +02:00
|
|
|
$this->writable = false;
|
2017-05-12 22:43:23 +02:00
|
|
|
$promise->onResolve([$this, "close"]);
|
|
|
|
return $promise;
|
|
|
|
}
|
|
|
|
|
2017-06-20 07:06:12 +02:00
|
|
|
private function onWrite(Deferred $deferred, $result, $req) {
|
2017-06-20 05:58:11 +02:00
|
|
|
$this->poll->done();
|
2017-06-20 07:06:12 +02:00
|
|
|
|
|
|
|
if ($this->queue->isEmpty()) {
|
|
|
|
$deferred->fail(new FilesystemException('No pending write, the file may have been closed'));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->queue->shift();
|
|
|
|
if ($this->queue->isEmpty()) {
|
|
|
|
$this->isActive = false;
|
|
|
|
}
|
|
|
|
|
2015-08-13 01:02:41 +02:00
|
|
|
if ($result === -1) {
|
2017-06-20 07:06:12 +02:00
|
|
|
$deferred->fail(new FilesystemException(
|
|
|
|
sprintf('Writing to the file failed: %s', \eio_get_last_error($req))
|
2015-08-13 01:02:41 +02:00
|
|
|
));
|
|
|
|
} else {
|
2017-06-20 07:06:12 +02:00
|
|
|
$this->position += $result;
|
|
|
|
if ($this->position > $this->size) {
|
|
|
|
$this->size = $this->position;
|
|
|
|
}
|
|
|
|
|
|
|
|
$deferred->resolve($result);
|
2015-08-13 01:02:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-11-15 06:17:19 +01:00
|
|
|
public function close(): Promise {
|
2017-06-20 05:58:11 +02:00
|
|
|
$this->poll->listen();
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred = new Deferred;
|
2016-08-24 07:07:22 +02:00
|
|
|
\eio_close($this->fh, \EIO_PRI_DEFAULT, [$this, "onClose"], $deferred);
|
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
|
|
|
}
|
|
|
|
|
2016-07-21 01:33:03 +02:00
|
|
|
private function onClose($deferred, $result, $req) {
|
2017-06-20 05:58:11 +02:00
|
|
|
$this->poll->done();
|
2015-08-13 01:02:41 +02:00
|
|
|
if ($result === -1) {
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred->fail(new FilesystemException(
|
2015-08-13 01:02:41 +02:00
|
|
|
\eio_get_last_error($req)
|
|
|
|
));
|
|
|
|
} else {
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred->resolve();
|
2015-08-13 01:02:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-11-15 06:17:19 +01: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}
|
|
|
|
*/
|
2016-08-24 06:55:06 +02:00
|
|
|
public function tell(): int {
|
2015-08-13 01:02:41 +02:00
|
|
|
return $this->position;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-24 06:55:06 +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}
|
|
|
|
*/
|
2016-08-24 06:55:06 +02:00
|
|
|
public function path(): string {
|
2015-08-13 01:02:41 +02:00
|
|
|
return $this->path;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-24 06:55:06 +02:00
|
|
|
public function mode(): string {
|
2015-08-13 01:02:41 +02:00
|
|
|
return $this->mode;
|
|
|
|
}
|
|
|
|
}
|