fixed a bug I introduced in the last big commit
[rsync/rsync.git] / io.c
1 /* 
2    Copyright (C) Andrew Tridgell 1996
3    Copyright (C) Paul Mackerras 1996
4    
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.
9    
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.
14    
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.
18 */
19
20 /*
21   Utilities used in rsync 
22
23   tridge, June 1996
24   */
25 #include "rsync.h"
26
27 static int64 total_written;
28 static int64 total_read;
29
30 extern int verbose;
31 extern int sparse_files;
32 extern int io_timeout;
33
34 int64 write_total(void)
35 {
36   return total_written;
37 }
38
39 int64 read_total(void)
40 {
41   return total_read;
42 }
43
44 static int buffer_f_in = -1;
45
46 void setup_nonblocking(int f_in,int f_out)
47 {
48   set_blocking(f_out,0);
49   buffer_f_in = f_in;
50 }
51
52
53 static char *read_buffer;
54 static char *read_buffer_p;
55 static int read_buffer_len;
56 static int read_buffer_size;
57
58
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)
63 {
64   int n;
65
66   if (f == -1) return;
67
68   if (read_buffer_len == 0) {
69     read_buffer_p = read_buffer;
70   }
71
72   if ((n=num_waiting(f)) <= 0)
73     return;
74
75   /* things could deteriorate if we read in really small chunks */
76   if (n < 10) n = 1024;
77
78   if (read_buffer_p != read_buffer) {
79     memmove(read_buffer,read_buffer_p,read_buffer_len);
80     read_buffer_p = read_buffer;
81   }
82
83   if (n > (read_buffer_size - read_buffer_len)) {
84     read_buffer_size += n;
85     if (!read_buffer)
86       read_buffer = (char *)malloc(read_buffer_size);
87     else
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;      
91   }
92
93   n = read(f,read_buffer+read_buffer_len,n);
94   if (n > 0) {
95     read_buffer_len += n;
96   }
97 }
98
99 static time_t last_io;
100
101
102 static void check_timeout(void)
103 {
104         time_t t;
105         
106         if (!io_timeout) return;
107
108         if (!last_io) {
109                 last_io = time(NULL);
110                 return;
111         }
112
113         t = time(NULL);
114
115         if (last_io && io_timeout && (t-last_io)>io_timeout) {
116                 fprintf(FERROR,"read timeout after %d second - exiting\n", 
117                         (int)(t-last_io));
118                 exit_cleanup(1);
119         }
120 }
121
122 static int readfd(int fd,char *buffer,int N)
123 {
124         int  ret;
125         int total=0;  
126         struct timeval tv;
127         
128         if (read_buffer_len < N)
129                 read_check(buffer_f_in);
130         
131         while (total < N) {
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;
137                         total += ret;
138                         continue;
139                 } 
140
141                 while ((ret = read(fd,buffer + total,N-total)) == -1) {
142                         fd_set fds;
143
144                         if (errno != EAGAIN && errno != EWOULDBLOCK)
145                                 return -1;
146                         FD_ZERO(&fds);
147                         FD_SET(fd, &fds);
148                         tv.tv_sec = io_timeout;
149                         tv.tv_usec = 0;
150
151                         if (select(fd+1, &fds, NULL, NULL, &tv) != 1) {
152                                 check_timeout();
153                         }
154                 }
155
156                 if (ret <= 0)
157                         return total;
158                 total += ret;
159         }
160
161         if (io_timeout)
162                 last_io = time(NULL);
163         return total;
164 }
165
166
167 int read_int(int f)
168 {
169   int ret;
170   char b[4];
171   if ((ret=readfd(f,b,4)) != 4) {
172     if (verbose > 1) 
173       fprintf(FERROR,"(%d) Error reading %d bytes : %s\n",
174               getpid(),4,ret==-1?strerror(errno):"EOF");
175     exit_cleanup(1);
176   }
177   total_read += 4;
178   return IVAL(b,0);
179 }
180
181 int64 read_longint(int f)
182 {
183         extern int remote_version;
184         int64 ret;
185         char b[8];
186         ret = read_int(f);
187
188         if (ret != -1) return ret;
189
190 #ifndef HAVE_LONGLONG
191         fprintf(FERROR,"Integer overflow - attempted 64 bit offset\n");
192         exit_cleanup(1);
193 #else
194         if (remote_version >= 16) {
195                 if ((ret=readfd(f,b,8)) != 8) {
196                         if (verbose > 1) 
197                                 fprintf(FERROR,"(%d) Error reading %d bytes : %s\n",
198                                         getpid(),8,ret==-1?strerror(errno):"EOF");
199                         exit_cleanup(1);
200                 }
201                 total_read += 8;
202                 ret = IVAL(b,0) | (((int64)IVAL(b,4))<<32);
203         }
204 #endif
205
206         return ret;
207 }
208
209 void read_buf(int f,char *buf,int len)
210 {
211   int ret;
212   if ((ret=readfd(f,buf,len)) != len) {
213     if (verbose > 1) 
214       fprintf(FERROR,"(%d) Error reading %d bytes : %s\n",
215               getpid(),len,ret==-1?strerror(errno):"EOF");
216     exit_cleanup(1);
217   }
218   total_read += len;
219 }
220
221 unsigned char read_byte(int f)
222 {
223   unsigned char c;
224   read_buf(f,(char *)&c,1);
225   return c;
226 }
227
228
229 static char last_byte;
230 static int last_sparse;
231
232 int sparse_end(int f)
233 {
234   if (last_sparse) {
235     lseek(f,-1,SEEK_CUR);
236     return (write(f,&last_byte,1) == 1 ? 0 : -1);
237   }
238   last_sparse = 0;
239   return 0;
240 }
241
242 int write_sparse(int f,char *buf,int len)
243 {
244   int l1=0,l2=0;
245   int ret;
246
247   if (!sparse_files) 
248     return write(f,buf,len);
249
250   for (l1=0;l1<len && buf[l1]==0;l1++) ;
251   for (l2=0;l2<(len-l1) && buf[len-(l2+1)]==0;l2++) ;
252
253   last_byte = buf[len-1];
254
255   if (l1 == len || l2 > 0)
256     last_sparse=1;
257
258   if (l1 > 0)
259     lseek(f,l1,SEEK_CUR);  
260
261   if (l1 == len) 
262     return len;
263
264   if ((ret=write(f,buf+l1,len-(l1+l2))) != len-(l1+l2)) {
265     if (ret == -1 || ret == 0) return ret;
266     return (l1+ret);
267   }
268
269   if (l2 > 0)
270     lseek(f,l2,SEEK_CUR);
271
272   return len;
273 }
274
275
276 static int writefd(int fd,char *buf,int len)
277 {
278   int total = 0;
279   fd_set w_fds, r_fds;
280   int fd_count, count, got_select=0;
281   struct timeval tv;
282
283   if (buffer_f_in == -1) 
284     return write(fd,buf,len);
285
286   while (total < len) {
287     int ret = write(fd,buf+total,len-total);
288
289     if (ret == 0) return total;
290
291     if (ret == -1 && !(errno == EWOULDBLOCK || errno == EAGAIN)) 
292       return -1;
293
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) */
298             u_sleep(1000);
299 #if 0
300             fprintf(FERROR,"write exception\n");
301             exit_cleanup(1);
302 #endif
303     }
304
305     got_select = 0;
306
307
308     if (ret == -1) {
309       read_check(buffer_f_in);
310
311       fd_count = fd+1;
312       FD_ZERO(&w_fds);
313       FD_ZERO(&r_fds);
314       FD_SET(fd,&w_fds);
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;
319       }
320
321       tv.tv_sec = BLOCKING_TIMEOUT;
322       tv.tv_usec = 0;
323       count = select(fd_count,buffer_f_in == -1? NULL: &r_fds,
324                      &w_fds,NULL,&tv);
325       if (count == -1 && errno != EINTR) {
326               if (verbose > 1) 
327                       fprintf(FERROR,"select error: %s\n", strerror(errno));
328               exit_cleanup(1);
329       }
330
331       if (count == 0) {
332               check_timeout();
333               continue;
334       }
335       
336       if (FD_ISSET(fd, &w_fds)) {
337               got_select = 1;
338       }
339     } else {
340       total += ret;
341     }
342   }
343
344   if (io_timeout)
345           last_io = time(NULL);
346
347   return total;
348 }
349
350
351
352 void write_int(int f,int x)
353 {
354   int ret;
355   char b[4];
356   SIVAL(b,0,x);
357   if ((ret=writefd(f,b,4)) != 4) {
358     fprintf(FERROR,"write_int failed : %s\n",
359             ret==-1?strerror(errno):"EOF");
360     exit_cleanup(1);
361   }
362   total_written += 4;
363 }
364
365 void write_longint(int f, int64 x)
366 {
367         extern int remote_version;
368         char b[8];
369         int ret;
370
371         if (remote_version < 16 || x <= 0x7FFFFFFF) {
372                 write_int(f, (int)x);
373                 return;
374         }
375
376         write_int(f, -1);
377         SIVAL(b,0,(x&0xFFFFFFFF));
378         SIVAL(b,4,((x>>32)&0xFFFFFFFF));
379
380         if ((ret=writefd(f,b,8)) != 8) {
381                 fprintf(FERROR,"write_longint failed : %s\n",
382                         ret==-1?strerror(errno):"EOF");
383                 exit_cleanup(1);
384         }
385         total_written += 8;
386 }
387
388 void write_buf(int f,char *buf,int len)
389 {
390   int ret;
391   if ((ret=writefd(f,buf,len)) != len) {
392     fprintf(FERROR,"write_buf failed : %s\n",
393             ret==-1?strerror(errno):"EOF");
394     exit_cleanup(1);
395   }
396   total_written += len;
397 }
398
399
400 void write_byte(int f,unsigned char c)
401 {
402   write_buf(f,(char *)&c,1);
403 }
404
405 void write_flush(int f)
406 {
407 }
408
409