3 #include "network_backends.h"
5 #if defined(USE_LINUX_SENDFILE)
10 #include <sys/sendfile.h>
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
;
21 force_assert(NULL
!= c
);
22 force_assert(FILE_CHUNK
== c
->type
);
23 force_assert(c
->offset
>= 0 && c
->offset
<= c
->file
.length
);
25 offset
= c
->file
.start
+ c
->offset
;
26 toSend
= c
->file
.length
- c
->offset
;
27 if (toSend
> *p_max_bytes
) toSend
= *p_max_bytes
;
30 chunkqueue_remove_finished_chunks(cq
);
34 if (0 != network_open_file_chunk(srv
, con
, cq
)) return -1;
36 if (-1 == (r
= sendfile(fd
, c
->file
.fd
, &offset
, toSend
))) {
46 #if defined(ENOTSUP) \
47 && (!defined(EOPNOTSUPP) || EOPNOTSUPP != ENOTSUP)
53 #ifdef ESOCKTNOSUPPORT
60 return network_write_file_chunk_mmap(srv
, con
, fd
, cq
, p_max_bytes
);
62 return network_write_file_chunk_no_mmap(srv
, con
, fd
, cq
, p_max_bytes
);
65 log_error_write(srv
, __FILE__
, __LINE__
, "ssd",
66 "sendfile failed:", strerror(errno
), fd
);
72 chunkqueue_mark_written(cq
, r
);
76 return (r
> 0 && r
== toSend
) ? 0 : -3;
79 #endif /* USE_LINUX_SENDFILE */