]> arthur.barton.de Git - netatalk.git/blob - libatalk/adouble/ad_sendfile.c
remove gcc warnings and cleanup inline mess
[netatalk.git] / libatalk / adouble / ad_sendfile.c
1 /*
2  * $Id: ad_sendfile.c,v 1.6.6.2.2.1 2008-11-25 15:16:34 didg 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  * sendfile chainsaw from samba.
11  Unix SMB/Netbios implementation.
12  Version 2.2.x / 3.0.x
13  sendfile implementations.
14  Copyright (C) Jeremy Allison 2002.
15  
16  This program is free software; you can redistribute it and/or modify
17  it under the terms of the GNU General Public License as published by
18  the Free Software Foundation; either version 2 of the License, or
19  (at your option) any later version.
20  This program is distributed in the hope that it will be useful,
21  but WITHOUT ANY WARRANTY; without even the implied warranty of
22  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  GNU General Public License for more details.
24  
25  You should have received a copy of the GNU General Public License
26  along with this program; if not, write to the Free Software
27  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28  */
29
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif /* HAVE_CONFIG_H */
33
34 #ifdef WITH_SENDFILE
35
36 #include <atalk/adouble.h>
37
38 #include <stdio.h>
39
40 #include <sys/socket.h>
41 #include <sys/uio.h>
42
43 #include <errno.h>  
44
45 #include <atalk/logger.h>
46
47 #include "ad_private.h"
48
49 #if defined(LINUX_BROKEN_SENDFILE_API)
50
51 extern int32_t sendfile (int fdout, int fdin, int32_t *offset, u_int32_t count);
52
53 ssize_t sys_sendfile(int tofd, int fromfd, off_t *offset, size_t count)
54 {
55 u_int32_t small_total = 0;
56 int32_t small_offset;
57
58     /*
59      * Fix for broken Linux 2.4 systems with no working sendfile64().
60      * If the offset+count > 2 GB then pretend we don't have the
61      * system call sendfile at all. The upper layer catches this
62      * and uses a normal read. JRA.
63      */
64  
65      if ((sizeof(off_t) >= 8) && (*offset + count > (off_t)0x7FFFFFFF)) {
66          errno = ENOSYS;
67          return -1;
68      }
69      small_total = (u_int32_t)count;
70      small_offset = (int32_t)*offset;
71  
72      while (small_total) {
73          int32_t nwritten;
74          do {
75              nwritten = sendfile(tofd, fromfd, &small_offset, small_total);
76          } while (nwritten == -1 && errno == EINTR);
77          if (nwritten == -1)
78              return -1;
79          if (nwritten == 0)
80              return -1; /* I think we're at EOF here... */
81          small_total -= nwritten;
82     }
83     *offset += count;
84     return count;
85 }
86
87 #elif defined(SENDFILE_FLAVOR_LINUX)
88 #include <sys/sendfile.h>
89
90 ssize_t sys_sendfile(int tofd, int fromfd, off_t *offset, size_t count)
91 {
92 size_t total=0;
93
94     total = count;
95     while (total) {
96         ssize_t nwritten;
97         do {
98             nwritten = sendfile(tofd, fromfd, offset, total);
99         } while (nwritten == -1 && errno == EINTR);
100         if (nwritten == -1)
101             return -1;
102         if (nwritten == 0)
103             return -1; /* I think we're at EOF here... */
104         total -= nwritten;
105     }
106     return count;
107 }
108
109
110 #elif defined(SENDFILE_FLAVOR_BSD )
111 /* FIXME untested */
112 #include <sys/sendfile.h>
113 ssize_t sys_sendfile(int tofd, int fromfd, off_t *offset, size_t count)
114 {
115 size_t total=0;
116 int    ret;
117
118     total = count;
119     while (total) {
120         ssize_t nwritten;
121         do {
122            ret = sendfile(fromfd, tofd, offset, count, NULL, &nwritten, 0);
123         while (ret == -1 && errno == EINTR);
124         if (ret == -1)
125             return -1;
126         total -= nwritten;
127         offset += nwritten;
128     }
129     return count;
130 }
131
132 #else
133
134 ssize_t sys_sendfile(int out_fd, int in_fd, off_t *_offset, size_t count)
135 {
136     /* No sendfile syscall. */
137     errno = ENOSYS;
138     return -1;
139 }
140 #endif
141
142 /* ------------------------------- */
143 static int ad_sendfile_init(const struct adouble *ad, 
144                                        const int eid, off_t *off,
145                                        const int end)
146 {
147   int fd;
148
149   if (end) 
150     *off = ad_size(ad, eid) - *off;
151
152   if (eid == ADEID_DFORK) {
153     fd = ad_dfileno(ad);
154   } else {
155     *off += ad_getentryoff(ad, eid);
156     fd = ad_hfileno(ad);
157   }
158
159   return fd;
160 }
161
162
163 /* --------------------------------
164  * read from adouble file and write to socket. sendfile doesn't change
165  * the file pointer position. */
166 ssize_t ad_readfile(const struct adouble *ad, const int eid, 
167                     const int sock, off_t off, const size_t len)
168 {
169   off_t cc;
170   int fd;
171
172   fd = ad_sendfile_init(ad, eid, &off, 0);
173   cc = sys_sendfile(sock, fd, &off, len);
174   return cc;
175 }
176
177 /* ------------------------ */
178 #if 0
179 #ifdef HAVE_SENDFILE_WRITE
180 /* read from a socket and write to an adouble file */
181 ssize_t ad_writefile(struct adouble *ad, const int eid, 
182                      const int sock, off_t off, const int end,
183                      const size_t len)
184 {
185 #ifdef __linux__
186   ssize_t cc;
187   int fd;
188
189   fd = ad_sendfile_init(ad, eid, &off, end);
190   if ((cc = sys_sendfile(fd, sock, &off, len)) < 0)
191     return -1;
192
193   if ((eid != ADEID_DFORK) && (off > ad_getentrylen(ad, eid))) 
194     ad_setentrylen(ad, eid, off);
195
196   return cc;
197 #endif /* __linux__ */
198 }
199 #endif /* HAVE_SENDFILE_WRITE */
200 #endif /* 0 */
201 #endif