2016-12-30 03:59:59 +01:00
|
|
|
<?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
|
|
|
|
2017-06-17 23:41:57 +02:00
|
|
|
use Amp\Coroutine;
|
|
|
|
use Amp\Deferred;
|
|
|
|
use Amp\Loop;
|
|
|
|
use Amp\Promise;
|
|
|
|
use Amp\Success;
|
2015-07-11 03:59:39 +02:00
|
|
|
|
2015-07-30 16:10:53 +02:00
|
|
|
class UvDriver implements Driver {
|
2017-03-17 04:39:49 +01:00
|
|
|
/** @var \Amp\Loop\Driver */
|
2016-07-21 01:33:03 +02:00
|
|
|
private $driver;
|
2017-01-11 17:29:14 +01:00
|
|
|
|
2017-05-17 02:55:24 +02:00
|
|
|
/** @var \UVLoop|resource Loop resource of type uv_loop or instance of \UVLoop. */
|
2015-07-11 03:59:39 +02:00
|
|
|
private $loop;
|
2017-01-11 17:29:14 +01:00
|
|
|
|
|
|
|
/** @var string Loop onReadable watcher. */
|
2016-07-21 01:33:03 +02:00
|
|
|
private $busy;
|
2015-07-11 03:59:39 +02:00
|
|
|
|
2017-06-20 13:38:36 +02:00
|
|
|
/** @var int Number of pending operations for disabling / enabling the busy watcher */
|
|
|
|
private $pendingOperations = 0;
|
|
|
|
|
2015-07-11 03:59:39 +02:00
|
|
|
/**
|
2017-05-17 02:55:24 +02:00
|
|
|
* @param \Amp\Loop\UvDriver $driver
|
2015-07-11 03:59:39 +02:00
|
|
|
*/
|
2017-05-17 02:55:24 +02:00
|
|
|
public function __construct(Loop\UvDriver $driver) {
|
2016-07-21 01:33:03 +02:00
|
|
|
$this->driver = $driver;
|
2017-05-17 02:55:24 +02:00
|
|
|
$this->loop = $driver->getHandle();
|
2017-01-11 14:22:06 +01:00
|
|
|
|
2017-06-20 13:38:36 +02: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.
|
2017-06-20 07:06:12 +02:00
|
|
|
$this->busy = $driver->repeat(PHP_INT_MAX, function () {});
|
2016-07-21 01:33:03 +02:00
|
|
|
$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 {
|
2017-05-18 05:34:27 +02:00
|
|
|
$flags = $this->parseMode($mode);
|
2015-08-13 01:02:41 +02:00
|
|
|
$chmod = ($flags & \UV::O_CREAT) ? 0644 : 0;
|
2017-06-20 13:38:36 +02:00
|
|
|
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred = new Deferred;
|
2017-06-20 13:38:36 +02:00
|
|
|
$this->watchOperation($deferred->promise());
|
|
|
|
|
2016-07-21 01:33:03 +02:00
|
|
|
$openArr = [$mode, $path, $deferred];
|
2017-01-11 17:29:14 +01:00
|
|
|
\uv_fs_open($this->loop, $path, $flags, $chmod, function ($fh) use ($openArr) {
|
2015-08-13 01:02:41 +02:00
|
|
|
if ($fh) {
|
|
|
|
$this->onOpenHandle($fh, $openArr);
|
|
|
|
} else {
|
2017-06-20 07:31:58 +02:00
|
|
|
list(, $path, $deferred) = $openArr;
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred->fail(new FilesystemException(
|
2015-09-11 00:40:33 +02:00
|
|
|
"Failed opening file handle to $path"
|
2015-08-13 01:02:41 +02:00
|
|
|
));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-06-20 07:06:12 +02:00
|
|
|
return $deferred->promise();
|
2015-08-13 01:02:41 +02:00
|
|
|
}
|
|
|
|
|
2017-05-18 05:34:27 +02:00
|
|
|
private function parseMode(string $mode): int {
|
|
|
|
$mode = \str_replace(['b', 't'], '', $mode);
|
|
|
|
|
|
|
|
switch ($mode) {
|
|
|
|
case "r": return \UV::O_RDONLY;
|
|
|
|
case "r+": return \UV::O_RDWR;
|
|
|
|
case "w": return \UV::O_WRONLY | \UV::O_CREAT;
|
|
|
|
case "w+": return \UV::O_RDWR | \UV::O_CREAT;
|
|
|
|
case "a": return \UV::O_WRONLY | \UV::O_CREAT | \UV::O_APPEND;
|
|
|
|
case "a+": return \UV::O_RDWR | \UV::O_CREAT | \UV::O_APPEND;
|
|
|
|
case "x": return \UV::O_WRONLY | \UV::O_CREAT | \UV::O_EXCL;
|
|
|
|
case "x+": return \UV::O_RDWR | \UV::O_CREAT | \UV::O_EXCL;
|
|
|
|
case "c": return \UV::O_WRONLY | \UV::O_CREAT;
|
|
|
|
case "c+": return \UV::O_RDWR | \UV::O_CREAT;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new \Error('Invalid file mode');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-13 01:02:41 +02:00
|
|
|
private function onOpenHandle($fh, array $openArr) {
|
|
|
|
list($mode) = $openArr;
|
2017-06-20 13:38:36 +02:00
|
|
|
|
2015-08-13 01:02:41 +02:00
|
|
|
if ($mode[0] === "w") {
|
2017-01-11 17:29:14 +01:00
|
|
|
\uv_fs_ftruncate($this->loop, $fh, $length = 0, function ($fh) use ($openArr) {
|
2015-08-13 01:02:41 +02:00
|
|
|
if ($fh) {
|
|
|
|
$this->finalizeHandle($fh, $size = 0, $openArr);
|
|
|
|
} else {
|
2017-06-20 07:31:58 +02:00
|
|
|
list(, $path, $deferred) = $openArr;
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred->fail(new FilesystemException(
|
2015-09-11 00:40:33 +02:00
|
|
|
"Failed truncating file $path"
|
2015-08-13 01:02:41 +02:00
|
|
|
));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
2017-06-20 07:31:58 +02:00
|
|
|
\uv_fs_fstat($this->loop, $fh, function ($fh, $stat) use ($openArr) {
|
2015-08-13 01:02:41 +02:00
|
|
|
if ($fh) {
|
|
|
|
StatCache::set($openArr[1], $stat);
|
|
|
|
$this->finalizeHandle($fh, $stat["size"], $openArr);
|
|
|
|
} else {
|
2017-06-20 07:31:58 +02:00
|
|
|
list(, $path, $deferred) = $openArr;
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred->fail(new FilesystemException(
|
2015-09-11 00:40:33 +02:00
|
|
|
"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;
|
2017-01-11 17:29:14 +01:00
|
|
|
$handle = new UvHandle($this->driver, $this->busy, $fh, $path, $mode, $size);
|
2016-07-21 01:33:03 +02:00
|
|
|
$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
|
|
|
$deferred = new Deferred;
|
2017-06-20 13:38:36 +02:00
|
|
|
$this->watchOperation($deferred->promise());
|
|
|
|
|
2017-06-20 07:31:58 +02:00
|
|
|
\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
|
|
|
}
|
2017-06-20 13:38:36 +02:00
|
|
|
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred->resolve($stat);
|
2015-07-11 03:59:39 +02:00
|
|
|
});
|
|
|
|
|
2017-06-20 07:06:12 +02: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;
|
2017-06-20 13:38:36 +02:00
|
|
|
|
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-06-20 13:38:36 +02:00
|
|
|
|
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"] & \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;
|
2017-06-20 13:38:36 +02:00
|
|
|
|
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"] & \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;
|
2017-06-20 13:38:36 +02:00
|
|
|
|
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"] & \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;
|
2017-06-20 13:38:36 +02:00
|
|
|
|
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-06-20 13:38:36 +02:00
|
|
|
|
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-06-20 13:38:36 +02:00
|
|
|
|
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-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
|
|
|
$deferred = new Deferred;
|
2017-06-20 13:38:36 +02:00
|
|
|
$this->watchOperation($deferred->promise());
|
|
|
|
|
2017-06-20 07:31:58 +02:00
|
|
|
\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;
|
|
|
|
}
|
2017-06-20 13:38:36 +02:00
|
|
|
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred->resolve($stat);
|
2015-07-11 03:59:39 +02:00
|
|
|
});
|
|
|
|
|
2017-06-20 07:06:12 +02: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
|
|
|
$deferred = new Deferred;
|
2017-06-20 13:38:36 +02:00
|
|
|
$this->watchOperation($deferred->promise());
|
|
|
|
|
2017-01-11 17:29:14 +01:00
|
|
|
\uv_fs_symlink($this->loop, $target, $link, \UV::S_IRWXU | \UV::S_IRUSR, function ($fh) use ($deferred) {
|
|
|
|
$deferred->resolve((bool) $fh);
|
2015-07-11 03:59:39 +02:00
|
|
|
});
|
|
|
|
|
2017-06-20 07:06:12 +02:00
|
|
|
return $deferred->promise();
|
2015-07-11 03:59:39 +02:00
|
|
|
}
|
2017-01-11 14:22:06 +01:00
|
|
|
|
2016-08-24 06:55:06 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-11-15 06:17:19 +01:00
|
|
|
public function link(string $target, string $link): Promise {
|
2016-08-24 06:55:06 +02:00
|
|
|
$deferred = new Deferred;
|
2017-06-20 13:38:36 +02:00
|
|
|
$this->watchOperation($deferred->promise());
|
|
|
|
|
2017-01-11 17:29:14 +01:00
|
|
|
\uv_fs_link($this->loop, $target, $link, \UV::S_IRWXU | \UV::S_IRUSR, function ($fh) use ($deferred) {
|
|
|
|
$deferred->resolve((bool) $fh);
|
2016-08-24 06:55:06 +02:00
|
|
|
});
|
2017-01-11 14:22:06 +01:00
|
|
|
|
2017-06-20 07:06:12 +02:00
|
|
|
return $deferred->promise();
|
2016-08-24 06:55:06 +02:00
|
|
|
}
|
2017-01-11 14:22:06 +01:00
|
|
|
|
2016-08-24 06:55:06 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2016-11-15 06:17:19 +01:00
|
|
|
public function readlink(string $path): Promise {
|
2016-08-24 06:55:06 +02:00
|
|
|
$deferred = new Deferred;
|
2017-06-20 13:38:36 +02:00
|
|
|
$this->watchOperation($deferred->promise());
|
|
|
|
|
2017-01-11 17:29:14 +01:00
|
|
|
\uv_fs_readlink($this->loop, $path, function ($fh) use ($deferred) {
|
|
|
|
$deferred->resolve((bool) $fh);
|
2016-08-24 06:55:06 +02:00
|
|
|
});
|
2017-01-11 14:22:06 +01:00
|
|
|
|
2017-06-20 07:06:12 +02:00
|
|
|
return $deferred->promise();
|
2016-08-24 06:55:06 +02:00
|
|
|
}
|
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
|
|
|
$deferred = new Deferred;
|
2017-06-20 13:38:36 +02:00
|
|
|
$this->watchOperation($deferred->promise());
|
|
|
|
|
2017-01-11 17:29:14 +01:00
|
|
|
\uv_fs_rename($this->loop, $from, $to, function ($fh) use ($deferred, $from) {
|
2015-09-11 00:40:33 +02:00
|
|
|
StatCache::clear($from);
|
2017-01-11 17:29:14 +01:00
|
|
|
$deferred->resolve((bool) $fh);
|
2015-07-11 03:59:39 +02:00
|
|
|
});
|
|
|
|
|
2017-06-20 07:06:12 +02: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
|
|
|
$deferred = new Deferred;
|
2017-06-20 13:38:36 +02:00
|
|
|
$this->watchOperation($deferred->promise());
|
|
|
|
|
2017-01-11 17:29:14 +01:00
|
|
|
\uv_fs_unlink($this->loop, $path, function ($fh) use ($deferred, $path) {
|
2015-09-11 00:40:33 +02:00
|
|
|
StatCache::clear($path);
|
2017-01-11 17:29:14 +01:00
|
|
|
$deferred->resolve((bool) $fh);
|
2015-07-11 03:59:39 +02:00
|
|
|
});
|
|
|
|
|
2017-06-20 07:06:12 +02: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
|
|
|
$deferred = new Deferred;
|
2017-06-20 13:38:36 +02:00
|
|
|
$this->watchOperation($deferred->promise());
|
2016-09-28 12:39:24 +02:00
|
|
|
|
|
|
|
if ($recursive) {
|
2017-06-20 07:31:58 +02:00
|
|
|
$path = str_replace("/", DIRECTORY_SEPARATOR, $path);
|
2017-06-20 13:38:36 +02:00
|
|
|
$arrayPath = explode(DIRECTORY_SEPARATOR, $path);
|
2016-09-28 12:39:24 +02:00
|
|
|
$tmpPath = "";
|
|
|
|
|
2017-01-11 17:29:14 +01:00
|
|
|
$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 17:29:14 +01:00
|
|
|
\uv_fs_mkdir($this->loop, $tmpPath, $mode, function ($fh) use ($deferred) {
|
2017-01-11 07:05:18 +01:00
|
|
|
$deferred->resolve((bool) $fh);
|
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
|
|
|
|
) {
|
|
|
|
if ($result) {
|
|
|
|
$callback();
|
|
|
|
} else {
|
|
|
|
\uv_fs_mkdir($this->loop, $tmpPath, $mode, $callback);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$callback();
|
|
|
|
} else {
|
2017-01-11 17:29:14 +01:00
|
|
|
\uv_fs_mkdir($this->loop, $path, $mode, function ($fh) use ($deferred) {
|
2017-01-11 07:05:18 +01:00
|
|
|
$deferred->resolve((bool) $fh);
|
2016-09-28 12:39:24 +02:00
|
|
|
});
|
|
|
|
}
|
2015-07-11 03:59:39 +02:00
|
|
|
|
2017-06-20 07:06:12 +02: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
|
|
|
$deferred = new Deferred;
|
2017-06-20 13:38:36 +02:00
|
|
|
$this->watchOperation($deferred->promise());
|
|
|
|
|
2017-01-11 17:29:14 +01:00
|
|
|
\uv_fs_rmdir($this->loop, $path, function ($fh) use ($deferred, $path) {
|
2015-09-11 00:40:33 +02:00
|
|
|
StatCache::clear($path);
|
2017-01-11 17:29:14 +01:00
|
|
|
$deferred->resolve((bool) $fh);
|
2015-07-11 03:59:39 +02:00
|
|
|
});
|
|
|
|
|
2017-06-20 07:06:12 +02: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
|
|
|
$deferred = new Deferred;
|
2017-06-20 13:38:36 +02:00
|
|
|
$this->watchOperation($deferred->promise());
|
|
|
|
|
2017-01-11 17:29:14 +01:00
|
|
|
uv_fs_readdir($this->loop, $path, 0, function ($fh, $data) use ($deferred, $path) {
|
2015-07-11 03:59:39 +02:00
|
|
|
if (empty($fh)) {
|
2017-06-20 13:38:36 +02:00
|
|
|
$deferred->fail(new FilesystemException("Failed reading contents from {$path}"));
|
2015-07-11 03:59:39 +02:00
|
|
|
} else {
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred->resolve($data);
|
2015-07-11 03:59:39 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-06-20 07:06:12 +02: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
|
|
|
$deferred = new Deferred;
|
2017-06-20 13:38:36 +02:00
|
|
|
$this->watchOperation($deferred->promise());
|
|
|
|
|
2017-01-11 17:29:14 +01:00
|
|
|
\uv_fs_chmod($this->loop, $path, $mode, function ($fh) use ($deferred) {
|
|
|
|
$deferred->resolve((bool) $fh);
|
2015-07-11 03:59:39 +02:00
|
|
|
});
|
|
|
|
|
2017-06-20 07:06:12 +02: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
|
|
|
$deferred = new Deferred;
|
2017-06-20 13:38:36 +02:00
|
|
|
$this->watchOperation($deferred->promise());
|
|
|
|
|
2017-01-11 17:29:14 +01:00
|
|
|
\uv_fs_chown($this->loop, $path, $uid, $gid, function ($fh) use ($deferred) {
|
|
|
|
$deferred->resolve((bool) $fh);
|
2015-07-11 03:59:39 +02:00
|
|
|
});
|
|
|
|
|
2017-06-20 07:06:12 +02: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 {
|
2015-07-18 20:53:46 +02:00
|
|
|
$atime = $mtime = time();
|
2017-06-20 13:38:36 +02:00
|
|
|
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred = new Deferred;
|
2017-06-20 13:38:36 +02:00
|
|
|
$this->watchOperation($deferred->promise());
|
|
|
|
|
2017-01-11 17:29:14 +01:00
|
|
|
\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
|
|
|
$deferred->resolve(true);
|
2015-07-18 20:53:46 +02:00
|
|
|
});
|
|
|
|
|
2017-06-20 07:06:12 +02: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 {
|
2017-06-20 13:38:36 +02:00
|
|
|
$promise = new Coroutine($this->doGet($path));
|
|
|
|
$this->watchOperation($promise);
|
|
|
|
|
|
|
|
return $promise;
|
2015-07-11 03:59:39 +02:00
|
|
|
}
|
|
|
|
|
2015-07-17 16:27:38 +02:00
|
|
|
private function doGet($path): \Generator {
|
2016-11-15 06:17:19 +01:00
|
|
|
$promise = $this->doFsOpen($path, $flags = \UV::O_RDONLY, $mode = 0);
|
|
|
|
if (!$fh = yield $promise) {
|
2017-06-20 13:38:36 +02:00
|
|
|
throw new FilesystemException("Failed opening file handle: {$path}");
|
2015-07-11 03:59:39 +02:00
|
|
|
}
|
|
|
|
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred = new Deferred;
|
2017-06-20 13:38:36 +02:00
|
|
|
|
|
|
|
$stat = yield $this->doFsStat($fh);
|
|
|
|
|
2015-07-11 03:59:39 +02:00
|
|
|
if (empty($stat)) {
|
2017-06-20 13:38:36 +02:00
|
|
|
$deferred->fail(new FilesystemException("stat operation failed on open file handle"));
|
2015-07-11 03:59:39 +02:00
|
|
|
} elseif (!$stat["isfile"]) {
|
2017-01-11 17:29:14 +01:00
|
|
|
\uv_fs_close($this->loop, $fh, function () use ($deferred) {
|
2017-06-20 13:38:36 +02:00
|
|
|
$deferred->fail(new FilesystemException("cannot buffer contents: path is not a file"));
|
2015-07-11 03:59:39 +02:00
|
|
|
});
|
|
|
|
} else {
|
2017-06-20 13:38:36 +02:00
|
|
|
$buffer = yield $this->doFsRead($fh, $offset = 0, $stat["size"]);
|
|
|
|
|
2017-06-20 07:31:58 +02:00
|
|
|
if ($buffer === false) {
|
2017-01-11 17:29:14 +01:00
|
|
|
\uv_fs_close($this->loop, $fh, function () use ($deferred) {
|
2017-06-20 13:38:36 +02:00
|
|
|
$deferred->fail(new FilesystemException("read operation failed on open file handle"));
|
2015-07-11 03:59:39 +02:00
|
|
|
});
|
|
|
|
} else {
|
2017-01-11 17:29:14 +01:00
|
|
|
\uv_fs_close($this->loop, $fh, function () use ($deferred, $buffer) {
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred->resolve($buffer);
|
2015-07-11 03:59:39 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-20 07:06:12 +02: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;
|
2017-06-20 13:38:36 +02:00
|
|
|
|
2017-01-11 17:29:14 +01:00
|
|
|
\uv_fs_open($this->loop, $path, $flags, $mode, function ($fh) use ($deferred, $path) {
|
2016-07-21 01:33:03 +02:00
|
|
|
$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;
|
2017-06-20 13:38:36 +02:00
|
|
|
|
2017-01-11 17:29:14 +01:00
|
|
|
\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;
|
2017-06-20 13:38:36 +02:00
|
|
|
|
2017-01-11 17:29:14 +01:00
|
|
|
\uv_fs_read($this->loop, $fh, $offset, $len, function ($fh, $nread, $buffer) use ($deferred) {
|
2016-07-21 01:33:03 +02:00
|
|
|
$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 {
|
2017-06-20 13:38:36 +02:00
|
|
|
$promise = new Coroutine($this->doPut($path, $contents));
|
|
|
|
$this->watchOperation($promise);
|
|
|
|
|
|
|
|
return $promise;
|
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;
|
2017-06-20 13:38:36 +02:00
|
|
|
|
2016-11-15 06:17:19 +01:00
|
|
|
$promise = $this->doFsOpen($path, $flags, $mode);
|
2017-06-20 13:38:36 +02:00
|
|
|
|
2016-11-15 06:17:19 +01:00
|
|
|
if (!$fh = yield $promise) {
|
2017-06-20 13:38:36 +02:00
|
|
|
throw new FilesystemException("Failed opening write file handle");
|
2015-07-11 03:59:39 +02:00
|
|
|
}
|
|
|
|
|
2016-07-21 01:33:03 +02:00
|
|
|
$deferred = new Deferred;
|
2015-07-11 03:59:39 +02:00
|
|
|
$len = strlen($contents);
|
2017-06-20 13:38:36 +02:00
|
|
|
|
2017-01-11 17:29:14 +01:00
|
|
|
\uv_fs_write($this->loop, $fh, $contents, $offset = 0, function ($fh, $result) use ($deferred, $len) {
|
2017-06-20 07:31:58 +02:00
|
|
|
\uv_fs_close($this->loop, $fh, function () use ($deferred, $result, $len) {
|
2015-07-11 03:59:39 +02:00
|
|
|
if ($result < 0) {
|
2017-06-20 13:38:36 +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
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-06-20 07:06:12 +02:00
|
|
|
return yield $deferred->promise();
|
2015-07-11 03:59:39 +02:00
|
|
|
}
|
2017-06-20 13:38:36 +02:00
|
|
|
|
|
|
|
private function watchOperation(Promise $promise) {
|
|
|
|
$this->pendingOperations++;
|
|
|
|
|
|
|
|
if ($this->pendingOperations === 1) {
|
|
|
|
$this->driver->reference($this->busy);
|
|
|
|
}
|
|
|
|
|
|
|
|
$promise->onResolve(function () {
|
|
|
|
$this->pendingOperations--;
|
|
|
|
|
|
|
|
if ($this->pendingOperations === 0) {
|
|
|
|
$this->driver->unreference($this->busy);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2015-07-11 03:59:39 +02:00
|
|
|
}
|