2 * $Id: ad_sendfile.c,v 1.5 2002-10-11 14:18:38 didg Exp $
4 * Copyright (c) 1999 Adrian Sun (asun@zoology.washington.edu)
5 * All rights reserved. See COPYRIGHT.
7 * NOTE: the following uses the fact that sendfile() only exists on
8 * machines with SA_RESTART behaviour. this is all very machine specific.
15 #endif /* HAVE_CONFIG_H */
20 #endif /* HAVE_UNISTD_H */
21 #include <sys/types.h>
23 #include <sys/socket.h>
26 #include <atalk/adouble.h>
28 #include <atalk/logger.h>
30 #include "ad_private.h"
32 static int _ad_sendfile_dummy;
36 #if defined(HAVE_SENDFILE_READ)
38 int sendfile(int fdout, int fdin, off_t *off, size_t count)
40 #if _FILE_OFFSET_BITS == 64
42 return syscall(__NR_sendfile64, fdout, fdin, off, count);
44 return syscall(__NR_sendfile, fdout, fdin, off, count);
51 #if defined(HAVE_SENDFILE_READ) || defined(HAVE_SENDFILE_WRITE)
52 static __inline__ int ad_sendfile_init(const struct adouble *ad,
53 const int eid, off_t *off,
59 *off = ad_size(ad, eid) - *off;
61 if (eid == ADEID_DFORK) {
64 *off += ad_getentryoff(ad, eid);
73 /* read from adouble file and write to socket. sendfile doesn't change
74 * the file pointer position. */
75 #ifdef HAVE_SENDFILE_READ
76 ssize_t ad_readfile(const struct adouble *ad, const int eid,
77 const int sock, off_t off, const size_t len)
82 fd = ad_sendfile_init(ad, eid, &off, 0);
84 cc = sendfile(sock, fd, &off, len);
85 #endif /* __linux__ */
88 if (sendfile(fd, sock, off, len, NULL, &cc, 0) < 0)
94 #endif /* HAVE_SENDFILE_READ */
97 #ifdef HAVE_SENDFILE_WRITE
98 /* read from a socket and write to an adouble file */
99 ssize_t ad_writefile(struct adouble *ad, const int eid,
100 const int sock, off_t off, const int end,
107 fd = ad_sendfile_init(ad, eid, &off, end);
108 if ((cc = sendfile(fd, sock, &off, len)) < 0)
111 if ((eid != ADEID_DFORK) && (off > ad_getentrylen(ad, eid)))
112 ad_setentrylen(ad, eid, off);
115 #endif /* __linux__ */
117 #endif /* HAVE_SENDFILE_WRITE */