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"
29 /* Do this on our own in TRANSFER_BUF_SIZE chunks.
30 * It's safe to make direct syscalls to lseek/write here
31 * as we're below the Samba vfs layer.
33 * Returns -1 on short reads from fromfd (read error)
36 * Returns number of bytes written to 'tofd'
37 * return != count then sets errno.
38 * Returns count if complete success.
41 #ifndef TRANSFER_BUF_SIZE
42 #define TRANSFER_BUF_SIZE (128*1024)
45 static ssize_t default_sys_recvfile(int fromfd,
52 size_t bufsize = MIN(TRANSFER_BUF_SIZE,count);
53 size_t total_written = 0;
56 DEBUG(10,("default_sys_recvfile: from = %d, to = %d, "
57 "offset=%.0f, count = %lu\n",
58 fromfd, tofd, (double)offset,
59 (unsigned long)count));
65 if (tofd != -1 && offset != (off_t)-1) {
66 if (lseek(tofd, offset, SEEK_SET) == -1) {
67 if (errno != ESPIPE) {
73 while (total < count) {
74 size_t num_written = 0;
76 size_t toread = MIN(bufsize,count - total);
79 * Read from socket - ignore EINTR.
80 * Can't use sys_read() as that also
81 * ignores EAGAIN and EWOULDBLOCK.
84 read_ret = read(fromfd, buffer, toread);
85 } while (read_ret == -1 && errno == EINTR);
87 if (read_ret == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
89 * fromfd socket is in non-blocking mode.
90 * If we already read some and wrote
91 * it successfully, return that.
92 * Only return -1 if this is the first read
93 * attempt. Caller will handle both cases.
95 if (total_written != 0) {
102 /* EOF or socket error. */
108 /* Don't write any more after a write error. */
109 while (tofd != -1 && (num_written < read_ret)) {
112 /* Write to file - ignore EINTR. */
113 write_ret = sys_write(tofd,
114 buffer + num_written,
115 read_ret - num_written);
117 if (write_ret <= 0) {
118 /* write error - stop writing. */
120 if (total_written == 0) {
130 num_written += (size_t)write_ret;
131 total_written += (size_t)write_ret;
138 /* Return the correct write error. */
141 return (ssize_t)total_written;
144 #if defined(HAVE_LINUX_SPLICE)
147 * Try and use the Linux system call to do this.
148 * Remember we only return -1 if the socket read
149 * failed. Else we return the number of bytes
150 * actually written. We always read count bytes
151 * from the network in the case of return != -1.
155 ssize_t sys_recvfile(int fromfd,
160 static int pipefd[2] = { -1, -1 };
161 static bool try_splice_call = false;
162 size_t total_written = 0;
163 loff_t splice_offset = offset;
165 DEBUG(10,("sys_recvfile: from = %d, to = %d, "
166 "offset=%.0f, count = %lu\n",
167 fromfd, tofd, (double)offset,
168 (unsigned long)count));
175 * Older Linux kernels have splice for sendfile,
176 * but it fails for recvfile. Ensure we only try
177 * this once and always fall back to the userspace
178 * implementation if recvfile splice fails. JRA.
181 if (!try_splice_call) {
182 return default_sys_recvfile(fromfd,
188 if ((pipefd[0] == -1) && (pipe(pipefd) == -1)) {
189 try_splice_call = false;
190 return default_sys_recvfile(fromfd, tofd, offset, count);
196 nread = splice(fromfd, NULL, pipefd[1], NULL,
197 MIN(count, 16384), SPLICE_F_MOVE);
199 if (errno == EINTR) {
202 if (total_written == 0 &&
203 (errno == EBADF || errno == EINVAL)) {
204 try_splice_call = false;
205 return default_sys_recvfile(fromfd, tofd,
208 if (errno == EAGAIN || errno == EWOULDBLOCK) {
210 * fromfd socket is in non-blocking mode.
211 * If we already read some and wrote
212 * it successfully, return that.
213 * Only return -1 if this is the first read
214 * attempt. Caller will handle both cases.
216 if (total_written != 0) {
217 return total_written;
225 while (to_write > 0) {
227 thistime = splice(pipefd[0], NULL, tofd,
228 &splice_offset, to_write,
230 if (thistime == -1) {
233 to_write -= thistime;
236 total_written += nread;
242 int saved_errno = errno;
243 if (drain_socket(fromfd, count) != count) {
244 /* socket is dead. */
250 return total_written;
254 /*****************************************************************
255 No recvfile system call - use the default 128 chunk implementation.
256 *****************************************************************/
258 ssize_t sys_recvfile(int fromfd,
263 return default_sys_recvfile(fromfd, tofd, offset, count);
267 /*****************************************************************
268 Throw away "count" bytes from the client socket.
269 Returns count or -1 on error.
270 Must only operate on a blocking socket.
271 *****************************************************************/
273 ssize_t drain_socket(int sockfd, size_t count)
276 size_t bufsize = MIN(TRANSFER_BUF_SIZE,count);
277 char buffer[bufsize];
284 old_flags = fcntl(sockfd, F_GETFL, 0);
285 if (set_blocking(sockfd, true) == -1) {
289 while (total < count) {
291 size_t toread = MIN(bufsize,count - total);
293 /* Read from socket - ignore EINTR. */
294 read_ret = sys_read(sockfd, buffer, toread);
296 /* EOF or socket error. */
305 if (fcntl(sockfd, F_SETFL, old_flags) == -1) {