Skip to content

Commit 605a20a

Browse files
committed
merging offset changes from PR chobie#60
2 parents 99791cf + a25595f commit 605a20a

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2180,7 +2180,7 @@ uv_run();
21802180

21812181

21822182

2183-
### void uv_fs_read(resource $loop, zval $fd, long $length, callable $callback)
2183+
### void uv_fs_read(resource $loop, zval $fd, long $offset, long $length, callable $callback)
21842184

21852185
##### *Description*
21862186

@@ -2192,6 +2192,10 @@ async read.
21922192

21932193
*zval $fd*: this expects long $fd, resource $php_stream or resource $php_socket.
21942194

2195+
*long $offset*: the offset position in the file at which reading should commence.
2196+
2197+
*long $length*: the length in bytes that should be read starting at position *$offset*.
2198+
21952199
*resource $callback*: this callback parameter expects (zval $fd, long $nread, string $buffer).
21962200

21972201
##### *Return Value*

php_uv.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,14 +1015,15 @@ static void php_uv_fs_common(uv_fs_type fs_type, INTERNAL_FUNCTION_PARAMETERS)
10151015
zval *zstream = NULL;
10161016
unsigned long fd;
10171017
unsigned long length;
1018-
1019-
PHP_UV_FS_PARSE_PARAMETERS("zzlf", &zloop, &zstream, &length, &fci, &fcc);
1018+
unsigned long offset;
1019+
1020+
PHP_UV_FS_PARSE_PARAMETERS("zzllf", &zloop, &zstream, &offset, &length, &fci, &fcc);
10201021
memset(uv_fs_read_buf, 0, length);
10211022
uv_fs_read_buf_t = uv_buf_init(uv_fs_read_buf, length);
10221023

10231024
PHP_UV_FS_SETUP()
10241025
PHP_UV_ZVAL_TO_FD(fd, zstream);
1025-
PHP_UV_FS_ASYNC(loop, read, fd, &uv_fs_read_buf_t, 1, -1);
1026+
PHP_UV_FS_ASYNC(loop, read, fd, &uv_fs_read_buf_t, 1, offset);
10261027
break;
10271028
}
10281029
case UV_FS_SENDFILE:
@@ -3095,6 +3096,7 @@ ZEND_END_ARG_INFO()
30953096
ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_fs_read, 0, 0, 3)
30963097
ZEND_ARG_INFO(0, loop)
30973098
ZEND_ARG_INFO(0, fd)
3099+
ZEND_ARG_INFO(0, offset)
30983100
ZEND_ARG_INFO(0, size)
30993101
ZEND_ARG_INFO(0, callback)
31003102
ZEND_END_ARG_INFO()
@@ -5839,7 +5841,7 @@ PHP_FUNCTION(uv_fs_open)
58395841
/* }}} */
58405842

58415843

5842-
/* {{{ proto void uv_fs_read(resoruce $loop, zval $fd, callable $callback)
5844+
/* {{{ proto void uv_fs_read(resoruce $loop, zval $fd, long $offset, long $length, callable $callback)
58435845
*/
58445846
PHP_FUNCTION(uv_fs_read)
58455847
{

tests/300-fs.phpt

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,23 @@ Check for fs read and close
55
define("FIXTURE_PATH", dirname(__FILE__) . "/fixtures/hello.data");
66

77
uv_fs_open(uv_default_loop(),FIXTURE_PATH, UV::O_RDONLY, 0, function($r){
8-
uv_fs_read(uv_default_loop(),$r,32,function($stream, $nread, $data) {
8+
uv_fs_read(uv_default_loop(),$r, $offset=0, $len=32,function($stream, $nread, $data) {
9+
if ($nread <= 0) {
10+
if ($nread < 0) {
11+
throw new Exception("read error");
12+
}
13+
14+
uv_fs_close(uv_default_loop(), $stream, function(){
15+
});
16+
} else {
17+
echo $data ;
18+
}
19+
});
20+
});
21+
22+
// test offset
23+
uv_fs_open(uv_default_loop(),FIXTURE_PATH, UV::O_RDONLY, 0, function($r){
24+
uv_fs_read(uv_default_loop(),$r, $offset=1, $len=32,function($stream, $nread, $data) {
925
if ($nread <= 0) {
1026
if ($nread < 0) {
1127
throw new Exception("read error");
@@ -19,6 +35,8 @@ uv_fs_open(uv_default_loop(),FIXTURE_PATH, UV::O_RDONLY, 0, function($r){
1935
});
2036
});
2137

38+
2239
uv_run();
2340
--EXPECT--
24-
Hello
41+
Hello
42+
ello

tests/300-fs_close.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Check for fs read and close
55
define("FIXTURE_PATH", dirname(__FILE__) . "/fixtures/hello.data");
66

77
uv_fs_open(uv_default_loop(),FIXTURE_PATH, UV::O_RDONLY, 0, function($r){
8-
uv_fs_read(uv_default_loop(),$r,32,function($stream, $nread, $data) {
8+
uv_fs_read(uv_default_loop(),$r,0, 32,function($stream, $nread, $data) {
99
uv_fs_close(uv_default_loop(), 42, function($result) {
1010
if($result != 42) {
1111
echo "OK";

0 commit comments

Comments
 (0)