Skip to content

Commit 2ffaeee

Browse files
committed
Introduce set_proc_descriptor_to_file helper in proc_open.c
1 parent 8e09400 commit 2ffaeee

File tree

1 file changed

+33
-25
lines changed

1 file changed

+33
-25
lines changed

ext/standard/proc_open.c

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,38 @@ static int set_proc_descriptor_to_pipe(struct php_proc_open_descriptor_item *des
570570
return SUCCESS;
571571
}
572572

573+
static int set_proc_descriptor_to_file(struct php_proc_open_descriptor_item *desc, zval *zfile, zval *zmode)
574+
{
575+
php_socket_t fd;
576+
577+
/* try a wrapper */
578+
php_stream *stream = php_stream_open_wrapper(Z_STRVAL_P(zfile), Z_STRVAL_P(zmode),
579+
REPORT_ERRORS|STREAM_WILL_CAST, NULL);
580+
581+
/* force into an fd */
582+
if (stream == NULL) {
583+
return FAILURE;
584+
}
585+
if (php_stream_cast(stream, PHP_STREAM_CAST_RELEASE|PHP_STREAM_AS_FD, (void **)&fd, REPORT_ERRORS) == FAILURE) {
586+
return FAILURE;
587+
}
588+
589+
desc->mode = DESC_FILE;
590+
591+
#ifdef PHP_WIN32
592+
desc->childend = dup_fd_as_handle((int)fd);
593+
_close((int)fd);
594+
595+
/* simulate the append mode by fseeking to the end of the file
596+
this introduces a potential race-condition, but it is the best we can do, though */
597+
if (strchr(Z_STRVAL_P(zmode), 'a')) {
598+
SetFilePointer(desc->childend, 0, NULL, FILE_END);
599+
}
600+
#else
601+
desc->childend = fd;
602+
#endif
603+
}
604+
573605
static void close_all_descriptors(struct php_proc_open_descriptor_item *descriptors, int ndesc)
574606
{
575607
for (int i = 0; i < ndesc; i++) {
@@ -773,10 +805,6 @@ PHP_FUNCTION(proc_open)
773805
}
774806
} else if (strcmp(Z_STRVAL_P(ztype), "file") == 0) {
775807
zval *zfile, *zmode;
776-
php_socket_t fd;
777-
php_stream *stream;
778-
779-
descriptors[ndesc].mode = DESC_FILE;
780808

781809
if ((zfile = zend_hash_index_find(Z_ARRVAL_P(descitem), 1)) != NULL) {
782810
if (!try_convert_to_string(zfile)) {
@@ -796,29 +824,9 @@ PHP_FUNCTION(proc_open)
796824
goto exit_fail;
797825
}
798826

799-
/* try a wrapper */
800-
stream = php_stream_open_wrapper(Z_STRVAL_P(zfile), Z_STRVAL_P(zmode),
801-
REPORT_ERRORS|STREAM_WILL_CAST, NULL);
802-
803-
/* force into an fd */
804-
if (stream == NULL || FAILURE == php_stream_cast(stream,
805-
PHP_STREAM_CAST_RELEASE|PHP_STREAM_AS_FD,
806-
(void **)&fd, REPORT_ERRORS)) {
827+
if (set_proc_descriptor_to_file(&descriptors[ndesc], zfile, zmode) == FAILURE) {
807828
goto exit_fail;
808829
}
809-
810-
#ifdef PHP_WIN32
811-
descriptors[ndesc].childend = dup_fd_as_handle((int)fd);
812-
_close((int)fd);
813-
814-
/* simulate the append mode by fseeking to the end of the file
815-
this introduces a potential race-condition, but it is the best we can do, though */
816-
if (strchr(Z_STRVAL_P(zmode), 'a')) {
817-
SetFilePointer(descriptors[ndesc].childend, 0, NULL, FILE_END);
818-
}
819-
#else
820-
descriptors[ndesc].childend = fd;
821-
#endif
822830
} else if (strcmp(Z_STRVAL_P(ztype), "redirect") == 0) {
823831
zval *ztarget = zend_hash_index_find_deref(Z_ARRVAL_P(descitem), 1);
824832
struct php_proc_open_descriptor_item *target = NULL;

0 commit comments

Comments
 (0)