From 588ee5223e12296c7636fc3ebb12c899d486d0c1 Mon Sep 17 00:00:00 2001 From: Brian Candler Date: Mon, 22 Dec 2014 10:38:59 +0000 Subject: [PATCH] Fix calculation of sendfile throughput on OSX Note that sendfile can return -1 for EINTR whilst having already sent partial results; these were not being counted previously. --- src/net.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/net.c b/src/net.c index cc5a7c1..2edd041 100644 --- a/src/net.c +++ b/src/net.c @@ -302,23 +302,19 @@ Nsendfile(int fromfd, int tofd, const char *buf, size_t count) offset = count - nleft; #ifdef linux r = sendfile(tofd, fromfd, &offset, nleft); -#else -#ifdef __FreeBSD__ + if (r > 0) + nleft -= r; +#elif defined(__FreeBSD__) r = sendfile(fromfd, tofd, offset, nleft, NULL, &sent, 0); - if (r == 0) - r = sent; -#else -#if defined(__APPLE__) && defined(__MACH__) && defined(MAC_OS_X_VERSION_10_6) /* OS X */ + nleft -= sent; +#elif defined(__APPLE__) && defined(__MACH__) && defined(MAC_OS_X_VERSION_10_6) /* OS X */ sent = nleft; r = sendfile(fromfd, tofd, offset, &sent, NULL, 0); - if (r == 0) - r = sent; + nleft -= sent; #else /* Shouldn't happen. */ r = -1; errno = ENOSYS; -#endif -#endif #endif if (r < 0) { switch (errno) { @@ -333,14 +329,16 @@ Nsendfile(int fromfd, int tofd, const char *buf, size_t count) default: return NET_HARDERROR; } - } else if (r == 0) + } +#ifdef linux + else if (r == 0) return NET_SOFTERROR; - nleft -= r; +#endif } return count; #else /* HAVE_SENDFILE */ errno = ENOSYS; /* error if somehow get called without HAVE_SENDFILE */ - return -1; + return NET_HARDERROR; #endif /* HAVE_SENDFILE */ }