2 Unix SMB/Netbios implementation.
4 recvfile implementations.
5 Copyright (C) Jeremy Allison 2007.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/>.
21 * This file handles the OS dependent recvfile implementations.
22 * The API is such that it returns -1 on error, else returns the
23 * number of bytes written.
27 #include "system/filesys.h"
28 #include "lib/util/sys_rw.h"
30 /* Do this on our own in TRANSFER_BUF_SIZE chunks.
31 * It's safe to make direct syscalls to lseek/write here
32 * as we're below the Samba vfs layer.
34 * Returns -1 on short reads from fromfd (read error)
37 * Returns number of bytes written to 'tofd'
38 * return != count then sets errno.
39 * Returns count if complete success.
42 #ifndef TRANSFER_BUF_SIZE
43 #define TRANSFER_BUF_SIZE (128*1024)
46 static ssize_t
default_sys_recvfile(int fromfd
,
53 size_t bufsize
= MIN(TRANSFER_BUF_SIZE
,count
);
54 size_t total_written
= 0;
57 DEBUG(10,("default_sys_recvfile: from = %d, to = %d, "
58 "offset=%.0f, count = %lu\n",
59 fromfd
, tofd
, (double)offset
,
60 (unsigned long)count
));
66 if (tofd
!= -1 && offset
!= (off_t
)-1) {
67 if (lseek(tofd
, offset
, SEEK_SET
) == -1) {
68 if (errno
!= ESPIPE
) {
74 while (total
< count
) {
75 size_t num_written
= 0;
77 size_t toread
= MIN(bufsize
,count
- total
);
80 * Read from socket - ignore EINTR.
81 * Can't use sys_read() as that also
82 * ignores EAGAIN and EWOULDBLOCK.
85 read_ret
= read(fromfd
, buffer
, toread
);
86 } while (read_ret
== -1 && errno
== EINTR
);
88 if (read_ret
== -1 && (errno
== EAGAIN
|| errno
== EWOULDBLOCK
)) {
90 * fromfd socket is in non-blocking mode.
91 * If we already read some and wrote
92 * it successfully, return that.
93 * Only return -1 if this is the first read
94 * attempt. Caller will handle both cases.
96 if (total_written
!= 0) {
103 /* EOF or socket error. */
109 /* Don't write any more after a write error. */
110 while (tofd
!= -1 && (num_written
< read_ret
)) {
113 /* Write to file - ignore EINTR. */
114 write_ret
= sys_write(tofd
,
115 buffer
+ num_written
,
116 read_ret
- num_written
);
118 if (write_ret
<= 0) {
119 /* write error - stop writing. */
121 if (total_written
== 0) {
131 num_written
+= (size_t)write_ret
;
132 total_written
+= (size_t)write_ret
;
139 /* Return the correct write error. */
142 return (ssize_t
)total_written
;
145 #if defined(HAVE_LINUX_SPLICE)
148 * Try and use the Linux system call to do this.
149 * Remember we only return -1 if the socket read
150 * failed. Else we return the number of bytes
151 * actually written. We always read count bytes
152 * from the network in the case of return != -1.
156 ssize_t
sys_recvfile(int fromfd
,
161 static int pipefd
[2] = { -1, -1 };
162 static bool try_splice_call
= false;
163 size_t total_written
= 0;
164 loff_t splice_offset
= offset
;
166 DEBUG(10,("sys_recvfile: from = %d, to = %d, "
167 "offset=%.0f, count = %lu\n",
168 fromfd
, tofd
, (double)offset
,
169 (unsigned long)count
));
176 * Older Linux kernels have splice for sendfile,
177 * but it fails for recvfile. Ensure we only try
178 * this once and always fall back to the userspace
179 * implementation if recvfile splice fails. JRA.
182 if (!try_splice_call
) {
183 return default_sys_recvfile(fromfd
,
189 if ((pipefd
[0] == -1) && (pipe(pipefd
) == -1)) {
190 try_splice_call
= false;
191 return default_sys_recvfile(fromfd
, tofd
, offset
, count
);
197 nread
= splice(fromfd
, NULL
, pipefd
[1], NULL
,
198 MIN(count
, 16384), SPLICE_F_MOVE
);
200 if (errno
== EINTR
) {
203 if (total_written
== 0 &&
204 (errno
== EBADF
|| errno
== EINVAL
)) {
205 try_splice_call
= false;
206 return default_sys_recvfile(fromfd
, tofd
,
209 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
211 * fromfd socket is in non-blocking mode.
212 * If we already read some and wrote
213 * it successfully, return that.
214 * Only return -1 if this is the first read
215 * attempt. Caller will handle both cases.
217 if (total_written
!= 0) {
218 return total_written
;
226 while (to_write
> 0) {
228 thistime
= splice(pipefd
[0], NULL
, tofd
,
229 &splice_offset
, to_write
,
231 if (thistime
== -1) {
234 to_write
-= thistime
;
237 total_written
+= nread
;
243 int saved_errno
= errno
;
244 if (drain_socket(fromfd
, count
) != count
) {
245 /* socket is dead. */
251 return total_written
;
255 /*****************************************************************
256 No recvfile system call - use the default 128 chunk implementation.
257 *****************************************************************/
259 ssize_t
sys_recvfile(int fromfd
,
264 return default_sys_recvfile(fromfd
, tofd
, offset
, count
);
268 /*****************************************************************
269 Throw away "count" bytes from the client socket.
270 Returns count or -1 on error.
271 Must only operate on a blocking socket.
272 *****************************************************************/
274 ssize_t
drain_socket(int sockfd
, size_t count
)
277 size_t bufsize
= MIN(TRANSFER_BUF_SIZE
,count
);
278 char buffer
[bufsize
];
285 old_flags
= fcntl(sockfd
, F_GETFL
, 0);
286 if (set_blocking(sockfd
, true) == -1) {
290 while (total
< count
) {
292 size_t toread
= MIN(bufsize
,count
- total
);
294 /* Read from socket - ignore EINTR. */
295 read_ret
= sys_read(sockfd
, buffer
, toread
);
297 /* EOF or socket error. */
306 if (fcntl(sockfd
, F_SETFL
, old_flags
) == -1) {