From 5d90b7ceb5e0bd2fd41ded8ec706b04cacfc647c Mon Sep 17 00:00:00 2001 From: Nate Straz Date: Thu, 16 Nov 2006 02:39:48 +0000 Subject: Add fallback to read/write when sendfile doesn't work. --- qacp.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'qacp.c') diff --git a/qacp.c b/qacp.c index 3d90c91..c9d76d7 100644 --- a/qacp.c +++ b/qacp.c @@ -129,6 +129,8 @@ qacp_sendonefile(const char *host, const char *srcfile, const char *destfile) ssize_t nbytes; off_t offset = 0; struct stat sb; + char *fallbackbuf; + int bufsize; if ((fd = open(srcfile, O_RDONLY)) <0) { @@ -160,6 +162,35 @@ qacp_sendonefile(const char *host, const char *srcfile, const char *destfile) nbytes = sendfile(outsd, fd, &offset, sb.st_size); + if (nbytes == -1 && errno == EINVAL) { + fprintf(stderr, "Falling back to reads\n"); + + if (sb.st_size > 1024000) { + bufsize = 8192; + } else { + bufsize = 1024000; + } + fallbackbuf = malloc(bufsize); + if (fallbackbuf == NULL) { + fprintf(stderr, "Could not allocate transfer buffer\n"); + goto sendone_failure; + } + + do { + nbytes = read(fd, fallbackbuf, bufsize); + if (nbytes < 0) { + fprintf(stderr, "read() error: %s\n", strerror(errno)); + goto sendone_failure; + } else if (nbytes == 0) { /* EOF */ + break; + } + write(outsd, fallbackbuf, nbytes); + } while (nbytes > 0); + } else { + fprintf(stderr, "error: %s\n", strerror(errno)); + } +sendone_failure: + close(sd); close(outsd); close(fd); -- cgit