]> arthur.barton.de Git - netatalk.git/blob - libatalk/adouble/ad_sendfile.c
Initial revision
[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 #include <stdio.h>
12 #include <unistd.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <sys/socket.h>
16 #include <sys/uio.h>
17
18 #include <atalk/adouble.h>
19
20 #include <syslog.h>
21
22 #include "ad_private.h"
23
24 static int _ad_sendfile_dummy;
25
26 #if defined(HAVE_SENDFILE_READ) || defined(HAVE_SENDFILE_WRITE)
27 static __inline__ int ad_sendfile_init(const struct adouble *ad, 
28                                        const int eid, off_t *off,
29                                        const int end)
30 {
31   int fd;
32
33   if (end) 
34     *off = ad_size(ad, eid) - *off;
35
36   if (eid == ADEID_DFORK) {
37     fd = ad_dfileno(ad);
38   } else {
39     *off += ad_getentryoff(ad, eid);
40     fd = ad_hfileno(ad);
41   }
42
43   return fd;
44 }
45 #endif
46
47
48 /* read from adouble file and write to socket. sendfile doesn't change
49  * the file pointer position. */
50 #ifdef HAVE_SENDFILE_READ
51 ssize_t ad_readfile(const struct adouble *ad, const int eid, 
52                     const int sock, off_t off, const size_t len)
53 {
54   off_t cc;
55   int fd;
56
57   fd = ad_sendfile_init(ad, eid, &off, 0);
58 #ifdef __linux__
59   cc = sendfile(sock, fd, &off, len);
60 #endif
61
62 #ifdef BSD4_4
63   if (sendfile(fd, sock, off, len, NULL, &cc, 0) < 0)
64     return -1;
65 #endif
66
67   return cc;
68 }
69 #endif
70
71 #if 0
72 #ifdef HAVE_SENDFILE_WRITE
73 /* read from a socket and write to an adouble file */
74 ssize_t ad_writefile(struct adouble *ad, const int eid, 
75                      const int sock, off_t off, const int end,
76                      const size_t len)
77 {
78 #ifdef __linux__
79   ssize_t cc;
80   int fd;
81
82   fd = ad_sendfile_init(ad, eid, &off, end);
83   if ((cc = sendfile(fd, sock, &off, len)) < 0)
84     return -1;
85
86   if ((eid != ADEID_DFORK) && (off > ad_getentrylen(ad, eid))) 
87     ad_setentrylen(ad, eid, off);
88
89   return cc;
90 #endif
91 }
92 #endif
93 #endif