2016-08-24 07:01:41 +02:00
|
|
|
<?php declare(strict_types = 1);
|
2015-08-13 01:02:41 +02:00
|
|
|
|
|
|
|
namespace Amp\File;
|
|
|
|
|
2016-08-30 21:05:14 +02:00
|
|
|
use Amp\{ Deferred, Success };
|
2016-11-15 06:17:19 +01:00
|
|
|
use Interop\Async\{ Promise, Loop\Driver };
|
2015-08-13 01:02:41 +02:00
|
|
|
|
|
|
|
class UvHandle implements Handle {
|
|
|
|
const OP_READ = 1;
|
|
|
|
const OP_WRITE = 2;
|
|
|
|
|
2016-07-21 01:33:03 +02:00
|
|
|
private $busy;
|
|
|
|
private $driver;
|
2015-08-13 01:02:41 +02:00
|
|
|
private $fh;
|
|
|
|
private $path;
|
|
|
|
private $mode;
|
|
|
|
private $size;
|
|
|
|
private $loop;
|
|
|
|
private $position;
|
|
|
|
private $queue = [];
|
|
|
|
private $pendingWriteOps = 0;
|
|
|
|
private $isActive = false;
|
|
|
|
private $isCloseInitialized = false;
|
|
|
|
|
2016-08-24 06:55:06 +02:00
|
|
|
public function __construct(Driver $driver, $busy, $fh, $path, $mode, $size) {
|
2016-07-21 01:33:03 +02:00
|
|
|
$this->driver = $driver;
|
|
|
|
$this->busy = $busy;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-11-15 06:17:19 +01:00
|
|
|
public function read(int $readLen): Promise {
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred = new Deferred;
|
2015-08-13 01:02:41 +02:00
|
|
|
$op = new \StdClass;
|
|
|
|
$op->type = self::OP_READ;
|
|
|
|
$op->position = $this->position;
|
2016-07-21 01:33:03 +02:00
|
|
|
$op->promisor = $deferred;
|
2016-02-08 01:40:23 +01:00
|
|
|
$op->readLen = $readLen;
|
2015-08-13 01:02:41 +02:00
|
|
|
if ($this->isActive) {
|
|
|
|
$this->queue[] = $op;
|
|
|
|
} else {
|
|
|
|
$this->isActive = true;
|
|
|
|
$this->doRead($op);
|
|
|
|
}
|
|
|
|
|
2016-11-15 06:17:19 +01:00
|
|
|
return $deferred->promise();
|
2015-08-13 01:02:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-11-15 06:17:19 +01:00
|
|
|
public function write(string $writeData): Promise {
|
2015-08-13 01:02:41 +02:00
|
|
|
$this->pendingWriteOps++;
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred = new Deferred;
|
2015-08-13 01:02:41 +02:00
|
|
|
$op = new \StdClass;
|
|
|
|
$op->type = self::OP_WRITE;
|
|
|
|
$op->position = $this->position;
|
2016-07-21 01:33:03 +02:00
|
|
|
$op->promisor = $deferred;
|
2015-08-13 01:02:41 +02:00
|
|
|
$op->writeData = $writeData;
|
|
|
|
if ($this->isActive) {
|
|
|
|
$this->queue[] = $op;
|
|
|
|
} else {
|
|
|
|
$this->isActive = true;
|
|
|
|
$this->doWrite($op);
|
|
|
|
}
|
|
|
|
|
2016-11-15 06:17:19 +01:00
|
|
|
return $deferred->promise();
|
2015-08-13 01:02:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function doRead($op) {
|
2016-07-21 01:33:03 +02:00
|
|
|
$this->driver->reference($this->busy);
|
2015-08-13 01:02:41 +02:00
|
|
|
$onRead = function ($fh, $result, $buffer) use ($op) {
|
|
|
|
$this->isActive = false;
|
2016-07-21 01:33:03 +02:00
|
|
|
$this->driver->unreference($this->busy);
|
2015-08-13 01:02:41 +02:00
|
|
|
if ($result < 0) {
|
|
|
|
$op->promisor->fail(new FilesystemException(
|
|
|
|
\uv_strerror($result)
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
$this->position = $op->position + strlen($buffer);
|
2016-07-21 01:33:03 +02:00
|
|
|
$op->promisor->resolve($buffer);
|
2015-08-13 01:02:41 +02:00
|
|
|
}
|
|
|
|
if ($this->queue) {
|
|
|
|
$this->dequeue();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
\uv_fs_read($this->loop, $this->fh, $op->position, $op->readLen, $onRead);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function doWrite($op) {
|
2016-07-21 01:33:03 +02:00
|
|
|
$this->driver->reference($this->busy);
|
2015-08-13 01:02:41 +02:00
|
|
|
$onWrite = function ($fh, $result) use ($op) {
|
|
|
|
$this->isActive = false;
|
2016-07-21 01:33:03 +02:00
|
|
|
$this->driver->unreference($this->busy);
|
2015-08-13 01:02:41 +02:00
|
|
|
if ($result < 0) {
|
|
|
|
$op->promisor->fail(new FilesystemException(
|
|
|
|
\uv_strerror($result)
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
StatCache::clear($this->path);
|
|
|
|
$bytesWritten = \strlen($op->writeData);
|
|
|
|
$this->pendingWriteOps--;
|
|
|
|
$newPosition = $op->position + $bytesWritten;
|
|
|
|
$delta = $newPosition - $this->position;
|
|
|
|
$this->position = ($this->mode[0] === "a") ? $this->position : $newPosition;
|
|
|
|
$this->size += $delta;
|
2016-07-21 01:33:03 +02:00
|
|
|
$op->promisor->resolve($result);
|
2015-08-13 01:02:41 +02:00
|
|
|
}
|
|
|
|
if ($this->queue) {
|
|
|
|
$this->dequeue();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
\uv_fs_write($this->loop, $this->fh, $op->writeData, $op->position, $onWrite);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function dequeue() {
|
|
|
|
$this->isActive = true;
|
|
|
|
$op = \array_shift($this->queue);
|
|
|
|
switch ($op->type) {
|
|
|
|
case self::OP_READ: $this->doRead($op); break;
|
|
|
|
case self::OP_WRITE: $this->doWrite($op); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-11-15 06:17:19 +01:00
|
|
|
public function seek(int $offset, int $whence = \SEEK_SET): Promise {
|
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"
|
|
|
|
);
|
|
|
|
}
|
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 {
|
2015-08-13 01:02:41 +02:00
|
|
|
return ($this->pendingWriteOps > 0) ? false : ($this->size <= $this->position);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-11-15 06:17:19 +01:00
|
|
|
public function close(): Promise {
|
2015-08-13 01:02:41 +02:00
|
|
|
$this->isCloseInitialized = true;
|
2016-07-21 01:33:03 +02:00
|
|
|
$this->driver->reference($this->busy);
|
|
|
|
$deferred = new Deferred;
|
|
|
|
\uv_fs_close($this->loop, $this->fh, function($fh) use ($deferred) {
|
|
|
|
$this->driver->unreference($this->busy);
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
|
|
|
public function __destruct() {
|
|
|
|
if (empty($this->isCloseInitialized)) {
|
|
|
|
$this->close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|