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