cataphract Sat, 19 Feb 2011 01:28:37 +0000 Revision: http://svn.php.net/viewvc?view=revision&revision=308477
Log: - PHP_STREAM_OPTION_WRITE_BUFFER no longer disables the read buffer of a plain stream when 0 is given as the value. - PHP_STREAM_OPTION_WRITE_BUFFER no longer changes the chunk size in socket streams. - Added stream_set_chunk_size() function. - Some signedness fixes. - Test for commit r308474, now that it's possible to actually test it. Changed paths: U php/php-src/trunk/UPGRADING U php/php-src/trunk/ext/standard/basic_functions.c U php/php-src/trunk/ext/standard/streamsfuncs.c U php/php-src/trunk/ext/standard/streamsfuncs.h A php/php-src/trunk/ext/standard/tests/file/userstreams_006.phpt A php/php-src/trunk/ext/standard/tests/streams/stream_set_chunk_size.phpt U php/php-src/trunk/main/streams/plain_wrapper.c U php/php-src/trunk/main/streams/xp_socket.c
Modified: php/php-src/trunk/UPGRADING =================================================================== --- php/php-src/trunk/UPGRADING 2011-02-19 01:11:32 UTC (rev 308476) +++ php/php-src/trunk/UPGRADING 2011-02-19 01:28:37 UTC (rev 308477) @@ -164,7 +164,9 @@ strings. This breaks code that iterated the resulting stream array using a numeric index, but makes easier to identify which of the passed streams are present in the result. - +- stream_set_write_buffer() no longer disables the read buffer of a plain + stream when 0 is given as the second argument. +- stream_set_write_buffer() no longer changes the chunk size in socket streams. =================================== 5. Changes made to existing methods @@ -325,6 +327,7 @@ - get_declared_traits() - http_response_code() - trait_exists() + - stream_set_chunk_size() f. New global constants Modified: php/php-src/trunk/ext/standard/basic_functions.c =================================================================== --- php/php-src/trunk/ext/standard/basic_functions.c 2011-02-19 01:11:32 UTC (rev 308476) +++ php/php-src/trunk/ext/standard/basic_functions.c 2011-02-19 01:28:37 UTC (rev 308477) @@ -2117,6 +2117,11 @@ ZEND_ARG_INFO(0, fp) ZEND_ARG_INFO(0, buffer) ZEND_END_ARG_INFO() + +ZEND_BEGIN_ARG_INFO(arginfo_stream_set_chunk_size, 0) + ZEND_ARG_INFO(0, fp) + ZEND_ARG_INFO(0, chunk_size) +ZEND_END_ARG_INFO() ZEND_BEGIN_ARG_INFO_EX(arginfo_stream_socket_enable_crypto, 0, 0, 2) ZEND_ARG_INFO(0, stream) @@ -3119,6 +3124,7 @@ PHP_FE(stream_set_read_buffer, arginfo_stream_set_read_buffer) PHP_FE(stream_set_write_buffer, arginfo_stream_set_write_buffer) PHP_FALIAS(set_file_buffer, stream_set_write_buffer, arginfo_stream_set_write_buffer) + PHP_FE(stream_set_chunk_size, arginfo_stream_set_chunk_size) PHP_DEP_FALIAS(set_socket_blocking, stream_set_blocking, arginfo_stream_set_blocking) PHP_FE(stream_set_blocking, arginfo_stream_set_blocking) Modified: php/php-src/trunk/ext/standard/streamsfuncs.c =================================================================== --- php/php-src/trunk/ext/standard/streamsfuncs.c 2011-02-19 01:11:32 UTC (rev 308476) +++ php/php-src/trunk/ext/standard/streamsfuncs.c 2011-02-19 01:28:37 UTC (rev 308477) @@ -557,7 +557,7 @@ { HashTable *stream_xport_hash; char *stream_xport; - int stream_xport_len; + uint stream_xport_len; ulong num_key; if (zend_parse_parameters_none() == FAILURE) { @@ -586,7 +586,8 @@ { HashTable *url_stream_wrappers_hash; char *stream_protocol; - int key_flags, stream_protocol_len = 0; + int key_flags; + uint stream_protocol_len = 0; ulong num_key; if (zend_parse_parameters_none() == FAILURE) { @@ -924,7 +925,7 @@ HashPosition pos, opos; zval **wval, **oval; char *wkey, *okey; - int wkey_len, okey_len; + uint wkey_len, okey_len; int ret = SUCCESS; ulong num_key; @@ -1433,6 +1434,40 @@ } /* }}} */ +/* {{{ proto int stream_set_chunk_size(resource fp, int chunk_size) + Set the stream chunk size */ +PHP_FUNCTION(stream_set_chunk_size) +{ + int ret; + long csize; + zval *zstream; + php_stream *stream; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rl", &zstream, &csize) == FAILURE) { + RETURN_FALSE; + } + + if (csize <= 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "The chunk size must be a positive integer, given %ld", csize); + RETURN_FALSE; + } + /* stream.chunk_size is actually a size_t, but php_stream_set_option + * can only use an int to accept the new value and return the old one. + * In any case, values larger than INT_MAX for a chunk size make no sense. + */ + if (csize > INT_MAX) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "The chunk size cannot be larger than %d", INT_MAX); + RETURN_FALSE; + } + + php_stream_from_zval(stream, &zstream); + + ret = php_stream_set_option(stream, PHP_STREAM_OPTION_SET_CHUNK_SIZE, (int)csize, NULL); + + RETURN_LONG(ret > 0 ? (long)ret : (long)EOF); +} +/* }}} */ + /* {{{ proto int stream_set_read_buffer(resource fp, int buffer) Set file read buffer */ PHP_FUNCTION(stream_set_read_buffer) Modified: php/php-src/trunk/ext/standard/streamsfuncs.h =================================================================== --- php/php-src/trunk/ext/standard/streamsfuncs.h 2011-02-19 01:11:32 UTC (rev 308476) +++ php/php-src/trunk/ext/standard/streamsfuncs.h 2011-02-19 01:28:37 UTC (rev 308477) @@ -38,6 +38,7 @@ PHP_FUNCTION(stream_set_timeout); PHP_FUNCTION(stream_set_read_buffer); PHP_FUNCTION(stream_set_write_buffer); +PHP_FUNCTION(stream_set_chunk_size); PHP_FUNCTION(stream_get_transports); PHP_FUNCTION(stream_get_wrappers); PHP_FUNCTION(stream_get_line); Added: php/php-src/trunk/ext/standard/tests/file/userstreams_006.phpt =================================================================== --- php/php-src/trunk/ext/standard/tests/file/userstreams_006.phpt (rev 0) +++ php/php-src/trunk/ext/standard/tests/file/userstreams_006.phpt 2011-02-19 01:28:37 UTC (rev 308477) @@ -0,0 +1,38 @@ +--TEST-- +User-space streams: set_options returns "not implemented" for unhandled option types +--FILE-- +<?php +class test_wrapper { + function stream_open($path, $mode, $openedpath) { + return true; + } + function stream_eof() { + return false; + } + function stream_write($data) { + echo "size: ", strlen($data), "\n"; + return strlen($data); + } + function stream_set_option($option, $arg1, $arg2) { + echo "option: ", $option, ", ", $arg1, ", ", $arg2, "\n"; + return false; + } +} + +var_dump(stream_wrapper_register('test', 'test_wrapper')); + +$fd = fopen("test://foo","r"); + +var_dump(stream_set_write_buffer($fd, 50)); +var_dump(stream_set_chunk_size($fd, 42)); + +var_dump(fwrite($fd, str_repeat('0', 70))); + +--EXPECT-- +bool(true) +option: 3, 2, 50 +int(-1) +int(8192) +size: 42 +size: 28 +int(70) Added: php/php-src/trunk/ext/standard/tests/streams/stream_set_chunk_size.phpt =================================================================== --- php/php-src/trunk/ext/standard/tests/streams/stream_set_chunk_size.phpt (rev 0) +++ php/php-src/trunk/ext/standard/tests/streams/stream_set_chunk_size.phpt 2011-02-19 01:28:37 UTC (rev 308477) @@ -0,0 +1,93 @@ +--TEST-- +stream_set_chunk_size basic tests +--FILE-- +<?php +class test_wrapper { + function stream_open($path, $mode, $openedpath) { + return true; + } + function stream_eof() { + return false; + } + function stream_read($count) { + echo "read with size: ", $count, "\n"; + return str_repeat('a', $count); + } + function stream_write($data) { + echo "write with size: ", strlen($data), "\n"; + return strlen($data); + } + function stream_set_option($option, $arg1, $arg2) { + echo "option: ", $option, ", ", $arg1, ", ", $arg2, "\n"; + return false; + } +} + +var_dump(stream_wrapper_register('test', 'test_wrapper')); + +$f = fopen("test://foo","r"); + +/* when the chunk size is 1, the read buffer is skipped, but the + * the writes are made in chunks of size 1 (business as usual) + * This should probably be revisited */ +echo "should return previous chunk size (8192)\n"; +var_dump(stream_set_chunk_size($f, 1)); +echo "should be read without buffer (\$count == 10000)\n"; +var_dump(strlen(fread($f, 10000))); +echo "should elicit 3 writes of size 1 and return 3\n"; +var_dump(fwrite($f, str_repeat('b', 3))); + +echo "should return previous chunk size (1)\n"; +var_dump(stream_set_chunk_size($f, 100)); +echo "should elicit one read of size 100 (chunk size)\n"; +var_dump(strlen(fread($f, 250))); +echo "should elicit one read of size 100 (chunk size)\n"; +var_dump(strlen(fread($f, 50))); +echo "should elicit no read because there is sufficient cached data\n"; +var_dump(strlen(fread($f, 50))); +echo "should elicit 2 writes of size 100 and one of size 50\n"; +var_dump(strlen(fwrite($f, str_repeat('b', 250)))); + +echo "\nerror conditions\n"; +var_dump(stream_set_chunk_size($f, 0)); +var_dump(stream_set_chunk_size($f, -1)); +var_dump(stream_set_chunk_size($f, array())); + +--EXPECTF-- +bool(true) +should return previous chunk size (8192) +int(8192) +should be read without buffer ($count == 10000) +read with size: 10000 +int(10000) +should elicit 3 writes of size 1 and return 3 +write with size: 1 +write with size: 1 +write with size: 1 +int(3) +should return previous chunk size (1) +int(1) +should elicit one read of size 100 (chunk size) +read with size: 100 +int(100) +should elicit one read of size 100 (chunk size) +read with size: 100 +int(50) +should elicit no read because there is sufficient cached data +int(50) +should elicit 2 writes of size 100 and one of size 50 +write with size: 100 +write with size: 100 +write with size: 50 +int(3) + +error conditions + +Warning: stream_set_chunk_size(): The chunk size must be a positive integer, given 0 in %s on line %d +bool(false) + +Warning: stream_set_chunk_size(): The chunk size must be a positive integer, given -1 in %s on line %d +bool(false) + +Warning: stream_set_chunk_size() expects parameter 2 to be long, array given in %s on line %d +bool(false) Modified: php/php-src/trunk/main/streams/plain_wrapper.c =================================================================== --- php/php-src/trunk/main/streams/plain_wrapper.c 2011-02-19 01:11:32 UTC (rev 308476) +++ php/php-src/trunk/main/streams/plain_wrapper.c 2011-02-19 01:28:37 UTC (rev 308477) @@ -588,15 +588,12 @@ switch(value) { case PHP_STREAM_BUFFER_NONE: - stream->flags |= PHP_STREAM_FLAG_NO_BUFFER; return setvbuf(data->file, NULL, _IONBF, 0); case PHP_STREAM_BUFFER_LINE: - stream->flags ^= PHP_STREAM_FLAG_NO_BUFFER; return setvbuf(data->file, NULL, _IOLBF, size); case PHP_STREAM_BUFFER_FULL: - stream->flags ^= PHP_STREAM_FLAG_NO_BUFFER; return setvbuf(data->file, NULL, _IOFBF, size); default: Modified: php/php-src/trunk/main/streams/xp_socket.c =================================================================== --- php/php-src/trunk/main/streams/xp_socket.c 2011-02-19 01:11:32 UTC (rev 308476) +++ php/php-src/trunk/main/streams/xp_socket.c 2011-02-19 01:28:37 UTC (rev 308477) @@ -400,10 +400,6 @@ } #endif - case PHP_STREAM_OPTION_WRITE_BUFFER: - php_stream_set_chunk_size(stream, (ptrparam ? *(size_t *)ptrparam : PHP_SOCK_CHUNK_SIZE)); - return PHP_STREAM_OPTION_RETURN_OK; - default: return PHP_STREAM_OPTION_RETURN_NOTIMPL; }
-- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php