3 #include "network_backends.h"
5 #if defined(USE_DARWIN_SENDFILE)
10 #include <sys/types.h>
11 #include <sys/socket.h>
17 int network_write_file_chunk_sendfile(server
*srv
, connection
*con
, int fd
, chunkqueue
*cq
, off_t
*p_max_bytes
) {
18 chunk
* const c
= cq
->first
;
19 off_t offset
, written
= 0;
23 force_assert(NULL
!= c
);
24 force_assert(FILE_CHUNK
== c
->type
);
25 force_assert(c
->offset
>= 0 && c
->offset
<= c
->file
.length
);
27 offset
= c
->file
.start
+ c
->offset
;
28 toSend
= c
->file
.length
- c
->offset
;
29 if (toSend
> *p_max_bytes
) toSend
= *p_max_bytes
;
32 chunkqueue_remove_finished_chunks(cq
);
36 if (0 != network_open_file_chunk(srv
, con
, cq
)) return -1;
38 /* Darwin sendfile() */
40 if (-1 == (r
= sendfile(c
->file
.fd
, fd
, offset
, &written
, NULL
, 0))) {
44 /* for EAGAIN/EINTR written still contains the sent bytes */
45 break; /* try again later */
50 log_error_write(srv
, __FILE__
, __LINE__
, "ssd", "sendfile: ", strerror(errno
), errno
);
56 chunkqueue_mark_written(cq
, written
);
57 *p_max_bytes
-= written
;
60 return (r
>= 0 && written
== toSend
) ? 0 : -3;
63 #endif /* USE_DARWIN_SENDFILE */