[core] cleanup: consolidate FAM code in stat_cache
[lighttpd.git] / src / network_darwin_sendfile.c
blob9b4ba6cf21eac27baa02733c17dbec1a394cddd4
1 #include "first.h"
3 #include "network_backends.h"
5 #if defined(USE_DARWIN_SENDFILE)
7 #include "log.h"
9 #include <sys/types.h>
10 #include <sys/socket.h>
11 #include <sys/uio.h>
13 #include <errno.h>
14 #include <string.h>
16 int network_write_file_chunk_sendfile(server *srv, connection *con, int fd, chunkqueue *cq, off_t *p_max_bytes) {
17 chunk* const c = cq->first;
18 off_t offset, written = 0;
19 off_t toSend;
20 int r;
22 force_assert(NULL != c);
23 force_assert(FILE_CHUNK == c->type);
24 force_assert(c->offset >= 0 && c->offset <= c->file.length);
26 offset = c->file.start + c->offset;
27 toSend = c->file.length - c->offset;
28 if (toSend > *p_max_bytes) toSend = *p_max_bytes;
30 if (0 == toSend) {
31 chunkqueue_remove_finished_chunks(cq);
32 return 0;
35 if (0 != chunkqueue_open_file_chunk(srv, cq)) return -1;
37 /* Darwin sendfile() */
38 written = toSend;
39 if (-1 == (r = sendfile(c->file.fd, fd, offset, &written, NULL, 0))) {
40 switch(errno) {
41 case EAGAIN:
42 case EINTR:
43 /* for EAGAIN/EINTR written still contains the sent bytes */
44 break; /* try again later */
45 case EPIPE:
46 case ENOTCONN:
47 return -2;
48 case EINVAL:
49 case ENOSYS:
50 #if defined(ENOTSUP) \
51 && (!defined(EOPNOTSUPP) || EOPNOTSUPP != ENOTSUP)
52 case ENOTSUP:
53 #endif
54 #ifdef EOPNOTSUPP
55 case EOPNOTSUPP:
56 #endif
57 #ifdef ESOCKTNOSUPPORT
58 case ESOCKTNOSUPPORT:
59 #endif
60 #ifdef EAFNOSUPPORT
61 case EAFNOSUPPORT:
62 #endif
63 #ifdef USE_MMAP
64 return network_write_file_chunk_mmap(srv, con, fd, cq, p_max_bytes);
65 #else
66 return network_write_file_chunk_no_mmap(srv, con, fd, cq, p_max_bytes);
67 #endif
68 default:
69 log_error_write(srv, __FILE__, __LINE__, "ssd", "sendfile: ", strerror(errno), errno);
70 return -1;
74 if (written >= 0) {
75 chunkqueue_mark_written(cq, written);
76 *p_max_bytes -= written;
79 return (r >= 0 && written == toSend) ? 0 : -3;
82 #endif /* USE_DARWIN_SENDFILE */