Skip to content

Commit 5d7a1d7

Browse files
committed
Extract fstat into a seperate function
1 parent 9668877 commit 5d7a1d7

File tree

4 files changed

+76
-32
lines changed

4 files changed

+76
-32
lines changed

ext/spl/spl_directory.c

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2699,28 +2699,17 @@ PHP_METHOD(SplFileObject, fread)
26992699
}
27002700

27012701
/* {{{ Stat() on a filehandle */
2702-
// TODO Don't call fstat? As it may be undefined if it is disabled via disable_functions
27032702
PHP_METHOD(SplFileObject, fstat)
27042703
{
27052704
spl_filesystem_object *intern = Z_SPLFILESYSTEM_P(ZEND_THIS);
2706-
zend_function *func_ptr;
27072705

27082706
if (zend_parse_parameters_none() == FAILURE) {
27092707
RETURN_THROWS();
27102708
}
27112709

2712-
func_ptr = (zend_function *)zend_hash_str_find_ptr(EG(function_table), "fstat", sizeof("fstat") - 1);
2713-
if (func_ptr == NULL) {
2714-
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "fstat() is not defined");
2715-
RETURN_THROWS();
2716-
}
2717-
2718-
if (Z_ISUNDEF_P(&intern->u.file.zresource)) {
2719-
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Object not initialized");
2720-
RETURN_THROWS();
2721-
}
2710+
CHECK_SPL_FILE_OBJECT_IS_INITIALIZED();
27222711

2723-
zend_call_known_function(func_ptr, NULL, NULL, return_value, 0, NULL, NULL);
2712+
php_fstat(intern->u.file.stream, return_value);
27242713
}
27252714
/* }}} */
27262715

ext/spl/tests/SplFileObject_fstat_with_basic_fstat_disabled.phpt

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,60 @@ disable_functions="fstat"
55
--FILE--
66
<?php
77
$obj = New SplFileObject(__DIR__.'/SplFileObject_testinput.csv');
8-
try {
9-
var_dump($obj->fstat());
10-
} catch (\RuntimeException $e) {
11-
echo $e->getMessage() . \PHP_EOL;
12-
}
8+
var_dump($obj->fstat());
139
?>
14-
--EXPECT--
15-
fstat() is not defined
10+
--EXPECTF--
11+
array(26) {
12+
[0]=>
13+
int(%i)
14+
[1]=>
15+
int(%i)
16+
[2]=>
17+
int(%i)
18+
[3]=>
19+
int(%i)
20+
[4]=>
21+
int(%i)
22+
[5]=>
23+
int(%i)
24+
[6]=>
25+
int(%i)
26+
[7]=>
27+
int(%i)
28+
[8]=>
29+
int(%i)
30+
[9]=>
31+
int(%i)
32+
[10]=>
33+
int(%i)
34+
[11]=>
35+
int(%i)
36+
[12]=>
37+
int(%i)
38+
["dev"]=>
39+
int(%i)
40+
["ino"]=>
41+
int(%i)
42+
["mode"]=>
43+
int(%i)
44+
["nlink"]=>
45+
int(%i)
46+
["uid"]=>
47+
int(%i)
48+
["gid"]=>
49+
int(%i)
50+
["rdev"]=>
51+
int(%i)
52+
["size"]=>
53+
int(%i)
54+
["atime"]=>
55+
int(%i)
56+
["mtime"]=>
57+
int(%i)
58+
["ctime"]=>
59+
int(%i)
60+
["blksize"]=>
61+
int(%i)
62+
["blocks"]=>
63+
int(%i)
64+
}

ext/standard/file.c

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1489,26 +1489,16 @@ PHP_FUNCTION(ftruncate)
14891489
RETURN_BOOL(0 == php_stream_truncate_set_size(stream, size));
14901490
}
14911491
/* }}} */
1492-
1493-
/* {{{ Stat() on a filehandle */
1494-
PHP_FUNCTION(fstat)
1492+
PHPAPI void php_fstat(php_stream *stream, zval *return_value)
14951493
{
1496-
zval *fp;
1494+
php_stream_statbuf stat_ssb;
14971495
zval stat_dev, stat_ino, stat_mode, stat_nlink, stat_uid, stat_gid, stat_rdev,
14981496
stat_size, stat_atime, stat_mtime, stat_ctime, stat_blksize, stat_blocks;
1499-
php_stream *stream;
1500-
php_stream_statbuf stat_ssb;
15011497
char *stat_sb_names[13] = {
15021498
"dev", "ino", "mode", "nlink", "uid", "gid", "rdev",
15031499
"size", "atime", "mtime", "ctime", "blksize", "blocks"
15041500
};
15051501

1506-
ZEND_PARSE_PARAMETERS_START(1, 1)
1507-
Z_PARAM_RESOURCE(fp)
1508-
ZEND_PARSE_PARAMETERS_END();
1509-
1510-
PHP_STREAM_TO_ZVAL(stream, fp);
1511-
15121502
if (php_stream_stat(stream, &stat_ssb)) {
15131503
RETURN_FALSE;
15141504
}
@@ -1570,6 +1560,21 @@ PHP_FUNCTION(fstat)
15701560
zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[11], strlen(stat_sb_names[11]), &stat_blksize);
15711561
zend_hash_str_add_new(Z_ARRVAL_P(return_value), stat_sb_names[12], strlen(stat_sb_names[12]), &stat_blocks);
15721562
}
1563+
1564+
/* {{{ Stat() on a filehandle */
1565+
PHP_FUNCTION(fstat)
1566+
{
1567+
zval *fp;
1568+
php_stream *stream;
1569+
1570+
ZEND_PARSE_PARAMETERS_START(1, 1)
1571+
Z_PARAM_RESOURCE(fp)
1572+
ZEND_PARSE_PARAMETERS_END();
1573+
1574+
PHP_STREAM_TO_ZVAL(stream, fp);
1575+
1576+
php_fstat(stream, return_value);
1577+
}
15731578
/* }}} */
15741579

15751580
/* {{{ Copy a file */

ext/standard/file.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ PHPAPI int php_copy_file_ex(const char *src, const char *dest, int src_chk);
4343
PHPAPI int php_copy_file_ctx(const char *src, const char *dest, int src_chk, php_stream_context *ctx);
4444
PHPAPI int php_mkdir_ex(const char *dir, zend_long mode, int options);
4545
PHPAPI int php_mkdir(const char *dir, zend_long mode);
46+
PHPAPI void php_fstat(php_stream *stream, zval *return_value);
4647

4748
#define PHP_CSV_NO_ESCAPE EOF
4849
PHPAPI void php_fgetcsv(php_stream *stream, char delimiter, char enclosure, int escape_char, size_t buf_len, char *buf, zval *return_value);

0 commit comments

Comments
 (0)