Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -580,10 +580,12 @@ AC_CHECK_FUNCS(m4_normalize([
putenv
reallocarray
scandir
sendfile
setenv
setitimer
shutdown
sigprocmask
splice
statfs
statvfs
std_syslog
Expand Down Expand Up @@ -1688,6 +1690,12 @@ PHP_ADD_SOURCES_X([main],
[PHP_FASTCGI_OBJS],
[no])

PHP_ADD_SOURCES([main/io], m4_normalize([
php_io.c
php_io_copy_linux.c
]),
[-DZEND_ENABLE_STATIC_TSRMLS_CACHE=1])

PHP_ADD_SOURCES([main/streams], m4_normalize([
cast.c
filter.c
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
--TEST--
stream_copy_to_stream() 16k with file as $source and socket as $dest
--SKIPIF--
<?php
$sockets = @stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, 0);
if (!$sockets) die("skip stream_socket_pair");
?>
--FILE--
<?php

$sockets = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, 0);
$src = tmpfile();

$data = str_repeat('data', 4096);
fwrite($src, $data);
rewind($src);

$copied = stream_copy_to_stream($src, $sockets[0]);
var_dump($copied);

stream_socket_shutdown($sockets[0], STREAM_SHUT_WR);

$result = stream_get_contents($sockets[1]);
var_dump(strlen($result));
var_dump($result === $data);

fclose($src);
fclose($sockets[0]);
fclose($sockets[1]);

?>
--EXPECT--
int(16384)
int(16384)
bool(true)
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
--TEST--
stream_copy_to_stream() 200k bytes with socket as $source and file as $dest
--SKIPIF--
<?php
$sockets = @stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, 0);
if (!$sockets) die("skip stream_socket_pair");
?>
--FILE--
<?php

$sockets = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, 0);
$tmp = tmpfile();

fwrite($sockets[0], str_repeat("a", 200000));
stream_socket_shutdown($sockets[0], STREAM_SHUT_WR);
stream_copy_to_stream($sockets[1], $tmp);

fseek($tmp, 0, SEEK_SET);
var_dump(strlen(stream_get_contents($tmp)));


?>
--EXPECT--
int(200000)
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
--TEST--
stream_copy_to_stream() 2048 bytes with socket as $source and file as $dest
--SKIPIF--
<?php
$sockets = @stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, 0);
if (!$sockets) die("skip stream_socket_pair");
?>
--FILE--
<?php

$sockets = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, 0);
$tmp = tmpfile();

fwrite($sockets[0], str_repeat("a", 2048));
stream_socket_shutdown($sockets[0], STREAM_SHUT_WR);
stream_copy_to_stream($sockets[1], $tmp);

fseek($tmp, 0, SEEK_SET);
var_dump(stream_get_contents($tmp));


?>
--EXPECTF--
string(2048) "aaaaa%saaa"

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--TEST--
stream_copy_to_stream() with socket as $source
stream_copy_to_stream() single byte with socket as $source and file as $dest
--SKIPIF--
<?php
$sockets = @stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, 0);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
--TEST--
stream_copy_to_stream() socket to socket (splice both directions)
--SKIPIF--
<?php
$sockets = @stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, 0);
if (!$sockets) die("skip stream_socket_pair not available");
?>
--FILE--
<?php

$sockets1 = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, 0);
$sockets2 = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, 0);

$data = str_repeat('test data ', 1000);
fwrite($sockets1[0], $data);
stream_socket_shutdown($sockets1[0], STREAM_SHUT_WR);

$copied = stream_copy_to_stream($sockets1[1], $sockets2[0]);
var_dump($copied);

stream_socket_shutdown($sockets2[0], STREAM_SHUT_WR);

$result = stream_get_contents($sockets2[1]);
var_dump(strlen($result));
var_dump($result === $data);

fclose($sockets1[0]);
fclose($sockets1[1]);
fclose($sockets2[0]);
fclose($sockets2[1]);

