3 #include "network_backends.h"
5 #if defined(USE_LINUX_SENDFILE)
9 #include <sys/sendfile.h>
14 int network_write_file_chunk_sendfile(server
*srv
, connection
*con
, int fd
, chunkqueue
*cq
, off_t
*p_max_bytes
) {
15 chunk
* const c
= cq
->first
;
20 force_assert(NULL
!= c
);
21 force_assert(FILE_CHUNK
== c
->type
);
22 force_assert(c
->offset
>= 0 && c
->offset
<= c
->file
.length
);
24 offset
= c
->file
.start
+ c
->offset
;
25 toSend
= c
->file
.length
- c
->offset
;
26 if (toSend
> *p_max_bytes
) toSend
= *p_max_bytes
;
29 chunkqueue_remove_finished_chunks(cq
);
33 if (0 != chunkqueue_open_file_chunk(srv
, cq
)) return -1;
35 if (-1 == (r
= sendfile(fd
, c
->file
.fd
, &offset
, toSend
))) {
45 #if defined(ENOTSUP) \
46 && (!defined(EOPNOTSUPP) || EOPNOTSUPP != ENOTSUP)
52 #ifdef ESOCKTNOSUPPORT
59 return network_write_file_chunk_mmap(srv
, con
, fd
, cq
, p_max_bytes
);
61 return network_write_file_chunk_no_mmap(srv
, con
, fd
, cq
, p_max_bytes
);
64 log_error_write(srv
, __FILE__
, __LINE__
, "ssd",
65 "sendfile failed:", strerror(errno
), fd
);
71 chunkqueue_mark_written(cq
, r
);
75 return (r
> 0 && r
== toSend
) ? 0 : -3;
78 #endif /* USE_LINUX_SENDFILE */