]> arthur.barton.de Git - netatalk.git/blob - libatalk/adouble/ad_sendfile.c
Removed "dummy" global variables. (-Wall cleanliness)
[netatalk.git] / libatalk / adouble / ad_sendfile.c
1 /*
2  * $Id: ad_sendfile.c,v 1.6 2003-02-17 01:51:07 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 #ifdef ATACC
33
34 #if defined(HAVE_SENDFILE_READ)
35 #ifdef __NR_sendfile
36 int sendfile(int fdout, int fdin, off_t *off, size_t count)
37 {
38 #if _FILE_OFFSET_BITS == 64 
39 #error sendfile
40   return syscall(__NR_sendfile64, fdout, fdin, off, count);
41 #else
42   return syscall(__NR_sendfile, fdout, fdin, off, count);
43 #endif
44 }
45 #endif
46 #endif
47 #endif
48
49 #if defined(HAVE_SENDFILE_READ) || defined(HAVE_SENDFILE_WRITE)
50 static __inline__ int ad_sendfile_init(const struct adouble *ad, 
51                                        const int eid, off_t *off,
52                                        const int end)
53 {
54   int fd;
55
56   if (end) 
57     *off = ad_size(ad, eid) - *off;
58
59   if (eid == ADEID_DFORK) {
60     fd = ad_dfileno(ad);
61   } else {
62     *off += ad_getentryoff(ad, eid);
63     fd = ad_hfileno(ad);
64   }
65
66   return fd;
67 }
68 #endif
69
70
71 /* read from adouble file and write to socket. sendfile doesn't change
72  * the file pointer position. */
73 #ifdef HAVE_SENDFILE_READ
74 ssize_t ad_readfile(const struct adouble *ad, const int eid, 
75                     const int sock, off_t off, const size_t len)
76 {
77   off_t cc;
78   int fd;
79
80   fd = ad_sendfile_init(ad, eid, &off, 0);
81 #ifdef __linux__
82   cc = sendfile(sock, fd, &off, len);
83 #endif /* __linux__ */
84
85 #ifdef BSD4_4
86   if (sendfile(fd, sock, off, len, NULL, &cc, 0) < 0)
87     return -1;
88 #endif /* BSD4_4 */
89
90   return cc;
91 }
92 #endif /* HAVE_SENDFILE_READ */
93
94 #if 0
95 #ifdef HAVE_SENDFILE_WRITE
96 /* read from a socket and write to an adouble file */
97 ssize_t ad_writefile(struct adouble *ad, const int eid, 
98                      const int sock, off_t off, const int end,
99                      const size_t len)
100 {
101 #ifdef __linux__
102   ssize_t cc;
103   int fd;
104
105   fd = ad_sendfile_init(ad, eid, &off, end);
106   if ((cc = sendfile(fd, sock, &off, len)) < 0)
107     return -1;
108
109   if ((eid != ADEID_DFORK) && (off > ad_getentrylen(ad, eid))) 
110     ad_setentrylen(ad, eid, off);
111
112   return cc;
113 #endif /* __linux__ */
114 }
115 #endif /* HAVE_SENDFILE_WRITE */
116 #endif /* 0 */