1: #include "first.h"
2:
3: #include "network_backends.h"
4:
5: #if defined(USE_LINUX_SENDFILE)
6:
7: #include "network.h"
8: #include "log.h"
9:
10: #include <sys/sendfile.h>
11:
12: #include <errno.h>
13: #include <string.h>
14:
15: int network_write_file_chunk_sendfile(server *srv, connection *con, int fd, chunkqueue *cq, off_t *p_max_bytes) {
16: chunk* const c = cq->first;
17: ssize_t r;
18: off_t offset;
19: off_t toSend;
20:
21: force_assert(NULL != c);
22: force_assert(FILE_CHUNK == c->type);
23: force_assert(c->offset >= 0 && c->offset <= c->file.length);
24:
25: offset = c->file.start + c->offset;
26: toSend = c->file.length - c->offset;
27: if (toSend > *p_max_bytes) toSend = *p_max_bytes;
28:
29: if (0 == toSend) {
30: chunkqueue_remove_finished_chunks(cq);
31: return 0;
32: }
33:
34: if (0 != network_open_file_chunk(srv, con, cq)) return -1;
35:
36: if (-1 == (r = sendfile(fd, c->file.fd, &offset, toSend))) {
37: switch (errno) {
38: case EAGAIN:
39: case EINTR:
40: break;
41: case EPIPE:
42: case ECONNRESET:
43: return -2;
44: case EINVAL:
45: case ENOSYS:
46: #if defined(ENOTSUP) \
47: && (!defined(EOPNOTSUPP) || EOPNOTSUPP != ENOTSUP)
48: case ENOTSUP:
49: #endif
50: #ifdef EOPNOTSUPP
51: case EOPNOTSUPP:
52: #endif
53: #ifdef ESOCKTNOSUPPORT
54: case ESOCKTNOSUPPORT:
55: #endif
56: #ifdef EAFNOSUPPORT
57: case EAFNOSUPPORT:
58: #endif
59: #ifdef USE_MMAP
60: return network_write_file_chunk_mmap(srv, con, fd, cq, p_max_bytes);
61: #else
62: return network_write_file_chunk_no_mmap(srv, con, fd, cq, p_max_bytes);
63: #endif
64: default:
65: log_error_write(srv, __FILE__, __LINE__, "ssd",
66: "sendfile failed:", strerror(errno), fd);
67: return -1;
68: }
69: }
70:
71: if (r >= 0) {
72: chunkqueue_mark_written(cq, r);
73: *p_max_bytes -= r;
74: }
75:
76: return (r > 0 && r == toSend) ? 0 : -3;
77: }
78:
79: #endif /* USE_LINUX_SENDFILE */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>