From 7361db1995bd0f733c4a562857d0d3ae0c4c2b49 Mon Sep 17 00:00:00 2001 From: Leah Neukirchen Date: Mon, 24 Jan 2022 11:35:29 +0100 Subject: write_client: when sendfile(2) fails, try pread/write. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Some filesystems don't support sendfile, either due to bugs or other reasons. Found by Érico Nogueira. --- hittpd.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/hittpd.c b/hittpd.c index 9ddd2c4..3bc6bc6 100644 --- a/hittpd.c +++ b/hittpd.c @@ -854,7 +854,21 @@ write_client(int i) ssize_t w = 0; if (data->stream_fd >= 0) { -#ifndef __linux__ +#ifdef __linux__ + w = sendfile(sockfd, data->stream_fd, + &(data->off), data->last - data->off); + if (data->off == data->last) { + finish_response(i); + return; + } else if (w == 0) { + close_connection(i); // file was truncated! + return; + } else if (w > 0) { + return; + } + + /* use default code when sendfile failed with w < 0 */ +#endif char buf[16*4096]; ssize_t n = pread(data->stream_fd, buf, sizeof buf, data->off); if (n < 0) { @@ -872,14 +886,6 @@ write_client(int i) else if (w == 0) close_connection(i); // file was truncated! } -#else - w = sendfile(sockfd, data->stream_fd, - &(data->off), data->last - data->off); - if (data->off == data->last) - finish_response(i); - else if (w == 0) - close_connection(i); // file was truncated! -#endif } else if (data->buf) { if (data->off == data->last) { finish_response(i); -- cgit 1.4.1