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