]> arthur.barton.de Git - netatalk.git/blob - libatalk/adouble/ad_sendfile.c
Add recvfile support with splice() on Linux
[netatalk.git] / libatalk / adouble / ad_sendfile.c
1 /*
2  * Copyright (c) 1999 Adrian Sun (asun@zoology.washington.edu)
3  * All rights reserved. See COPYRIGHT.
4  *
5  * NOTE: the following uses the fact that sendfile() only exists on
6  * machines with SA_RESTART behaviour. this is all very machine specific. 
7  *
8  * sendfile chainsaw from samba.
9  Unix SMB/Netbios implementation.
10  Version 2.2.x / 3.0.x
11  sendfile implementations.
12  Copyright (C) Jeremy Allison 2002.
13  
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.
22  
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.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif /* HAVE_CONFIG_H */
31
32 #ifdef WITH_SENDFILE
33 #include <stdio.h>
34 #include <sys/socket.h>
35 #include <sys/uio.h>
36 #include <errno.h>  
37
38 #include <atalk/adouble.h>
39 #include <atalk/logger.h>
40
41 #include "ad_lock.h"
42
43 #if defined(SENDFILE_FLAVOR_LINUX)
44 #include <sys/sendfile.h>
45
46 ssize_t sys_sendfile(int tofd, int fromfd, off_t *offset, size_t count)
47 {
48     return sendfile(tofd, fromfd, offset, count);
49 }
50
51 #elif defined(SENDFILE_FLAVOR_SOLARIS)
52 #include <sys/sendfile.h>
53
54 ssize_t sys_sendfile(int tofd, int fromfd, off_t *offset, size_t count)
55 {
56     return sendfile(tofd, fromfd, offset, count);
57 }
58
59 #elif defined(SENDFILE_FLAVOR_BSD )
60 #include <sys/types.h>
61 #include <sys/socket.h>
62 #include <sys/uio.h>
63 ssize_t sys_sendfile(int tofd, int fromfd, off_t *offset, size_t count)
64 {
65     off_t len;
66     int ret;
67
68     ret = sendfile(fromfd, tofd, *offset, count, NULL, &len, 0);
69
70     *offset += len;
71
72     if (ret != 0)
73         return -1;
74     return len;
75 }
76
77 #else
78
79 ssize_t sys_sendfile(int out_fd, int in_fd, off_t *_offset, size_t count)
80 {
81     /* No sendfile syscall. */
82     errno = ENOSYS;
83     return -1;
84 }
85 #endif
86
87 /* ------------------------------- */
88 int ad_readfile_init(const struct adouble *ad, 
89                                        const int eid, off_t *off,
90                                        const int end)
91 {
92   int fd;
93
94   if (end) 
95     *off = ad_size(ad, eid) - *off;
96
97   if (eid == ADEID_DFORK) {
98     fd = ad_data_fileno(ad);
99   } else {
100     *off += ad_getentryoff(ad, eid);
101     fd = ad_reso_fileno(ad);
102   }
103
104   return fd;
105 }
106 #endif