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.
28 /* Do this on our own in TRANSFER_BUF_SIZE chunks.
29 * It's safe to make direct syscalls to lseek/write here
30 * as we're below the Samba vfs layer.
32 * Returns -1 on short reads from fromfd (read error)
35 * Returns number of bytes written to 'tofd'
36 * return != count then sets errno.
37 * Returns count if complete success.
40 #ifndef TRANSFER_BUF_SIZE
41 #define TRANSFER_BUF_SIZE (128*1024)
44 static ssize_t
default_sys_recvfile(int fromfd
,
51 size_t bufsize
= MIN(TRANSFER_BUF_SIZE
,count
);
52 size_t total_written
= 0;
55 DEBUG(10,("default_sys_recvfile: from = %d, to = %d, "
56 "offset=%.0f, count = %lu\n",
57 fromfd
, tofd
, (double)offset
,
58 (unsigned long)count
));
64 if (tofd
!= -1 && offset
!= (SMB_OFF_T
)-1) {
65 if (sys_lseek(tofd
, offset
, SEEK_SET
) == -1) {
66 if (errno
!= ESPIPE
) {
72 buffer
= SMB_MALLOC_ARRAY(char, bufsize
);
77 while (total
< count
) {
78 size_t num_written
= 0;
80 size_t toread
= MIN(bufsize
,count
- total
);
82 /* Read from socket - ignore EINTR. */
83 read_ret
= sys_read(fromfd
, buffer
, toread
);
85 /* EOF or socket error. */
92 /* Don't write any more after a write error. */
93 while (tofd
!= -1 && (num_written
< read_ret
)) {
96 /* Write to file - ignore EINTR. */
97 write_ret
= sys_write(tofd
,
99 read_ret
- num_written
);
101 if (write_ret
<= 0) {
102 /* write error - stop writing. */
104 if (total_written
== 0) {
114 num_written
+= (size_t)write_ret
;
115 total_written
+= (size_t)write_ret
;
123 /* Return the correct write error. */
126 return (ssize_t
)total_written
;
129 #if defined(HAVE_LINUX_SPLICE)
132 * Try and use the Linux system call to do this.
133 * Remember we only return -1 if the socket read
134 * failed. Else we return the number of bytes
135 * actually written. We always read count bytes
136 * from the network in the case of return != -1.
140 ssize_t
sys_recvfile(int fromfd
,
145 static int pipefd
[2] = { -1, -1 };
146 static bool try_splice_call
= false;
147 size_t total_written
= 0;
148 loff_t splice_offset
= offset
;
150 DEBUG(10,("sys_recvfile: from = %d, to = %d, "
151 "offset=%.0f, count = %lu\n",
152 fromfd
, tofd
, (double)offset
,
153 (unsigned long)count
));
160 * Older Linux kernels have splice for sendfile,
161 * but it fails for recvfile. Ensure we only try
162 * this once and always fall back to the userspace
163 * implementation if recvfile splice fails. JRA.
166 if (!try_splice_call
) {
167 return default_sys_recvfile(fromfd
,
173 if ((pipefd
[0] == -1) && (pipe(pipefd
) == -1)) {
174 try_splice_call
= false;
175 return default_sys_recvfile(fromfd
, tofd
, offset
, count
);
181 nread
= splice(fromfd
, NULL
, pipefd
[1], NULL
,
182 MIN(count
, 16384), SPLICE_F_MOVE
);
184 if (errno
== EINTR
) {
187 if (total_written
== 0 &&
188 (errno
== EBADF
|| errno
== EINVAL
)) {
189 try_splice_call
= false;
190 return default_sys_recvfile(fromfd
, tofd
,
197 while (to_write
> 0) {
199 thistime
= splice(pipefd
[0], NULL
, tofd
,
200 &splice_offset
, to_write
,
202 if (thistime
== -1) {
205 to_write
-= thistime
;
208 total_written
+= nread
;
214 int saved_errno
= errno
;
215 if (drain_socket(fromfd
, count
) != count
) {
216 /* socket is dead. */
222 return total_written
;
226 /*****************************************************************
227 No recvfile system call - use the default 128 chunk implementation.
228 *****************************************************************/
230 ssize_t
sys_recvfile(int fromfd
,
235 return default_sys_recvfile(fromfd
, tofd
, offset
, count
);
239 /*****************************************************************
240 Throw away "count" bytes from the client socket.
241 Returns count or -1 on error.
242 *****************************************************************/
244 ssize_t
drain_socket(int sockfd
, size_t count
)
247 size_t bufsize
= MIN(TRANSFER_BUF_SIZE
,count
);
254 buffer
= SMB_MALLOC_ARRAY(char, bufsize
);
255 if (buffer
== NULL
) {
259 while (total
< count
) {
261 size_t toread
= MIN(bufsize
,count
- total
);
263 /* Read from socket - ignore EINTR. */
264 read_ret
= sys_read(sockfd
, buffer
, toread
);
266 /* EOF or socket error. */