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 int64 total_written;
28 static int64 total_read;
31 extern int sparse_files;
32 extern int io_timeout;
34 int64 write_total(void)
39 int64 read_total(void)
44 static int buffer_f_in = -1;
46 void setup_nonblocking(int f_in,int f_out)
48 set_blocking(f_out,0);
53 static char *read_buffer;
54 static char *read_buffer_p;
55 static int read_buffer_len;
56 static int read_buffer_size;
59 /* This function was added to overcome a deadlock problem when using
60 * ssh. It looks like we can't allow our receive queue to get full or
61 * ssh will clag up. Uggh. */
62 static void read_check(int f)
68 if (read_buffer_len == 0) {
69 read_buffer_p = read_buffer;
72 if ((n=num_waiting(f)) <= 0)
75 /* things could deteriorate if we read in really small chunks */
78 if (read_buffer_p != read_buffer) {
79 memmove(read_buffer,read_buffer_p,read_buffer_len);
80 read_buffer_p = read_buffer;
83 if (n > (read_buffer_size - read_buffer_len)) {
84 read_buffer_size += n;
86 read_buffer = (char *)malloc(read_buffer_size);
88 read_buffer = (char *)realloc(read_buffer,read_buffer_size);
89 if (!read_buffer) out_of_memory("read check");
90 read_buffer_p = read_buffer;
93 n = read(f,read_buffer+read_buffer_len,n);
99 static time_t last_io;
102 static void check_timeout(void)
106 if (!io_timeout) return;
109 last_io = time(NULL);
115 if (last_io && io_timeout && (t-last_io)>io_timeout) {
116 fprintf(FERROR,"read timeout after %d second - exiting\n",
122 static int readfd(int fd,char *buffer,int N)
128 if (read_buffer_len < N)
129 read_check(buffer_f_in);
132 if (read_buffer_len > 0 && buffer_f_in == fd) {
133 ret = MIN(read_buffer_len,N-total);
134 memcpy(buffer+total,read_buffer_p,ret);
135 read_buffer_p += ret;
136 read_buffer_len -= ret;
141 while ((ret = read(fd,buffer + total,N-total)) == -1) {
144 if (errno != EAGAIN && errno != EWOULDBLOCK)
148 tv.tv_sec = io_timeout;
151 if (select(fd+1, &fds, NULL, NULL, &tv) != 1) {
162 last_io = time(NULL);
171 if ((ret=readfd(f,b,4)) != 4) {
173 fprintf(FERROR,"(%d) Error reading %d bytes : %s\n",
174 getpid(),4,ret==-1?strerror(errno):"EOF");
181 int64 read_longint(int f)
183 extern int remote_version;
188 if (ret != -1) return ret;
190 #ifndef HAVE_LONGLONG
191 fprintf(FERROR,"Integer overflow - attempted 64 bit offset\n");
194 if (remote_version >= 16) {
195 if ((ret=readfd(f,b,8)) != 8) {
197 fprintf(FERROR,"(%d) Error reading %d bytes : %s\n",
198 getpid(),8,ret==-1?strerror(errno):"EOF");
202 ret = IVAL(b,0) | (((int64)IVAL(b,4))<<32);
209 void read_buf(int f,char *buf,int len)
212 if ((ret=readfd(f,buf,len)) != len) {
214 fprintf(FERROR,"(%d) Error reading %d bytes : %s\n",
215 getpid(),len,ret==-1?strerror(errno):"EOF");
221 unsigned char read_byte(int f)
224 read_buf(f,(char *)&c,1);
229 static char last_byte;
230 static int last_sparse;
232 int sparse_end(int f)
235 lseek(f,-1,SEEK_CUR);
236 return (write(f,&last_byte,1) == 1 ? 0 : -1);
242 int write_sparse(int f,char *buf,int len)
248 return write(f,buf,len);
250 for (l1=0;l1<len && buf[l1]==0;l1++) ;
251 for (l2=0;l2<(len-l1) && buf[len-(l2+1)]==0;l2++) ;
253 last_byte = buf[len-1];
255 if (l1 == len || l2 > 0)
259 lseek(f,l1,SEEK_CUR);
264 if ((ret=write(f,buf+l1,len-(l1+l2))) != len-(l1+l2)) {
265 if (ret == -1 || ret == 0) return ret;
270 lseek(f,l2,SEEK_CUR);
276 static int writefd(int fd,char *buf,int len)
280 int fd_count, count, got_select=0;
283 if (buffer_f_in == -1)
284 return write(fd,buf,len);
286 while (total < len) {
287 int ret = write(fd,buf+total,len-total);
289 if (ret == 0) return total;
291 if (ret == -1 && !(errno == EWOULDBLOCK || errno == EAGAIN))
294 if (ret == -1 && got_select) {
295 /* hmmm, we got a write select on the fd and then failed to write.
296 Why doesn't that mean that the fd is dead? It doesn't on some
297 systems it seems (eg. IRIX) */
300 fprintf(FERROR,"write exception\n");
309 read_check(buffer_f_in);
315 if (buffer_f_in != -1) {
316 FD_SET(buffer_f_in,&r_fds);
317 if (buffer_f_in > fd)
318 fd_count = buffer_f_in+1;
321 tv.tv_sec = BLOCKING_TIMEOUT;
323 count = select(fd_count,buffer_f_in == -1? NULL: &r_fds,
325 if (count == -1 && errno != EINTR) {
327 fprintf(FERROR,"select error: %s\n", strerror(errno));
336 if (FD_ISSET(fd, &w_fds)) {
345 last_io = time(NULL);
352 void write_int(int f,int x)
357 if ((ret=writefd(f,b,4)) != 4) {
358 fprintf(FERROR,"write_int failed : %s\n",
359 ret==-1?strerror(errno):"EOF");
365 void write_longint(int f, int64 x)
367 extern int remote_version;
371 if (remote_version < 16 || x <= 0x7FFFFFFF) {
372 write_int(f, (int)x);
377 SIVAL(b,0,(x&0xFFFFFFFF));
378 SIVAL(b,4,((x>>32)&0xFFFFFFFF));
380 if ((ret=writefd(f,b,8)) != 8) {
381 fprintf(FERROR,"write_longint failed : %s\n",
382 ret==-1?strerror(errno):"EOF");
388 void write_buf(int f,char *buf,int len)
391 if ((ret=writefd(f,buf,len)) != len) {
392 fprintf(FERROR,"write_buf failed : %s\n",
393 ret==-1?strerror(errno):"EOF");
396 total_written += len;
400 void write_byte(int f,unsigned char c)
402 write_buf(f,(char *)&c,1);
405 void write_flush(int f)