2016-08-24 07:01:41 +02:00
|
|
|
<?php declare(strict_types = 1);
|
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
|
|
|
|
2016-08-24 06:55:06 +02:00
|
|
|
use Amp\{ Deferred, Failure, Success };
|
|
|
|
use Interop\Async\{ Awaitable, Loop };
|
2015-07-18 18:08:50 +02:00
|
|
|
|
2015-07-30 16:10:53 +02:00
|
|
|
class EioDriver implements Driver {
|
2015-07-18 18:08:50 +02:00
|
|
|
private $watcher;
|
|
|
|
private $pending = 0;
|
2015-08-13 01:02:41 +02:00
|
|
|
private $incrementor;
|
|
|
|
private $callableDecrementor;
|
2015-08-10 18:20:24 +02:00
|
|
|
private static $stream;
|
2015-07-18 18:08:50 +02:00
|
|
|
|
2015-08-10 18:20:24 +02:00
|
|
|
/**
|
2016-07-21 01:33:03 +02:00
|
|
|
* We have to keep a static reference of eio event streams,
|
|
|
|
* because if we don't, garbage collection can unload eio's
|
2015-08-10 18:20:24 +02:00
|
|
|
* underlying pipe via a system close() call before it's
|
2016-07-21 01:33:03 +02:00
|
|
|
* finished which generates a SIGPIPE.
|
2015-08-10 18:20:24 +02:00
|
|
|
*/
|
2015-07-30 15:07:01 +02:00
|
|
|
public function __construct() {
|
2015-08-10 18:20:24 +02:00
|
|
|
if (empty(self::$stream)) {
|
2015-07-30 15:07:01 +02:00
|
|
|
\eio_init();
|
2015-08-10 18:20:24 +02:00
|
|
|
self::$stream = \eio_get_event_stream();
|
2015-07-30 15:07:01 +02:00
|
|
|
}
|
2015-08-13 01:02:41 +02:00
|
|
|
$this->callableDecrementor = function() {
|
2016-08-24 06:55:06 +02:00
|
|
|
($this->incrementor)(-1);
|
2015-08-13 01:02:41 +02:00
|
|
|
};
|
|
|
|
$this->incrementor = function ($increment) {
|
|
|
|
switch ($increment) {
|
|
|
|
case 1:
|
|
|
|
case -1:
|
|
|
|
$this->pending += $increment;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new FilesystemException(
|
|
|
|
"Invalid pending event increment; 1 or -1 required"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if ($this->pending === 0) {
|
2016-07-21 01:33:03 +02:00
|
|
|
Loop::disable($this->watcher);
|
2015-08-13 01:02:41 +02:00
|
|
|
} elseif ($this->pending === 1) {
|
2016-07-21 01:33:03 +02:00
|
|
|
Loop::enable($this->watcher);
|
2015-08-13 01:02:41 +02:00
|
|
|
}
|
2015-07-18 18:08:50 +02:00
|
|
|
};
|
2016-08-24 06:55:06 +02:00
|
|
|
$this->watcher = Loop::onReadable(self::$stream, function () {
|
2015-07-18 18:08:50 +02:00
|
|
|
while (\eio_npending()) {
|
|
|
|
\eio_poll();
|
|
|
|
}
|
2016-07-21 01:33:03 +02:00
|
|
|
});
|
|
|
|
Loop::disable($this->watcher);
|
2015-07-18 18:08:50 +02:00
|
|
|
}
|
|
|
|
|
2015-08-13 01:02:41 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-24 06:55:06 +02:00
|
|
|
public function open(string $path, string $mode): Awaitable {
|
2015-08-13 01:02:41 +02:00
|
|
|
switch ($mode) {
|
|
|
|
case "r": $flags = \EIO_O_RDONLY; break;
|
|
|
|
case "r+": $flags = \EIO_O_RDWR; break;
|
|
|
|
case "w": $flags = \EIO_O_WRONLY | \EIO_O_CREAT; break;
|
|
|
|
case "w+": $flags = \EIO_O_RDWR | \EIO_O_CREAT; break;
|
|
|
|
case "a": $flags = \EIO_O_WRONLY | \EIO_O_CREAT | \EIO_O_APPEND; break;
|
|
|
|
case "a+": $flags = \EIO_O_RDWR | \EIO_O_CREAT | \EIO_O_APPEND; break;
|
|
|
|
case "x": $flags = \EIO_O_WRONLY | \EIO_O_CREAT | \EIO_O_EXCL; break;
|
|
|
|
case "x+": $flags = \EIO_O_RDWR | \EIO_O_CREAT | \EIO_O_EXCL; break;
|
|
|
|
case "c": $flags = \EIO_O_WRONLY | \EIO_O_CREAT; break;
|
|
|
|
case "c+": $flags = \EIO_O_RDWR | \EIO_O_CREAT; break;
|
|
|
|
default: return new Failure(new FilesystemException(
|
|
|
|
"Invalid open mode"
|
|
|
|
));
|
|
|
|
}
|
|
|
|
$chmod = ($flags & \EIO_O_CREAT) ? 0644 : 0;
|
2016-08-24 06:55:06 +02:00
|
|
|
($this->incrementor)(1);
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred = new Deferred;
|
|
|
|
$openArr = [$mode, $path, $deferred];
|
2015-08-13 01:02:41 +02:00
|
|
|
\eio_open($path, $flags, $chmod, $priority = null, [$this, "onOpenHandle"], $openArr);
|
|
|
|
|
2016-07-21 01:33:03 +02:00
|
|
|
return $deferred->getAwaitable();
|
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) {
|
2016-08-24 06:55:06 +02:00
|
|
|
($this->incrementor)(-1);
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred->fail(new FilesystemException(
|
2015-08-13 01:02:41 +02:00
|
|
|
\eio_get_last_error($req)
|
|
|
|
));
|
|
|
|
} elseif ($mode[0] === "a") {
|
|
|
|
\array_unshift($openArr, $result);
|
|
|
|
\eio_ftruncate($result, $offset = 0, $priority = null, [$this, "onOpenFtruncate"], $openArr);
|
|
|
|
} else {
|
|
|
|
\array_unshift($openArr, $result);
|
|
|
|
\eio_fstat($result, $priority = null, [$this, "onOpenFstat"], $openArr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private function onOpenFtruncate($openArr, $result, $req) {
|
2016-08-24 06:55:06 +02:00
|
|
|
($this->incrementor)(-1);
|
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) {
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred->fail(new FilesystemException(
|
2015-08-13 01:02:41 +02:00
|
|
|
\eio_get_last_error($req)
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
$handle = new EioHandle($this->incrementor, $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-08-24 06:55:06 +02:00
|
|
|
($this->incrementor)(-1);
|
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) {
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred->fail(new FilesystemException(
|
2015-08-13 01:02:41 +02:00
|
|
|
\eio_get_last_error($req)
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
StatCache::set($path, $result);
|
|
|
|
$handle = new EioHandle($this->incrementor, $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-08-24 06:55:06 +02:00
|
|
|
public function stat(string $path): Awaitable {
|
2015-08-08 16:09:07 +02:00
|
|
|
if ($stat = StatCache::get($path)) {
|
|
|
|
return new Success($stat);
|
|
|
|
}
|
|
|
|
|
2016-08-24 06:55:06 +02:00
|
|
|
($this->incrementor)(1);
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred = new Deferred;
|
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-07-21 01:33:03 +02:00
|
|
|
return $deferred->getAwaitable();
|
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;
|
2016-08-24 06:55:06 +02:00
|
|
|
($this->incrementor)(-1);
|
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-08-24 06:55:06 +02:00
|
|
|
public function exists(string $path): Awaitable {
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred = new Deferred;
|
|
|
|
$this->stat($path)->when(function ($error, $result) use ($deferred) {
|
|
|
|
$deferred->resolve((bool) $result);
|
2015-08-08 16:09:07 +02:00
|
|
|
});
|
|
|
|
|
2016-07-21 01:33:03 +02:00
|
|
|
return $deferred->getAwaitable();
|
2015-08-08 16:09:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-24 06:55:06 +02:00
|
|
|
public function isdir(string $path): Awaitable {
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred = new Deferred;
|
|
|
|
$this->stat($path)->when(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-07-21 01:33:03 +02:00
|
|
|
return $deferred->getAwaitable();
|
2015-08-08 16:09:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-24 06:55:06 +02:00
|
|
|
public function isfile(string $path): Awaitable {
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred = new Deferred;
|
|
|
|
$this->stat($path)->when(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-07-21 01:33:03 +02:00
|
|
|
return $deferred->getAwaitable();
|
2015-08-08 16:09:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-24 06:55:06 +02:00
|
|
|
public function size(string $path): Awaitable {
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred = new Deferred;
|
|
|
|
$this->stat($path)->when(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"
|
|
|
|
));
|
2016-08-24 06:55:06 +02:00
|
|
|
} 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-07-21 01:33:03 +02:00
|
|
|
return $deferred->getAwaitable();
|
2015-08-08 16:09:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-24 06:55:06 +02:00
|
|
|
public function mtime(string $path): Awaitable {
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred = new Deferred;
|
|
|
|
$this->stat($path)->when(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-07-21 01:33:03 +02:00
|
|
|
return $deferred->getAwaitable();
|
2015-08-08 16:09:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-24 06:55:06 +02:00
|
|
|
public function atime(string $path): Awaitable {
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred = new Deferred;
|
|
|
|
$this->stat($path)->when(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-07-21 01:33:03 +02:00
|
|
|
return $deferred->getAwaitable();
|
2015-08-08 16:09:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-24 06:55:06 +02:00
|
|
|
public function ctime(string $path): Awaitable {
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred = new Deferred;
|
|
|
|
$this->stat($path)->when(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-07-21 01:33:03 +02:00
|
|
|
return $deferred->getAwaitable();
|
2015-07-18 18:08:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-24 06:55:06 +02:00
|
|
|
public function lstat(string $path): Awaitable {
|
|
|
|
($this->incrementor)(1);
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred = new Deferred;
|
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-07-21 01:33:03 +02:00
|
|
|
return $deferred->getAwaitable();
|
2015-07-18 18:08:50 +02:00
|
|
|
}
|
|
|
|
|
2016-07-21 01:33:03 +02:00
|
|
|
private function onLstat($deferred, $result, $req) {
|
2016-08-24 06:55:06 +02:00
|
|
|
($this->incrementor)(-1);
|
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-08-24 06:55:06 +02:00
|
|
|
public function symlink(string $target, string $link): Awaitable {
|
|
|
|
($this->incrementor)(1);
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred = new Deferred;
|
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-07-21 01:33:03 +02:00
|
|
|
return $deferred->getAwaitable();
|
2015-07-18 18:08:50 +02:00
|
|
|
}
|
2016-08-24 06:55:06 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function link(string $target, string $link): Awaitable {
|
|
|
|
($this->incrementor)(1);
|
|
|
|
$deferred = new Deferred;
|
|
|
|
$priority = \EIO_PRI_DEFAULT;
|
|
|
|
\eio_link($target, $link, $priority, [$this, "onGenericResult"], $deferred);
|
|
|
|
|
|
|
|
return $deferred->getAwaitable();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function readlink(string $path): Awaitable {
|
|
|
|
($this->incrementor)(1);
|
|
|
|
$deferred = new Deferred;
|
|
|
|
$priority = \EIO_PRI_DEFAULT;
|
|
|
|
\eio_readlink($path, $priority, [$this, "onGenericResult"], $deferred);
|
|
|
|
|
|
|
|
return $deferred->getAwaitable();
|
|
|
|
}
|
2016-07-21 01:33:03 +02:00
|
|
|
private function onGenericResult($deferred, $result, $req) {
|
2016-08-24 06:55:06 +02:00
|
|
|
($this->incrementor)(-1);
|
2015-07-18 18:08:50 +02:00
|
|
|
if ($result === -1) {
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred->fail(new FilesystemException(
|
2015-07-18 18:08:50 +02:00
|
|
|
\eio_get_last_error($req)
|
|
|
|
));
|
|
|
|
} else {
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred->resolve(true);
|
2015-07-18 18:08:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-24 06:55:06 +02:00
|
|
|
public function rename(string $from, string $to): Awaitable {
|
|
|
|
($this->incrementor)(1);
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred = new Deferred;
|
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-07-21 01:33:03 +02:00
|
|
|
return $deferred->getAwaitable();
|
2015-07-18 18:08:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-24 06:55:06 +02:00
|
|
|
public function unlink(string $path): Awaitable {
|
|
|
|
($this->incrementor)(1);
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred = new Deferred;
|
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-07-21 01:33:03 +02:00
|
|
|
return $deferred->getAwaitable();
|
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;
|
2016-08-24 06:55:06 +02:00
|
|
|
($this->incrementor)(-1);
|
2015-08-08 16:09:07 +02:00
|
|
|
if ($result === -1) {
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred->fail(new FilesystemException(
|
2015-08-08 16:09:07 +02:00
|
|
|
\eio_get_last_error($req)
|
|
|
|
));
|
|
|
|
} 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-08-24 06:55:06 +02:00
|
|
|
public function mkdir(string $path, int $mode = 0644): Awaitable {
|
|
|
|
($this->incrementor)(1);
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred = new Deferred;
|
2015-07-18 18:08:50 +02:00
|
|
|
$priority = \EIO_PRI_DEFAULT;
|
2016-07-21 01:33:03 +02:00
|
|
|
\eio_mkdir($path, $mode, $priority, [$this, "onGenericResult"], $deferred);
|
2015-07-18 18:08:50 +02:00
|
|
|
|
2016-07-21 01:33:03 +02:00
|
|
|
return $deferred->getAwaitable();
|
2015-07-18 18:08:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-24 06:55:06 +02:00
|
|
|
public function rmdir(string $path): Awaitable {
|
|
|
|
($this->incrementor)(1);
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred = new Deferred;
|
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-07-21 01:33:03 +02:00
|
|
|
return $deferred->getAwaitable();
|
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;
|
2016-08-24 06:55:06 +02:00
|
|
|
($this->incrementor)(-1);
|
2015-08-08 16:09:07 +02:00
|
|
|
if ($result === -1) {
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred->fail(new FilesystemException(
|
2015-08-08 16:09:07 +02:00
|
|
|
\eio_get_last_error($req)
|
|
|
|
));
|
|
|
|
} 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-08-24 06:55:06 +02:00
|
|
|
public function scandir(string $path): Awaitable {
|
|
|
|
($this->incrementor)(1);
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred = new Deferred;
|
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-07-21 01:33:03 +02:00
|
|
|
return $deferred->getAwaitable();
|
2015-07-18 18:08:50 +02:00
|
|
|
}
|
|
|
|
|
2016-07-21 01:33:03 +02:00
|
|
|
private function onScandir($deferred, $result, $req) {
|
2016-08-24 06:55:06 +02:00
|
|
|
($this->incrementor)(-1);
|
2015-07-18 18:08:50 +02:00
|
|
|
if ($result === -1) {
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred->fail(new FilesystemException(
|
2015-07-18 18:08:50 +02:00
|
|
|
\eio_get_last_error($req)
|
|
|
|
));
|
|
|
|
} else {
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred->resolve($result["names"]);
|
2015-07-18 18:08:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-24 06:55:06 +02:00
|
|
|
public function chmod(string $path, int $mode): Awaitable {
|
|
|
|
($this->incrementor)(1);
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred = new Deferred;
|
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-07-21 01:33:03 +02:00
|
|
|
return $deferred->getAwaitable();
|
2015-07-18 18:08:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-24 07:01:55 +02:00
|
|
|
public function chown(string $path, int $uid, int $gid): Awaitable {
|
2016-08-24 06:55:06 +02:00
|
|
|
($this->incrementor)(1);
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred = new Deferred;
|
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-07-21 01:33:03 +02:00
|
|
|
return $deferred->getAwaitable();
|
2015-07-18 18:08:50 +02:00
|
|
|
}
|
|
|
|
|
2015-07-18 20:53:46 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-24 06:55:06 +02:00
|
|
|
public function touch(string $path): Awaitable {
|
2015-07-18 20:53:46 +02:00
|
|
|
$atime = $mtime = time();
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred = new Deferred;
|
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-07-21 01:33:03 +02:00
|
|
|
return $deferred->getAwaitable();
|
2015-07-18 20:53:46 +02:00
|
|
|
}
|
|
|
|
|
2015-07-18 18:08:50 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-24 06:55:06 +02:00
|
|
|
public function get(string $path): Awaitable {
|
2015-07-18 18:08:50 +02:00
|
|
|
$flags = $flags = \EIO_O_RDONLY;
|
|
|
|
$mode = 0;
|
|
|
|
$priority = \EIO_PRI_DEFAULT;
|
|
|
|
|
2016-08-24 06:55:06 +02:00
|
|
|
($this->incrementor)(1);
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred = new Deferred;
|
|
|
|
\eio_open($path, $flags, $mode, $priority, [$this, "onGetOpen"], $deferred);
|
2015-07-18 18:08:50 +02:00
|
|
|
|
2016-07-21 01:33:03 +02:00
|
|
|
return $deferred->getAwaitable();
|
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) {
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred->fail(new FilesystemException(
|
2015-07-18 18:08:50 +02:00
|
|
|
\eio_get_last_error($req)
|
|
|
|
));
|
|
|
|
} 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) {
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred->fail(new FilesystemException(
|
2015-07-18 18:08:50 +02:00
|
|
|
\eio_get_last_error($req)
|
|
|
|
));
|
|
|
|
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;
|
2015-07-18 18:08:50 +02:00
|
|
|
$priority = \EIO_PRI_DEFAULT;
|
2015-08-13 01:02:41 +02:00
|
|
|
\eio_close($fh, $priority, $this->callableDecrementor);
|
2015-07-18 18:08:50 +02:00
|
|
|
if ($result === -1) {
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred->fail(new FilesystemException(
|
2015-07-18 18:08:50 +02:00
|
|
|
\eio_get_last_error($req)
|
|
|
|
));
|
|
|
|
} else {
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred->resolve($result);
|
2015-07-18 18:08:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-08-24 06:55:06 +02:00
|
|
|
public function put(string $path, string $contents): Awaitable {
|
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-08-24 06:55:06 +02:00
|
|
|
($this->incrementor)(1);
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred = new Deferred;
|
|
|
|
$data = [$contents, $deferred];
|
2015-07-18 18:08:50 +02:00
|
|
|
\eio_open($path, $flags, $mode, $priority, [$this, "onPutOpen"], $data);
|
|
|
|
|
2016-07-21 01:33:03 +02:00
|
|
|
return $deferred->getAwaitable();
|
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) {
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred->fail(new FilesystemException(
|
2015-07-18 18:08:50 +02:00
|
|
|
\eio_get_last_error($req)
|
|
|
|
));
|
|
|
|
} 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);
|
|
|
|
$priority = \EIO_PRI_DEFAULT;
|
2015-08-13 01:02:41 +02:00
|
|
|
\eio_close($fh, $priority, $this->callableDecrementor);
|
2015-07-18 18:08:50 +02:00
|
|
|
if ($result === -1) {
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred->fail(new FilesystemException(
|
2015-07-18 18:08:50 +02:00
|
|
|
\eio_get_last_error($req)
|
|
|
|
));
|
|
|
|
} else {
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred->resolve($result);
|
2015-07-18 18:08:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function __destruct() {
|
2016-07-21 01:33:03 +02:00
|
|
|
Loop::cancel($this->watcher);
|
2015-07-18 18:08:50 +02:00
|
|
|
}
|
|
|
|
}
|