Skip to content

Commit

Permalink
Remove the fread/fwrite/fgets methods from Coroutine\System
Browse files Browse the repository at this point in the history
  • Loading branch information
matyhtf committed Jul 5, 2024
1 parent 312ee74 commit 862ddaf
Show file tree
Hide file tree
Showing 6 changed files with 1 addition and 309 deletions.
15 changes: 0 additions & 15 deletions ext-src/stubs/php_swoole_coroutine_system.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,5 @@ public static function wait(float $timeout = -1): array|false {}
public static function waitPid(int $pid, float $timeout = -1): array|false {}
public static function waitSignal(int $signo, float $timeout = -1): bool {}
public static function waitEvent(mixed $socket, int $events = SWOOLE_EVENT_READ, float $timeout = -1): int|false {}
/**
* @param resource $handle
* @deprecated
*/
public static function fread($handle, int $length = 0): string|false {}
/**
* @param resource $handle
* @deprecated
*/
public static function fwrite($handle, string $data, int $length = 0): int|false {}
/**
* @param resource $handle
* @deprecated
*/
public static function fgets($handle): string|false {}
}
}
17 changes: 1 addition & 16 deletions ext-src/stubs/php_swoole_coroutine_system_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: ca0f59f1be355a804c432a3ed7243d3306ff53e5 */
* Stub hash: 8c900b375f6550820f059936b75e7a80beb088f5 */

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_class_Swoole_Coroutine_System_gethostbyname, 0, 1, MAY_BE_FALSE|MAY_BE_STRING)
ZEND_ARG_TYPE_INFO(0, domain_name, IS_STRING, 0)
Expand Down Expand Up @@ -65,18 +65,3 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_class_Swoole_Coroutine_System_wa
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, events, IS_LONG, 0, "SWOOLE_EVENT_READ")
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, timeout, IS_DOUBLE, 0, "-1")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_class_Swoole_Coroutine_System_fread, 0, 1, MAY_BE_STRING|MAY_BE_FALSE)
ZEND_ARG_INFO(0, handle)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, length, IS_LONG, 0, "0")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_class_Swoole_Coroutine_System_fwrite, 0, 2, MAY_BE_LONG|MAY_BE_FALSE)
ZEND_ARG_INFO(0, handle)
ZEND_ARG_TYPE_INFO(0, data, IS_STRING, 0)
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, length, IS_LONG, 0, "0")
ZEND_END_ARG_INFO()

ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_class_Swoole_Coroutine_System_fgets, 0, 1, MAY_BE_STRING|MAY_BE_FALSE)
ZEND_ARG_INFO(0, handle)
ZEND_END_ARG_INFO()
4 changes: 0 additions & 4 deletions ext-src/swoole_coroutine.cc
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,6 @@ static const zend_function_entry swoole_coroutine_methods[] =
PHP_ME(swoole_coroutine_system, waitPid, arginfo_class_Swoole_Coroutine_System_waitPid, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_ME(swoole_coroutine_system, waitSignal, arginfo_class_Swoole_Coroutine_System_waitSignal, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_ME(swoole_coroutine_system, waitEvent, arginfo_class_Swoole_Coroutine_System_waitEvent, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
/* Deprecated file methods */
PHP_ME(swoole_coroutine_system, fread, arginfo_class_Swoole_Coroutine_System_fread, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC | ZEND_ACC_DEPRECATED)
PHP_ME(swoole_coroutine_system, fgets, arginfo_class_Swoole_Coroutine_System_fgets, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC | ZEND_ACC_DEPRECATED)
PHP_ME(swoole_coroutine_system, fwrite, arginfo_class_Swoole_Coroutine_System_fwrite, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC | ZEND_ACC_DEPRECATED)
PHP_FE_END
};
// clang-format on
Expand Down
212 changes: 0 additions & 212 deletions ext-src/swoole_coroutine_system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ static const zend_function_entry swoole_coroutine_system_methods[] =
PHP_ME(swoole_coroutine_system, waitPid, arginfo_class_Swoole_Coroutine_System_waitPid, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_ME(swoole_coroutine_system, waitSignal, arginfo_class_Swoole_Coroutine_System_waitSignal, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
PHP_ME(swoole_coroutine_system, waitEvent, arginfo_class_Swoole_Coroutine_System_waitEvent, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
/* Deprecated file methods */
PHP_ME(swoole_coroutine_system, fread, arginfo_class_Swoole_Coroutine_System_fread, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC | ZEND_ACC_DEPRECATED)
PHP_ME(swoole_coroutine_system, fwrite, arginfo_class_Swoole_Coroutine_System_fwrite, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC | ZEND_ACC_DEPRECATED)
PHP_ME(swoole_coroutine_system, fgets, arginfo_class_Swoole_Coroutine_System_fgets, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC | ZEND_ACC_DEPRECATED)
PHP_FE_END
};

Expand All @@ -76,214 +72,6 @@ PHP_METHOD(swoole_coroutine_system, sleep) {
RETURN_BOOL(System::sleep(seconds) == 0);
}

static void co_socket_read(int fd, zend_long length, INTERNAL_FUNCTION_PARAMETERS) {
php_swoole_check_reactor();
Socket _socket(fd, SW_SOCK_RAW);

zend_string *buf = zend_string_alloc(length + 1, 0);
size_t nbytes = length <= 0 ? SW_BUFFER_SIZE_STD : length;
ssize_t n = _socket.read(ZSTR_VAL(buf), nbytes);
if (n < 0) {
ZVAL_FALSE(return_value);
zend_string_free(buf);
} else if (n == 0) {
ZVAL_EMPTY_STRING(return_value);
zend_string_free(buf);
} else {
ZSTR_VAL(buf)[n] = 0;
ZSTR_LEN(buf) = n;
ZVAL_STR(return_value, buf);
}
_socket.move_fd();
}

static void co_socket_write(int fd, char *str, size_t l_str, INTERNAL_FUNCTION_PARAMETERS) {
php_swoole_check_reactor();
Socket _socket(fd, SW_SOCK_RAW);

ssize_t n = _socket.write(str, l_str);
if (n < 0) {
swoole_set_last_error(errno);
ZVAL_FALSE(return_value);
} else {
ZVAL_LONG(return_value, n);
}
_socket.move_fd();
}

PHP_METHOD(swoole_coroutine_system, fread) {
Coroutine::get_current_safe();

zval *handle;
zend_long length = 0;

ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_RESOURCE(handle)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(length)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);

int async;
int fd = php_swoole_convert_to_fd_ex(handle, &async);
if (fd < 0) {
RETURN_FALSE;
}

if (async) {
co_socket_read(fd, length, INTERNAL_FUNCTION_PARAM_PASSTHRU);
return;
}

if (length <= 0) {
struct stat file_stat;
if (swoole_coroutine_fstat(fd, &file_stat) < 0) {
swoole_set_last_error(errno);
RETURN_FALSE;
}
off_t _seek = swoole_coroutine_lseek(fd, 0, SEEK_CUR);
if (_seek < 0) {
swoole_set_last_error(errno);
RETURN_FALSE;
}
if (_seek >= file_stat.st_size) {
length = SW_BUFFER_SIZE_STD;
} else {
length = file_stat.st_size - _seek;
}
}

zend_string *buf = zend_string_alloc(length, 0);
ssize_t ret = -1;
swoole_trace("fd=%d, length=" ZEND_LONG_FMT, fd, length);
php_swoole_check_reactor();
bool async_success = swoole::coroutine::async([&]() {
while (1) {
ret = read(fd, buf->val, length);
if (ret < 0 && errno == EINTR) {
continue;
}
break;
}
});

if (async_success && ret >= 0) {
buf->len = ret;
buf->val[buf->len] = 0;
RETURN_STR(buf);
} else {
zend_string_release(buf);
RETURN_FALSE;
}
}

PHP_METHOD(swoole_coroutine_system, fgets) {
Coroutine::get_current_safe();

zval *handle;
php_stream *stream;

ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_RESOURCE(handle)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);

int async;
int fd = php_swoole_convert_to_fd_ex(handle, &async);
if (fd < 0) {
RETURN_FALSE;
}

if (async == 1) {
php_swoole_fatal_error(E_WARNING, "only support file resources");
RETURN_FALSE;
}

php_stream_from_res(stream, Z_RES_P(handle));

FILE *file;
if (stream->stdiocast) {
file = stream->stdiocast;
} else {
if (php_stream_cast(stream, PHP_STREAM_AS_STDIO, (void **) &file, 1) != SUCCESS || file == nullptr) {
RETURN_FALSE;
}
}

if (stream->readbuf == nullptr) {
stream->readbuflen = stream->chunk_size;
stream->readbuf = (uchar *) emalloc(stream->chunk_size);
}

if (!stream->readbuf) {
RETURN_FALSE;
}

int ret = 0;
swoole_trace("fd=%d, length=%ld", fd, stream->readbuflen);
php_swoole_check_reactor();
bool async_success = swoole::coroutine::async([&]() {
char *data = fgets((char *) stream->readbuf, stream->readbuflen, file);
if (data == nullptr) {
ret = -1;
stream->eof = 1;
}
});

if (async_success && ret != -1) {
ZVAL_STRING(return_value, (char *) stream->readbuf);
} else {
ZVAL_FALSE(return_value);
}
}

PHP_METHOD(swoole_coroutine_system, fwrite) {
Coroutine::get_current_safe();

zval *handle;
char *str;
size_t l_str;
zend_long length = 0;

ZEND_PARSE_PARAMETERS_START(2, 3)
Z_PARAM_RESOURCE(handle)
Z_PARAM_STRING(str, l_str)
Z_PARAM_OPTIONAL
Z_PARAM_LONG(length)
ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);

int async;
int fd = php_swoole_convert_to_fd_ex(handle, &async);
if (fd < 0) {
RETURN_FALSE;
}
if (length <= 0 || (size_t) length > l_str) {
length = l_str;
}
if (async) {
co_socket_write(fd, str, length, INTERNAL_FUNCTION_PARAM_PASSTHRU);
return;
}

zend::CharPtr buf(str, length);
ssize_t ret = -1;
swoole_trace("fd=%d, length=" ZEND_LONG_FMT, fd, length);
php_swoole_check_reactor();
bool async_success = swoole::coroutine::async([&]() {
while (1) {
ret = write(fd, buf.get(), length);
if (ret < 0 && errno == EINTR) {
continue;
}
break;
}
});

if (async_success && ret >= 0) {
ZVAL_LONG(return_value, ret);
} else {
ZVAL_FALSE(return_value);
}
}

PHP_METHOD(swoole_coroutine_system, readFile) {
char *filename;
size_t l_filename;
Expand Down
27 changes: 0 additions & 27 deletions tests/swoole_coroutine_system/fread.phpt

This file was deleted.

35 changes: 0 additions & 35 deletions tests/swoole_coroutine_system/fwrite.phpt

This file was deleted.

0 comments on commit 862ddaf

Please sign in to comment.