-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[RFC]: Add file_descriptor() function to retrieve the file descriptor of a PHP Stream #10342
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Girgias
wants to merge
1
commit into
php:master
Choose a base branch
from
Girgias:fileno-function
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -864,6 +864,32 @@ PHP_FUNCTION(pclose) | |||||
} | ||||||
/* }}} */ | ||||||
|
||||||
PHP_FUNCTION(file_descriptor) | ||||||
{ | ||||||
zval *zsrc; | ||||||
php_stream *stream; | ||||||
php_socket_t fileno; | ||||||
|
||||||
ZEND_PARSE_PARAMETERS_START(1, 1) | ||||||
Z_PARAM_RESOURCE(zsrc) | ||||||
ZEND_PARSE_PARAMETERS_END(); | ||||||
|
||||||
php_stream_from_zval(stream, zsrc); | ||||||
|
||||||
/* TODO Should support streams that can be cast with PHP_STREAM_AS_FD_FOR_SELECT ? */ | ||||||
/* get the fd. | ||||||
* NB: Most other code will NOT use the PHP_STREAM_CAST_INTERNAL flag when casting. | ||||||
* It is only used here so that the buffered data warning is not displayed. | ||||||
*/ | ||||||
if (php_stream_can_cast(stream, PHP_STREAM_AS_FD | PHP_STREAM_CAST_INTERNAL) == FAILURE) { | ||||||
zend_argument_type_error(1, "cannot represent as a file descriptor"); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
RETURN_THROWS(); | ||||||
} | ||||||
php_stream_cast(stream, PHP_STREAM_AS_FD | PHP_STREAM_CAST_INTERNAL, (void*)&fileno, true); | ||||||
|
||||||
RETURN_LONG(fileno); | ||||||
} | ||||||
|
||||||
/* {{{ Test for end-of-file on a file pointer */ | ||||||
PHPAPI PHP_FUNCTION(feof) | ||||||
{ | ||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
--TEST-- | ||
Test file_descriptor() function: basic functionality | ||
--FILE-- | ||
<?php | ||
echo "*** Testing fileno() : basic functionality ***\n"; | ||
$tmpFile1 = __DIR__ . '/fileno.tmp1'; | ||
$file = fopen($tmpFile1, 'wb'); | ||
fclose($file); | ||
|
||
$file = fopen($tmpFile1, 'rb'); | ||
var_dump(file_descriptor($file)); | ||
|
||
var_dump(file_descriptor(STDOUT)); | ||
|
||
echo "Done"; | ||
?> | ||
--CLEAN-- | ||
<?php | ||
$tmpFile1 = __DIR__ . '/fileno.tmp1'; | ||
unlink($tmpFile1); | ||
?> | ||
--EXPECTF-- | ||
*** Testing fileno() : basic functionality *** | ||
int(%d) | ||
int(1) | ||
Done |
44 changes: 44 additions & 0 deletions
44
ext/standard/tests/file/file_descriptor_non_castable_stream.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
--TEST-- | ||
Test file_descriptor() function: retrieve file descriptor on TCP socket | ||
--EXTENSIONS-- | ||
pcntl | ||
posix | ||
--SKIPIF-- | ||
<?php | ||
require __DIR__ . '/../http/server.inc'; http_server_skipif(); | ||
?> | ||
--INI-- | ||
allow_url_fopen=1 | ||
--FILE-- | ||
<?php | ||
|
||
require __DIR__ . '/../http/server.inc'; | ||
|
||
$responses = array( | ||
"data://text/plain,HTTP/1.0 200 Ok\r\nSome: Header\r\nSome: Header\r\n\r\nBody", | ||
); | ||
|
||
['pid' => $pid, 'uri' => $uri] = http_server($responses, $output); | ||
|
||
/* Note: the warning is bogus in this case as no data actually gets lost, | ||
* but this checks that stream casting works */ | ||
$handle = fopen($uri, 'r'); | ||
$fd = file_descriptor($handle); | ||
var_dump($fd); | ||
var_dump(fread($handle, 20)); | ||
fclose($handle); | ||
|
||
$socket = stream_socket_client('tcp://example.com:80', timeout: 0.5); | ||
$fds = file_descriptor($socket); | ||
var_dump($fds); | ||
fclose($socket); | ||
|
||
var_dump($fd == $fds); | ||
|
||
http_server_kill($pid); | ||
?> | ||
--EXPECTF-- | ||
int(%d) | ||
string(4) "Body" | ||
int(%d) | ||
bool(true) |
79 changes: 79 additions & 0 deletions
79
ext/standard/tests/file/file_descriptor_non_castable_user_stream.phpt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
--TEST-- | ||
Test file_descriptor() function: error on uncastable user stream | ||
--FILE-- | ||
<?php | ||
|
||
// TODO Improve by mimicking tests from GH 10173 PR | ||
class DummyStreamWrapper | ||
{ | ||
/** @var resource|null */ | ||
public $context; | ||
|
||
/** @var resource|null */ | ||
public $handle; | ||
|
||
public function stream_cast(int $castAs) | ||
{ | ||
return false; | ||
} | ||
|
||
public function stream_close(): void { } | ||
|
||
public function stream_open(string $path, string $mode, int $options = 0, ?string &$openedPath = null): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function stream_read(int $count) | ||
{ | ||
return 0; | ||
} | ||
|
||
public function stream_seek(int $offset, int $whence = SEEK_SET): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function stream_set_option(int $option, int $arg1, ?int $arg2): bool | ||
{ | ||
return false; | ||
} | ||
|
||
public function stream_stat() | ||
{ | ||
return []; | ||
} | ||
|
||
public function stream_tell() | ||
{ | ||
return []; | ||
} | ||
|
||
public function stream_truncate(int $newSize): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function stream_write(string $data) { } | ||
|
||
|
||
public function unlink(string $path): bool | ||
{ | ||
return false; | ||
} | ||
} | ||
stream_wrapper_register('custom', DummyStreamWrapper::class); | ||
|
||
$fp = fopen("custom://myvar", "r+"); | ||
try { | ||
var_dump(file_descriptor($fp)); | ||
} catch (\TypeError $e) { | ||
echo $e->getMessage(), PHP_EOL; | ||
} | ||
Girgias marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fclose($fp); | ||
|
||
echo "Done"; | ||
?> | ||
--EXPECT-- | ||
file_descriptor(): Argument #1 ($stream) cannot represent as a file descriptor | ||
Done |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general
PHP_STREAM_AS_FD
should be supported ifPHP_STREAM_AS_FD_FOR_SELECT
which should be the case for all core stream wrappers - I just checked and don't see a case where it would be different. So I wouldn't bother with checkingPHP_STREAM_AS_FD_FOR_SELECT
as well.