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;
33 int64 write_total(void)
38 int64 read_total(void)
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;
53 static char *read_buffer_p;
54 static int read_buffer_len;
55 static int read_buffer_size;
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)
67 if (read_buffer_len == 0) {
68 read_buffer_p = read_buffer;
71 if ((n=num_waiting(f)) <= 0)
74 /* things could deteriorate if we read in really small chunks */
77 if (read_buffer_p != read_buffer) {
78 memmove(read_buffer,read_buffer_p,read_buffer_len);
79 read_buffer_p = read_buffer;
82 if (n > (read_buffer_size - read_buffer_len)) {
83 read_buffer_size += n;
85 read_buffer = (char *)malloc(read_buffer_size);
87 read_buffer = (char *)realloc(read_buffer,read_buffer_size);
88 if (!read_buffer) out_of_memory("read check");
89 read_buffer_p = read_buffer;
92 n = read(f,read_buffer+read_buffer_len,n);
99 static int readfd(int fd,char *buffer,int N)
104 if (read_buffer_len < N)
105 read_check(buffer_f_in);
109 if (read_buffer_len > 0 && buffer_f_in == fd) {
110 ret = MIN(read_buffer_len,N-total);
111 memcpy(buffer+total,read_buffer_p,ret);
112 read_buffer_p += ret;
113 read_buffer_len -= ret;
115 while ((ret = read(fd,buffer + total,N - total)) == -1) {
118 if (errno != EAGAIN && errno != EWOULDBLOCK)
122 select(fd+1, &fds, NULL, NULL, NULL);
138 if ((ret=readfd(f,b,4)) != 4) {
140 fprintf(FERROR,"(%d) Error reading %d bytes : %s\n",
141 getpid(),4,ret==-1?strerror(errno):"EOF");
148 int64 read_longint(int f)
150 extern int remote_version;
155 if (ret != -1) return ret;
157 #ifndef HAVE_LONGLONG
158 fprintf(FERROR,"Integer overflow - attempted 64 bit offset\n");
161 if (remote_version >= 16) {
162 if ((ret=readfd(f,b,8)) != 8) {
164 fprintf(FERROR,"(%d) Error reading %d bytes : %s\n",
165 getpid(),8,ret==-1?strerror(errno):"EOF");
169 ret = IVAL(b,0) | (((int64)IVAL(b,4))<<32);
176 void read_buf(int f,char *buf,int len)
179 if ((ret=readfd(f,buf,len)) != len) {
181 fprintf(FERROR,"(%d) Error reading %d bytes : %s\n",
182 getpid(),len,ret==-1?strerror(errno):"EOF");
188 unsigned char read_byte(int f)
191 read_buf(f,(char *)&c,1);
196 static char last_byte;
197 static int last_sparse;
199 int sparse_end(int f)
202 lseek(f,-1,SEEK_CUR);
203 return (write(f,&last_byte,1) == 1 ? 0 : -1);
209 int write_sparse(int f,char *buf,int len)
215 return write(f,buf,len);
217 for (l1=0;l1<len && buf[l1]==0;l1++) ;
218 for (l2=0;l2<(len-l1) && buf[len-(l2+1)]==0;l2++) ;
220 last_byte = buf[len-1];
222 if (l1 == len || l2 > 0)
226 lseek(f,l1,SEEK_CUR);
231 if ((ret=write(f,buf+l1,len-(l1+l2))) != len-(l1+l2)) {
232 if (ret == -1 || ret == 0) return ret;
237 lseek(f,l2,SEEK_CUR);
243 static int writefd(int fd,char *buf,int len)
247 int fd_count, count, got_select=0;
250 if (buffer_f_in == -1)
251 return write(fd,buf,len);
253 while (total < len) {
254 int ret = write(fd,buf+total,len-total);
256 if (ret == 0) return total;
258 if (ret == -1 && !(errno == EWOULDBLOCK || errno == EAGAIN))
261 if (ret == -1 && got_select) {
262 /* hmmm, we got a write select on the fd and then failed to write.
263 Why doesn't that mean that the fd is dead? It doesn't on some
264 systems it seems (eg. IRIX) */
267 fprintf(FERROR,"write exception\n");
276 read_check(buffer_f_in);
282 if (buffer_f_in != -1) {
283 FD_SET(buffer_f_in,&r_fds);
284 if (buffer_f_in > fd)
285 fd_count = buffer_f_in+1;
288 tv.tv_sec = BLOCKING_TIMEOUT;
290 count = select(fd_count,buffer_f_in == -1? NULL: &r_fds,
292 if (count == -1 && errno != EINTR) {
294 fprintf(FERROR,"select error: %s\n", strerror(errno));
298 if (count == 0) continue;
300 if (FD_ISSET(fd, &w_fds)) {
313 void write_int(int f,int x)
318 if ((ret=writefd(f,b,4)) != 4) {
319 fprintf(FERROR,"write_int failed : %s\n",
320 ret==-1?strerror(errno):"EOF");
326 void write_longint(int f, int64 x)
328 extern int remote_version;
332 if (remote_version < 16 || x <= 0x7FFFFFFF) {
333 write_int(f, (int)x);
338 SIVAL(b,0,(x&0xFFFFFFFF));
339 SIVAL(b,4,((x>>32)&0xFFFFFFFF));
341 if ((ret=writefd(f,b,8)) != 8) {
342 fprintf(FERROR,"write_longint failed : %s\n",
343 ret==-1?strerror(errno):"EOF");
349 void write_buf(int f,char *buf,int len)
352 if ((ret=writefd(f,buf,len)) != len) {
353 fprintf(FERROR,"write_buf failed : %s\n",
354 ret==-1?strerror(errno):"EOF");
357 total_written += len;
361 void write_byte(int f,unsigned char c)
363 write_buf(f,(char *)&c,1);
366 void write_flush(int f)