?>
--EXPECT--
int(10000)
int(10000)
bool(true)
2 changes: 1 addition & 1 deletion ext/zend_test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1820,7 +1820,7 @@ typedef off_t off64_t;
PHP_ZEND_TEST_API ssize_t copy_file_range(int fd_in, off64_t *off_in, int fd_out, off64_t *off_out, size_t len, unsigned int flags)
{
ssize_t (*original_copy_file_range)(int, off64_t *, int, off64_t *, size_t, unsigned int) = dlsym(RTLD_NEXT, "copy_file_range");
if (ZT_G(limit_copy_file_range) >= Z_L(0)) {
if (ZT_G(limit_copy_file_range) >= Z_L(0) && ZT_G(limit_copy_file_range) < len) {
len = ZT_G(limit_copy_file_range);
}
return original_copy_file_range(fd_in, off_in, fd_out, off_out, len, flags);
Expand Down
98 changes: 98 additions & 0 deletions main/io/php_io.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
+----------------------------------------------------------------------+
| Copyright © The PHP Group and Contributors. |
+----------------------------------------------------------------------+
| This source file is subject to the Modified BSD License that is |
| bundled with this package in the file LICENSE, and is available |
| through the World Wide Web at <https://www.php.net/license/>. |
| |
| SPDX-License-Identifier: BSD-3-Clause |
+----------------------------------------------------------------------+
| Authors: Jakub Zelenka <bukka@php.net> |
+----------------------------------------------------------------------+
*/

#include "php.h"
#include "php_io.h"
#include "php_io_internal.h"
#include <sys/stat.h>

#ifdef PHP_WIN32
#include <io.h>
#include <winsock2.h>
#else
#include <unistd.h>
#endif

/* Global instance - initialized at compile time */
static php_io php_io_instance = {
.copy = PHP_IO_PLATFORM_COPY_OPS,
.platform_name = PHP_IO_PLATFORM_NAME,
};

/* Get global instance */
PHPAPI php_io *php_io_get(void)
{
return &php_io_instance;
}

/* High-level copy function with dispatch */
PHPAPI ssize_t php_io_copy(
int src_fd, php_io_fd_type src_type, int dest_fd, php_io_fd_type dest_type, size_t maxlen)
{
php_io *io = php_io_get();

/* Dispatch to appropriate copy function based on fd types */
if (src_type == PHP_IO_FD_FILE && dest_type == PHP_IO_FD_FILE) {
return io->copy.file_to_file(src_fd, dest_fd, maxlen);
} else if (src_type == PHP_IO_FD_FILE && dest_type == PHP_IO_FD_GENERIC) {
return io->copy.file_to_generic(src_fd, dest_fd, maxlen);
} else if (src_type == PHP_IO_FD_GENERIC && dest_type == PHP_IO_FD_FILE) {
return io->copy.generic_to_file(src_fd, dest_fd, maxlen);
} else {
/* generic to generic */
return io->copy.generic_to_generic(src_fd, dest_fd, maxlen);
}
}

/* Generic read/write fallback implementation */
ssize_t php_io_generic_copy_fallback(int src_fd, int dest_fd, size_t maxlen)
{
char buf[8192];
size_t total_copied = 0;
size_t remaining = (maxlen == PHP_IO_COPY_ALL) ? SIZE_MAX : maxlen;

while (remaining > 0) {
size_t to_read = (remaining < sizeof(buf)) ? remaining : sizeof(buf);
ssize_t bytes_read = read(src_fd, buf, to_read);

if (bytes_read < 0) {
/* Read error */
return total_copied > 0 ? (ssize_t) total_copied : -1;
} else if (bytes_read == 0) {
/* EOF reached */
return (ssize_t) total_copied;
}

ssize_t bytes_written = write(dest_fd, buf, bytes_read);
if (bytes_written < 0) {
/* Write error */
return total_copied > 0 ? (ssize_t) total_copied : -1;
} else if (bytes_written == 0) {
/* Couldn't write anything */
return total_copied > 0 ? (ssize_t) total_copied : -1;
}

total_copied += bytes_written;
if (maxlen != PHP_IO_COPY_ALL) {
remaining -= bytes_written;
}

if (bytes_written != bytes_read) {
/* Partial write - stop here */
return (ssize_t) total_copied;
}
}

return (ssize_t) total_copied;
}
Loading
Loading