2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 Utilities used in rsync
27 static int total_written = 0;
28 static int total_read = 0;
42 static int buffer_f_in = -1;
44 void setup_nonblocking(int f_in,int f_out)
46 set_blocking(f_out,0);
51 static char *read_buffer = NULL;
52 static char *read_buffer_p = NULL;
53 static int read_buffer_len = 0;
54 static int read_buffer_size = 0;
57 /* This function was added to overcome a deadlock problem when using
58 * ssh. It looks like we can't allow our receive queue to get full or
59 * ssh will clag up. Uggh. */
60 static void read_check(int f)
64 if (read_buffer_len == 0) {
65 read_buffer_p = read_buffer;
68 if ((n=num_waiting(f)) <= 0)
71 if (read_buffer_p != read_buffer) {
72 memmove(read_buffer,read_buffer_p,read_buffer_len);
73 read_buffer_p = read_buffer;
76 if (n > (read_buffer_size - read_buffer_len)) {
77 read_buffer_size += n;
79 read_buffer = (char *)malloc(read_buffer_size);
81 read_buffer = (char *)realloc(read_buffer,read_buffer_size);
82 if (!read_buffer) out_of_memory("read check");
83 read_buffer_p = read_buffer;
86 n = read(f,read_buffer+read_buffer_len,n);
93 static int readfd(int fd,char *buffer,int N)
100 if (read_buffer_len > 0) {
101 ret = MIN(read_buffer_len,N-total);
102 memcpy(buffer+total,read_buffer_p,ret);
103 read_buffer_p += ret;
104 read_buffer_len -= ret;
106 if ((ret = read(fd,buffer + total,N - total)) == -1)
122 if ((ret=readfd(f,b,4)) != 4) {
124 fprintf(stderr,"Error reading %d bytes : %s\n",
125 4,ret==-1?strerror(errno):"EOF");
132 void read_buf(int f,char *buf,int len)
135 if ((ret=readfd(f,buf,len)) != len) {
137 fprintf(stderr,"Error reading %d bytes : %s\n",
138 len,ret==-1?strerror(errno):"EOF");
144 unsigned char read_byte(int f)
148 return (unsigned char)c;
151 int read_write(int fd_in,int fd_out,int size)
153 static char *buf=NULL;
154 static int bufsize = WRITE_BLOCK_SIZE;
158 buf = (char *)malloc(bufsize);
159 if (!buf) out_of_memory("read_write");
162 while (total < size) {
163 int n = MIN(size-total,bufsize);
164 read_buf(fd_in,buf,n);
165 if (write(fd_out,buf,n) != n)
173 static int writefd(int fd,char *buf,int len)
179 if (buffer_f_in == -1)
180 return write(fd,buf,len);
182 while (total < len) {
183 int ret = write(fd,buf+total,len-total);
185 if (ret == 0) return total;
187 if (ret == -1 && !(errno == EWOULDBLOCK || errno == EAGAIN))
191 read_check(buffer_f_in);
195 tv.tv_sec = BLOCKING_TIMEOUT;
197 select(16,NULL,&fds,NULL,&tv);
208 void write_int(int f,int x)
213 if ((ret=writefd(f,b,4)) != 4) {
214 fprintf(stderr,"write_int failed : %s\n",
215 ret==-1?strerror(errno):"EOF");
221 void write_buf(int f,char *buf,int len)
224 if ((ret=writefd(f,buf,len)) != len) {
225 fprintf(stderr,"write_buf failed : %s\n",
226 ret==-1?strerror(errno):"EOF");
229 total_written += len;
233 void write_byte(int f,unsigned char c)
235 write_buf(f,(char *)&c,1);
238 void write_flush(int f)