1
0
mirror of https://github.com/danog/file.git synced 2024-11-27 04:14:50 +01:00
file/lib/UvDriver.php

583 lines
19 KiB
PHP
Raw Normal View History

<?php
2015-07-11 03:59:39 +02:00
2015-08-05 16:55:56 +02:00
namespace Amp\File;
2015-07-11 03:59:39 +02:00
use Amp\{ Coroutine, Deferred, Failure, Success };
2017-01-11 14:22:06 +01:00
use AsyncInterop\Promise;
2015-07-11 03:59:39 +02:00
2015-07-30 16:10:53 +02:00
class UvDriver implements Driver {
2016-07-21 01:33:03 +02:00
private $driver;
2015-07-11 03:59:39 +02:00
private $loop;
2016-07-21 01:33:03 +02:00
private $busy;
2015-07-11 03:59:39 +02:00
/**
2017-01-11 14:22:06 +01:00
* @param \AsyncInterop\Loop\Driver $driver
2015-07-11 03:59:39 +02:00
*/
2017-01-11 14:22:06 +01:00
public function __construct(\AsyncInterop\Loop\Driver $driver) {
2016-07-21 01:33:03 +02:00
$loop = $driver->getHandle();
if (!is_resource($loop) || get_resource_type($loop) != "uv_loop") {
throw new \InvalidArgumentException("Expected a driver whose underlying loop is an uv_loop");
}
2017-01-11 14:22:06 +01:00
2016-07-21 01:33:03 +02:00
$this->driver = $driver;
$this->loop = $loop;
2017-01-11 14:22:06 +01:00
// dummy handle to be able to tell the loop that there is work being done and it shouldn't abort if there are no other watchers at a given moment
2016-07-21 01:33:03 +02:00
$this->busy = $driver->repeat(PHP_INT_MAX, function(){ });
$driver->unreference($this->busy);
2015-07-11 03:59:39 +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 = \UV::O_RDONLY; break;
case "r+": $flags = \UV::O_RDWR; break;
case "w": $flags = \UV::O_WRONLY | \UV::O_CREAT; break;
case "w+": $flags = \UV::O_RDWR | \UV::O_CREAT; break;
case "a": $flags = \UV::O_WRONLY | \UV::O_CREAT | \UV::O_APPEND; break;
case "a+": $flags = \UV::O_RDWR | \UV::O_CREAT | \UV::O_APPEND; break;
case "x": $flags = \UV::O_WRONLY | \UV::O_CREAT | \UV::O_EXCL; break;
case "x+": $flags = \UV::O_RDWR | \UV::O_CREAT | \UV::O_EXCL; break;
case "c": $flags = \UV::O_WRONLY | \UV::O_CREAT; break;
case "c+": $flags = \UV::O_RDWR | \UV::O_CREAT; break;
default: return new Failure(new FilesystemException(
"Invalid open mode"
));
}
$chmod = ($flags & \UV::O_CREAT) ? 0644 : 0;
2016-07-21 01:33:03 +02:00
$this->driver->reference($this->busy);
$deferred = new Deferred;
$openArr = [$mode, $path, $deferred];
2015-08-13 01:02:41 +02:00
\uv_fs_open($this->loop, $path, $flags, $chmod, function($fh) use ($openArr) {
if ($fh) {
$this->onOpenHandle($fh, $openArr);
} else {
2016-07-21 01:33:03 +02:00
$this->driver->unreference($this->busy);
list( , $path, $deferred) = $openArr;
$deferred->fail(new FilesystemException(
"Failed opening file handle to $path"
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($fh, array $openArr) {
list($mode) = $openArr;
if ($mode[0] === "w") {
\uv_fs_ftruncate($this->loop, $fh, $length = 0, function($fh) use ($openArr) {
2016-07-21 01:33:03 +02:00
$this->driver->unreference($this->busy);
2015-08-13 01:02:41 +02:00
if ($fh) {
$this->finalizeHandle($fh, $size = 0, $openArr);
} else {
2016-07-21 01:33:03 +02:00
list( , $path, $deferred) = $openArr;
$deferred->fail(new FilesystemException(
"Failed truncating file $path"
2015-08-13 01:02:41 +02:00
));
}
});
} else {
\uv_fs_fstat($this->loop, $fh, function($fh, $stat) use ($openArr) {
2016-07-21 01:33:03 +02:00
$this->driver->unreference($this->busy);
2015-08-13 01:02:41 +02:00
if ($fh) {
StatCache::set($openArr[1], $stat);
$this->finalizeHandle($fh, $stat["size"], $openArr);
} else {
2016-07-21 01:33:03 +02:00
list( , $path, $deferred) = $openArr;
$deferred->fail(new FilesystemException(
"Failed reading file size from open handle pointing to $path"
2015-08-13 01:02:41 +02:00
));
}
});
}
}
private function finalizeHandle($fh, $size, array $openArr) {
2016-07-21 01:33:03 +02:00
list($mode, $path, $deferred) = $openArr;
$handle = new UvHandle($this->loop, $this->busy, $fh, $path, $mode, $size);
$deferred->resolve($handle);
2015-08-13 01:02:41 +02:00
}
2015-07-11 03:59:39 +02:00
/**
* {@inheritdoc}
*/
2016-11-15 06:17:19 +01:00
public function stat(string $path): Promise {
2015-08-29 13:34:18 +02:00
if ($stat = StatCache::get($path)) {
return new Success($stat);
}
2016-07-21 01:33:03 +02:00
$this->driver->reference($this->busy);
$deferred = new Deferred;
\uv_fs_stat($this->loop, $path, function($fh, $stat) use ($deferred, $path) {
2015-08-08 16:09:07 +02:00
if (empty($fh)) {
2015-07-11 03:59:39 +02:00
$stat = null;
2015-08-08 16:09:07 +02:00
} else {
StatCache::set($path, $stat);
2015-07-11 03:59:39 +02:00
}
2016-07-21 01:33:03 +02:00
$this->driver->unreference($this->busy);
$deferred->resolve($stat);
2015-07-11 03:59:39 +02:00
});
2016-11-15 06:17:19 +01:00
return $deferred->promise();
2015-07-11 03:59:39 +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"] & \UV::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"] & \UV::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"] & \UV::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-08-08 16:09:07 +02:00
}
2015-07-11 03:59:39 +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
$this->driver->reference($this->busy);
$deferred = new Deferred;
\uv_fs_lstat($this->loop, $path, function($fh, $stat) use ($deferred) {
2015-08-08 16:09:07 +02:00
if (empty($fh)) {
2015-07-11 03:59:39 +02:00
$stat = null;
}
2016-07-21 01:33:03 +02:00
$this->driver->unreference($this->busy);
$deferred->resolve($stat);
2015-07-11 03:59:39 +02:00
});
2016-11-15 06:17:19 +01:00
return $deferred->promise();
2015-07-11 03:59:39 +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
$this->driver->reference($this->busy);
$deferred = new Deferred;
\uv_fs_symlink($this->loop, $target, $link, \UV::S_IRWXU | \UV::S_IRUSR, function($fh) use ($deferred) {
2016-07-21 01:33:03 +02:00
$this->driver->unreference($this->busy);
$deferred->resolve((bool)$fh);
2015-07-11 03:59:39 +02:00
});
2016-11-15 06:17:19 +01:00
return $deferred->promise();
2015-07-11 03:59:39 +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 {
$this->driver->reference($this->busy);
$deferred = new Deferred;
\uv_fs_link($this->loop, $target, $link, \UV::S_IRWXU | \UV::S_IRUSR, function($fh) use ($deferred) {
$this->driver->unreference($this->busy);
$deferred->resolve((bool)$fh);
});
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 {
$this->driver->reference($this->busy);
$deferred = new Deferred;
\uv_fs_readlink($this->loop, $path, function($fh) use ($deferred) {
$this->driver->unreference($this->busy);
$deferred->resolve((bool)$fh);
});
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
2015-07-11 03:59:39 +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
$this->driver->reference($this->busy);
$deferred = new Deferred;
\uv_fs_rename($this->loop, $from, $to, function($fh) use ($deferred, $from) {
$this->driver->unreference($this->busy);
StatCache::clear($from);
2016-07-21 01:33:03 +02:00
$deferred->resolve((bool)$fh);
2015-07-11 03:59:39 +02:00
});
2016-11-15 06:17:19 +01:00
return $deferred->promise();
2015-07-11 03:59:39 +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
$this->driver->reference($this->busy);
$deferred = new Deferred;
\uv_fs_unlink($this->loop, $path, function($fh) use ($deferred, $path) {
$this->driver->unreference($this->busy);
StatCache::clear($path);
2016-07-21 01:33:03 +02:00
$deferred->resolve((bool)$fh);
2015-07-11 03:59:39 +02:00
});
2016-11-15 06:17:19 +01:00
return $deferred->promise();
2015-07-11 03:59:39 +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
$this->driver->reference($this->busy);
$deferred = new Deferred;
2016-09-28 12:39:24 +02:00
if ($recursive) {
2016-09-28 14:18:05 +02:00
$path = str_replace("/", DIRECTORY_SEPARATOR, $path);
2016-09-28 12:39:24 +02:00
$arrayPath = array_filter(explode(DIRECTORY_SEPARATOR, $path));
$tmpPath = "";
$callback = function() use (
2017-01-11 07:05:18 +01:00
&$callback, &$arrayPath, &$tmpPath, $mode, $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
\uv_fs_mkdir($this->loop, $tmpPath, $mode, function($fh) use ($deferred) {
$this->driver->unreference($this->busy);
$deferred->resolve((bool) $fh);
2016-09-28 12:39:24 +02:00
});
} else {
$this->isdir($tmpPath)->when(function ($error, $result) use (
$callback, $tmpPath, $mode
) {
if ($result) {
$callback();
} else {
\uv_fs_mkdir($this->loop, $tmpPath, $mode, $callback);
}
});
}
};
$callback();
} else {
2017-01-11 07:05:18 +01:00
\uv_fs_mkdir($this->loop, $path, $mode, function($fh) use ($deferred) {
$this->driver->unreference($this->busy);
$deferred->resolve((bool) $fh);
2016-09-28 12:39:24 +02:00
});
}
2015-07-11 03:59:39 +02:00
2016-11-15 06:17:19 +01:00
return $deferred->promise();
2015-07-11 03:59:39 +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
$this->driver->reference($this->busy);
$deferred = new Deferred;
\uv_fs_rmdir($this->loop, $path, function($fh) use ($deferred, $path) {
$this->driver->unreference($this->busy);
StatCache::clear($path);
2016-07-21 01:33:03 +02:00
$deferred->resolve((bool)$fh);
2015-07-11 03:59:39 +02:00
});
2016-11-15 06:17:19 +01:00
return $deferred->promise();
2015-07-11 03:59:39 +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
$this->driver->reference($this->busy);
$deferred = new Deferred;
uv_fs_readdir($this->loop, $path, 0, function($fh, $data) use ($deferred, $path) {
$this->driver->unreference($this->busy);
2015-07-11 03:59:39 +02:00
if (empty($fh)) {
2016-07-21 01:33:03 +02:00
$deferred->fail(new FilesystemException(
2015-07-11 03:59:39 +02:00
"Failed reading contents from {$path}"
));
} else {
2016-07-21 01:33:03 +02:00
$deferred->resolve($data);
2015-07-11 03:59:39 +02:00
}
});
2016-11-15 06:17:19 +01:00
return $deferred->promise();
2015-07-11 03:59:39 +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
$this->driver->reference($this->busy);
$deferred = new Deferred;
\uv_fs_chmod($this->loop, $path, $mode, function($fh) use ($deferred) {
$this->driver->unreference($this->busy);
$deferred->resolve((bool)$fh);
2015-07-11 03:59:39 +02:00
});
2016-11-15 06:17:19 +01:00
return $deferred->promise();
2015-07-11 03:59:39 +02:00
}
/**
* {@inheritdoc}
*/
2016-11-15 06:17:19 +01:00
public function chown(string $path, int $uid, int $gid): Promise {
2015-07-11 03:59:39 +02:00
// @TODO Return a failure in windows environments
2016-07-21 01:33:03 +02:00
$this->driver->reference($this->busy);
$deferred = new Deferred;
\uv_fs_chown($this->loop, $path, $uid, $gid, function($fh) use ($deferred) {
$this->driver->unreference($this->busy);
$deferred->resolve((bool)$fh);
2015-07-11 03:59:39 +02:00
});
2016-11-15 06:17:19 +01:00
return $deferred->promise();
2015-07-11 03:59:39 +02:00
}
/**
* {@inheritdoc}
2015-07-18 20:53:46 +02:00
*/
2016-11-15 06:17:19 +01:00
public function touch(string $path): Promise {
2016-07-21 01:33:03 +02:00
$this->driver->reference($this->busy);
2015-07-18 20:53:46 +02:00
$atime = $mtime = time();
2016-07-21 01:33:03 +02:00
$deferred = new Deferred;
\uv_fs_utime($this->loop, $path, $mtime, $atime, function() use ($deferred) {
2015-07-18 20:53:46 +02:00
// The uv_fs_utime() callback does not receive any args at this time
2016-07-21 01:33:03 +02:00
$this->driver->unreference($this->busy);
$deferred->resolve(true);
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
}
/**
* {@inheritdoc}
2015-07-11 03:59:39 +02:00
*/
2016-11-15 06:17:19 +01:00
public function get(string $path): Promise {
return new Coroutine($this->doGet($path));
2015-07-11 03:59:39 +02:00
}
2015-07-17 16:27:38 +02:00
private function doGet($path): \Generator {
2016-07-21 01:33:03 +02:00
$this->driver->reference($this->busy);
2016-11-15 06:17:19 +01:00
$promise = $this->doFsOpen($path, $flags = \UV::O_RDONLY, $mode = 0);
if (!$fh = yield $promise) {
2016-07-21 01:33:03 +02:00
$this->driver->unreference($this->busy);
throw new FilesystemException(
2015-07-11 03:59:39 +02:00
"Failed opening file handle: {$path}"
);
}
2016-07-21 01:33:03 +02:00
$deferred = new Deferred;
2015-07-18 18:08:34 +02:00
$stat = (yield $this->doFsStat($fh));
2015-07-11 03:59:39 +02:00
if (empty($stat)) {
2016-07-21 01:33:03 +02:00
$this->driver->unreference($this->busy);
$deferred->fail(new FilesystemException(
2015-07-11 03:59:39 +02:00
"stat operation failed on open file handle"
));
} elseif (!$stat["isfile"]) {
2016-07-21 01:33:03 +02:00
\uv_fs_close($this->loop, $fh, function() use ($deferred) {
$this->driver->unreference($this->busy);
$deferred->fail(new FilesystemException(
2015-07-11 03:59:39 +02:00
"cannot buffer contents: path is not a file"
));
});
} else {
2015-07-18 18:08:34 +02:00
$buffer = (yield $this->doFsRead($fh, $offset = 0, $stat["size"]));
2015-07-11 03:59:39 +02:00
if ($buffer === false ) {
2016-07-21 01:33:03 +02:00
\uv_fs_close($this->loop, $fh, function() use ($deferred) {
$this->driver->unreference($this->busy);
$deferred->fail(new FilesystemException(
2015-07-11 03:59:39 +02:00
"read operation failed on open file handle"
));
});
} else {
2016-07-21 01:33:03 +02:00
\uv_fs_close($this->loop, $fh, function() use ($deferred, $buffer) {
$this->driver->unreference($this->busy);
$deferred->resolve($buffer);
2015-07-11 03:59:39 +02:00
});
}
}
2016-11-15 06:17:19 +01:00
return yield $deferred->promise();
2015-07-11 03:59:39 +02:00
}
2015-07-17 16:27:38 +02:00
private function doFsOpen($path, $flags, $mode) {
2016-07-21 01:33:03 +02:00
$deferred = new Deferred;
\uv_fs_open($this->loop, $path, $flags, $mode, function($fh) use ($deferred, $path) {
$deferred->resolve($fh);
2015-07-11 03:59:39 +02:00
});
2016-11-15 06:17:19 +01:00
return $deferred->promise();
2015-07-11 03:59:39 +02:00
}
2015-07-17 16:27:38 +02:00
private function doFsStat($fh) {
2016-07-21 01:33:03 +02:00
$deferred = new Deferred;
\uv_fs_fstat($this->loop, $fh, function($fh, $stat) use ($deferred) {
2015-07-11 03:59:39 +02:00
if ($fh) {
$stat["isdir"] = (bool) ($stat["mode"] & \UV::S_IFDIR);
$stat["isfile"] = !$stat["isdir"];
2016-07-21 01:33:03 +02:00
$deferred->resolve($stat);
2015-07-11 03:59:39 +02:00
} else {
2016-07-21 01:33:03 +02:00
$deferred->resolve();
2015-07-11 03:59:39 +02:00
}
});
2016-11-15 06:17:19 +01:00
return $deferred->promise();
2015-07-11 03:59:39 +02:00
}
2015-07-17 16:27:38 +02:00
private function doFsRead($fh, $offset, $len) {
2016-07-21 01:33:03 +02:00
$deferred = new Deferred;
\uv_fs_read($this->loop, $fh, $offset, $len, function($fh, $nread, $buffer) use ($deferred) {
$deferred->resolve($nread < 0 ? false : $buffer);
2015-07-11 03:59:39 +02:00
});
2016-11-15 06:17:19 +01:00
return $deferred->promise();
2015-07-11 03:59:39 +02:00
}
/**
* {@inheritdoc}
*/
2016-11-15 06:17:19 +01:00
public function put(string $path, string $contents): Promise {
return new Coroutine($this->doPut($path, $contents));
2015-07-11 03:59:39 +02:00
}
2015-07-17 16:27:38 +02:00
private function doPut($path, $contents): \Generator {
2015-07-11 03:59:39 +02:00
$flags = \UV::O_WRONLY | \UV::O_CREAT;
$mode = \UV::S_IRWXU | \UV::S_IRUSR;
2016-07-21 01:33:03 +02:00
$this->driver->reference($this->busy);
2016-11-15 06:17:19 +01:00
$promise = $this->doFsOpen($path, $flags, $mode);
if (!$fh = yield $promise) {
2016-07-21 01:33:03 +02:00
$this->driver->unreference($this->busy);
throw new FilesystemException(
2015-07-11 03:59:39 +02:00
"Failed opening write file handle"
);
}
2016-07-21 01:33:03 +02:00
$deferred = new Deferred;
2015-07-11 03:59:39 +02:00
$len = strlen($contents);
2016-07-21 01:33:03 +02:00
\uv_fs_write($this->loop, $fh, $contents, $offset = 0, function($fh, $result) use ($deferred, $len) {
\uv_fs_close($this->loop, $fh, function() use ($deferred, $result, $len) {
$this->driver->unreference($this->busy);
2015-07-11 03:59:39 +02:00
if ($result < 0) {
2016-07-21 01:33:03 +02:00
$deferred->fail(new FilesystemException(
\uv_strerror($result)
2015-07-11 03:59:39 +02:00
));
} else {
2016-07-21 01:33:03 +02:00
$deferred->resolve($len);
2015-07-11 03:59:39 +02:00
}
});
});
2016-11-15 06:17:19 +01:00
return yield $deferred->promise();
2015-07-11 03:59:39 +02:00
}
}