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\Failure;
|
|
|
|
use Amp\Promise;
|
|
|
|
use Amp\Success;
|
2017-06-21 11:01:19 +02:00
|
|
|
use function Amp\call;
|
2015-08-13 01:02:41 +02:00
|
|
|
|
|
|
|
class BlockingHandle implements Handle {
|
|
|
|
private $fh;
|
|
|
|
private $path;
|
|
|
|
private $mode;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param resource $fh An open uv filesystem descriptor
|
2016-08-30 21:05:14 +02:00
|
|
|
* @param string $path
|
|
|
|
* @param string $mode
|
2015-08-13 01:02:41 +02:00
|
|
|
*/
|
2016-08-24 06:55:06 +02:00
|
|
|
public function __construct($fh, string $path, string $mode) {
|
2015-08-13 01:02:41 +02:00
|
|
|
$this->fh = $fh;
|
|
|
|
$this->path = $path;
|
|
|
|
$this->mode = $mode;
|
|
|
|
}
|
2017-01-11 14:22:06 +01:00
|
|
|
|
2016-08-30 21:05:14 +02:00
|
|
|
public function __destruct() {
|
|
|
|
if ($this->fh !== null) {
|
|
|
|
\fclose($this->fh);
|
|
|
|
}
|
|
|
|
}
|
2017-01-11 14:22:06 +01:00
|
|
|
|
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 {
|
2016-08-30 21:05:14 +02:00
|
|
|
if ($this->fh === 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
|
|
|
|
2016-08-24 06:55:06 +02:00
|
|
|
$data = \fread($this->fh, $length);
|
2017-06-17 23:41:57 +02:00
|
|
|
|
2015-08-13 01:02:41 +02:00
|
|
|
if ($data !== false) {
|
2017-05-12 22:43:23 +02:00
|
|
|
return new Success(\strlen($data) ? $data : null);
|
2015-08-13 01:02:41 +02:00
|
|
|
}
|
2017-06-17 23:41:57 +02:00
|
|
|
|
2017-06-21 13:51:59 +02:00
|
|
|
return new Failure(new StreamException(
|
2017-06-17 23:41:57 +02:00
|
|
|
"Failed reading from file handle"
|
|
|
|
));
|
2015-08-13 01:02:41 +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->fh === 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
|
|
|
|
2015-08-13 01:02:41 +02:00
|
|
|
$len = \fwrite($this->fh, $data);
|
2017-06-17 23:41:57 +02:00
|
|
|
|
2015-08-13 01:02:41 +02:00
|
|
|
if ($len !== false) {
|
2016-08-30 21:05:14 +02:00
|
|
|
return new Success($len);
|
2015-08-13 01:02:41 +02:00
|
|
|
}
|
2017-06-17 23:41:57 +02:00
|
|
|
|
2017-06-21 13:51:59 +02:00
|
|
|
return new Failure(new StreamException(
|
2017-06-17 23:41:57 +02:00
|
|
|
"Failed writing to file handle"
|
|
|
|
));
|
2015-08-13 01:02:41 +02: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);
|
|
|
|
|
|
|
|
// ignore any errors
|
|
|
|
yield Promise\any([$this->close()]);
|
|
|
|
|
|
|
|
return $promise;
|
|
|
|
});
|
2017-05-12 22:43:23 +02:00
|
|
|
}
|
|
|
|
|
2015-08-13 01:02:41 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-11-15 06:17:19 +01:00
|
|
|
public function close(): Promise {
|
2016-08-30 21:05:14 +02:00
|
|
|
if ($this->fh === null) {
|
2017-06-21 14:07:49 +02:00
|
|
|
return new Success;
|
2016-08-30 21:05:14 +02:00
|
|
|
}
|
2017-01-11 14:22:06 +01:00
|
|
|
|
2016-08-30 21:05:14 +02:00
|
|
|
$fh = $this->fh;
|
|
|
|
$this->fh = null;
|
2017-01-11 14:22:06 +01:00
|
|
|
|
2017-06-17 01:21:56 +02:00
|
|
|
if (@\fclose($fh)) {
|
2015-08-13 01:02:41 +02:00
|
|
|
return new Success;
|
|
|
|
}
|
2017-06-17 23:41:57 +02:00
|
|
|
|
2017-06-21 13:51:59 +02:00
|
|
|
return new Failure(new StreamException(
|
2017-06-17 23:41:57 +02:00
|
|
|
"Failed closing file handle"
|
|
|
|
));
|
2015-08-13 01:02:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-11-15 06:17:19 +01:00
|
|
|
public function seek(int $position, int $whence = \SEEK_SET): Promise {
|
2016-08-30 21:05:14 +02:00
|
|
|
if ($this->fh === null) {
|
2017-06-21 13:51:59 +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
|
|
|
|
2015-08-13 01:02:41 +02:00
|
|
|
switch ($whence) {
|
|
|
|
case \SEEK_SET:
|
|
|
|
case \SEEK_CUR:
|
|
|
|
case \SEEK_END:
|
2016-08-30 21:05:14 +02:00
|
|
|
if (@\fseek($this->fh, $position, $whence) === -1) {
|
2017-06-21 13:51:59 +02:00
|
|
|
return new Failure(new StreamException("Could not seek in file"));
|
2016-08-30 21:05:14 +02:00
|
|
|
}
|
|
|
|
return new Success($this->tell());
|
2015-08-13 01:02:41 +02:00
|
|
|
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"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-24 06:55:06 +02:00
|
|
|
public function tell(): int {
|
2016-08-30 21:05:14 +02:00
|
|
|
if ($this->fh === null) {
|
2017-06-21 13:51:59 +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
|
|
|
|
2015-08-13 01:02:41 +02:00
|
|
|
return \ftell($this->fh);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-24 06:55:06 +02:00
|
|
|
public function eof(): bool {
|
2016-08-30 21:05:14 +02:00
|
|
|
if ($this->fh === null) {
|
2017-06-21 13:51:59 +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
|
|
|
|
2015-08-13 01:02:41 +02:00
|
|
|
return \feof($this->fh);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@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;
|
|
|
|
}
|
2016-03-24 18:16:20 +01:00
|
|
|
}
|