]> arthur.barton.de Git - netatalk.git/blob - libatalk/adouble/ad_sendfile.c
Implement sendfile() as macro.
[netatalk.git] / libatalk / adouble / ad_sendfile.c
1 /*
2  * $Id: ad_sendfile.c,v 1.7 2003-06-06 20:43:14 srittau Exp $
3  *
4  * Copyright (c) 1999 Adrian Sun (asun@zoology.washington.edu)
5  * All rights reserved. See COPYRIGHT.
6  *
7  * NOTE: the following uses the fact that sendfile() only exists on
8  * machines with SA_RESTART behaviour. this is all very machine specific. 
9  *
10  * 
11  */
12
13 #ifdef HAVE_CONFIG_H
14 #include "config.h"
15 #endif /* HAVE_CONFIG_H */
16
17 #include <stdio.h>
18 #ifdef HAVE_UNISTD_H
19 #include <unistd.h>
20 #endif /* HAVE_UNISTD_H */
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <sys/socket.h>
24 #include <sys/uio.h>
25
26 #include <atalk/adouble.h>
27
28 #include <atalk/logger.h>
29
30 #include "ad_private.h"
31
32 #if defined(HAVE_SENDFILE_READ) || defined(HAVE_SENDFILE_WRITE)
33 static __inline__ int ad_sendfile_init(const struct adouble *ad, 
34                                        const int eid, off_t *off,
35                                        const int end)
36 {
37   int fd;
38
39   if (end) 
40     *off = ad_size(ad, eid) - *off;
41
42   if (eid == ADEID_DFORK) {
43     fd = ad_dfileno(ad);
44   } else {
45     *off += ad_getentryoff(ad, eid);
46     fd = ad_hfileno(ad);
47   }
48
49   return fd;
50 }
51 #endif
52
53
54 /* read from adouble file and write to socket. sendfile doesn't change
55  * the file pointer position. */
56 #ifdef HAVE_SENDFILE_READ
57 ssize_t ad_readfile(const struct adouble *ad, const int eid, 
58                     const int sock, off_t off, const size_t len)
59 {
60   off_t cc;
61   int fd;
62
63   fd = ad_sendfile_init(ad, eid, &off, 0);
64 #ifdef __linux__
65   cc = sendfile(sock, fd, &off, len);
66 #endif /* __linux__ */
67
68 #ifdef BSD4_4
69   if (sendfile(fd, sock, off, len, NULL, &cc, 0) < 0)
70     return -1;
71 #endif /* BSD4_4 */
72
73   return cc;
74 }
75 #endif /* HAVE_SENDFILE_READ */
76
77 #if 0
78 #ifdef HAVE_SENDFILE_WRITE
79 /* read from a socket and write to an adouble file */
80 ssize_t ad_writefile(struct adouble *ad, const int eid, 
81                      const int sock, off_t off, const int end,
82                      const size_t len)
83 {
84 #ifdef __linux__
85   ssize_t cc;
86   int fd;
87
88   fd = ad_sendfile_init(ad, eid, &off, end);
89   if ((cc = sendfile(fd, sock, &off, len)) < 0)
90     return -1;
91
92   if ((eid != ADEID_DFORK) && (off > ad_getentrylen(ad, eid))) 
93     ad_setentrylen(ad, eid, off);
94
95   return cc;
96 #endif /* __linux__ */
97 }
98 #endif /* HAVE_SENDFILE_WRITE */
99 #endif /* 0 */