From 0da942a14b45ff96508b0abd8c1d00fec8c233ad Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Wed, 16 Feb 2022 09:59:23 -0500 Subject: [PATCH] rework zm_sendfile to try again if not all bytes were sent. According to the docs, this is entirely possible. --- src/zm_sendfile.h | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/zm_sendfile.h b/src/zm_sendfile.h index fd72e2e61..3568e2fea 100644 --- a/src/zm_sendfile.h +++ b/src/zm_sendfile.h @@ -3,22 +3,25 @@ #ifdef HAVE_SENDFILE4_SUPPORT #include -int zm_sendfile(int out_fd, int in_fd, off_t *offset, size_t size) { - int err; +ssize_t zm_sendfile(int out_fd, int in_fd, off_t *offset, size_t size) { + size_t remaining = size; + while (remaining) { + ssize_t err = sendfile(out_fd, in_fd, offset, remaining); + if (err < 0) { + return -errno; + } + remaining -= err; + offset += err; + } - err = sendfile(out_fd, in_fd, offset, size); - if ( err < 0 ) - return -errno; - - return err; + return size-remaining; } #elif HAVE_SENDFILE7_SUPPORT #include #include #include -int zm_sendfile(int out_fd, int in_fd, off_t *offset, off_t size) { - int err; - err = sendfile(in_fd, out_fd, *offset, size, nullptr, &size, 0); +ssize_t zm_sendfile(int out_fd, int in_fd, off_t *offset, off_t size) { + ssize_t err = sendfile(in_fd, out_fd, *offset, size, nullptr, &size, 0); if (err && errno != EAGAIN) return -errno;