]> arthur.barton.de Git - netatalk.git/blob - libatalk/adouble/ad_sendfile.c
Remove broken sendfile on Linux stuff, it wasn't properly used by the upper DSI layer...
[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)
49 #include <sys/sendfile.h>
50
51 ssize_t sys_sendfile(int tofd, int fromfd, off_t *offset, size_t count)
52 {
53     return sendfile(tofd, fromfd, offset, count);
54 }
55
56 #elif defined(SENDFILE_FLAVOR_SOLARIS)
57 #include <sys/sendfile.h>
58
59 ssize_t sys_sendfile(int tofd, int fromfd, off_t *offset, size_t count)
60 {
61     return sendfile(tofd, fromfd, offset, count);
62 }
63
64 #elif defined(SENDFILE_FLAVOR_BSD )
65 #include <sys/sendfile.h>
66 ssize_t sys_sendfile(int tofd, int fromfd, off_t *offset, size_t count)
67 {
68 size_t total=0;
69 int    ret;
70
71     total = count;
72     while (total) {
73         ssize_t nwritten;
74         do {
75            ret = sendfile(fromfd, tofd, offset, count, NULL, &nwritten, 0);
76         while (ret == -1 && errno == EINTR);
77         if (ret == -1)
78             return -1;
79         total -= nwritten;
80         offset += nwritten;
81     }
82     return count;
83 }
84
85 #else
86
87 ssize_t sys_sendfile(int out_fd, int in_fd, off_t *_offset, size_t count)
88 {
89     /* No sendfile syscall. */
90     errno = ENOSYS;
91     return -1;
92 }
93 #endif
94
95 /* ------------------------------- */
96 int ad_readfile_init(const struct adouble *ad, 
97                                        const int eid, off_t *off,
98                                        const int end)
99 {
100   int fd;
101
102   if (end) 
103     *off = ad_size(ad, eid) - *off;
104
105   if (eid == ADEID_DFORK) {
106     fd = ad_data_fileno(ad);
107   } else {
108     *off += ad_getentryoff(ad, eid);
109     fd = ad_reso_fileno(ad);
110   }
111
112   return fd;
113 }
114
115
116 /* ------------------------ */
117 #if 0
118 #ifdef HAVE_SENDFILE_WRITE
119 /* read from a socket and write to an adouble file */
120 ssize_t ad_writefile(struct adouble *ad, const int eid, 
121                      const int sock, off_t off, const int end,
122                      const size_t len)
123 {
124 #ifdef __linux__
125   ssize_t cc;
126   int fd;
127
128   fd = ad_sendfile_init(ad, eid, &off, end);
129   if ((cc = sys_sendfile(fd, sock, &off, len)) < 0)
130     return -1;
131
132   if ((eid != ADEID_DFORK) && (off > ad_getentrylen(ad, eid))) 
133     ad_setentrylen(ad, eid, off);
134
135   return cc;
136 #endif /* __linux__ */
137 }
138 #endif /* HAVE_SENDFILE_WRITE */
139 #endif /* 0 */
140 #endif