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 * If tofd is -1 we just drain the incoming socket of count
33 * bytes without writing to the outgoing fd.
34 * If a write fails we do the same (to cope with disk full)
37 * Returns -1 on short reads from fromfd (read error)
40 * Returns number of bytes written to 'tofd'
41 * or thrown away if 'tofd == -1'.
42 * eturn != count then sets errno.
43 * Returns count if complete success.
46 #ifndef TRANSFER_BUF_SIZE
47 #define TRANSFER_BUF_SIZE (128*1024)
50 static ssize_t default_sys_recvfile(int fromfd,
57 size_t bufsize = MIN(TRANSFER_BUF_SIZE,count);
58 size_t total_written = 0;
65 if (tofd != -1 && offset != (SMB_OFF_T)-1) {
66 if (sys_lseek(tofd, offset, SEEK_SET) == -1) {
67 if (errno != ESPIPE) {
73 buffer = SMB_MALLOC_ARRAY(char, bufsize);
78 while (total < count) {
79 size_t num_written = 0;
81 size_t toread = MIN(bufsize,count - total);
83 /* Read from socket - ignore EINTR. */
84 read_ret = sys_read(fromfd, buffer, toread);
86 /* EOF or socket error. */
93 while (num_written < read_ret) {
99 /* Write to file - ignore EINTR. */
100 write_ret = sys_write(tofd,
101 buffer + num_written,
102 read_ret - num_written);
104 if (write_ret <= 0) {
105 /* write error - stop writing. */
112 num_written += (size_t)write_ret;
113 total_written += (size_t)write_ret;
121 /* Return the correct write error. */
124 return (ssize_t)total_written;
127 #if defined(HAVE_SPLICE_SYSCALL)
128 ssize_t sys_recvfile(int fromfd,
138 /*****************************************************************
139 No recvfile system call - use the default 128 chunk implementation.
140 *****************************************************************/
142 ssize_t sys_recvfile(int fromfd,
147 return default_sys_recvfile(fromfd, tofd, offset, count);
151 /*****************************************************************
152 Throw away "count" bytes from the client socket.
153 *****************************************************************/
155 ssize_t drain_socket(int sockfd, size_t count)
157 return default_sys_recvfile(sockfd, -1, (SMB_OFF_T)-1, count);