protected function readNextChunk() : \Generator { if ($this->bufferOffset >= $this->size) { return; } $len = \min($this->bufferSize, $this->size - $this->bufferOffset); return (yield $this->filesystem->handleCallback('eio_read', $this->resource, $len, $this->bufferOffset, \EIO_PRI_DEFAULT, function ($chunk, $request) { if ($chunk === -1) { throw new StreamException(\sprintf('Failed to read data from file "%s": %s', $this->file, \eio_get_last_error($request))); } $this->bufferOffset += \strlen($chunk); return $chunk; })); }
/** * {@inheritdoc} */ public function write(string $data) : Awaitable { if ($this->resource === null) { return new Failure(new StreamClosedException('Cannot write to closed stream')); } if ($data === '') { return new Success(0); } return $this->filesystem->handleCallback('eio_write', $this->resource, $data, \strlen($data), $this->size, \EIO_PRI_DEFAULT, function (int $result, $request) { if ($result < 1) { throw new StreamException(\sprintf('Failed to write data to file "%s": %s', $this->file, \eio_get_last_error($request))); } $this->size += $result; return $result; }); }
public function unlinkFile(string $file) : Awaitable { return $this->handleCallback('eio_unlink', $file, \EIO_PRI_DEFAULT, function ($result, $request) use($file) { if ($result === -1 && !@\unlink($file)) { // @codeCoverageIgnoreStart throw new FilesystemException(\sprintf('Failed to unlink file "%s": %s', $file, \eio_get_last_error($request))); // @codeCoverageIgnoreEnd } }); }
private function onPutWrite($fhAndPromisor, $result, $req) { list($fh, $promisor) = $fhAndPromisor; \eio_close($fh); $priority = \EIO_PRI_DEFAULT; \eio_close($fh, $priority, $this->callableDelReq); if ($result === -1) { $promisor->fail(new \RuntimeException(\eio_get_last_error($req))); } else { $promisor->succeed($result); } }
/** * {@inheritdoc} */ public function chmod(string $path, int $mode) : \Generator { $delayed = new Delayed(); \eio_chmod($path, $mode, null, function (Delayed $delayed, $result, $req) { if (-1 === $result) { $delayed->reject(new FileException(sprintf('Changing the owner failed: %s.', \eio_get_last_error($req)))); } else { $delayed->resolve(true); } }, $delayed); $this->poll->listen(); try { $result = (yield $delayed); } finally { $this->poll->done(); } return $result; }
protected function executeDelayedCall($function, $args, $errorResultCode, Deferred $deferred) { $this->register(); $args[] = EIO_PRI_DEFAULT; $args[] = function ($data, $result, $req) use($deferred, $errorResultCode, $function, $args) { if ($result == $errorResultCode) { $exception = new UnexpectedValueException(@eio_get_last_error($req)); $exception->setArgs($args); $deferred->reject($exception); return; } $deferred->resolve($result); }; if (!call_user_func_array($function, $args)) { $name = $function; if (!is_string($function)) { $name = get_class($function); } $exception = new RuntimeException('Unknown error calling "' . $name . '"'); $exception->setArgs($args); $deferred->reject($exception); } }
/** * @coroutine * * @param string $path * * @return \Generator * * @resolve int */ public function copy(string $path) : \Generator { $delayed = new Delayed(); \eio_open($path, \EIO_O_WRONLY | \EIO_O_CREAT | \EIO_O_TRUNC, 0644, null, function (Delayed $delayed, $handle, $req) { if (-1 === $handle) { $delayed->reject(new FileException(sprintf('Opening the file failed: %s.', \eio_get_last_error($req)))); } else { $delayed->resolve($handle); } }, $delayed); $this->poll->listen(); try { $handle = (yield $delayed); } finally { $this->poll->done(); } $delayed = new Delayed(); \eio_sendfile($handle, $this->handle, 0, $this->size, null, function (Delayed $delayed, $result, $req) { if (-1 === $result) { $delayed->reject(new FileException(sprintf('Copying the file failed: %s.', \eio_get_last_error($req)))); } else { $delayed->resolve(true); } }, $delayed); $this->poll->listen(); try { (yield $delayed); } finally { $this->poll->done(); \eio_close($handle); } return $this->size; }
private function onClose($promisor, $result, $req) { \call_user_func($this->incrementor, -1); if ($result === -1) { $promisor->fail(new FilesystemException(\eio_get_last_error($req))); } else { $promisor->succeed(); } }