Skip to content

Commit 8787fa2

Browse files
committed
ext/standard: Use new php_streams fast ZPP specifier for file functions
Fix a corresponding test
1 parent 11876f9 commit 8787fa2

File tree

2 files changed

+32
-83
lines changed

2 files changed

+32
-83
lines changed

ext/standard/file.c

Lines changed: 21 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,6 @@ php_file_globals file_globals;
102102

103103
/* }}} */
104104

105-
#define PHP_STREAM_FROM_ZVAL(stream, arg) \
106-
ZEND_ASSERT(Z_TYPE_P(arg) == IS_RESOURCE); \
107-
php_stream_from_res(stream, Z_RES_P(arg));
108-
109105
/* {{{ ZTS-stuff / Globals / Prototypes */
110106

111107
/* sharing globals is *evil* */
@@ -215,19 +211,17 @@ PHPAPI void php_flock_common(php_stream *stream, zend_long operation,
215211
/* {{{ Portable file locking */
216212
PHP_FUNCTION(flock)
217213
{
218-
zval *res, *wouldblock = NULL;
214+
zval *wouldblock = NULL;
219215
php_stream *stream;
220216
zend_long operation = 0;
221217

222218
ZEND_PARSE_PARAMETERS_START(2, 3)
223-
Z_PARAM_RESOURCE(res)
219+
PHP_Z_PARAM_STREAM(stream)
224220
Z_PARAM_LONG(operation)
225221
Z_PARAM_OPTIONAL
226222
Z_PARAM_ZVAL(wouldblock)
227223
ZEND_PARSE_PARAMETERS_END();
228224

229-
PHP_STREAM_FROM_ZVAL(stream, res);
230-
231225
php_flock_common(stream, operation, 2, wouldblock, return_value);
232226
}
233227
/* }}} */
@@ -756,15 +750,12 @@ PHP_FUNCTION(fopen)
756750
/* {{{ Close an open file pointer */
757751
PHPAPI PHP_FUNCTION(fclose)
758752
{
759-
zval *res;
760753
php_stream *stream;
761754

762755
ZEND_PARSE_PARAMETERS_START(1, 1)
763-
Z_PARAM_RESOURCE(res)
756+
PHP_Z_PARAM_STREAM(stream)
764757
ZEND_PARSE_PARAMETERS_END();
765758

766-
PHP_STREAM_FROM_ZVAL(stream, res);
767-
768759
if ((stream->flags & PHP_STREAM_FLAG_NO_FCLOSE) != 0) {
769760
php_error_docref(NULL, E_WARNING, ZEND_LONG_FMT " is not a valid stream resource", stream->res->handle);
770761
RETURN_FALSE;
@@ -836,15 +827,12 @@ PHP_FUNCTION(popen)
836827
/* {{{ Close a file pointer opened by popen() */
837828
PHP_FUNCTION(pclose)
838829
{
839-
zval *res;
840830
php_stream *stream;
841831

842832
ZEND_PARSE_PARAMETERS_START(1, 1)
843-
Z_PARAM_RESOURCE(res)
833+
PHP_Z_PARAM_STREAM(stream)
844834
ZEND_PARSE_PARAMETERS_END();
845835

846-
PHP_STREAM_FROM_ZVAL(stream, res);
847-
848836
FG(pclose_wait) = 1;
849837
zend_list_close(stream->res);
850838
FG(pclose_wait) = 0;
@@ -855,15 +843,12 @@ PHP_FUNCTION(pclose)
855843
/* {{{ Test for end-of-file on a file pointer */
856844
PHPAPI PHP_FUNCTION(feof)
857845
{
858-
zval *res;
859846
php_stream *stream;
860847

861848
ZEND_PARSE_PARAMETERS_START(1, 1)
862-
Z_PARAM_RESOURCE(res)
849+
PHP_Z_PARAM_STREAM(stream)
863850
ZEND_PARSE_PARAMETERS_END();
864851

865-
PHP_STREAM_FROM_ZVAL(stream, res);
866-
867852
if (php_stream_eof(stream)) {
868853
RETURN_TRUE;
869854
} else {
@@ -875,7 +860,6 @@ PHPAPI PHP_FUNCTION(feof)
875860
/* {{{ Get a line from file pointer */
876861
PHPAPI PHP_FUNCTION(fgets)
877862
{
878-
zval *res;
879863
zend_long len = 1024;
880864
bool len_is_null = 1;
881865
char *buf = NULL;
@@ -884,13 +868,11 @@ PHPAPI PHP_FUNCTION(fgets)
884868
php_stream *stream;
885869

886870
ZEND_PARSE_PARAMETERS_START(1, 2)
887-
Z_PARAM_RESOURCE(res)
871+
PHP_Z_PARAM_STREAM(stream)
888872
Z_PARAM_OPTIONAL
889873
Z_PARAM_LONG_OR_NULL(len, len_is_null)
890874
ZEND_PARSE_PARAMETERS_END();
891875

892-
PHP_STREAM_FROM_ZVAL(stream, res);
893-
894876
if (len_is_null) {
895877
/* ask streams to give us a buffer of an appropriate size */
896878
buf = php_stream_get_line(stream, NULL, 0, &line_len);
@@ -926,15 +908,12 @@ PHPAPI PHP_FUNCTION(fgets)
926908
/* {{{ Get a character from file pointer */
927909
PHPAPI PHP_FUNCTION(fgetc)
928910
{
929-
zval *res;
930911
php_stream *stream;
931912

932913
ZEND_PARSE_PARAMETERS_START(1, 1)
933-
Z_PARAM_RESOURCE(res)
914+
PHP_Z_PARAM_STREAM(stream)
934915
ZEND_PARSE_PARAMETERS_END();
935916

936-
PHP_STREAM_FROM_ZVAL(stream, res);
937-
938917
int result = php_stream_getc(stream);
939918

940919
if (result == EOF) {
@@ -989,7 +968,6 @@ PHP_FUNCTION(fscanf)
989968
/* {{{ Binary-safe file write */
990969
PHPAPI PHP_FUNCTION(fwrite)
991970
{
992-
zval *res;
993971
char *input;
994972
size_t inputlen;
995973
ssize_t ret;
@@ -999,7 +977,7 @@ PHPAPI PHP_FUNCTION(fwrite)
999977
php_stream *stream;
1000978

1001979
ZEND_PARSE_PARAMETERS_START(2, 3)
1002-
Z_PARAM_RESOURCE(res)
980+
PHP_Z_PARAM_STREAM(stream)
1003981
Z_PARAM_STRING(input, inputlen)
1004982
Z_PARAM_OPTIONAL
1005983
Z_PARAM_LONG_OR_NULL(maxlen, maxlen_is_null)
@@ -1017,8 +995,6 @@ PHPAPI PHP_FUNCTION(fwrite)
1017995
RETURN_LONG(0);
1018996
}
1019997

1020-
PHP_STREAM_FROM_ZVAL(stream, res);
1021-
1022998
ret = php_stream_write(stream, input, num_bytes);
1023999
if (ret < 0) {
10241000
RETURN_FALSE;
@@ -1031,16 +1007,13 @@ PHPAPI PHP_FUNCTION(fwrite)
10311007
/* {{{ Flushes output */
10321008
PHPAPI PHP_FUNCTION(fflush)
10331009
{
1034-
zval *res;
10351010
int ret;
10361011
php_stream *stream;
10371012

10381013
ZEND_PARSE_PARAMETERS_START(1, 1)
1039-
Z_PARAM_RESOURCE(res)
1014+
PHP_Z_PARAM_STREAM(stream)
10401015
ZEND_PARSE_PARAMETERS_END();
10411016

1042-
PHP_STREAM_FROM_ZVAL(stream, res);
1043-
10441017
ret = php_stream_flush(stream);
10451018
if (ret) {
10461019
RETURN_FALSE;
@@ -1052,15 +1025,12 @@ PHPAPI PHP_FUNCTION(fflush)
10521025
/* {{{ Rewind the position of a file pointer */
10531026
PHPAPI PHP_FUNCTION(rewind)
10541027
{
1055-
zval *res;
10561028
php_stream *stream;
10571029

10581030
ZEND_PARSE_PARAMETERS_START(1, 1)
1059-
Z_PARAM_RESOURCE(res)
1031+
PHP_Z_PARAM_STREAM(stream)
10601032
ZEND_PARSE_PARAMETERS_END();
10611033

1062-
PHP_STREAM_FROM_ZVAL(stream, res);
1063-
10641034
if (-1 == php_stream_rewind(stream)) {
10651035
RETURN_FALSE;
10661036
}
@@ -1071,16 +1041,13 @@ PHPAPI PHP_FUNCTION(rewind)
10711041
/* {{{ Get file pointer's read/write position */
10721042
PHPAPI PHP_FUNCTION(ftell)
10731043
{
1074-
zval *res;
10751044
zend_long ret;
10761045
php_stream *stream;
10771046

10781047
ZEND_PARSE_PARAMETERS_START(1, 1)
1079-
Z_PARAM_RESOURCE(res)
1048+
PHP_Z_PARAM_STREAM(stream)
10801049
ZEND_PARSE_PARAMETERS_END();
10811050

1082-
PHP_STREAM_FROM_ZVAL(stream, res);
1083-
10841051
ret = php_stream_tell(stream);
10851052
if (ret == -1) {
10861053
RETURN_FALSE;
@@ -1092,19 +1059,16 @@ PHPAPI PHP_FUNCTION(ftell)
10921059
/* {{{ Seek on a file pointer */
10931060
PHPAPI PHP_FUNCTION(fseek)
10941061
{
1095-
zval *res;
10961062
zend_long offset, whence = SEEK_SET;
10971063
php_stream *stream;
10981064

10991065
ZEND_PARSE_PARAMETERS_START(2, 3)
1100-
Z_PARAM_RESOURCE(res)
1066+
PHP_Z_PARAM_STREAM(stream)
11011067
Z_PARAM_LONG(offset)
11021068
Z_PARAM_OPTIONAL
11031069
Z_PARAM_LONG(whence)
11041070
ZEND_PARSE_PARAMETERS_END();
11051071

1106-
PHP_STREAM_FROM_ZVAL(stream, res);
1107-
11081072
RETURN_LONG(php_stream_seek(stream, offset, (int) whence));
11091073
}
11101074
/* }}} */
@@ -1215,16 +1179,13 @@ PHP_FUNCTION(umask)
12151179
/* {{{ Output all remaining data from a file pointer */
12161180
PHPAPI PHP_FUNCTION(fpassthru)
12171181
{
1218-
zval *res;
12191182
size_t size;
12201183
php_stream *stream;
12211184

12221185
ZEND_PARSE_PARAMETERS_START(1, 1)
1223-
Z_PARAM_RESOURCE(res)
1186+
PHP_Z_PARAM_STREAM(stream)
12241187
ZEND_PARSE_PARAMETERS_END();
12251188

1226-
PHP_STREAM_FROM_ZVAL(stream, res);
1227-
12281189
size = php_stream_passthru(stream);
12291190
RETURN_LONG(size);
12301191
}
@@ -1303,15 +1264,12 @@ PHP_FUNCTION(unlink)
13031264

13041265
PHP_FUNCTION(fsync)
13051266
{
1306-
zval *res;
13071267
php_stream *stream;
13081268

13091269
ZEND_PARSE_PARAMETERS_START(1, 1)
1310-
Z_PARAM_RESOURCE(res)
1270+
PHP_Z_PARAM_STREAM(stream)
13111271
ZEND_PARSE_PARAMETERS_END();
13121272

1313-
PHP_STREAM_FROM_ZVAL(stream, res);
1314-
13151273
if (!php_stream_sync_supported(stream)) {
13161274
php_error_docref(NULL, E_WARNING, "Can't fsync this stream!");
13171275
RETURN_FALSE;
@@ -1322,15 +1280,12 @@ PHP_FUNCTION(fsync)
13221280

13231281
PHP_FUNCTION(fdatasync)
13241282
{
1325-
zval *res;
13261283
php_stream *stream;
13271284

13281285
ZEND_PARSE_PARAMETERS_START(1, 1)
1329-
Z_PARAM_RESOURCE(res)
1286+
PHP_Z_PARAM_STREAM(stream)
13301287
ZEND_PARSE_PARAMETERS_END();
13311288

1332-
PHP_STREAM_FROM_ZVAL(stream, res);
1333-
13341289
if (!php_stream_sync_supported(stream)) {
13351290
php_error_docref(NULL, E_WARNING, "Can't fsync this stream!");
13361291
RETURN_FALSE;
@@ -1342,12 +1297,11 @@ PHP_FUNCTION(fdatasync)
13421297
/* {{{ Truncate file to 'size' length */
13431298
PHP_FUNCTION(ftruncate)
13441299
{
1345-
zval *fp;
13461300
zend_long size;
13471301
php_stream *stream;
13481302

13491303
ZEND_PARSE_PARAMETERS_START(2, 2)
1350-
Z_PARAM_RESOURCE(fp)
1304+
PHP_Z_PARAM_STREAM(stream)
13511305
Z_PARAM_LONG(size)
13521306
ZEND_PARSE_PARAMETERS_END();
13531307

@@ -1356,8 +1310,6 @@ PHP_FUNCTION(ftruncate)
13561310
RETURN_THROWS();
13571311
}
13581312

1359-
PHP_STREAM_FROM_ZVAL(stream, fp);
1360-
13611313
if (!php_stream_truncate_supported(stream)) {
13621314
php_error_docref(NULL, E_WARNING, "Can't truncate this stream!");
13631315
RETURN_FALSE;
@@ -1441,15 +1393,12 @@ PHPAPI void php_fstat(php_stream *stream, zval *return_value)
14411393
/* {{{ Stat() on a filehandle */
14421394
PHP_FUNCTION(fstat)
14431395
{
1444-
zval *fp;
14451396
php_stream *stream;
14461397

14471398
ZEND_PARSE_PARAMETERS_START(1, 1)
1448-
Z_PARAM_RESOURCE(fp)
1399+
PHP_Z_PARAM_STREAM(stream)
14491400
ZEND_PARSE_PARAMETERS_END();
14501401

1451-
PHP_STREAM_FROM_ZVAL(stream, fp);
1452-
14531402
php_fstat(stream, return_value);
14541403
}
14551404
/* }}} */
@@ -1588,18 +1537,15 @@ PHPAPI zend_result php_copy_file_ctx(const char *src, const char *dest, int src_
15881537
/* {{{ Binary-safe file read */
15891538
PHPAPI PHP_FUNCTION(fread)
15901539
{
1591-
zval *res;
15921540
zend_long len;
15931541
php_stream *stream;
15941542
zend_string *str;
15951543

15961544
ZEND_PARSE_PARAMETERS_START(2, 2)
1597-
Z_PARAM_RESOURCE(res)
1545+
PHP_Z_PARAM_STREAM(stream)
15981546
Z_PARAM_LONG(len)
15991547
ZEND_PARSE_PARAMETERS_END();
16001548

1601-
PHP_STREAM_FROM_ZVAL(stream, res);
1602-
16031549
if (len <= 0) {
16041550
zend_argument_value_error(2, "must be greater than 0");
16051551
RETURN_THROWS();
@@ -1683,15 +1629,15 @@ PHP_FUNCTION(fputcsv)
16831629
char delimiter = ','; /* allow this to be set as parameter */
16841630
char enclosure = '"'; /* allow this to be set as parameter */
16851631
php_stream *stream;
1686-
zval *fp = NULL, *fields = NULL;
1632+
zval *fields = NULL;
16871633
ssize_t ret;
16881634
char *delimiter_str = NULL, *enclosure_str = NULL;
16891635
zend_string *escape_str = NULL;
16901636
size_t delimiter_str_len = 0, enclosure_str_len = 0;
16911637
zend_string *eol_str = NULL;
16921638

16931639
ZEND_PARSE_PARAMETERS_START(2, 6)
1694-
Z_PARAM_RESOURCE(fp)
1640+
PHP_Z_PARAM_STREAM(stream)
16951641
Z_PARAM_ARRAY(fields)
16961642
Z_PARAM_OPTIONAL
16971643
Z_PARAM_STRING(delimiter_str, delimiter_str_len)
@@ -1725,8 +1671,6 @@ PHP_FUNCTION(fputcsv)
17251671
RETURN_THROWS();
17261672
}
17271673

1728-
PHP_STREAM_FROM_ZVAL(stream, fp);
1729-
17301674
ret = php_fputcsv(stream, fields, delimiter, enclosure, escape_char, eol_str);
17311675
if (ret < 0) {
17321676
RETURN_FALSE;
@@ -1811,7 +1755,6 @@ PHP_FUNCTION(fgetcsv)
18111755
char *buf;
18121756
php_stream *stream;
18131757

1814-
zval *fd;
18151758
bool len_is_null = 1;
18161759
char *delimiter_str = NULL;
18171760
size_t delimiter_str_len = 0;
@@ -1820,7 +1763,7 @@ PHP_FUNCTION(fgetcsv)
18201763
zend_string *escape_str = NULL;
18211764

18221765
ZEND_PARSE_PARAMETERS_START(1, 5)
1823-
Z_PARAM_RESOURCE(fd)
1766+
PHP_Z_PARAM_STREAM(stream)
18241767
Z_PARAM_OPTIONAL
18251768
Z_PARAM_LONG_OR_NULL(len, len_is_null)
18261769
Z_PARAM_STRING(delimiter_str, delimiter_str_len)
@@ -1861,8 +1804,6 @@ PHP_FUNCTION(fgetcsv)
18611804
RETURN_THROWS();
18621805
}
18631806

1864-
PHP_STREAM_FROM_ZVAL(stream, fd);
1865-
18661807
if (len < 0) {
18671808
if ((buf = php_stream_get_line(stream, NULL, 0, &buf_len)) == NULL) {
18681809
RETURN_FALSE;

0 commit comments

Comments
 (0)