Files
Jakub ZelenkaandDavid Carlier 32a0f3b6cf IO copy API for stream copying (#20399)
This introduces new API for fd copying and modifies
php_stream_copy_to_stream_ex to use it. The implementation is separated
for various platforms and the end result have couple of implications:

- sendfile is used for copying file to generic fd (e.g. sockets) on all
  platforms except Windows that use TransmitFile
- splice is used for copying between generic fds (e.g. sockets) on
  Linux
- copy_file_range should get used on alpine linux with directly using
  syscall (as musl does not seem to implement it)
- copy_file_range is used in the loop so it is used multiple times for
  files bigger than 2GB on Linux.
- file mmap for copying is removed as it allowed crashing PHP when
  another process modified mapped file - this was used as a fallback
  for file copying. Sendfile should partially replace it.
- File to file copying was optimized on Windows with use of ReadFile
  and WriteFile.

This also adds various tests including Linux unit tests.

Closes GH-20399

Co-authored-by: David Carlier <devnexen@gmail.com>
2026-07-19 17:15:16 +02:00

54 lines
1.8 KiB
C

/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Jakub Zelenka <bukka@php.net> |
+----------------------------------------------------------------------+
*/
#ifndef PHP_IO_H
#define PHP_IO_H
#include "php.h"
#include "php_network.h"
#define PHP_IO_COPY_ALL SIZE_MAX
typedef enum php_io_fd_type {
PHP_IO_FD_FILE = 1,
PHP_IO_FD_SOCKET,
PHP_IO_FD_PIPE,
} php_io_fd_type;
typedef struct php_io_fd {
union {
int fd;
php_socket_t socket;
};
php_io_fd_type fd_type;
struct timeval timeout;
unsigned is_blocked:1;
} php_io_fd;
typedef zend_result (*php_io_copy_fn)(php_io_fd *src, php_io_fd *dest, size_t maxlen, size_t *copied);
typedef struct php_io {
php_io_copy_fn copy;
const char *platform_name;
} php_io;
PHPAPI php_io *php_io_get(void);
/* Copies up to maxlen bytes from src to dest; *copied is set even on FAILURE */
PHPAPI zend_result php_io_copy(php_io_fd *src, php_io_fd *dest, size_t maxlen, size_t *copied);
#endif /* PHP_IO_H */