2005-02-20 14:27:00 +00:00
|
|
|
#include "network_backends.h"
|
|
|
|
|
2015-08-22 16:00:59 +00:00
|
|
|
#if defined(USE_LINUX_SENDFILE)
|
2009-10-11 14:31:42 +00:00
|
|
|
|
|
|
|
#include "network.h"
|
|
|
|
#include "log.h"
|
|
|
|
|
2015-08-22 16:00:59 +00:00
|
|
|
#include <sys/sendfile.h>
|
2005-02-20 14:27:00 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-08-22 16:00:59 +00:00
|
|
|
int network_write_file_chunk_sendfile(server *srv, connection *con, int fd, chunkqueue *cq, off_t *p_max_bytes) {
|
|
|
|
chunk* const c = cq->first;
|
|
|
|
ssize_t r;
|
|
|
|
off_t offset;
|
|
|
|
off_t toSend;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-08-22 16:00:59 +00:00
|
|
|
force_assert(NULL != c);
|
|
|
|
force_assert(FILE_CHUNK == c->type);
|
|
|
|
force_assert(c->offset >= 0 && c->offset <= c->file.length);
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-08-22 16:00:59 +00:00
|
|
|
offset = c->file.start + c->offset;
|
|
|
|
toSend = c->file.length - c->offset;
|
|
|
|
if (toSend > *p_max_bytes) toSend = *p_max_bytes;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-08-22 16:00:59 +00:00
|
|
|
if (0 == toSend) {
|
|
|
|
chunkqueue_remove_finished_chunks(cq);
|
|
|
|
return 0;
|
|
|
|
}
|
2005-09-26 08:52:37 +00:00
|
|
|
|
2015-08-22 16:00:59 +00:00
|
|
|
if (0 != network_open_file_chunk(srv, con, cq)) return -1;
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-08-22 16:00:59 +00:00
|
|
|
if (-1 == (r = sendfile(fd, c->file.fd, &offset, toSend))) {
|
|
|
|
switch (errno) {
|
|
|
|
case EAGAIN:
|
|
|
|
case EINTR:
|
2005-02-20 14:27:00 +00:00
|
|
|
break;
|
2015-08-22 16:00:59 +00:00
|
|
|
case EPIPE:
|
|
|
|
case ECONNRESET:
|
|
|
|
return -2;
|
2005-02-20 14:27:00 +00:00
|
|
|
default:
|
2015-08-22 16:00:59 +00:00
|
|
|
log_error_write(srv, __FILE__, __LINE__, "ssd",
|
|
|
|
"sendfile failed:", strerror(errno), fd);
|
2005-02-20 14:27:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2015-08-22 16:00:59 +00:00
|
|
|
}
|
2006-10-04 13:26:23 +00:00
|
|
|
|
2015-08-22 16:00:59 +00:00
|
|
|
if (r >= 0) {
|
|
|
|
chunkqueue_mark_written(cq, r);
|
|
|
|
*p_max_bytes -= r;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2015-08-22 16:00:59 +00:00
|
|
|
return (r > 0 && r == toSend) ? 0 : -3;
|
2005-02-20 14:27:00 +00:00
|
|
|
}
|
|
|
|
|
2015-08-22 16:00:59 +00:00
|
|
|
#endif /* USE_LINUX_SENDFILE */
|