improve do_splice()

This commit is contained in:
Kent Overstreet 2024-01-03 20:30:35 -05:00
parent 378ae738d5
commit 3054e5debb

View File

@ -35,7 +35,7 @@ static void setnonblocking(int fd)
static int do_splice(int rfd, int wfd) static int do_splice(int rfd, int wfd)
{ {
char buf[4096]; char buf[4096], *b = buf;
int r = read(rfd, buf, sizeof(buf)); int r = read(rfd, buf, sizeof(buf));
if (r < 0 && errno == EAGAIN) if (r < 0 && errno == EAGAIN)
@ -44,8 +44,13 @@ static int do_splice(int rfd, int wfd)
return r; return r;
if (!r) if (!r)
return 1; return 1;
if (write(wfd, buf, r) != r) do {
die("write error"); ssize_t w = write(wfd, b, r);
if (w < 0)
die("%s: write error: %m", __func__);
r -= w;
b += w;
} while (r);
return 0; return 0;
} }