]> arthur.barton.de Git - netatalk.git/blob - libatalk/adouble/ad_sendfile.c
Merge master
[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   return sendfile(fromfd, tofd, *offset, count, NULL, offset, 0);
66 }
67
68 #else
69
70 ssize_t sys_sendfile(int out_fd, int in_fd, off_t *_offset, size_t count)
71 {
72     /* No sendfile syscall. */
73     errno = ENOSYS;
74     return -1;
75 }
76 #endif
77
78 /* ------------------------------- */
79 int ad_readfile_init(const struct adouble *ad, 
80                                        const int eid, off_t *off,
81                                        const int end)
82 {
83   int fd;
84
85   if (end) 
86     *off = ad_size(ad, eid) - *off;
87
88   if (eid == ADEID_DFORK) {
89     fd = ad_data_fileno(ad);
90   } else {
91     *off += ad_getentryoff(ad, eid);
92     fd = ad_reso_fileno(ad);
93   }
94
95   return fd;
96 }
97
98
99 /* ------------------------ */
100 #if 0
101 #ifdef HAVE_SENDFILE_WRITE
102 /* read from a socket and write to an adouble file */
103 ssize_t ad_writefile(struct adouble *ad, const int eid, 
104                      const int sock, off_t off, const int end,
105                      const size_t len)
106 {
107 #ifdef __linux__
108   ssize_t cc;
109   int fd;
110
111   fd = ad_sendfile_init(ad, eid, &off, end);
112   if ((cc = sys_sendfile(fd, sock, &off, len)) < 0)
113     return -1;
114
115   if ((eid != ADEID_DFORK) && (off > ad_getentrylen(ad, eid))) 
116     ad_setentrylen(ad, eid, off);
117
118   return cc;
119 #endif /* __linux__ */
120 }
121 #endif /* HAVE_SENDFILE_WRITE */
122 #endif /* 0 */
123 #endif