Skip to content

Commit a25595f

Browse files
committed
Add $offset param to uv_fs_read() to allow seeking on open handles
This change modifies the uv_fs_read() function signature slightly by adding a new $offset parameter. This allows the same file handle to be reused for multiple reads. Previously the only way to access a section of a file that had already been read was to open a new handle. Old: uv_fs_read(resoruce $loop, zval $fd, long $length, callable $callback) New: uv_fs_read(resoruce $loop, zval $fd, long $offest, long $length, callable $callback) This change represents a minor BC break for existing code using uv_fs_read().
1 parent 5817bfe commit a25595f

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
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
@@ -950,12 +950,13 @@ static void php_uv_fs_common(uv_fs_type fs_type, INTERNAL_FUNCTION_PARAMETERS)
950950
zval *zstream = NULL;
951951
unsigned long fd;
952952
unsigned long length;
953-
954-
PHP_UV_FS_PARSE_PARAMETERS("zzlf", &zloop, &zstream, &length, &fci, &fcc);
953+
unsigned long offset;
954+
955+
PHP_UV_FS_PARSE_PARAMETERS("zzllf", &zloop, &zstream, &offset, &length, &fci, &fcc);
955956
memset(uv_fs_read_buf, 0, length);
956957
PHP_UV_FS_SETUP()
957958
PHP_UV_ZVAL_TO_FD(fd, zstream);
958-
PHP_UV_FS_ASYNC(loop, read, fd, uv_fs_read_buf, length, -1);
959+
PHP_UV_FS_ASYNC(loop, read, fd, uv_fs_read_buf, length, offset);
959960
break;
960961
}
961962
case UV_FS_SENDFILE:
@@ -3023,6 +3024,7 @@ ZEND_END_ARG_INFO()
30233024
ZEND_BEGIN_ARG_INFO_EX(arginfo_uv_fs_read, 0, 0, 3)
30243025
ZEND_ARG_INFO(0, loop)
30253026
ZEND_ARG_INFO(0, fd)
3027+
ZEND_ARG_INFO(0, offset)
30263028
ZEND_ARG_INFO(0, size)
30273029
ZEND_ARG_INFO(0, callback)
30283030
ZEND_END_ARG_INFO()
@@ -5713,7 +5715,7 @@ PHP_FUNCTION(uv_fs_open)
57135715
/* }}} */
57145716

57155717

5716-
/* {{{ proto void uv_fs_read(resoruce $loop, zval $fd, callable $callback)
5718+
/* {{{ proto void uv_fs_read(resoruce $loop, zval $fd, long $offset, long $length, callable $callback)
57175719
*/
57185720
PHP_FUNCTION(uv_fs_read)
57195721
{

tests/300-fs.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, $offset=0, $len=32,function($stream, $nread, $data) {
99
if ($nread <= 0) {
1010
if ($nread < 0) {
1111
throw new Exception("read error");

0 commit comments

Comments
 (0)