From 1ea6a5c1487a38a0bb04227a57573021caeee41f Mon Sep 17 00:00:00 2001 From: David Carlier Date: Sat, 7 Jan 2023 08:34:56 +0000 Subject: [PATCH] posix adding posix_fpathconf. follow-up on GH-10238 but with the file descriptor flavor. --- ext/posix/posix.c | 32 ++++++++++++++++++++++++++++ ext/posix/posix.stub.php | 2 ++ ext/posix/posix_arginfo.h | 9 +++++++- ext/posix/tests/posix_fpathconf.phpt | 22 +++++++++++++++++++ 4 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 ext/posix/tests/posix_fpathconf.phpt diff --git a/ext/posix/posix.c b/ext/posix/posix.c index f521fffb2b66..295a60a32cd1 100644 --- a/ext/posix/posix.c +++ b/ext/posix/posix.c @@ -1225,3 +1225,35 @@ PHP_FUNCTION(posix_pathconf) RETURN_LONG(ret); } + +PHP_FUNCTION(posix_fpathconf) +{ + zend_long name, ret, fd; + zval *z_fd; + + ZEND_PARSE_PARAMETERS_START(2, 2) + Z_PARAM_ZVAL(z_fd) + Z_PARAM_LONG(name); + ZEND_PARSE_PARAMETERS_END(); + + if (Z_TYPE_P(z_fd) == IS_RESOURCE) { + if (!php_posix_stream_get_fd(z_fd, &fd)) { + RETURN_FALSE; + } + } else { + if (!zend_parse_arg_long(z_fd, &fd, /* is_null */ false, /* check_null */ false, /* arg_num */ 1)) { + zend_argument_type_error(1, "must be of type int|resource, %s given", + zend_zval_type_name(z_fd)); + RETURN_THROWS(); + } + } + + ret = fpathconf(fd, name); + + if (ret < 0 && errno != 0) { + POSIX_G(last_error) = errno; + RETURN_FALSE; + } + + RETURN_LONG(ret); +} diff --git a/ext/posix/posix.stub.php b/ext/posix/posix.stub.php index 94caf3cc261e..16f19e91490e 100644 --- a/ext/posix/posix.stub.php +++ b/ext/posix/posix.stub.php @@ -429,3 +429,5 @@ function posix_initgroups(string $username, int $group_id): bool {} function posix_sysconf(int $conf_id): int {} function posix_pathconf(string $path, int $name): int|false {} +/** @param resource|int $file_descriptor */ +function posix_fpathconf($file_descriptor, int $name): int|false {} diff --git a/ext/posix/posix_arginfo.h b/ext/posix/posix_arginfo.h index 6bbf4ca25dce..74bc0021676c 100644 --- a/ext/posix/posix_arginfo.h +++ b/ext/posix/posix_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 68daa5f0b270b307501a785ca6a197ea161a2ded */ + * Stub hash: b0c74c2ad41d4ae6a624f73109815fc9fe47fd1f */ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_posix_kill, 0, 2, _IS_BOOL, 0) ZEND_ARG_TYPE_INFO(0, process_id, IS_LONG, 0) @@ -169,6 +169,11 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_posix_pathconf, 0, 2, MAY_BE_LON ZEND_ARG_TYPE_INFO(0, name, IS_LONG, 0) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_posix_fpathconf, 0, 2, MAY_BE_LONG|MAY_BE_FALSE) + ZEND_ARG_INFO(0, file_descriptor) + ZEND_ARG_TYPE_INFO(0, name, IS_LONG, 0) +ZEND_END_ARG_INFO() + ZEND_FUNCTION(posix_kill); ZEND_FUNCTION(posix_getpid); @@ -234,6 +239,7 @@ ZEND_FUNCTION(posix_initgroups); #endif ZEND_FUNCTION(posix_sysconf); ZEND_FUNCTION(posix_pathconf); +ZEND_FUNCTION(posix_fpathconf); static const zend_function_entry ext_functions[] = { @@ -302,6 +308,7 @@ static const zend_function_entry ext_functions[] = { #endif ZEND_FE(posix_sysconf, arginfo_posix_sysconf) ZEND_FE(posix_pathconf, arginfo_posix_pathconf) + ZEND_FE(posix_fpathconf, arginfo_posix_fpathconf) ZEND_FE_END }; diff --git a/ext/posix/tests/posix_fpathconf.phpt b/ext/posix/tests/posix_fpathconf.phpt new file mode 100644 index 000000000000..a809066e7822 --- /dev/null +++ b/ext/posix/tests/posix_fpathconf.phpt @@ -0,0 +1,22 @@ +--TEST-- +Test posix_fpathconf +--EXTENSIONS-- +posix +--FILE-- +getMessage() . "\n"; +} +$fd = fopen(sys_get_temp_dir(), "r"); +var_dump(posix_fpathconf($fd, POSIX_PC_PATH_MAX)); +fclose($fd); +?> +--EXPECTF-- +bool(false) +bool(true) +posix_fpathconf(): Argument #1 ($file_descriptor) must be of type int|resource, string given +int(%d)