|
| 1 | +#ifdef __FreeBSD__ |
| 2 | + |
| 3 | + #include "php_io_internal.h" |
| 4 | + #include <sys/types.h> |
| 5 | + #include <sys/socket.h> |
| 6 | + #include <sys/uio.h> |
| 7 | + #include <unistd.h> |
| 8 | + #include <errno.h> |
| 9 | + |
| 10 | + static ssize_t php_io_freebsd_sendfile(int src_fd, int dest_fd, size_t maxlen) |
| 11 | + { |
| 12 | + off_t total_sent = 0; |
| 13 | + size_t remaining = (maxlen == PHP_IO_COPY_ALL) ? 0 : maxlen; |
| 14 | + |
| 15 | + while (maxlen == PHP_IO_COPY_ALL || remaining > 0) { |
| 16 | + off_t sent_in_this_call = 0; |
| 17 | + int result = sendfile(src_fd, dest_fd, total_sent, remaining, NULL, &sent_in_this_call, 0); |
| 18 | + |
| 19 | + if (sent_in_this_call > 0) { |
| 20 | + total_sent += sent_in_this_call; |
| 21 | + if (maxlen != PHP_IO_COPY_ALL) { |
| 22 | + remaining -= (size_t)sent_in_this_call; |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + if (result == 0) { |
| 27 | + if (sent_in_this_call == 0 || remaining == 0) { |
| 28 | + break; |
| 29 | + } |
| 30 | + } else { |
| 31 | + if (errno == EAGAIN || errno == EINTR) { |
| 32 | + if (sent_in_this_call > 0) { |
| 33 | + continue; |
| 34 | + } |
| 35 | + break; |
| 36 | + } |
| 37 | + if (total_sent == 0) { |
| 38 | + return php_io_generic_copy_fallback(src_fd, dest_fd, maxlen); |
| 39 | + } |
| 40 | + break; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + return (total_sent > 0) ? (ssize_t)total_sent : -1; |
| 45 | + } |
| 46 | + |
| 47 | + ssize_t php_io_freebsd_copy(php_io_fd *src, php_io_fd *dest, size_t maxlen) |
| 48 | + { |
| 49 | + /* unlike linux, sendfile on freebsd works only under those conditions */ |
| 50 | + if (src->fd_type == PHP_IO_FD_FILE && dest->fd_type == PHP_IO_FD_SOCKET) { |
| 51 | + return php_io_freebsd_sendfile(src->fd, dest->fd, maxlen); |
| 52 | + } |
| 53 | + |
| 54 | + return php_io_generic_copy_fallback(src->fd, dest->fd, maxlen); |
| 55 | + } |
| 56 | + |
| 57 | + #endif |
| 58 | + |
0 commit comments