Skip to content

[0.2.x] Improve file read/write tests #115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: 0.2.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Eio/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class Adapter implements AdapterInterface
public function __construct()
{
$this->loop = Loop::get();
$this->poll = new Poll($this->loop);
$this->poll = new Poll();
}

public function detect(string $path): PromiseInterface
Expand Down
9 changes: 7 additions & 2 deletions src/Eio/Directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,15 @@ public function stat(): PromiseInterface
public function ls(): PromiseInterface
{
$this->activate();
return new Promise(function (callable $resolve): void {
\eio_readdir($this->path . $this->name . DIRECTORY_SEPARATOR, \EIO_READDIR_STAT_ORDER | \EIO_READDIR_DIRS_FIRST, \EIO_PRI_DEFAULT, function ($_, $contents) use ($resolve): void {
return new Promise(function (callable $resolve, callable $reject): void {
\eio_readdir($this->path . $this->name . DIRECTORY_SEPARATOR, \EIO_READDIR_STAT_ORDER | \EIO_READDIR_DIRS_FIRST, \EIO_PRI_DEFAULT, function ($_, $contents, $resource) use ($resolve, $reject): void {
$this->deactivate();
$list = [];
if ($contents === -1) {
$reject(new \RuntimeException('Error reading from directory "' . $this->path . $this->name . DIRECTORY_SEPARATOR . '": ' . \eio_get_last_error($resource)));
return;
}

foreach ($contents['dents'] as $node) {
$fullPath = $this->path . $this->name . DIRECTORY_SEPARATOR . $node['name'];
switch ($node['type'] ?? null) {
Expand Down
63 changes: 36 additions & 27 deletions src/Eio/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

final class File implements FileInterface
{
private const READ_CHUNK_FIZE = 65536;
// private const READ_CHUNK_FIZE = 1;

use StatTrait;

private PollInterface $poll;
Expand Down Expand Up @@ -37,22 +40,37 @@ public function getContents(int $offset = 0 , ?int $maxlen = null): PromiseInter
0,
)->then(
function ($fileDescriptor) use ($offset, $maxlen): PromiseInterface {
if ($maxlen === null) {
$sizePromise = $this->statFileDescriptor($fileDescriptor)->then(static function ($stat): int {
return (int)$stat['size'];
});
} else {
$sizePromise = resolve($maxlen);
}
return $sizePromise->then(function ($length) use ($fileDescriptor, $offset): PromiseInterface {
return new Promise (function (callable $resolve) use ($fileDescriptor, $offset, $length): void {
\eio_read($fileDescriptor, $length, $offset, \EIO_PRI_DEFAULT, function ($fileDescriptor, string $buffer) use ($resolve): void {
$resolve($this->closeOpenFile($fileDescriptor)->then(function () use ($buffer): string {
return $buffer;
}));
}, $fileDescriptor);
$buffer = '';
$bufferLength = 0;
$read = function (bool $finalAttempt, int $offset) use ($fileDescriptor, $maxlen, &$read, &$buffer, &$bufferLength): PromiseInterface {
return new Promise (function (callable $resolve) use ($fileDescriptor, $offset, $maxlen, $finalAttempt, &$read, &$buffer, &$bufferLength): void {
\eio_read($fileDescriptor, $maxlen ?? self::READ_CHUNK_FIZE, $offset, \PHP_INT_MAX, function ($fileDescriptor, string $contents) use ($resolve, $maxlen, $finalAttempt, &$read, &$buffer, &$bufferLength): void {
$contentLength = strlen($contents);
$buffer .= $contents;
$bufferLength += $contentLength;

if (
($maxlen === null && $finalAttempt) ||
($maxlen !== null && $bufferLength >= $maxlen)
) {
if ($maxlen !== null && $bufferLength > $maxlen) {
$buffer = substr($buffer, 0, $maxlen);
}

$resolve($this->closeOpenFile($fileDescriptor)->then(function () use ($buffer): string {
$this->deactivate();
return $buffer;
}));
} else if ($maxlen === null && !$finalAttempt && $contentLength === 0) {
$resolve($read(true, $bufferLength));
} else {
$resolve($read(false, $bufferLength));
}
});
});
});
};

return $read(false, $offset);
}
);
}
Expand All @@ -61,9 +79,9 @@ public function putContents(string $contents, int $flags = 0)
{
$this->activate();
return $this->openFile(
$this->path . DIRECTORY_SEPARATOR . $this->name,
(($flags & \FILE_APPEND) == \FILE_APPEND) ? \EIO_O_RDWR | \EIO_O_APPEND : \EIO_O_RDWR | \EIO_O_CREAT,
0644
$this->path . DIRECTORY_SEPARATOR . $this->name,
(($flags & \FILE_APPEND) == \FILE_APPEND) ? \EIO_O_RDWR | \EIO_O_APPEND : \EIO_O_RDWR | \EIO_O_CREAT,
0644
)->then(
function ($fileDescriptor) use ($contents, $flags): PromiseInterface {
return new Promise (function (callable $resolve) use ($contents, $fileDescriptor): void {
Expand All @@ -77,15 +95,6 @@ function ($fileDescriptor) use ($contents, $flags): PromiseInterface {
);
}

private function statFileDescriptor($fileDescriptor): PromiseInterface
{
return new Promise(function (callable $resolve, callable $reject) use ($fileDescriptor) {
\eio_fstat($fileDescriptor, \EIO_PRI_DEFAULT, function ($_, $stat) use ($resolve): void {
$resolve($stat);
}, $fileDescriptor);
});
}

private function openFile(string $path, int $flags, int $mode): PromiseInterface
{
return new Promise(function (callable $resolve, callable $reject) use ($path, $flags, $mode): void {
Expand Down
13 changes: 7 additions & 6 deletions src/Eio/Poll.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@

namespace React\Filesystem\Eio;

use React\EventLoop\LoopInterface;
use React\EventLoop\Loop;
use React\Filesystem\PollInterface;

/**
* @internal
*/
final class Poll implements PollInterface
{
private LoopInterface $loop;
private $fd;
private \Closure $handleEvent;
private int $workInProgress = 0;

public function __construct(LoopInterface $loop)
public function __construct()
{
$this->fd = EventStream::get();
$this->loop = $loop;
$this->handleEvent = function () {
$this->handleEvent();
};
Expand All @@ -24,7 +25,7 @@ public function __construct(LoopInterface $loop)
public function activate(): void
{
if ($this->workInProgress++ === 0) {
$this->loop->addReadStream($this->fd, $this->handleEvent);
Loop::addReadStream($this->fd, $this->handleEvent);
}
}

Expand All @@ -38,7 +39,7 @@ private function handleEvent()
public function deactivate(): void
{
if (--$this->workInProgress <= 0) {
$this->loop->removeReadStream($this->fd);
Loop::removeReadStream($this->fd);
}
}
}
2 changes: 1 addition & 1 deletion src/Fallback/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function stat(): PromiseInterface
public function getContents(int $offset = 0 , ?int $maxlen = null): PromiseInterface
{
$path = $this->path . $this->name;
return resolve(file_get_contents($path, false, null, $offset, $maxlen ?? (int)stat($path)['size']));
return resolve(file_get_contents($path, false, null, $offset, $maxlen));
}

public function putContents(string $contents, int $flags = 0): PromiseInterface
Expand Down
105 changes: 85 additions & 20 deletions src/Uv/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace React\Filesystem\Uv;

use React\EventLoop\ExtUvLoop;
use React\EventLoop\Loop;
use React\Filesystem\Node\FileInterface;
use React\Filesystem\PollInterface;
use React\Promise\Promise;
Expand All @@ -11,6 +12,9 @@

final class File implements FileInterface
{
// private const READ_CHUNK_FIZE = 65536;
private const READ_CHUNK_FIZE = 1;

use StatTrait;

private ExtUvLoop $loop;
Expand All @@ -36,41 +40,102 @@ public function stat(): PromiseInterface
public function getContents(int $offset = 0 , ?int $maxlen = null): PromiseInterface
{
$this->activate();
return new Promise(function (callable $resolve) use ($offset, $maxlen): void {
uv_fs_open($this->uvLoop, $this->path . DIRECTORY_SEPARATOR . $this->name, UV::O_RDONLY, 0, function ($fileDescriptor) use ($resolve, $offset, $maxlen): void {
uv_fs_fstat($this->uvLoop, $fileDescriptor, function ($fileDescriptor, array $stat) use ($resolve, $offset, $maxlen): void {
uv_fs_read($this->uvLoop, $fileDescriptor, $offset, $maxlen ?? (int)$stat['size'], function ($fileDescriptor, string $buffer) use ($resolve): void {
$resolve($buffer);
uv_fs_close($this->uvLoop, $fileDescriptor, function () {
$this->deactivate();
return $this->openFile(
$this->path . DIRECTORY_SEPARATOR . $this->name,
UV::O_RDONLY,
0,
)->then(
function ($fileDescriptor) use ($offset, $maxlen): PromiseInterface {
$buffer = '';
$bufferLength = 0;
$read = function (bool $finalAttempt, int $offset) use ($fileDescriptor, $maxlen, &$read, &$buffer, &$bufferLength): PromiseInterface {
return new Promise (function (callable $resolve) use ($fileDescriptor, $offset, $maxlen, $finalAttempt, &$read, &$buffer, &$bufferLength): void {
\uv_fs_read($this->uvLoop, $fileDescriptor, $offset, $maxlen ?? self::READ_CHUNK_FIZE, function ($fileDescriptor, string $contents) use ($resolve, $maxlen, $finalAttempt, &$read, &$buffer, &$bufferLength): void {
$contentLength = strlen($contents);
$buffer .= $contents;
$bufferLength += $contentLength;

if (
($maxlen === null && $finalAttempt) ||
($maxlen !== null && $bufferLength >= $maxlen)
) {
if ($maxlen !== null && $bufferLength > $maxlen) {
$buffer = substr($buffer, 0, $maxlen);
}

$resolve($this->closeOpenFile($fileDescriptor)->then(function () use ($buffer): string {
$this->deactivate();

return $buffer;
}));
} else if ($maxlen === null && !$finalAttempt && $contentLength === 0) {
$resolve($read(true, $bufferLength));
} else {
$resolve($read(false, $bufferLength));
}
});
});
});
});
});
};

return $read(false, $offset);
}
);
}

public function putContents(string $contents, int $flags = 0)
{
$this->activate();
return new Promise(function (callable $resolve) use ($contents, $flags): void {
uv_fs_open(
$this->uvLoop,
$this->path . DIRECTORY_SEPARATOR . $this->name,
(($flags & \FILE_APPEND) == \FILE_APPEND) ? UV::O_RDWR | UV::O_CREAT | UV::O_APPEND : UV::O_RDWR | UV::O_CREAT,
0644,
function ($fileDescriptor) use ($resolve, $contents, $flags): void {
return $this->openFile(
$this->path . DIRECTORY_SEPARATOR . $this->name,
(($flags & \FILE_APPEND) == \FILE_APPEND) ? UV::O_RDWR | UV::O_CREAT | UV::O_APPEND : UV::O_RDWR | UV::O_CREAT,
0644,
)->then(
function ($fileDescriptor) use ($contents): PromiseInterface {
return new Promise (function (callable $resolve) use ($contents, $fileDescriptor): void {
uv_fs_write($this->uvLoop, $fileDescriptor, $contents, 0, function ($fileDescriptor, int $bytesWritten) use ($resolve): void {
$resolve($bytesWritten);
uv_fs_close($this->uvLoop, $fileDescriptor, function () {
$resolve($this->closeOpenFile($fileDescriptor)->then(function () use ($bytesWritten): int {
$this->deactivate();
});
return $bytesWritten;
}));
});
}
);
});
}

private function openFile(string $path, int $flags, int $mode): PromiseInterface
{
$this->activate();
return new Promise(function (callable $resolve) use ($path, $flags, $mode): void {
uv_fs_open(
$this->uvLoop,
$this->path . DIRECTORY_SEPARATOR . $this->name,
$flags,
$mode,
function ($fileDescriptor) use ($resolve): void {
$this->deactivate();
$resolve($fileDescriptor);
}
);
});
}

private function closeOpenFile($fileDescriptor): PromiseInterface
{
$this->activate();
return new Promise(function (callable $resolve) use ($fileDescriptor) {
try {
uv_fs_close($this->uvLoop, $fileDescriptor, function () use ($resolve) {
$this->deactivate();
$resolve();
});
} catch (\Throwable $error) {
$this->deactivate();
throw $error;
}
});
}

public function unlink(): PromiseInterface
{
$this->activate();
Expand Down
2 changes: 1 addition & 1 deletion tests/AbstractFilesystemTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ final public function provideFilesystems(): iterable
yield 'uv' => [new Uv\Adapter()];
}

yield 'factory' => [Factory::create()];
// yield 'factory' => [Factory::create()];
}
}
1 change: 0 additions & 1 deletion tests/DirectoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace React\Tests\Filesystem;

use React\EventLoop\LoopInterface;
use React\Filesystem\AdapterInterface;
use React\Filesystem\Node\DirectoryInterface;
use React\Filesystem\Node\FileInterface;
Expand Down
Loading