1
0
mirror of https://github.com/danog/file.git synced 2024-11-26 20:04:51 +01:00
file/lib/EioDriver.php

574 lines
16 KiB
PHP
Raw Normal View History

<?php
2015-07-18 18:08:50 +02:00
2015-08-05 16:55:56 +02:00
namespace Amp\File;
2015-07-18 18:08:50 +02:00
2017-06-17 23:41:57 +02:00
use Amp\Deferred;
use Amp\Promise;
use Amp\Success;
2015-07-18 18:08:50 +02:00
2015-07-30 16:10:53 +02:00
class EioDriver implements Driver {
2017-06-20 05:58:11 +02:00
/** @var \Amp\File\Internal\EioPoll */
private $poll;
2015-07-18 18:08:50 +02:00
public function __construct() {
2017-06-20 05:58:11 +02:00
$this->poll = new Internal\EioPoll;
2015-07-18 18:08:50 +02:00
}
2015-08-13 01:02:41 +02:00
/**
* {@inheritdoc}
*/
2016-11-15 06:17:19 +01:00
public function open(string $path, string $mode): Promise {
$flags = \EIO_O_NONBLOCK | \EIO_O_FSYNC | $this->parseMode($mode);
2015-08-13 01:02:41 +02:00
$chmod = ($flags & \EIO_O_CREAT) ? 0644 : 0;
2016-07-21 01:33:03 +02:00
$deferred = new Deferred;
$this->poll->listen($deferred->promise());
2016-07-21 01:33:03 +02:00
$openArr = [$mode, $path, $deferred];
\eio_open($path, $flags, $chmod, \EIO_PRI_DEFAULT, [$this, "onOpenHandle"], $openArr);
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
}
2017-05-12 22:38:03 +02:00
private function parseMode(string $mode): int {
$mode = \str_replace(['b', 't', 'e'], '', $mode);
2017-05-12 22:38:03 +02:00
switch ($mode) {
case 'r': return \EIO_O_RDONLY;
case 'r+': return \EIO_O_RDWR;
case 'w': return \EIO_O_WRONLY | \EIO_O_TRUNC | \EIO_O_CREAT;
case 'w+': return \EIO_O_RDWR | \EIO_O_TRUNC | \EIO_O_CREAT;
case 'a': return \EIO_O_WRONLY | \EIO_O_APPEND | \EIO_O_CREAT;
case 'a+': return \EIO_O_RDWR | \EIO_O_APPEND | \EIO_O_CREAT;
case 'x': return \EIO_O_WRONLY | \EIO_O_CREAT | \EIO_O_EXCL;
case 'x+': return \EIO_O_RDWR | \EIO_O_CREAT | \EIO_O_EXCL;
case 'c': return \EIO_O_WRONLY | \EIO_O_CREAT;
case 'c+': return \EIO_O_RDWR | \EIO_O_CREAT;
default:
throw new \Error('Invalid file mode');
2017-05-12 22:38:03 +02:00
}
}
2015-08-13 01:02:41 +02:00
private function onOpenHandle($openArr, $result, $req) {
2016-07-21 01:33:03 +02:00
list($mode, $path, $deferred) = $openArr;
2015-08-13 01:02:41 +02:00
if ($result === -1) {
$deferred->fail(new FilesystemException(\eio_get_last_error($req)));
2015-08-13 01:02:41 +02:00
} elseif ($mode[0] === "a") {
\array_unshift($openArr, $result);
\eio_ftruncate($result, $offset = 0, \EIO_PRI_DEFAULT, [$this, "onOpenFtruncate"], $openArr);
2015-08-13 01:02:41 +02:00
} else {
\array_unshift($openArr, $result);
\eio_fstat($result, \EIO_PRI_DEFAULT, [$this, "onOpenFstat"], $openArr);
2015-08-13 01:02:41 +02:00
}
}
private function onOpenFtruncate($openArr, $result, $req) {
2016-07-21 01:33:03 +02:00
list($fh, $mode, $path, $deferred) = $openArr;
2015-08-13 01:02:41 +02:00
if ($result === -1) {
$deferred->fail(new FilesystemException(\eio_get_last_error($req)));
2015-08-13 01:02:41 +02:00
} else {
2017-06-20 05:58:11 +02:00
$handle = new EioHandle($this->poll, $fh, $path, $mode, $size = 0);
2016-07-21 01:33:03 +02:00
$deferred->resolve($handle);
2015-07-18 18:08:50 +02:00
}
}
2015-08-13 01:02:41 +02:00
private function onOpenFstat($openArr, $result, $req) {
2016-07-21 01:33:03 +02:00
list($fh, $mode, $path, $deferred) = $openArr;
2015-08-13 01:02:41 +02:00
if ($result === -1) {
$deferred->fail(new FilesystemException(\eio_get_last_error($req)));
2015-08-13 01:02:41 +02:00
} else {
StatCache::set($path, $result);
2017-06-20 05:58:11 +02:00
$handle = new EioHandle($this->poll, $fh, $path, $mode, $result["size"]);
2016-07-21 01:33:03 +02:00
$deferred->resolve($handle);
2015-07-18 18:08:50 +02:00
}
}
/**
* {@inheritdoc}
*/
2016-11-15 06:17:19 +01:00
public function stat(string $path): Promise {
2015-08-08 16:09:07 +02:00
if ($stat = StatCache::get($path)) {
return new Success($stat);
}
2016-07-21 01:33:03 +02:00
$deferred = new Deferred;
$this->poll->listen($deferred->promise());
2015-07-18 18:08:50 +02:00
$priority = \EIO_PRI_DEFAULT;
2016-07-21 01:33:03 +02:00
$data = [$deferred, $path];
2015-08-08 16:09:07 +02:00
\eio_stat($path, $priority, [$this, "onStat"], $data);
2015-07-18 18:08:50 +02:00
2016-11-15 06:17:19 +01:00
return $deferred->promise();
2015-07-18 18:08:50 +02:00
}
2015-08-08 16:09:07 +02:00
private function onStat($data, $result, $req) {
2016-07-21 01:33:03 +02:00
list($deferred, $path) = $data;
2015-07-18 18:08:50 +02:00
if ($result === -1) {
2016-07-21 01:33:03 +02:00
$deferred->resolve(null);
2015-07-18 18:08:50 +02:00
} else {
2015-08-08 16:09:07 +02:00
StatCache::set($path, $result);
2016-07-21 01:33:03 +02:00
$deferred->resolve($result);
2015-07-18 18:08:50 +02:00
}
2015-08-08 16:09:07 +02:00
}
/**
* {@inheritdoc}
*/
2016-11-15 06:17:19 +01:00
public function exists(string $path): Promise {
2016-07-21 01:33:03 +02:00
$deferred = new Deferred;
2017-03-22 00:53:55 +01:00
$this->stat($path)->onResolve(function ($error, $result) use ($deferred) {
2016-07-21 01:33:03 +02:00
$deferred->resolve((bool) $result);
2015-08-08 16:09:07 +02:00
});
2016-11-15 06:17:19 +01:00
return $deferred->promise();
2015-08-08 16:09:07 +02:00
}
/**
* {@inheritdoc}
*/
2016-11-15 06:17:19 +01:00
public function isdir(string $path): Promise {
2016-07-21 01:33:03 +02:00
$deferred = new Deferred;
2017-03-22 00:53:55 +01:00
$this->stat($path)->onResolve(function ($error, $result) use ($deferred) {
2015-08-08 16:09:07 +02:00
if ($result) {
2016-07-21 01:33:03 +02:00
$deferred->resolve(!($result["mode"] & \EIO_S_IFREG));
2015-08-08 16:09:07 +02:00
} else {
2016-07-21 01:33:03 +02:00
$deferred->resolve(false);
2015-08-08 16:09:07 +02:00
}
});
2016-11-15 06:17:19 +01:00
return $deferred->promise();
2015-08-08 16:09:07 +02:00
}
/**
* {@inheritdoc}
*/
2016-11-15 06:17:19 +01:00
public function isfile(string $path): Promise {
2016-07-21 01:33:03 +02:00
$deferred = new Deferred;
2017-03-22 00:53:55 +01:00
$this->stat($path)->onResolve(function ($error, $result) use ($deferred) {
2015-08-08 16:09:07 +02:00
if ($result) {
2016-07-21 01:33:03 +02:00
$deferred->resolve((bool) ($result["mode"] & \EIO_S_IFREG));
2015-08-08 16:09:07 +02:00
} else {
2016-07-21 01:33:03 +02:00
$deferred->resolve(false);
2015-08-08 16:09:07 +02:00
}
});
2016-11-15 06:17:19 +01:00
return $deferred->promise();
2015-08-08 16:09:07 +02:00
}
/**
* {@inheritdoc}
*/
2016-11-15 06:17:19 +01:00
public function size(string $path): Promise {
2016-07-21 01:33:03 +02:00
$deferred = new Deferred;
2017-03-22 00:53:55 +01:00
$this->stat($path)->onResolve(function ($error, $result) use ($deferred) {
2015-08-08 16:09:07 +02:00
if (empty($result)) {
2016-07-21 01:33:03 +02:00
$deferred->fail(new FilesystemException(
2015-08-08 16:09:07 +02:00
"Specified path does not exist"
));
} elseif ($result["mode"] & \EIO_S_IFREG) {
2016-07-21 01:33:03 +02:00
$deferred->resolve($result["size"]);
2015-08-08 16:09:07 +02:00
} else {
2016-07-21 01:33:03 +02:00
$deferred->fail(new FilesystemException(
2015-08-08 16:09:07 +02:00
"Specified path is not a regular file"
));
}
});
2016-11-15 06:17:19 +01:00
return $deferred->promise();
2015-08-08 16:09:07 +02:00
}
/**
* {@inheritdoc}
*/
2016-11-15 06:17:19 +01:00
public function mtime(string $path): Promise {
2016-07-21 01:33:03 +02:00
$deferred = new Deferred;
2017-03-22 00:53:55 +01:00
$this->stat($path)->onResolve(function ($error, $result) use ($deferred) {
2015-08-08 16:09:07 +02:00
if ($result) {
2016-07-21 01:33:03 +02:00
$deferred->resolve($result["mtime"]);
2015-08-08 16:09:07 +02:00
} else {
2016-07-21 01:33:03 +02:00
$deferred->fail(new FilesystemException(
2015-08-08 16:09:07 +02:00
"Specified path does not exist"
));
}
});
2016-11-15 06:17:19 +01:00
return $deferred->promise();
2015-08-08 16:09:07 +02:00
}
/**
* {@inheritdoc}
*/
2016-11-15 06:17:19 +01:00
public function atime(string $path): Promise {
2016-07-21 01:33:03 +02:00
$deferred = new Deferred;
2017-03-22 00:53:55 +01:00
$this->stat($path)->onResolve(function ($error, $result) use ($deferred) {
2015-08-08 16:09:07 +02:00
if ($result) {
2016-07-21 01:33:03 +02:00
$deferred->resolve($result["atime"]);
2015-08-08 16:09:07 +02:00
} else {
2016-07-21 01:33:03 +02:00
$deferred->fail(new FilesystemException(
2015-08-08 16:09:07 +02:00
"Specified path does not exist"
));
}
});
2016-11-15 06:17:19 +01:00
return $deferred->promise();
2015-08-08 16:09:07 +02:00
}
/**
* {@inheritdoc}
*/
2016-11-15 06:17:19 +01:00
public function ctime(string $path): Promise {
2016-07-21 01:33:03 +02:00
$deferred = new Deferred;
2017-03-22 00:53:55 +01:00
$this->stat($path)->onResolve(function ($error, $result) use ($deferred) {
2015-08-08 16:09:07 +02:00
if ($result) {
2016-07-21 01:33:03 +02:00
$deferred->resolve($result["ctime"]);
2015-08-08 16:09:07 +02:00
} else {
2016-07-21 01:33:03 +02:00
$deferred->fail(new FilesystemException(
2015-08-08 16:09:07 +02:00
"Specified path does not exist"
));
}
});
2016-11-15 06:17:19 +01:00
return $deferred->promise();
2015-07-18 18:08:50 +02:00
}
/**
* {@inheritdoc}
*/
2016-11-15 06:17:19 +01:00
public function lstat(string $path): Promise {
2016-07-21 01:33:03 +02:00
$deferred = new Deferred;
$this->poll->listen($deferred->promise());
2015-07-18 18:08:50 +02:00
$priority = \EIO_PRI_DEFAULT;
2016-07-21 01:33:03 +02:00
\eio_lstat($path, $priority, [$this, "onLstat"], $deferred);
2015-07-18 18:08:50 +02:00
2016-11-15 06:17:19 +01:00
return $deferred->promise();
2015-07-18 18:08:50 +02:00
}
2016-07-21 01:33:03 +02:00
private function onLstat($deferred, $result, $req) {
2015-08-08 16:09:07 +02:00
if ($result === -1) {
2016-07-21 01:33:03 +02:00
$deferred->resolve(null);
2015-08-08 16:09:07 +02:00
} else {
2016-07-21 01:33:03 +02:00
$deferred->resolve($result);
2015-08-08 16:09:07 +02:00
}
}
2015-07-18 18:08:50 +02:00
/**
* {@inheritdoc}
*/
2016-11-15 06:17:19 +01:00
public function symlink(string $target, string $link): Promise {
2016-07-21 01:33:03 +02:00
$deferred = new Deferred;
$this->poll->listen($deferred->promise());
2015-07-18 18:08:50 +02:00
$priority = \EIO_PRI_DEFAULT;
2016-07-21 01:33:03 +02:00
\eio_symlink($target, $link, $priority, [$this, "onGenericResult"], $deferred);
2015-07-18 18:08:50 +02:00
2016-11-15 06:17:19 +01:00
return $deferred->promise();
2015-07-18 18:08:50 +02:00
}
2017-01-11 14:22:06 +01:00
/**
* {@inheritdoc}
*/
2016-11-15 06:17:19 +01:00
public function link(string $target, string $link): Promise {
$deferred = new Deferred;
$this->poll->listen($deferred->promise());
$priority = \EIO_PRI_DEFAULT;
\eio_link($target, $link, $priority, [$this, "onGenericResult"], $deferred);
2017-01-11 14:22:06 +01:00
2016-11-15 06:17:19 +01:00
return $deferred->promise();
}
2017-01-11 14:22:06 +01:00
/**
* {@inheritdoc}
*/
2016-11-15 06:17:19 +01:00
public function readlink(string $path): Promise {
$deferred = new Deferred;
$this->poll->listen($deferred->promise());
$priority = \EIO_PRI_DEFAULT;
\eio_readlink($path, $priority, [$this, "onGenericResult"], $deferred);
2017-01-11 14:22:06 +01:00
2016-11-15 06:17:19 +01:00
return $deferred->promise();
}
2016-07-21 01:33:03 +02:00
private function onGenericResult($deferred, $result, $req) {
2015-07-18 18:08:50 +02:00
if ($result === -1) {
$deferred->fail(new FilesystemException(\eio_get_last_error($req)));
2015-07-18 18:08:50 +02:00
} else {
2016-07-21 01:33:03 +02:00
$deferred->resolve(true);
2015-07-18 18:08:50 +02:00
}
}
/**
* {@inheritdoc}
*/
2016-11-15 06:17:19 +01:00
public function rename(string $from, string $to): Promise {
2016-07-21 01:33:03 +02:00
$deferred = new Deferred;
$this->poll->listen($deferred->promise());
2015-07-18 18:08:50 +02:00
$priority = \EIO_PRI_DEFAULT;
2016-07-21 01:33:03 +02:00
\eio_rename($from, $to, $priority, [$this, "onGenericResult"], $deferred);
2015-07-18 18:08:50 +02:00
2016-11-15 06:17:19 +01:00
return $deferred->promise();
2015-07-18 18:08:50 +02:00
}
/**
* {@inheritdoc}
*/
2016-11-15 06:17:19 +01:00
public function unlink(string $path): Promise {
2016-07-21 01:33:03 +02:00
$deferred = new Deferred;
$this->poll->listen($deferred->promise());
2015-07-18 18:08:50 +02:00
$priority = \EIO_PRI_DEFAULT;
2016-07-21 01:33:03 +02:00
$data = [$deferred, $path];
2015-08-08 16:09:07 +02:00
\eio_unlink($path, $priority, [$this, "onUnlink"], $data);
2015-07-18 18:08:50 +02:00
2016-11-15 06:17:19 +01:00
return $deferred->promise();
2015-07-18 18:08:50 +02:00
}
2015-08-08 16:09:07 +02:00
private function onUnlink($data, $result, $req) {
2016-07-21 01:33:03 +02:00
list($deferred, $path) = $data;
2015-08-08 16:09:07 +02:00
if ($result === -1) {
$deferred->fail(new FilesystemException(\eio_get_last_error($req)));
2015-08-08 16:09:07 +02:00
} else {
StatCache::clear($path);
2016-07-21 01:33:03 +02:00
$deferred->resolve(true);
2015-08-08 16:09:07 +02:00
}
}
2015-07-18 18:08:50 +02:00
/**
* {@inheritdoc}
*/
2017-01-11 07:05:18 +01:00
public function mkdir(string $path, int $mode = 0644, bool $recursive = false): Promise {
2016-07-21 01:33:03 +02:00
$deferred = new Deferred;
$this->poll->listen($deferred->promise());
2015-07-18 18:08:50 +02:00
$priority = \EIO_PRI_DEFAULT;
2016-09-28 12:39:24 +02:00
if ($recursive) {
2017-06-18 00:33:22 +02:00
$path = str_replace("/", DIRECTORY_SEPARATOR, $path);
$arrayPath = explode(DIRECTORY_SEPARATOR, $path);
2016-09-28 12:39:24 +02:00
$tmpPath = "";
2017-06-17 23:41:57 +02:00
$callback = function () use (
2017-01-11 07:05:18 +01:00
&$callback, &$arrayPath, &$tmpPath, $mode, $priority, $deferred
2016-09-28 12:39:24 +02:00
) {
$tmpPath .= DIRECTORY_SEPARATOR . array_shift($arrayPath);
if (empty($arrayPath)) {
2017-01-11 07:05:18 +01:00
\eio_mkdir($tmpPath, $mode, $priority, [$this, "onGenericResult"], $deferred);
2016-09-28 12:39:24 +02:00
} else {
2017-03-22 00:53:55 +01:00
$this->isdir($tmpPath)->onResolve(function ($error, $result) use (
2016-09-28 12:39:24 +02:00
$callback, $tmpPath, $mode, $priority
) {
if ($result) {
$callback();
} else {
\eio_mkdir($tmpPath, $mode, $priority, $callback);
}
});
}
};
$callback();
} else {
2017-01-11 07:05:18 +01:00
\eio_mkdir($path, $mode, $priority, [$this, "onGenericResult"], $deferred);
2016-09-28 12:39:24 +02:00
}
2015-07-18 18:08:50 +02:00
2016-11-15 06:17:19 +01:00
return $deferred->promise();
2015-07-18 18:08:50 +02:00
}
/**
* {@inheritdoc}
*/
2016-11-15 06:17:19 +01:00
public function rmdir(string $path): Promise {
2016-07-21 01:33:03 +02:00
$deferred = new Deferred;
$this->poll->listen($deferred->promise());
2015-07-18 18:08:50 +02:00
$priority = \EIO_PRI_DEFAULT;
2016-07-21 01:33:03 +02:00
$data = [$deferred, $path];
2015-08-08 16:09:07 +02:00
\eio_rmdir($path, $priority, [$this, "onRmdir"], $data);
2015-07-18 18:08:50 +02:00
2016-11-15 06:17:19 +01:00
return $deferred->promise();
2015-07-18 18:08:50 +02:00
}
2015-08-08 16:09:07 +02:00
private function onRmdir($data, $result, $req) {
2016-07-21 01:33:03 +02:00
list($deferred, $path) = $data;
2015-08-08 16:09:07 +02:00
if ($result === -1) {
$deferred->fail(new FilesystemException(\eio_get_last_error($req)));
2015-08-08 16:09:07 +02:00
} else {
StatCache::clear($path);
2016-07-21 01:33:03 +02:00
$deferred->resolve(true);
2015-08-08 16:09:07 +02:00
}
}
2015-07-18 18:08:50 +02:00
/**
* {@inheritdoc}
*/
2016-11-15 06:17:19 +01:00
public function scandir(string $path): Promise {
2016-07-21 01:33:03 +02:00
$deferred = new Deferred;
$this->poll->listen($deferred->promise());
2015-07-18 18:08:50 +02:00
$flags = \EIO_READDIR_STAT_ORDER | \EIO_READDIR_DIRS_FIRST;
$priority = \EIO_PRI_DEFAULT;
2016-07-21 01:33:03 +02:00
\eio_readdir($path, $flags, $priority, [$this, "onScandir"], $deferred);
2015-07-18 18:08:50 +02:00
2016-11-15 06:17:19 +01:00
return $deferred->promise();
2015-07-18 18:08:50 +02:00
}
2016-07-21 01:33:03 +02:00
private function onScandir($deferred, $result, $req) {
2015-07-18 18:08:50 +02:00
if ($result === -1) {
$deferred->fail(new FilesystemException(\eio_get_last_error($req)));
2015-07-18 18:08:50 +02:00
} else {
2016-07-21 01:33:03 +02:00
$deferred->resolve($result["names"]);
2015-07-18 18:08:50 +02:00
}
}
/**
* {@inheritdoc}
*/
2016-11-15 06:17:19 +01:00
public function chmod(string $path, int $mode): Promise {
2016-07-21 01:33:03 +02:00
$deferred = new Deferred;
$this->poll->listen($deferred->promise());
2015-07-18 18:08:50 +02:00
$priority = \EIO_PRI_DEFAULT;
2016-07-21 01:33:03 +02:00
\eio_chmod($path, $mode, $priority, [$this, "onGenericResult"], $deferred);
2015-07-18 18:08:50 +02:00
2016-11-15 06:17:19 +01:00
return $deferred->promise();
2015-07-18 18:08:50 +02:00
}
/**
* {@inheritdoc}
*/
2016-11-15 06:17:19 +01:00
public function chown(string $path, int $uid, int $gid): Promise {
2016-07-21 01:33:03 +02:00
$deferred = new Deferred;
$this->poll->listen($deferred->promise());
2015-07-18 18:08:50 +02:00
$priority = \EIO_PRI_DEFAULT;
2016-07-21 01:33:03 +02:00
\eio_chown($path, $uid, $gid, $priority, [$this, "onGenericResult"], $deferred);
2015-07-18 18:08:50 +02:00
2016-11-15 06:17:19 +01:00
return $deferred->promise();
2015-07-18 18:08:50 +02:00
}
2015-07-18 20:53:46 +02:00
/**
* {@inheritdoc}
*/
2016-11-15 06:17:19 +01:00
public function touch(string $path): Promise {
$atime = $mtime = \time();
2017-01-11 14:22:06 +01:00
2016-07-21 01:33:03 +02:00
$deferred = new Deferred;
$this->poll->listen($deferred->promise());
2015-07-18 20:53:46 +02:00
$priority = \EIO_PRI_DEFAULT;
2016-07-21 01:33:03 +02:00
\eio_utime($path, $atime, $mtime, $priority, [$this, "onGenericResult"], $deferred);
2015-07-18 20:53:46 +02:00
2016-11-15 06:17:19 +01:00
return $deferred->promise();
2015-07-18 20:53:46 +02:00
}
2015-07-18 18:08:50 +02:00
/**
* {@inheritdoc}
*/
2016-11-15 06:17:19 +01:00
public function get(string $path): Promise {
2015-07-18 18:08:50 +02:00
$flags = $flags = \EIO_O_RDONLY;
$mode = 0;
$priority = \EIO_PRI_DEFAULT;
2016-07-21 01:33:03 +02:00
$deferred = new Deferred;
$this->poll->listen($deferred->promise());
2016-07-21 01:33:03 +02:00
\eio_open($path, $flags, $mode, $priority, [$this, "onGetOpen"], $deferred);
2015-07-18 18:08:50 +02:00
2016-11-15 06:17:19 +01:00
return $deferred->promise();
2015-07-18 18:08:50 +02:00
}
2016-07-21 01:33:03 +02:00
private function onGetOpen($deferred, $result, $req) {
2015-07-18 18:08:50 +02:00
if ($result === -1) {
$deferred->fail(new FilesystemException(\eio_get_last_error($req)));
2015-07-18 18:08:50 +02:00
} else {
$priority = \EIO_PRI_DEFAULT;
2016-07-21 01:33:03 +02:00
\eio_fstat($result, $priority, [$this, "onGetFstat"], [$result, $deferred]);
2015-07-18 18:08:50 +02:00
}
}
private function onGetFstat($fhAndPromisor, $result, $req) {
2016-07-21 01:33:03 +02:00
list($fh, $deferred) = $fhAndPromisor;
2015-07-18 18:08:50 +02:00
if ($result === -1) {
$deferred->fail(new FilesystemException(\eio_get_last_error($req)));
2015-07-18 18:08:50 +02:00
return;
}
$offset = 0;
$length = $result["size"];
$priority = \EIO_PRI_DEFAULT;
\eio_read($fh, $length, $offset, $priority, [$this, "onGetRead"], $fhAndPromisor);
}
private function onGetRead($fhAndPromisor, $result, $req) {
2016-07-21 01:33:03 +02:00
list($fh, $deferred) = $fhAndPromisor;
\eio_close($fh);
2015-07-18 18:08:50 +02:00
if ($result === -1) {
$deferred->fail(new FilesystemException(\eio_get_last_error($req)));
2015-07-18 18:08:50 +02:00
} else {
2016-07-21 01:33:03 +02:00
$deferred->resolve($result);
2015-07-18 18:08:50 +02:00
}
}
/**
* {@inheritdoc}
*/
2016-11-15 06:17:19 +01:00
public function put(string $path, string $contents): Promise {
2015-07-18 18:08:50 +02:00
$flags = \EIO_O_RDWR | \EIO_O_CREAT;
$mode = \EIO_S_IRUSR | \EIO_S_IWUSR | \EIO_S_IXUSR;
$priority = \EIO_PRI_DEFAULT;
2016-07-21 01:33:03 +02:00
$deferred = new Deferred;
$this->poll->listen($deferred->promise());
2016-07-21 01:33:03 +02:00
$data = [$contents, $deferred];
2015-07-18 18:08:50 +02:00
\eio_open($path, $flags, $mode, $priority, [$this, "onPutOpen"], $data);
2016-11-15 06:17:19 +01:00
return $deferred->promise();
2015-07-18 18:08:50 +02:00
}
private function onPutOpen($data, $result, $req) {
2016-07-21 01:33:03 +02:00
list($contents, $deferred) = $data;
2015-07-18 18:08:50 +02:00
if ($result === -1) {
$deferred->fail(new FilesystemException(\eio_get_last_error($req)));
2015-07-18 18:08:50 +02:00
} else {
$length = strlen($contents);
$offset = 0;
$priority = \EIO_PRI_DEFAULT;
$callback = [$this, "onPutWrite"];
2016-07-21 01:33:03 +02:00
$fhAndPromisor = [$result, $deferred];
2015-07-18 18:08:50 +02:00
\eio_write($result, $contents, $length, $offset, $priority, $callback, $fhAndPromisor);
}
}
private function onPutWrite($fhAndPromisor, $result, $req) {
2016-07-21 01:33:03 +02:00
list($fh, $deferred) = $fhAndPromisor;
2015-07-18 18:08:50 +02:00
\eio_close($fh);
2015-07-18 18:08:50 +02:00
if ($result === -1) {
$deferred->fail(new FilesystemException(\eio_get_last_error($req)));
2015-07-18 18:08:50 +02:00
} else {
2016-07-21 01:33:03 +02:00
$deferred->resolve($result);
2015-07-18 18:08:50 +02:00
}
}
}