]> arthur.barton.de Git - netatalk.git/blob - libatalk/adouble/ad_sendfile.c
merged logging code into main branch. use configure option --without-logfile to...
[netatalk.git] / libatalk / adouble / ad_sendfile.c
1 /*
2  * $Id: ad_sendfile.c,v 1.4 2002-01-04 04:45:48 sibaz 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 static int _ad_sendfile_dummy;
33
34 #if defined(HAVE_SENDFILE_READ) || defined(HAVE_SENDFILE_WRITE)
35 static __inline__ int ad_sendfile_init(const struct adouble *ad, 
36                                        const int eid, off_t *off,
37                                        const int end)
38 {
39   int fd;
40
41   if (end) 
42     *off = ad_size(ad, eid) - *off;
43
44   if (eid == ADEID_DFORK) {
45     fd = ad_dfileno(ad);
46   } else {
47     *off += ad_getentryoff(ad, eid);
48     fd = ad_hfileno(ad);
49   }
50
51   return fd;
52 }
53 #endif
54
55
56 /* read from adouble file and write to socket. sendfile doesn't change
57  * the file pointer position. */
58 #ifdef HAVE_SENDFILE_READ
59 ssize_t ad_readfile(const struct adouble *ad, const int eid, 
60                     const int sock, off_t off, const size_t len)
61 {
62   off_t cc;
63   int fd;
64
65   fd = ad_sendfile_init(ad, eid, &off, 0);
66 #ifdef __linux__
67   cc = sendfile(sock, fd, &off, len);
68 #endif /* __linux__ */
69
70 #ifdef BSD4_4
71   if (sendfile(fd, sock, off, len, NULL, &cc, 0) < 0)
72     return -1;
73 #endif /* BSD4_4 */
74
75   return cc;
76 }
77 #endif /* HAVE_SENDFILE_READ */
78
79 #if 0
80 #ifdef HAVE_SENDFILE_WRITE
81 /* read from a socket and write to an adouble file */
82 ssize_t ad_writefile(struct adouble *ad, const int eid, 
83                      const int sock, off_t off, const int end,
84                      const size_t len)
85 {
86 #ifdef __linux__
87   ssize_t cc;
88   int fd;
89
90   fd = ad_sendfile_init(ad, eid, &off, end);
91   if ((cc = sendfile(fd, sock, &off, len)) < 0)
92     return -1;
93
94   if ((eid != ADEID_DFORK) && (off > ad_getentrylen(ad, eid))) 
95     ad_setentrylen(ad, eid, off);
96
97   return cc;
98 #endif /* __linux__ */
99 }
100 #endif /* HAVE_SENDFILE_WRITE */
101 #endif /* 0 */