Skip to content

Commit

Permalink
various updates, bug fix for Linux file operation not writing correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
TheTechsTech committed Mar 11, 2020
1 parent c872031 commit 6cb195a
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 34 deletions.
7 changes: 5 additions & 2 deletions Coroutine/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ function file_fstat($fd, $info = null)
*
* @return resource|bool
*/
function file_open(string $path, string $flag, int $mode = 0)
function file_open(string $path, string $flag, int $mode = \UV::S_IRWXU)
{
return FileSystem::open($path, $flag, $mode);
}
Expand Down Expand Up @@ -732,7 +732,10 @@ function file_get(string $filename)
if (\file_meta($fd, 'wrapper_type') === 'http') {
$max = -1;
} else {
$max = yield \file_stat($filename, 'size');
if (\IS_LINUX)
$max = yield \file_fstat($fd, 'size');
else
$max = yield \file_stat($filename, 'size');
}

$contents = yield \file_read($fd, 0, (empty($max) ? 8192 * 2 : $max));
Expand Down
13 changes: 6 additions & 7 deletions Coroutine/Coroutine.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,7 @@ public function addProcess(
bool $display = false,
$channel = null,
$channelTask = null
): LauncherInterface
{
): LauncherInterface {
$launcher = $this->parallel->add($callable, $timeout, $channel, $channelTask);
return $display ? $launcher->displayOn() : $launcher;
}
Expand Down Expand Up @@ -790,12 +789,12 @@ public function removeSignal($signal, $listener)
}

/**
* Setup Signal listener.
*/
public function initSignals()
{
* Setup Signal listener.
*/
public function initSignals()
{
$this->isUvSignal = \function_exists('uv_loop_new');
if (empty($this->signaler) && ($this->isPcntl() || $this->isUvActive() || $this->isUvSignal)) {
if (empty($this->signaler) && ($this->isPcntl() || $this->isUvActive() || $this->isUvSignal)) {
$this->signaler = new Signaler($this);

if ($this->isPcntl()) {
Expand Down
7 changes: 2 additions & 5 deletions Coroutine/FileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ function ($out_fd, $result) use ($task, $coroutine) {
* - "`x+`" `Read/Write`: Creates a new file. Returns `FALSE` and an error if file already exists.
* - "`c+`" Open the file for reading and writing; otherwise it has the same behavior as "`c`".
*/
public static function open(string $path, string $flag, int $mode = 0)
public static function open(string $path, string $flag, int $mode = \UV::S_IRWXU)
{
if (isset(self::$fileFlags[$flag])) {
if (self::useUvFs() && (\strpos($path, '://') === false)) {
Expand Down Expand Up @@ -954,15 +954,12 @@ function ($fd, $status, $data) use ($task, $coroutine) {
return self::readFile($fd, $offset, $length);
}

/**
* @codeCoverageIgnore
*/
protected static function writeFile($fd, string $buffer)
{
yield;
$fwrite = 0;
for ($written = 0; $written < \strlen($buffer); $written += $fwrite) {
yield Kernel::writeWait($fd);
yield Kernel::writeWait($fd, true);
$fwrite = \fwrite($fd, \substr($buffer, $written));
// see https://www.php.net/manual/en/function.fwrite.php#96951
if (($fwrite === false) || ($fwrite == 0)) {
Expand Down
13 changes: 7 additions & 6 deletions examples/uv/fs.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php
uv_fs_open(uv_default_loop(),__FILE__, UV::O_RDONLY, 0, function($r){
uv_fs_read(uv_default_loop(),$r,function($stream, $data) {
if (is_long($data)) {
if ($nread < 0) {
throw new Exception("read error");
}
uv_fs_open(uv_default_loop(), __FILE__, \UV::O_RDONLY, 0, function ($r) {
uv_fs_fstat(uv_default_loop(), $r, function ($r, $stat) {
uv_fs_read(uv_default_loop(), $r, 0, $stat['size'], function ($stream, $nread, $data) {
if ($nread <= 0) {
if ($nread < 0) {
throw new \Exception("read error");
}

uv_fs_close(uv_default_loop(), $stream, function () {
});
Expand Down
2 changes: 0 additions & 2 deletions examples/uv/poll_sendfile.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

use Async\Coroutine\UV;

$socket = stream_socket_server("tcp://0.0.0.0:9999", $errno, $errstr);

$poll = uv_poll_init(uv_default_loop(), $socket);
Expand Down
2 changes: 0 additions & 2 deletions examples/uv/process_kill.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

use Async\Coroutine\UV;

$in = uv_pipe_init(uv_default_loop(), ('/' == \DIRECTORY_SEPARATOR));
$out = uv_pipe_init(uv_default_loop(), ('/' == \DIRECTORY_SEPARATOR));

Expand Down
2 changes: 0 additions & 2 deletions examples/uv/signal.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

use Async\Coroutine\UV;

$in = uv_pipe_init(uv_default_loop(), ('/' == \DIRECTORY_SEPARATOR));
$out = uv_pipe_init(uv_default_loop(), ('/' == \DIRECTORY_SEPARATOR));

Expand Down
16 changes: 8 additions & 8 deletions tests/FileSystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ protected function setUp(): void
{
\coroutine_clear();
if (!defined('FIXTURE_PATH'))
\define("FIXTURE_PATH", dirname(__FILE__) . \DS ."libuv". \DS . "fixtures" . \DS . "hello.data");
\define("FIXTURE_PATH", dirname(__FILE__) . \DS . "libuv" . \DS . "fixtures" . \DS . "hello.data");
if (!defined('FIXTURES'))
\define("FIXTURES", dirname(__FILE__) . \DS ."libuv". \DS . "fixtures" . \DS);
\define("FIXTURES", dirname(__FILE__) . \DS . "libuv" . \DS . "fixtures" . \DS);
if (!defined('DIRECTORY_PATH'))
\define("DIRECTORY_PATH", dirname(__FILE__) . \DS ."libuv". \DS . "fixtures" . \DS . "example_directory");
\define("DIRECTORY_PATH", dirname(__FILE__) . \DS . "libuv" . \DS . "fixtures" . \DS . "example_directory");
@rmdir(DIRECTORY_PATH);
}

Expand Down Expand Up @@ -85,10 +85,10 @@ public function testOpenReadOffsetFstat()
public function taskWrite()
{
yield \away($this->counterTask());
$fd = yield \file_open("./tmp", 'a', \UV::S_IRWXU | \UV::S_IRUSR);
$fd = yield \file_open("./tmp", 'a');
$this->assertEquals('resource', \is_type($fd));

$data = yield \file_write($fd, "hello", 0);
$data = yield \file_write($fd, "hello");
$this->assertEquals('int', \is_type($data));

$this->assertEquals(5, $data);
Expand Down Expand Up @@ -135,11 +135,11 @@ public function testWrite()

public function taskFilePut()
{
$contents1 = "rename test";
$new = FIXTURES . "rename1.txt";
$contents1 = "put test";
$new = FIXTURES . "put.txt";

$count = yield \file_put($new, $contents1);
$this->assertEquals(11, $count);
$this->assertEquals(8, $count);

$contents2 = yield \file_get($new);

Expand Down

0 comments on commit 6cb195a

Please sign in to comment.