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

582 lines
17 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
use Amp\{ Deferred, Failure, Success };
2016-11-15 06:17:19 +01:00
use Interop\Async\{ Loop, Promise };
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;
private static $stream;
2015-07-18 18:08:50 +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
* underlying pipe via a system close() call before it's
2016-07-21 01:33:03 +02:00
* finished which generates a SIGPIPE.
*/
public function __construct() {
if (empty(self::$stream)) {
\eio_init();
self::$stream = \eio_get_event_stream();
}
2015-08-13 01:02:41 +02:00
$this->callableDecrementor = function() {
($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
};
$this->watcher = Loop::onReadable(self::$stream, static 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-11-15 06:17:19 +01:00
public function open(string $path, string $mode): Promise {
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;
($this->incrementor)(1);
2016-07-21 01:33:03 +02:00
$deferred = new Deferred;
$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
}
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) {
($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, \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) {
($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) {
($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-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);
}
($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-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;
($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-11-15 06:17:19 +01:00
public function exists(string $path): Promise {
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-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;
$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-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;
$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-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;
$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"
));
} 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;
$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-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;
$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-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;
$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-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 {
($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-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) {
($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-11-15 06:17:19 +01:00
public function symlink(string $target, string $link): Promise {
($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-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 link(string $target, string $link): Promise {
($this->incrementor)(1);
$deferred = new Deferred;
$priority = \EIO_PRI_DEFAULT;
\eio_link($target, $link, $priority, [$this, "onGenericResult"], $deferred);
2016-11-15 06:17:19 +01:00
return $deferred->promise();
}
/**
* {@inheritdoc}
*/
2016-11-15 06:17:19 +01:00
public function readlink(string $path): Promise {
($this->incrementor)(1);
$deferred = new Deferred;
$priority = \EIO_PRI_DEFAULT;
\eio_readlink($path, $priority, [$this, "onGenericResult"], $deferred);
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) {
($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-11-15 06:17:19 +01:00
public function rename(string $from, string $to): Promise {
($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-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 {
($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-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;
($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-11-15 06:17:19 +01:00
public function mkdir(string $path, int $mode = 0644): Promise {
($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-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 {
($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-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;
($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-11-15 06:17:19 +01:00
public function scandir(string $path): Promise {
($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-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) {
($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-11-15 06:17:19 +01:00
public function chmod(string $path, int $mode): Promise {
($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-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 {
($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-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();
($this->incrementor)(1);
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-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;
($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-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) {
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-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;
($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-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) {
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
}
}