Commit | Line | Data |
---|---|---|
720b47f2 AT |
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 | ||
71c46176 AT |
27 | static int64 total_written; |
28 | static int64 total_read; | |
720b47f2 AT |
29 | |
30 | extern int verbose; | |
dc5ddbcc | 31 | extern int sparse_files; |
6ba9279f | 32 | extern int io_timeout; |
720b47f2 | 33 | |
71c46176 | 34 | int64 write_total(void) |
720b47f2 AT |
35 | { |
36 | return total_written; | |
37 | } | |
38 | ||
71c46176 | 39 | int64 read_total(void) |
720b47f2 AT |
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 | ||
3a6a366f AT |
53 | static char *read_buffer; |
54 | static char *read_buffer_p; | |
55 | static int read_buffer_len; | |
56 | static int read_buffer_size; | |
720b47f2 AT |
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 | ||
05c629f7 AT |
66 | if (f == -1) return; |
67 | ||
720b47f2 AT |
68 | if (read_buffer_len == 0) { |
69 | read_buffer_p = read_buffer; | |
70 | } | |
71 | ||
72 | if ((n=num_waiting(f)) <= 0) | |
73 | return; | |
74 | ||
05c629f7 AT |
75 | /* things could deteriorate if we read in really small chunks */ |
76 | if (n < 10) n = 1024; | |
77 | ||
720b47f2 AT |
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 | ||
6ba9279f AT |
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) { | |
9486289c | 116 | rprintf(FERROR,"read timeout after %d second - exiting\n", |
6ba9279f AT |
117 | (int)(t-last_io)); |
118 | exit_cleanup(1); | |
119 | } | |
120 | } | |
720b47f2 AT |
121 | |
122 | static int readfd(int fd,char *buffer,int N) | |
123 | { | |
6ba9279f AT |
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; | |
a070c37b | 137 | total += ret; |
6ba9279f AT |
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 | ||
344fb127 AT |
151 | if (select(fd+1, &fds, NULL, NULL, |
152 | io_timeout?&tv:NULL) != 1) { | |
6ba9279f AT |
153 | check_timeout(); |
154 | } | |
155 | } | |
156 | ||
157 | if (ret <= 0) | |
158 | return total; | |
159 | total += ret; | |
7f28dbee | 160 | } |
720b47f2 | 161 | |
6ba9279f AT |
162 | if (io_timeout) |
163 | last_io = time(NULL); | |
720b47f2 | 164 | return total; |
720b47f2 AT |
165 | } |
166 | ||
167 | ||
b7922338 | 168 | int32 read_int(int f) |
720b47f2 | 169 | { |
4fe159a8 | 170 | int ret; |
720b47f2 | 171 | char b[4]; |
4fe159a8 | 172 | if ((ret=readfd(f,b,4)) != 4) { |
720b47f2 | 173 | if (verbose > 1) |
9486289c | 174 | rprintf(FERROR,"(%d) Error reading %d bytes : %s\n", |
9e31c482 | 175 | getpid(),4,ret==-1?strerror(errno):"EOF"); |
34ccb63e | 176 | exit_cleanup(1); |
720b47f2 AT |
177 | } |
178 | total_read += 4; | |
179 | return IVAL(b,0); | |
180 | } | |
181 | ||
71c46176 | 182 | int64 read_longint(int f) |
3a6a366f AT |
183 | { |
184 | extern int remote_version; | |
71c46176 | 185 | int64 ret; |
3a6a366f AT |
186 | char b[8]; |
187 | ret = read_int(f); | |
71c46176 | 188 | |
b7922338 | 189 | if ((int32)ret != (int32)0xffffffff) return ret; |
71c46176 | 190 | |
3bee6733 | 191 | #ifdef NO_INT64 |
9486289c | 192 | rprintf(FERROR,"Integer overflow - attempted 64 bit offset\n"); |
71c46176 AT |
193 | exit_cleanup(1); |
194 | #else | |
195 | if (remote_version >= 16) { | |
3a6a366f AT |
196 | if ((ret=readfd(f,b,8)) != 8) { |
197 | if (verbose > 1) | |
9486289c | 198 | rprintf(FERROR,"(%d) Error reading %d bytes : %s\n", |
3a6a366f AT |
199 | getpid(),8,ret==-1?strerror(errno):"EOF"); |
200 | exit_cleanup(1); | |
201 | } | |
202 | total_read += 8; | |
71c46176 | 203 | ret = IVAL(b,0) | (((int64)IVAL(b,4))<<32); |
3a6a366f | 204 | } |
71c46176 AT |
205 | #endif |
206 | ||
3a6a366f AT |
207 | return ret; |
208 | } | |
209 | ||
720b47f2 AT |
210 | void read_buf(int f,char *buf,int len) |
211 | { | |
4fe159a8 AT |
212 | int ret; |
213 | if ((ret=readfd(f,buf,len)) != len) { | |
720b47f2 | 214 | if (verbose > 1) |
9486289c | 215 | rprintf(FERROR,"(%d) Error reading %d bytes : %s\n", |
9e31c482 | 216 | getpid(),len,ret==-1?strerror(errno):"EOF"); |
34ccb63e | 217 | exit_cleanup(1); |
720b47f2 AT |
218 | } |
219 | total_read += len; | |
220 | } | |
221 | ||
575f2fca AT |
222 | void read_sbuf(int f,char *buf,int len) |
223 | { | |
224 | read_buf(f,buf,len); | |
225 | buf[len] = 0; | |
226 | } | |
227 | ||
182dca5c AT |
228 | unsigned char read_byte(int f) |
229 | { | |
d89322c4 AT |
230 | unsigned char c; |
231 | read_buf(f,(char *)&c,1); | |
232 | return c; | |
182dca5c | 233 | } |
720b47f2 | 234 | |
7bec6a5c | 235 | |
3a6a366f AT |
236 | static char last_byte; |
237 | static int last_sparse; | |
7bec6a5c AT |
238 | |
239 | int sparse_end(int f) | |
240 | { | |
d867229b | 241 | if (last_sparse) { |
73233f0f | 242 | do_lseek(f,-1,SEEK_CUR); |
d867229b AT |
243 | return (write(f,&last_byte,1) == 1 ? 0 : -1); |
244 | } | |
245 | last_sparse = 0; | |
246 | return 0; | |
7bec6a5c AT |
247 | } |
248 | ||
d867229b AT |
249 | |
250 | static int write_sparse(int f,char *buf,int len) | |
7bec6a5c | 251 | { |
d867229b AT |
252 | int l1=0,l2=0; |
253 | int ret; | |
7bec6a5c | 254 | |
d867229b AT |
255 | for (l1=0;l1<len && buf[l1]==0;l1++) ; |
256 | for (l2=0;l2<(len-l1) && buf[len-(l2+1)]==0;l2++) ; | |
7bec6a5c | 257 | |
d867229b | 258 | last_byte = buf[len-1]; |
7bec6a5c | 259 | |
d867229b AT |
260 | if (l1 == len || l2 > 0) |
261 | last_sparse=1; | |
7bec6a5c | 262 | |
d867229b | 263 | if (l1 > 0) |
73233f0f | 264 | do_lseek(f,l1,SEEK_CUR); |
dc5ddbcc | 265 | |
d867229b AT |
266 | if (l1 == len) |
267 | return len; | |
dc5ddbcc | 268 | |
d867229b AT |
269 | if ((ret=write(f,buf+l1,len-(l1+l2))) != len-(l1+l2)) { |
270 | if (ret == -1 || ret == 0) return ret; | |
271 | return (l1+ret); | |
272 | } | |
7bec6a5c | 273 | |
d867229b | 274 | if (l2 > 0) |
73233f0f | 275 | do_lseek(f,l2,SEEK_CUR); |
d867229b AT |
276 | |
277 | return len; | |
278 | } | |
dc5ddbcc | 279 | |
7bec6a5c | 280 | |
d867229b AT |
281 | |
282 | int write_file(int f,char *buf,int len) | |
283 | { | |
284 | int ret = 0; | |
285 | ||
286 | if (!sparse_files) | |
287 | return write(f,buf,len); | |
288 | ||
289 | while (len>0) { | |
290 | int len1 = MIN(len, SPARSE_WRITE_SIZE); | |
291 | int r1 = write_sparse(f, buf, len1); | |
292 | if (r1 <= 0) { | |
293 | if (ret > 0) return ret; | |
294 | return r1; | |
295 | } | |
296 | len -= r1; | |
297 | buf += r1; | |
298 | ret += r1; | |
299 | } | |
300 | return ret; | |
7bec6a5c AT |
301 | } |
302 | ||
720b47f2 | 303 | |
d6dead6b | 304 | static int writefd_unbuffered(int fd,char *buf,int len) |
720b47f2 AT |
305 | { |
306 | int total = 0; | |
05c629f7 | 307 | fd_set w_fds, r_fds; |
e92338c8 | 308 | int fd_count, count, got_select=0; |
58d433ab | 309 | struct timeval tv; |
720b47f2 AT |
310 | |
311 | if (buffer_f_in == -1) | |
312 | return write(fd,buf,len); | |
313 | ||
314 | while (total < len) { | |
315 | int ret = write(fd,buf+total,len-total); | |
316 | ||
317 | if (ret == 0) return total; | |
318 | ||
4fe159a8 AT |
319 | if (ret == -1 && !(errno == EWOULDBLOCK || errno == EAGAIN)) |
320 | return -1; | |
720b47f2 | 321 | |
e92338c8 | 322 | if (ret == -1 && got_select) { |
97d6916e AT |
323 | /* hmmm, we got a write select on the fd and then failed to write. |
324 | Why doesn't that mean that the fd is dead? It doesn't on some | |
325 | systems it seems (eg. IRIX) */ | |
feaa89c4 | 326 | u_sleep(1000); |
97d6916e | 327 | #if 0 |
9486289c | 328 | rprintf(FERROR,"write exception\n"); |
e92338c8 | 329 | exit_cleanup(1); |
97d6916e | 330 | #endif |
e92338c8 AT |
331 | } |
332 | ||
9a52223b AT |
333 | got_select = 0; |
334 | ||
335 | ||
720b47f2 AT |
336 | if (ret == -1) { |
337 | read_check(buffer_f_in); | |
338 | ||
05c629f7 AT |
339 | fd_count = fd+1; |
340 | FD_ZERO(&w_fds); | |
341 | FD_ZERO(&r_fds); | |
342 | FD_SET(fd,&w_fds); | |
343 | if (buffer_f_in != -1) { | |
344 | FD_SET(buffer_f_in,&r_fds); | |
345 | if (buffer_f_in > fd) | |
346 | fd_count = buffer_f_in+1; | |
347 | } | |
e92338c8 | 348 | |
58d433ab AT |
349 | tv.tv_sec = BLOCKING_TIMEOUT; |
350 | tv.tv_usec = 0; | |
e92338c8 AT |
351 | count = select(fd_count,buffer_f_in == -1? NULL: &r_fds, |
352 | &w_fds,NULL,&tv); | |
353 | if (count == -1 && errno != EINTR) { | |
354 | if (verbose > 1) | |
9486289c | 355 | rprintf(FERROR,"select error: %s\n", strerror(errno)); |
e92338c8 AT |
356 | exit_cleanup(1); |
357 | } | |
358 | ||
6ba9279f AT |
359 | if (count == 0) { |
360 | check_timeout(); | |
361 | continue; | |
362 | } | |
e92338c8 AT |
363 | |
364 | if (FD_ISSET(fd, &w_fds)) { | |
365 | got_select = 1; | |
366 | } | |
720b47f2 AT |
367 | } else { |
368 | total += ret; | |
369 | } | |
370 | } | |
371 | ||
6ba9279f AT |
372 | if (io_timeout) |
373 | last_io = time(NULL); | |
374 | ||
720b47f2 AT |
375 | return total; |
376 | } | |
377 | ||
d6dead6b AT |
378 | static char *io_buffer; |
379 | static int io_buffer_count; | |
380 | ||
381 | void io_start_buffering(int fd) | |
382 | { | |
383 | io_buffer = (char *)malloc(IO_BUFFER_SIZE); | |
384 | if (!io_buffer) out_of_memory("writefd"); | |
385 | io_buffer_count = 0; | |
386 | } | |
387 | ||
388 | void io_end_buffering(int fd) | |
389 | { | |
390 | if (io_buffer_count) { | |
391 | if (writefd_unbuffered(fd, io_buffer, | |
392 | io_buffer_count) != | |
393 | io_buffer_count) { | |
394 | rprintf(FERROR,"write failed\n"); | |
395 | exit_cleanup(1); | |
396 | } | |
397 | io_buffer_count = 0; | |
398 | } | |
399 | free(io_buffer); | |
400 | io_buffer = NULL; | |
401 | } | |
402 | ||
403 | static int writefd(int fd,char *buf,int len1) | |
404 | { | |
405 | int len = len1; | |
406 | ||
407 | if (!io_buffer) return writefd_unbuffered(fd, buf, len); | |
408 | ||
409 | while (len) { | |
410 | int n = MIN(len, IO_BUFFER_SIZE-io_buffer_count); | |
411 | if (n > 0) { | |
412 | memcpy(io_buffer+io_buffer_count, buf, n); | |
413 | buf += n; | |
414 | len -= n; | |
415 | io_buffer_count += n; | |
416 | } | |
417 | ||
418 | if (io_buffer_count == IO_BUFFER_SIZE) { | |
419 | if (writefd_unbuffered(fd, io_buffer, | |
420 | io_buffer_count) != | |
421 | io_buffer_count) { | |
422 | return -1; | |
423 | } | |
424 | io_buffer_count = 0; | |
425 | } | |
426 | } | |
427 | ||
428 | return len1; | |
429 | } | |
720b47f2 AT |
430 | |
431 | ||
b7922338 | 432 | void write_int(int f,int32 x) |
720b47f2 | 433 | { |
4fe159a8 | 434 | int ret; |
720b47f2 AT |
435 | char b[4]; |
436 | SIVAL(b,0,x); | |
4fe159a8 | 437 | if ((ret=writefd(f,b,4)) != 4) { |
9486289c | 438 | rprintf(FERROR,"write_int failed : %s\n", |
4fe159a8 | 439 | ret==-1?strerror(errno):"EOF"); |
34ccb63e | 440 | exit_cleanup(1); |
720b47f2 AT |
441 | } |
442 | total_written += 4; | |
443 | } | |
444 | ||
71c46176 | 445 | void write_longint(int f, int64 x) |
3a6a366f AT |
446 | { |
447 | extern int remote_version; | |
448 | char b[8]; | |
449 | int ret; | |
450 | ||
451 | if (remote_version < 16 || x <= 0x7FFFFFFF) { | |
452 | write_int(f, (int)x); | |
453 | return; | |
454 | } | |
455 | ||
456 | write_int(f, -1); | |
457 | SIVAL(b,0,(x&0xFFFFFFFF)); | |
458 | SIVAL(b,4,((x>>32)&0xFFFFFFFF)); | |
459 | ||
460 | if ((ret=writefd(f,b,8)) != 8) { | |
9486289c | 461 | rprintf(FERROR,"write_longint failed : %s\n", |
3a6a366f AT |
462 | ret==-1?strerror(errno):"EOF"); |
463 | exit_cleanup(1); | |
464 | } | |
465 | total_written += 8; | |
466 | } | |
467 | ||
720b47f2 AT |
468 | void write_buf(int f,char *buf,int len) |
469 | { | |
4fe159a8 AT |
470 | int ret; |
471 | if ((ret=writefd(f,buf,len)) != len) { | |
9486289c | 472 | rprintf(FERROR,"write_buf failed : %s\n", |
4fe159a8 | 473 | ret==-1?strerror(errno):"EOF"); |
34ccb63e | 474 | exit_cleanup(1); |
720b47f2 AT |
475 | } |
476 | total_written += len; | |
477 | } | |
478 | ||
f0fca04e AT |
479 | /* write a string to the connection */ |
480 | void write_sbuf(int f,char *buf) | |
481 | { | |
482 | write_buf(f, buf, strlen(buf)); | |
483 | } | |
484 | ||
720b47f2 | 485 | |
182dca5c AT |
486 | void write_byte(int f,unsigned char c) |
487 | { | |
f0fca04e | 488 | write_buf(f,(char *)&c,1); |
182dca5c AT |
489 | } |
490 | ||
720b47f2 AT |
491 | void write_flush(int f) |
492 | { | |
493 | } | |
494 | ||
495 | ||
f0fca04e AT |
496 | int read_line(int f, char *buf, int maxlen) |
497 | { | |
498 | while (maxlen) { | |
499 | read_buf(f, buf, 1); | |
500 | if (buf[0] == '\n') { | |
501 | buf[0] = 0; | |
502 | break; | |
503 | } | |
504 | if (buf[0] != '\r') { | |
505 | buf++; | |
506 | maxlen--; | |
507 | } | |
508 | } | |
509 | if (maxlen == 0) { | |
510 | *buf = 0; | |
511 | return 0; | |
512 | } | |
513 | return 1; | |
514 | } | |
515 | ||
516 | ||
517 | void io_printf(int fd, const char *format, ...) | |
518 | { | |
519 | va_list ap; | |
520 | char buf[1024]; | |
521 | int len; | |
522 | ||
523 | va_start(ap, format); | |
e42c9458 | 524 | len = vslprintf(buf, sizeof(buf)-1, format, ap); |
f0fca04e AT |
525 | va_end(ap); |
526 | ||
527 | if (len < 0) exit_cleanup(1); | |
528 | ||
529 | write_sbuf(fd, buf); | |
530 | } |