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;
31 extern int sparse_files;
43 static int buffer_f_in = -1;
45 void setup_nonblocking(int f_in,int f_out)
47 set_blocking(f_out,0);
52 static char *read_buffer = NULL;
53 static char *read_buffer_p = NULL;
54 static int read_buffer_len = 0;
55 static int read_buffer_size = 0;
58 /* This function was added to overcome a deadlock problem when using
59 * ssh. It looks like we can't allow our receive queue to get full or
60 * ssh will clag up. Uggh. */
61 static void read_check(int f)
65 if (read_buffer_len == 0) {
66 read_buffer_p = read_buffer;
69 if ((n=num_waiting(f)) <= 0)
72 if (read_buffer_p != read_buffer) {
73 memmove(read_buffer,read_buffer_p,read_buffer_len);
74 read_buffer_p = read_buffer;
77 if (n > (read_buffer_size - read_buffer_len)) {
78 read_buffer_size += n;
80 read_buffer = (char *)malloc(read_buffer_size);
82 read_buffer = (char *)realloc(read_buffer,read_buffer_size);
83 if (!read_buffer) out_of_memory("read check");
84 read_buffer_p = read_buffer;
87 n = read(f,read_buffer+read_buffer_len,n);
94 static int readfd(int fd,char *buffer,int N)
101 if (read_buffer_len > 0) {
102 ret = MIN(read_buffer_len,N-total);
103 memcpy(buffer+total,read_buffer_p,ret);
104 read_buffer_p += ret;
105 read_buffer_len -= ret;
107 while ((ret = read(fd,buffer + total,N - total)) == -1) {
110 if (errno != EAGAIN && errno != EWOULDBLOCK)
114 select(fd+1, &fds, NULL, NULL, NULL);
130 if ((ret=readfd(f,b,4)) != 4) {
132 fprintf(FERROR,"(%d) Error reading %d bytes : %s\n",
133 getpid(),4,ret==-1?strerror(errno):"EOF");
140 void read_buf(int f,char *buf,int len)
143 if ((ret=readfd(f,buf,len)) != len) {
145 fprintf(FERROR,"(%d) Error reading %d bytes : %s\n",
146 getpid(),len,ret==-1?strerror(errno):"EOF");
152 unsigned char read_byte(int f)
155 read_buf(f,(char *)&c,1);
160 static char last_byte=0;
161 static int last_sparse = 0;
163 int sparse_end(int f)
166 lseek(f,-1,SEEK_CUR);
167 return (write(f,&last_byte,1) == 1 ? 0 : -1);
173 int write_sparse(int f,char *buf,int len)
179 return write(f,buf,len);
181 for (l1=0;l1<len && buf[l1]==0;l1++) ;
182 for (l2=0;l2<(len-l1) && buf[len-(l2+1)]==0;l2++) ;
184 last_byte = buf[len-1];
186 if (l1 == len || l2 > 0)
190 lseek(f,l1,SEEK_CUR);
195 if ((ret=write(f,buf+l1,len-(l1+l2))) != len-(l1+l2)) {
196 if (ret == -1 || ret == 0) return ret;
201 lseek(f,l2,SEEK_CUR);
206 int read_write(int fd_in,int fd_out,int size)
208 static char *buf=NULL;
209 int bufsize = sparse_files?SPARSE_WRITE_SIZE:WRITE_SIZE;
213 buf = (char *)malloc(bufsize);
214 if (!buf) out_of_memory("read_write");
217 while (total < size) {
218 int n = MIN(size-total,bufsize);
219 read_buf(fd_in,buf,n);
220 if (write_sparse(fd_out,buf,n) != n)
228 static int writefd(int fd,char *buf,int len)
234 if (buffer_f_in == -1)
235 return write(fd,buf,len);
237 while (total < len) {
238 int ret = write(fd,buf+total,len-total);
240 if (ret == 0) return total;
242 if (ret == -1 && !(errno == EWOULDBLOCK || errno == EAGAIN))
246 read_check(buffer_f_in);
250 tv.tv_sec = BLOCKING_TIMEOUT;
252 select(fd+1,NULL,&fds,NULL,&tv);
263 void write_int(int f,int x)
268 if ((ret=writefd(f,b,4)) != 4) {
269 fprintf(FERROR,"write_int failed : %s\n",
270 ret==-1?strerror(errno):"EOF");
276 void write_buf(int f,char *buf,int len)
279 if ((ret=writefd(f,buf,len)) != len) {
280 fprintf(FERROR,"write_buf failed : %s\n",
281 ret==-1?strerror(errno):"EOF");
284 total_written += len;
288 void write_byte(int f,unsigned char c)
290 write_buf(f,(char *)&c,1);
293 void write_flush(int f)