]> arthur.barton.de Git - netatalk.git/blob - libatalk/adouble/ad_sendfile.c
154c89d02473776968a91faec19e6f32c76d168f
[netatalk.git] / libatalk / adouble / ad_sendfile.c
1 /*
2  * $Id: ad_sendfile.c,v 1.11 2010-01-21 14:14:49 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 #include "ad_private.h"
47
48 #if defined(SENDFILE_FLAVOR_LINUX) && defined(LINUX_BROKEN_SENDFILE_API)
49
50 extern int32_t sendfile (int fdout, int fdin, int32_t *offset, u_int32_t count);
51
52 ssize_t sys_sendfile(int tofd, int fromfd, off_t *offset, size_t count)
53 {
54 u_int32_t small_total;
55 int32_t small_offset;
56 int32_t nwritten;
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_offset = (int32_t)*offset;
70      small_total = (u_int32_t)count;
71      nwritten = sendfile(tofd, fromfd, &small_offset, small_total);
72      if (nwritten > = 0)
73          *offset += nwritten;
74      
75     return nwritten;
76 }
77
78 #elif defined(SENDFILE_FLAVOR_LINUX)
79 #include <sys/sendfile.h>
80
81 ssize_t sys_sendfile(int tofd, int fromfd, off_t *offset, size_t count)
82 {
83     return sendfile(tofd, fromfd, offset, count);
84 }
85
86 #elif defined(SENDFILE_FLAVOR_SOLARIS)
87 #include <sys/sendfile.h>
88
89 ssize_t sys_sendfile(int tofd, int fromfd, off_t *offset, size_t count)
90 {
91     return sendfile(tofd, fromfd, offset, count);
92 }
93
94 #elif defined(SENDFILE_FLAVOR_BSD )
95 #include <sys/sendfile.h>
96 ssize_t sys_sendfile(int tofd, int fromfd, off_t *offset, size_t count)
97 {
98 size_t total=0;
99 int    ret;
100
101     total = count;
102     while (total) {
103         ssize_t nwritten;
104         do {
105            ret = sendfile(fromfd, tofd, offset, count, NULL, &nwritten, 0);
106         while (ret == -1 && errno == EINTR);
107         if (ret == -1)
108             return -1;
109         total -= nwritten;
110         offset += nwritten;
111     }
112     return count;
113 }
114
115 #else
116
117 ssize_t sys_sendfile(int out_fd, int in_fd, off_t *_offset, size_t count)
118 {
119     /* No sendfile syscall. */
120     errno = ENOSYS;
121     return -1;
122 }
123 #endif
124
125 /* ------------------------------- */
126 int ad_readfile_init(const struct adouble *ad, 
127                                        const int eid, off_t *off,
128                                        const int end)
129 {
130   int fd;
131
132   if (end) 
133     *off = ad_size(ad, eid) - *off;
134
135   if (eid == ADEID_DFORK) {
136     fd = ad_data_fileno(ad);
137   } else {
138     *off += ad_getentryoff(ad, eid);
139     fd = ad_reso_fileno(ad);
140   }
141
142   return fd;
143 }
144
145
146 /* ------------------------ */
147 #if 0
148 #ifdef HAVE_SENDFILE_WRITE
149 /* read from a socket and write to an adouble file */
150 ssize_t ad_writefile(struct adouble *ad, const int eid, 
151                      const int sock, off_t off, const int end,
152                      const size_t len)
153 {
154 #ifdef __linux__
155   ssize_t cc;
156   int fd;
157
158   fd = ad_sendfile_init(ad, eid, &off, end);
159   if ((cc = sys_sendfile(fd, sock, &off, len)) < 0)
160     return -1;
161
162   if ((eid != ADEID_DFORK) && (off > ad_getentrylen(ad, eid))) 
163     ad_setentrylen(ad, eid, off);
164
165   return cc;
166 #endif /* __linux__ */
167 }
168 #endif /* HAVE_SENDFILE_WRITE */
169 #endif /* 0 */
170 #endif