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 (n > MAX_READ_BUFFER/4)
79 n = MAX_READ_BUFFER/4;
81 if (read_buffer_p != read_buffer) {
82 memmove(read_buffer,read_buffer_p,read_buffer_len);
83 read_buffer_p = read_buffer;
86 if (n > (read_buffer_size - read_buffer_len)) {
87 read_buffer_size += n;
89 read_buffer = (char *)malloc(read_buffer_size);
91 read_buffer = (char *)realloc(read_buffer,read_buffer_size);
92 if (!read_buffer) out_of_memory("read check");
93 read_buffer_p = read_buffer;
96 n = read(f,read_buffer+read_buffer_len,n);
102 static time_t last_io;
105 static void check_timeout(void)
109 if (!io_timeout) return;
112 last_io = time(NULL);
118 if (last_io && io_timeout && (t-last_io)>io_timeout) {
119 rprintf(FERROR,"read timeout after %d second - exiting\n",
125 static int readfd(int fd,char *buffer,int N)
131 if (read_buffer_len < N)
132 read_check(buffer_f_in);
135 if (read_buffer_len > 0 && buffer_f_in == fd) {
136 ret = MIN(read_buffer_len,N-total);
137 memcpy(buffer+total,read_buffer_p,ret);
138 read_buffer_p += ret;
139 read_buffer_len -= ret;
144 while ((ret = read(fd,buffer + total,N-total)) == -1) {
147 if (errno != EAGAIN && errno != EWOULDBLOCK)
151 tv.tv_sec = io_timeout;
154 if (select(fd+1, &fds, NULL, NULL,
155 io_timeout?&tv:NULL) != 1) {
166 last_io = time(NULL);
171 int32 read_int(int f)
175 if ((ret=readfd(f,b,4)) != 4) {
177 rprintf(FERROR,"(%d) Error reading %d bytes : %s\n",
178 getpid(),4,ret==-1?strerror(errno):"EOF");
185 int64 read_longint(int f)
187 extern int remote_version;
192 if ((int32)ret != (int32)0xffffffff) return ret;
195 rprintf(FERROR,"Integer overflow - attempted 64 bit offset\n");
198 if (remote_version >= 16) {
199 if ((ret=readfd(f,b,8)) != 8) {
201 rprintf(FERROR,"(%d) Error reading %d bytes : %s\n",
202 getpid(),8,ret==-1?strerror(errno):"EOF");
206 ret = IVAL(b,0) | (((int64)IVAL(b,4))<<32);
213 void read_buf(int f,char *buf,int len)
216 if ((ret=readfd(f,buf,len)) != len) {
218 rprintf(FERROR,"(%d) Error reading %d bytes : %s\n",
219 getpid(),len,ret==-1?strerror(errno):"EOF");
225 void read_sbuf(int f,char *buf,int len)
231 unsigned char read_byte(int f)
234 read_buf(f,(char *)&c,1);
239 static char last_byte;
240 static int last_sparse;
242 int sparse_end(int f)
245 do_lseek(f,-1,SEEK_CUR);
246 return (write(f,&last_byte,1) == 1 ? 0 : -1);
253 static int write_sparse(int f,char *buf,int len)
258 for (l1=0;l1<len && buf[l1]==0;l1++) ;
259 for (l2=0;l2<(len-l1) && buf[len-(l2+1)]==0;l2++) ;
261 last_byte = buf[len-1];
263 if (l1 == len || l2 > 0)
267 do_lseek(f,l1,SEEK_CUR);
272 if ((ret=write(f,buf+l1,len-(l1+l2))) != len-(l1+l2)) {
273 if (ret == -1 || ret == 0) return ret;
278 do_lseek(f,l2,SEEK_CUR);
285 int write_file(int f,char *buf,int len)
290 return write(f,buf,len);
293 int len1 = MIN(len, SPARSE_WRITE_SIZE);
294 int r1 = write_sparse(f, buf, len1);
296 if (ret > 0) return ret;
307 static int writefd_unbuffered(int fd,char *buf,int len)
311 int fd_count, count, got_select=0;
314 if (buffer_f_in == -1)
315 return write(fd,buf,len);
317 while (total < len) {
318 int ret = write(fd,buf+total,len-total);
320 if (ret == 0) return total;
322 if (ret == -1 && !(errno == EWOULDBLOCK || errno == EAGAIN))
325 if (ret == -1 && got_select) {
326 /* hmmm, we got a write select on the fd and then failed to write.
327 Why doesn't that mean that the fd is dead? It doesn't on some
328 systems it seems (eg. IRIX) */
331 rprintf(FERROR,"write exception\n");
340 if (read_buffer_len < MAX_READ_BUFFER)
341 read_check(buffer_f_in);
347 if (buffer_f_in != -1) {
348 FD_SET(buffer_f_in,&r_fds);
349 if (buffer_f_in > fd)
350 fd_count = buffer_f_in+1;
353 tv.tv_sec = BLOCKING_TIMEOUT;
355 count = select(fd_count,buffer_f_in == -1? NULL: &r_fds,
358 if (count == -1 && errno != EINTR) {
360 rprintf(FERROR,"select error: %s\n", strerror(errno));
369 if (FD_ISSET(fd, &w_fds)) {
378 last_io = time(NULL);
383 static char *io_buffer;
384 static int io_buffer_count;
386 void io_start_buffering(int fd)
388 io_buffer = (char *)malloc(IO_BUFFER_SIZE);
389 if (!io_buffer) out_of_memory("writefd");
393 void io_end_buffering(int fd)
395 if (io_buffer_count) {
396 if (writefd_unbuffered(fd, io_buffer,
399 rprintf(FERROR,"write failed\n");
408 static int writefd(int fd,char *buf,int len1)
412 if (!io_buffer) return writefd_unbuffered(fd, buf, len);
415 int n = MIN(len, IO_BUFFER_SIZE-io_buffer_count);
417 memcpy(io_buffer+io_buffer_count, buf, n);
420 io_buffer_count += n;
423 if (io_buffer_count == IO_BUFFER_SIZE) {
424 if (writefd_unbuffered(fd, io_buffer,
437 void write_int(int f,int32 x)
442 if ((ret=writefd(f,b,4)) != 4) {
443 rprintf(FERROR,"write_int failed : %s\n",
444 ret==-1?strerror(errno):"EOF");
450 void write_longint(int f, int64 x)
452 extern int remote_version;
456 if (remote_version < 16 || x <= 0x7FFFFFFF) {
457 write_int(f, (int)x);
462 SIVAL(b,0,(x&0xFFFFFFFF));
463 SIVAL(b,4,((x>>32)&0xFFFFFFFF));
465 if ((ret=writefd(f,b,8)) != 8) {
466 rprintf(FERROR,"write_longint failed : %s\n",
467 ret==-1?strerror(errno):"EOF");
473 void write_buf(int f,char *buf,int len)
476 if ((ret=writefd(f,buf,len)) != len) {
477 rprintf(FERROR,"write_buf failed : %s\n",
478 ret==-1?strerror(errno):"EOF");
481 total_written += len;
484 /* write a string to the connection */
485 void write_sbuf(int f,char *buf)
487 write_buf(f, buf, strlen(buf));
491 void write_byte(int f,unsigned char c)
493 write_buf(f,(char *)&c,1);
496 void write_flush(int f)
501 int read_line(int f, char *buf, int maxlen)
505 if (buf[0] == '\n') {
509 if (buf[0] != '\r') {
522 void io_printf(int fd, const char *format, ...)
528 va_start(ap, format);
529 len = vslprintf(buf, sizeof(buf)-1, format, ap);
532 if (len < 0) exit_cleanup(1);