2016-12-30 03:59:59 +01:00
|
|
|
<?php
|
2016-11-15 06:17:19 +01:00
|
|
|
|
2016-08-30 21:05:14 +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\Coroutine;
|
|
|
|
use Amp\Parallel\Worker\TaskException;
|
|
|
|
use Amp\Parallel\Worker\Worker;
|
|
|
|
use Amp\Parallel\Worker\WorkerException;
|
|
|
|
use Amp\Promise;
|
|
|
|
use Amp\Success;
|
2017-06-21 11:01:19 +02:00
|
|
|
use function Amp\call;
|
2016-08-30 21:05:14 +02:00
|
|
|
|
|
|
|
class ParallelHandle implements Handle {
|
|
|
|
/** @var \Amp\Parallel\Worker\Worker */
|
|
|
|
private $worker;
|
2017-01-11 14:22:06 +01:00
|
|
|
|
2016-08-30 21:05:14 +02:00
|
|
|
/** @var int|null */
|
|
|
|
private $id;
|
2017-01-11 14:22:06 +01:00
|
|
|
|
2016-08-30 21:05:14 +02:00
|
|
|
/** @var string */
|
|
|
|
private $path;
|
2017-01-11 14:22:06 +01:00
|
|
|
|
2016-08-30 21:05:14 +02:00
|
|
|
/** @var int */
|
|
|
|
private $position;
|
2017-01-11 14:22:06 +01:00
|
|
|
|
2016-08-30 21:05:14 +02:00
|
|
|
/** @var int */
|
|
|
|
private $size;
|
2017-01-11 14:22:06 +01:00
|
|
|
|
2016-08-30 21:05:14 +02:00
|
|
|
/** @var string */
|
|
|
|
private $mode;
|
2017-01-11 14:22:06 +01:00
|
|
|
|
2017-06-20 07:06:12 +02:00
|
|
|
/** @var bool True if an operation is pending. */
|
|
|
|
private $busy = false;
|
|
|
|
|
2016-08-30 21:05:14 +02:00
|
|
|
/** @var int Number of pending write operations. */
|
|
|
|
private $pendingWrites = 0;
|
2017-01-11 14:22:06 +01:00
|
|
|
|
2017-06-20 07:06:12 +02:00
|
|
|
/** @var bool */
|
|
|
|
private $writable = true;
|
|
|
|
|
2017-06-22 17:18:55 +02:00
|
|
|
/** @var \Amp\Promise|null */
|
|
|
|
private $closing;
|
|
|
|
|
2016-08-30 21:05:14 +02:00
|
|
|
/**
|
|
|
|
* @param \Amp\Parallel\Worker\Worker $worker
|
|
|
|
* @param int $id
|
|
|
|
* @param string $path
|
|
|
|
* @param int $size
|
|
|
|
* @param string $mode
|
|
|
|
*/
|
|
|
|
public function __construct(Worker $worker, int $id, string $path, int $size, string $mode) {
|
|
|
|
$this->worker = $worker;
|
|
|
|
$this->id = $id;
|
|
|
|
$this->path = $path;
|
|
|
|
$this->size = $size;
|
|
|
|
$this->mode = $mode;
|
|
|
|
$this->position = $this->mode[0] === 'a' ? $this->size : 0;
|
|
|
|
}
|
2017-01-11 14:22:06 +01:00
|
|
|
|
2016-08-30 21:05:14 +02:00
|
|
|
public function __destruct() {
|
|
|
|
if ($this->id !== null) {
|
|
|
|
$this->close();
|
|
|
|
}
|
|
|
|
}
|
2017-01-11 14:22:06 +01:00
|
|
|
|
2016-08-30 21:05:14 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function path(): string {
|
|
|
|
return $this->path;
|
|
|
|
}
|
2017-01-11 14:22:06 +01:00
|
|
|
|
2016-08-30 21:05:14 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-11-15 06:17:19 +01:00
|
|
|
public function close(): Promise {
|
2017-06-22 17:18:55 +02:00
|
|
|
if ($this->closing) {
|
|
|
|
return $this->closing;
|
2017-06-21 14:07:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->writable = false;
|
2017-01-11 14:22:06 +01:00
|
|
|
|
2016-08-30 21:05:14 +02:00
|
|
|
if ($this->worker->isRunning()) {
|
2017-06-22 17:18:55 +02:00
|
|
|
$this->closing = $this->worker->enqueue(new Internal\FileTask('fclose', [], $this->id));
|
2016-08-30 21:05:14 +02:00
|
|
|
$this->id = null;
|
2017-06-22 17:18:55 +02:00
|
|
|
} else {
|
|
|
|
$this->closing = new Success;
|
2016-08-30 21:05:14 +02:00
|
|
|
}
|
2017-01-11 14:22:06 +01:00
|
|
|
|
2017-06-22 17:18:55 +02:00
|
|
|
return $this->closing;
|
2016-08-30 21:05:14 +02:00
|
|
|
}
|
2017-01-11 14:22:06 +01:00
|
|
|
|
2016-08-30 21:05:14 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function eof(): bool {
|
2017-07-06 23:51:20 +02:00
|
|
|
return $this->pendingWrites === 0 && $this->size <= $this->position;
|
2016-08-30 21:05:14 +02:00
|
|
|
}
|
2017-01-11 14:22:06 +01:00
|
|
|
|
2017-05-12 22:43:23 +02:00
|
|
|
public function read(int $length = self::DEFAULT_READ_LENGTH): Promise {
|
2016-08-30 21:05:14 +02:00
|
|
|
if ($this->id === null) {
|
2017-06-21 11:01:19 +02:00
|
|
|
throw new ClosedException("The file has been closed");
|
2016-08-30 21:05:14 +02:00
|
|
|
}
|
2017-01-11 14:22:06 +01:00
|
|
|
|
2017-06-20 07:06:12 +02:00
|
|
|
if ($this->busy) {
|
|
|
|
throw new PendingOperationError;
|
|
|
|
}
|
|
|
|
|
2016-08-30 21:05:14 +02:00
|
|
|
return new Coroutine($this->doRead($length));
|
|
|
|
}
|
2017-01-11 14:22:06 +01:00
|
|
|
|
2016-08-30 21:05:14 +02:00
|
|
|
private function doRead(int $length): \Generator {
|
2017-06-20 07:06:12 +02:00
|
|
|
$this->busy = true;
|
|
|
|
|
2016-08-30 21:05:14 +02:00
|
|
|
try {
|
|
|
|
$data = yield $this->worker->enqueue(new Internal\FileTask('fread', [$length], $this->id));
|
2017-06-20 07:06:12 +02:00
|
|
|
$this->position += \strlen($data);
|
|
|
|
return $data;
|
2016-08-30 21:05:14 +02:00
|
|
|
} catch (TaskException $exception) {
|
2017-06-21 13:51:59 +02:00
|
|
|
throw new StreamException("Reading from the file failed", 0, $exception);
|
2016-08-30 21:05:14 +02:00
|
|
|
} catch (WorkerException $exception) {
|
2017-06-21 13:51:59 +02:00
|
|
|
throw new StreamException("Sending the task to the worker failed", 0, $exception);
|
2017-06-20 07:06:12 +02:00
|
|
|
} finally {
|
|
|
|
$this->busy = false;
|
2016-08-30 21:05:14 +02:00
|
|
|
}
|
|
|
|
}
|
2017-01-11 14:22:06 +01:00
|
|
|
|
2016-08-30 21:05:14 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-11-15 06:17:19 +01:00
|
|
|
public function write(string $data): Promise {
|
2016-08-30 21:05:14 +02:00
|
|
|
if ($this->id === null) {
|
2017-06-21 11:01:19 +02:00
|
|
|
throw new ClosedException("The file has been closed");
|
2016-08-30 21:05:14 +02:00
|
|
|
}
|
2017-01-11 14:22:06 +01:00
|
|
|
|
2017-06-20 07:06:12 +02:00
|
|
|
if ($this->busy && $this->pendingWrites === 0) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-08-30 21:05:14 +02:00
|
|
|
return new Coroutine($this->doWrite($data));
|
|
|
|
}
|
2017-01-11 14:22:06 +01:00
|
|
|
|
2017-05-12 22:43:23 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-08-30 21:05:14 +02:00
|
|
|
private function doWrite(string $data): \Generator {
|
|
|
|
++$this->pendingWrites;
|
2017-06-20 07:06:12 +02:00
|
|
|
$this->busy = true;
|
2017-01-11 14:22:06 +01:00
|
|
|
|
2016-08-30 21:05:14 +02:00
|
|
|
try {
|
|
|
|
$length = yield $this->worker->enqueue(new Internal\FileTask('fwrite', [$data], $this->id));
|
|
|
|
} catch (TaskException $exception) {
|
2017-06-21 13:51:59 +02:00
|
|
|
throw new StreamException("Writing to the file failed", 0, $exception);
|
2016-08-30 21:05:14 +02:00
|
|
|
} catch (WorkerException $exception) {
|
2017-06-21 13:51:59 +02:00
|
|
|
throw new StreamException("Sending the task to the worker failed", 0, $exception);
|
2016-08-30 21:05:14 +02:00
|
|
|
} finally {
|
2017-06-20 07:06:12 +02:00
|
|
|
if (--$this->pendingWrites === 0) {
|
|
|
|
$this->busy = false;
|
|
|
|
}
|
2016-08-30 21:05:14 +02:00
|
|
|
}
|
2017-01-11 14:22:06 +01:00
|
|
|
|
2016-08-30 21:05:14 +02:00
|
|
|
$this->position += $length;
|
|
|
|
return $length;
|
|
|
|
}
|
2017-01-11 14:22:06 +01:00
|
|
|
|
2016-08-30 21:05:14 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-11-15 06:17:19 +01:00
|
|
|
public function seek(int $offset, int $whence = SEEK_SET): Promise {
|
2016-08-30 21:05:14 +02:00
|
|
|
if ($this->id === null) {
|
2017-06-21 11:01:19 +02:00
|
|
|
throw new ClosedException("The file has been closed");
|
2016-08-30 21:05:14 +02:00
|
|
|
}
|
2017-01-11 14:22:06 +01:00
|
|
|
|
2017-06-20 07:06:12 +02:00
|
|
|
if ($this->busy) {
|
|
|
|
throw new PendingOperationError;
|
|
|
|
}
|
|
|
|
|
2016-08-30 21:05:14 +02:00
|
|
|
return new Coroutine($this->doSeek($offset, $whence));
|
|
|
|
}
|
2017-01-11 14:22:06 +01:00
|
|
|
|
2016-08-30 21:05:14 +02:00
|
|
|
private function doSeek(int $offset, int $whence) {
|
|
|
|
switch ($whence) {
|
|
|
|
case \SEEK_SET:
|
|
|
|
case \SEEK_CUR:
|
|
|
|
case \SEEK_END:
|
|
|
|
try {
|
|
|
|
$this->position = yield $this->worker->enqueue(
|
|
|
|
new Internal\FileTask('fseek', [$offset, $whence], $this->id)
|
|
|
|
);
|
2017-06-20 07:06:12 +02:00
|
|
|
|
|
|
|
if ($this->position > $this->size) {
|
|
|
|
$this->size = $this->position;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->position;
|
2016-08-30 21:05:14 +02:00
|
|
|
} catch (TaskException $exception) {
|
2017-06-21 13:51:59 +02:00
|
|
|
throw new StreamException('Seeking in the file failed.', 0, $exception);
|
2016-08-30 21:05:14 +02:00
|
|
|
} catch (WorkerException $exception) {
|
2017-06-21 13:51:59 +02:00
|
|
|
throw new StreamException("Sending the task to the worker failed", 0, $exception);
|
2016-08-30 21:05:14 +02:00
|
|
|
}
|
2017-01-11 14:22:06 +01:00
|
|
|
|
2016-08-30 21:05:14 +02:00
|
|
|
default:
|
|
|
|
throw new \Error('Invalid whence value. Use SEEK_SET, SEEK_CUR, or SEEK_END.');
|
|
|
|
}
|
|
|
|
}
|
2017-01-11 14:22:06 +01:00
|
|
|
|
2016-08-30 21:05:14 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function tell(): int {
|
|
|
|
return $this->position;
|
|
|
|
}
|
2017-01-11 14:22:06 +01:00
|
|
|
|
2016-08-30 21:05:14 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function size(): int {
|
|
|
|
return $this->size;
|
|
|
|
}
|
2017-01-11 14:22:06 +01:00
|
|
|
|
2016-08-30 21:05:14 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function mode(): string {
|
|
|
|
return $this->mode;
|
|
|
|
}
|
|
|
|
}
|