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
12 changes: 12 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -580,10 +580,13 @@ AC_CHECK_FUNCS(m4_normalize([
putenv
reallocarray
scandir
sendfile
sendfilev
setenv
setitimer
shutdown
sigprocmask
splice
statfs
statvfs
std_syslog
Expand Down Expand Up @@ -1688,6 +1691,15 @@ PHP_ADD_SOURCES_X([main],
[PHP_FASTCGI_OBJS],
[no])

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

PHP_ADD_SOURCES([main/streams], m4_normalize([
cast.c
filter.c
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;
}
32 changes: 32 additions & 0 deletions main/io/php_io_bsd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
+----------------------------------------------------------------------+
| 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> |
+----------------------------------------------------------------------+
*/

#ifndef PHP_IO_BSD_H
#define PHP_IO_BSD_H

/* Copy operations */
ssize_t php_io_bsd_copy_file_to_generic(int src_fd, int dest_fd, size_t maxlen);

/* Instance initialization macros */
#define PHP_IO_PLATFORM_COPY_OPS \
{ \
.file_to_file = php_io_generic_copy_fallback, \
.file_to_generic = php_io_bsd_copy_file_to_generic, \
.generic_to_file = php_io_generic_copy_fallback, \
.generic_to_generic = php_io_generic_copy_fallback, \
}

#define PHP_IO_PLATFORM_NAME "bsd"

#endif /* PHP_IO_BSD_H */
98 changes: 98 additions & 0 deletions main/io/php_io_copy_bsd.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> |
+----------------------------------------------------------------------+
*/

#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)

#include "php_io_internal.h"
#include <unistd.h>
#include <errno.h>

#ifdef HAVE_SENDFILE
#include <sys/socket.h>
#include <sys/uio.h>
#endif

ssize_t php_io_bsd_copy_file_to_generic(int src_fd, int dest_fd, size_t maxlen)
{
#ifdef HAVE_SENDFILE
/* BSD sendfile signature: sendfile(fd, s, offset, nbytes, hdtr, sbytes, flags) */
size_t total_copied = 0;
size_t remaining = (maxlen == PHP_IO_COPY_ALL) ? SIZE_MAX : maxlen;
off_t src_offset = 0;

/* Get current source file position */
src_offset = lseek(src_fd, 0, SEEK_CUR);

if (src_offset == (off_t) -1) {
/* Can't get position, fall back to generic copy */
return php_io_generic_copy_fallback(src_fd, dest_fd, maxlen);
}

while (remaining > 0) {
off_t to_send = (remaining < OFF_MAX) ? (off_t) remaining : OFF_MAX;
off_t sbytes = 0;
int result = sendfile(src_fd, dest_fd, src_offset, to_send, NULL, &sbytes, 0);

if (result == 0 || sbytes > 0) {
/* Success or partial send */
total_copied += sbytes;
src_offset += sbytes;

if (maxlen != PHP_IO_COPY_ALL) {
remaining -= sbytes;
}

/* If result != 0, error occurred but some data was transferred */
if (result != 0) {
break;
}
} else {
/* Error occurred with no data transferred */
switch (errno) {
case EAGAIN:
case EBUSY:
case EINVAL:
case ENOTCONN:
/* Various errors */
if (total_copied == 0) {
return php_io_generic_copy_fallback(src_fd, dest_fd, maxlen);
}
/* Already copied some, return what we have */
break;
default:
/* Other errors */
if (total_copied == 0) {
return php_io_generic_copy_fallback(src_fd, dest_fd, maxlen);
}
break;
}
break;
}

/* For bounded copies, stop if we reached maxlen */
if (maxlen != PHP_IO_COPY_ALL && remaining == 0) {
break;
}
}

if (total_copied > 0) {
return (ssize_t) total_copied;
}
#endif /* HAVE_SENDFILE */

/* Fallback to generic implementation */
return php_io_generic_copy_fallback(src_fd, dest_fd, maxlen);
}

#endif /* FreeBSD, OpenBSD, NetBSD */
Loading
Loading