2 * Copyright (c) 1999 Adrian Sun (asun@zoology.washington.edu)
3 * All rights reserved. See COPYRIGHT.
5 * NOTE: the following uses the fact that sendfile() only exists on
6 * machines with SA_RESTART behaviour. this is all very machine specific.
8 * sendfile chainsaw from samba.
9 Unix SMB/Netbios implementation.
11 sendfile implementations.
12 Copyright (C) Jeremy Allison 2002.
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 #endif /* HAVE_CONFIG_H */
34 #include <sys/socket.h>
38 #include <atalk/adouble.h>
39 #include <atalk/logger.h>
43 #if defined(LINUX_BROKEN_SENDFILE_API)
45 extern int32_t sendfile (int fdout, int fdin, int32_t *offset, u_int32_t count);
47 ssize_t sys_sendfile(int tofd, int fromfd, off_t *offset, size_t count)
49 u_int32_t small_total;
54 * Fix for broken Linux 2.4 systems with no working sendfile64().
55 * If the offset+count > 2 GB then pretend we don't have the
56 * system call sendfile at all. The upper layer catches this
57 * and uses a normal read. JRA.
60 if ((sizeof(off_t) >= 8) && (*offset + count > (off_t)0x7FFFFFFF)) {
64 small_offset = (int32_t)*offset;
65 small_total = (u_int32_t)count;
66 nwritten = sendfile(tofd, fromfd, &small_offset, small_total);
73 #elif defined(SENDFILE_FLAVOR_LINUX)
74 #include <sys/sendfile.h>
76 ssize_t sys_sendfile(int tofd, int fromfd, off_t *offset, size_t count)
78 return sendfile(tofd, fromfd, offset, count);
81 #elif defined(SENDFILE_FLAVOR_BSD )
83 #error sendfile semantic broken
84 #include <sys/sendfile.h>
85 ssize_t sys_sendfile(int tofd, int fromfd, off_t *offset, size_t count)
94 ret = sendfile(fromfd, tofd, offset, count, NULL, &nwritten, 0);
95 while (ret == -1 && errno == EINTR);
106 ssize_t sys_sendfile(int out_fd, int in_fd, off_t *_offset, size_t count)
108 /* No sendfile syscall. */
114 /* ------------------------------- */
115 int ad_readfile_init(const struct adouble *ad,
116 const int eid, off_t *off,
122 *off = ad_size(ad, eid) - *off;
124 if (eid == ADEID_DFORK) {
125 fd = ad_data_fileno(ad);
127 *off += ad_getentryoff(ad, eid);
128 fd = ad_reso_fileno(ad);
135 /* ------------------------ */
137 #ifdef HAVE_SENDFILE_WRITE
138 /* read from a socket and write to an adouble file */
139 ssize_t ad_writefile(struct adouble *ad, const int eid,
140 const int sock, off_t off, const int end,
147 fd = ad_sendfile_init(ad, eid, &off, end);
148 if ((cc = sys_sendfile(fd, sock, &off, len)) < 0)
151 if ((eid != ADEID_DFORK) && (off > ad_getentrylen(ad, eid)))
152 ad_setentrylen(ad, eid, off);
155 #endif /* __linux__ */
157 #endif /* HAVE_SENDFILE_WRITE */