Added one more test.
[rsync/rsync.git] / io.c
CommitLineData
7a24c346 1/* -*- c-file-style: "linux" -*-
880da007
MP
2 *
3 * Copyright (C) 1996-2001 by Andrew Tridgell
4 * Copyright (C) Paul Mackerras 1996
5 * Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
720b47f2 21
87ee2481 22/**
87ee2481
MP
23 * @file io.c
24 *
25 * Socket and pipe IO utilities used in rsync.
26 *
27 * rsync provides its own multiplexing system, which is used to send
28 * stderr and stdout over a single socket. We need this because
29 * stdout normally carries the binary data stream, and stderr all our
30 * error messages.
31 *
32 * For historical reasons this is off during the start of the
33 * connection, but it's switched on quite early using
34 * io_start_multiplex_out() and io_start_multiplex_in().
35 **/
720b47f2 36
720b47f2
AT
37#include "rsync.h"
38
880da007 39/** If no timeout is specified then use a 60 second select timeout */
8cd9fd4e
AT
40#define SELECT_TIMEOUT 60
41
8d9dc9f9
AT
42static int io_multiplexing_out;
43static int io_multiplexing_in;
679e7657
AT
44static int multiplex_in_fd;
45static int multiplex_out_fd;
8d9dc9f9 46static time_t last_io;
7a55d06e
MP
47static int no_flush;
48
49extern int bwlimit;
720b47f2 50extern int verbose;
6ba9279f 51extern int io_timeout;
a800434a 52extern struct stats stats;
720b47f2 53
7a55d06e 54
a86179f4 55const char phase_unknown[] = "unknown";
805edf9d 56
98b332ed
MP
57/**
58 * The connection might be dropped at some point; perhaps because the
59 * remote instance crashed. Just giving the offset on the stream is
60 * not very helpful. So instead we try to make io_phase_name point to
61 * something useful.
eca2adb4 62 *
805edf9d
MP
63 * For buffered/multiplexed IO these names will be somewhat
64 * approximate; perhaps for ease of support we would rather make the
65 * buffer always flush when a single application-level IO finishes.
66 *
eca2adb4
MP
67 * @todo Perhaps we want some simple stack functionality, but there's
68 * no need to overdo it.
98b332ed 69 **/
805edf9d
MP
70const char *io_write_phase = phase_unknown;
71const char *io_read_phase = phase_unknown;
98b332ed 72
7a55d06e
MP
73/** Ignore EOF errors while reading a module listing if the remote
74 version is 24 or less. */
75int kludge_around_eof = False;
76
77
554e0a8d 78static int io_error_fd = -1;
56014c8c
WD
79static int io_filesfrom_f_in = -1;
80static int io_filesfrom_f_out = -1;
81static char io_filesfrom_buf[2048];
82static char *io_filesfrom_bp;
83static char io_filesfrom_lastchar;
84static int io_filesfrom_buflen;
720b47f2 85
9dd891bb 86static void read_loop(int fd, char *buf, size_t len);
ff41a59f 87
8d9dc9f9
AT
88static void check_timeout(void)
89{
0adb99b9 90 extern int am_server, am_daemon;
8d9dc9f9 91 time_t t;
90ba34e2
AT
92
93 err_list_push();
8d9dc9f9
AT
94
95 if (!io_timeout) return;
96
97 if (!last_io) {
98 last_io = time(NULL);
99 return;
100 }
101
102 t = time(NULL);
103
86ffe37f 104 if (last_io && io_timeout && (t-last_io) >= io_timeout) {
0adb99b9 105 if (!am_server && !am_daemon) {
ce6c7c63 106 rprintf(FERROR,"io timeout after %d seconds - exiting\n",
0adb99b9
AT
107 (int)(t-last_io));
108 }
65417579 109 exit_cleanup(RERR_TIMEOUT);
8d9dc9f9
AT
110 }
111}
112
c979dad5 113/** Setup the fd used to propagate errors */
554e0a8d
AT
114void io_set_error_fd(int fd)
115{
116 io_error_fd = fd;
117}
118
880da007 119/** Read some data from the error fd and write it to the write log code */
554e0a8d
AT
120static void read_error_fd(void)
121{
122 char buf[200];
06ce139f 123 size_t n;
554e0a8d 124 int fd = io_error_fd;
ff41a59f
AT
125 int tag, len;
126
56014c8c
WD
127 /* io_error_fd is temporarily disabled -- is this meant to
128 * prevent indefinite recursion? */
554e0a8d
AT
129 io_error_fd = -1;
130
ff41a59f
AT
131 read_loop(fd, buf, 4);
132 tag = IVAL(buf, 0);
133
134 len = tag & 0xFFFFFF;
135 tag = tag >> 24;
136 tag -= MPLEX_BASE;
137
138 while (len) {
139 n = len;
06ce139f
MP
140 if (n > (sizeof(buf)-1))
141 n = sizeof(buf)-1;
ff41a59f
AT
142 read_loop(fd, buf, n);
143 rwrite((enum logcode)tag, buf, n);
144 len -= n;
554e0a8d
AT
145 }
146
147 io_error_fd = fd;
148}
149
56014c8c
WD
150/**
151 * When we're the receiver and we have a local --files-from list of names
152 * that needs to be sent over the socket to the sender, we have to do two
153 * things at the same time: send the sender a list of what files we're
154 * processing and read the incoming file+info list from the sender. We do
155 * this by augmenting the read_timeout() function to copy this data. It
156 * uses the io_filesfrom_buf to read a block of data from f_in (when it is
157 * ready, since it might be a pipe) and then blast it out f_out (when it
158 * is ready to receive more data).
159 */
160void io_set_filesfrom_fds(int f_in, int f_out)
161{
162 io_filesfrom_f_in = f_in;
163 io_filesfrom_f_out = f_out;
164 io_filesfrom_bp = io_filesfrom_buf;
165 io_filesfrom_lastchar = '\0';
166 io_filesfrom_buflen = 0;
167}
720b47f2 168
880da007
MP
169/**
170 * It's almost always an error to get an EOF when we're trying to read
171 * from the network, because the protocol is self-terminating.
172 *
173 * However, there is one unfortunate cases where it is not, which is
174 * rsync <2.4.6 sending a list of modules on a server, since the list
175 * is terminated by closing the socket. So, for the section of the
176 * program where that is a problem (start_socket_client),
177 * kludge_around_eof is True and we just exit.
178 */
7a55d06e
MP
179static void whine_about_eof (void)
180{
7a55d06e
MP
181 if (kludge_around_eof)
182 exit_cleanup (0);
183 else {
184 rprintf (FERROR,
185 "%s: connection unexpectedly closed "
186 "(%.0f bytes read so far)\n",
187 RSYNC_NAME, (double)stats.total_read);
188
189 exit_cleanup (RERR_STREAMIO);
190 }
191}
720b47f2 192
7a55d06e
MP
193
194static void die_from_readerr (int err)
195{
196 /* this prevents us trying to write errors on a dead socket */
197 io_multiplexing_close();
198
199 rprintf(FERROR, "%s: read error: %s\n",
200 RSYNC_NAME, strerror (err));
201 exit_cleanup(RERR_STREAMIO);
202}
203
204
880da007 205/**
c3563c46
MP
206 * Read from a socket with IO timeout. return the number of bytes
207 * read. If no bytes can be read then exit, never return a number <= 0.
208 *
8886f8d0
MP
209 * TODO: If the remote shell connection fails, then current versions
210 * actually report an "unexpected EOF" error here. Since it's a
211 * fairly common mistake to try to use rsh when ssh is required, we
212 * should trap that: if we fail to read any data at all, we should
213 * give a better explanation. We can tell whether the connection has
214 * started by looking e.g. at whether the remote version is known yet.
c3563c46 215 */
9dd891bb 216static int read_timeout (int fd, char *buf, size_t len)
8d9dc9f9 217{
4c36ddbe
AT
218 int n, ret=0;
219
ea2111d1
AT
220 io_flush();
221
4c36ddbe 222 while (ret == 0) {
7a55d06e 223 /* until we manage to read *something* */
56014c8c 224 fd_set r_fds, w_fds;
4c36ddbe 225 struct timeval tv;
554e0a8d 226 int fd_count = fd+1;
a57873b7 227 int count;
4c36ddbe 228
56014c8c
WD
229 FD_ZERO(&r_fds);
230 FD_SET(fd, &r_fds);
554e0a8d 231 if (io_error_fd != -1) {
56014c8c
WD
232 FD_SET(io_error_fd, &r_fds);
233 if (io_error_fd >= fd_count) fd_count = io_error_fd+1;
234 }
235 if (io_filesfrom_f_out != -1) {
236 int new_fd;
237 if (io_filesfrom_buflen == 0) {
238 if (io_filesfrom_f_in != -1) {
239 FD_SET(io_filesfrom_f_in, &r_fds);
240 new_fd = io_filesfrom_f_in;
241 } else {
242 io_filesfrom_f_out = -1;
243 new_fd = -1;
244 }
245 } else {
246 FD_ZERO(&w_fds);
247 FD_SET(io_filesfrom_f_out, &w_fds);
248 new_fd = io_filesfrom_f_out;
249 }
250 if (new_fd >= fd_count) fd_count = new_fd+1;
554e0a8d
AT
251 }
252
8cd9fd4e 253 tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT;
4c36ddbe
AT
254 tv.tv_usec = 0;
255
554e0a8d
AT
256 errno = 0;
257
56014c8c
WD
258 count = select(fd_count, &r_fds,
259 io_filesfrom_buflen? &w_fds : NULL,
260 NULL, &tv);
a57873b7
AT
261
262 if (count == 0) {
263 check_timeout();
264 }
265
266 if (count <= 0) {
554e0a8d
AT
267 if (errno == EBADF) {
268 exit_cleanup(RERR_SOCKETIO);
269 }
4c36ddbe
AT
270 continue;
271 }
272
56014c8c
WD
273
274 if (io_error_fd != -1 && FD_ISSET(io_error_fd, &r_fds)) {
554e0a8d
AT
275 read_error_fd();
276 }
277
56014c8c
WD
278 if (io_filesfrom_f_out != -1) {
279 if (io_filesfrom_buflen) {
280 if (FD_ISSET(io_filesfrom_f_out, &w_fds)) {
281 int l = write(io_filesfrom_f_out,
282 io_filesfrom_bp,
283 io_filesfrom_buflen);
284 if (l > 0) {
285 if (!(io_filesfrom_buflen -= l))
286 io_filesfrom_bp = io_filesfrom_buf;
287 else
288 io_filesfrom_bp += l;
289 } else {
290 /* XXX should we complain? */
291 io_filesfrom_f_out = -1;
292 }
293 }
294 } else if (io_filesfrom_f_in != -1) {
295 if (FD_ISSET(io_filesfrom_f_in, &r_fds)) {
296 int l = read(io_filesfrom_f_in,
297 io_filesfrom_buf,
298 sizeof io_filesfrom_buf);
299 if (l <= 0) {
300 /* Send end-of-file marker */
301 io_filesfrom_buf[0] = '\0';
302 io_filesfrom_buf[1] = '\0';
303 io_filesfrom_buflen = io_filesfrom_lastchar? 2 : 1;
304 io_filesfrom_f_in = -1;
305 } else {
306 extern int eol_nulls;
307 if (!eol_nulls) {
308 char *s = io_filesfrom_buf + l;
309 /* Transform CR and/or LF into '\0' */
310 while (s-- > io_filesfrom_buf) {
311 if (*s == '\n' || *s == '\r')
312 *s = '\0';
313 }
314 }
315 if (!io_filesfrom_lastchar) {
316 /* Last buf ended with a '\0', so don't
317 * let this buf start with one. */
318 while (l && !*io_filesfrom_bp)
319 io_filesfrom_bp++, l--;
320 }
321 if (!l)
322 io_filesfrom_bp = io_filesfrom_buf;
323 else {
324 char *f = io_filesfrom_bp;
325 char *t = f;
326 char *eob = f + l;
327 /* Eliminate any multi-'\0' runs. */
328 while (f != eob) {
329 if (!(*t++ = *f++)) {
330 while (f != eob && !*f)
331 f++, l--;
332 }
333 }
334 io_filesfrom_lastchar = f[-1];
335 }
336 io_filesfrom_buflen = l;
337 }
338 }
339 }
340 }
341
342 if (!FD_ISSET(fd, &r_fds)) continue;
554e0a8d 343
4c36ddbe
AT
344 n = read(fd, buf, len);
345
8d9dc9f9
AT
346 if (n > 0) {
347 buf += n;
348 len -= n;
4c36ddbe
AT
349 ret += n;
350 if (io_timeout)
351 last_io = time(NULL);
352 continue;
7a55d06e
MP
353 } else if (n == 0) {
354 whine_about_eof ();
355 return -1; /* doesn't return */
356 } else if (n == -1) {
357 if (errno == EINTR || errno == EWOULDBLOCK ||
358 errno == EAGAIN)
359 continue;
360 else
361 die_from_readerr (errno);
8d9dc9f9 362 }
4c36ddbe 363 }
8d9dc9f9 364
4c36ddbe
AT
365 return ret;
366}
8d9dc9f9 367
56014c8c
WD
368/**
369 * Read a line into the "fname" buffer (which must be at least MAXPATHLEN
370 * characters long).
371 */
372int read_filesfrom_line(int fd, char *fname)
373{
374 char ch, *s, *eob = fname + MAXPATHLEN - 1;
375 int cnt;
376 extern int io_timeout;
377 extern int eol_nulls;
378 extern char *remote_filesfrom_file;
379 extern int am_server;
380 int reading_remotely = remote_filesfrom_file || (am_server && fd == 0);
381 int nulls = eol_nulls || reading_remotely;
382
383 start:
384 s = fname;
385 while (1) {
386 cnt = read(fd, &ch, 1);
387 if (cnt < 0 && (errno == EWOULDBLOCK
388 || errno == EINTR || errno == EAGAIN)) {
389 struct timeval tv;
390 fd_set fds;
391 FD_ZERO(&fds);
392 FD_SET(fd, &fds);
393 tv.tv_sec = io_timeout? io_timeout : SELECT_TIMEOUT;
394 tv.tv_usec = 0;
395 if (!select(fd+1, &fds, NULL, NULL, &tv))
396 check_timeout();
397 continue;
398 }
399 if (cnt != 1)
400 break;
401 if (nulls? !ch : (ch == '\r' || ch == '\n')) {
402 /* Skip empty lines if reading locally. */
403 if (!reading_remotely && s == fname)
404 continue;
405 break;
406 }
407 if (s < eob)
408 *s++ = ch;
409 }
410 *s = '\0';
7a55d06e 411
56014c8c
WD
412 /* Dump comments. */
413 if (*fname == '#' || *fname == ';')
414 goto start;
415
416 return s - fname;
417}
7a55d06e
MP
418
419
880da007
MP
420/**
421 * Continue trying to read len bytes - don't return until len has been
422 * read.
423 **/
9dd891bb 424static void read_loop (int fd, char *buf, size_t len)
4c36ddbe
AT
425{
426 while (len) {
427 int n = read_timeout(fd, buf, len);
428
429 buf += n;
430 len -= n;
8d9dc9f9
AT
431 }
432}
433
7a55d06e
MP
434
435/**
436 * Read from the file descriptor handling multiplexing - return number
437 * of bytes read.
438 *
439 * Never returns <= 0.
440 */
9dd891bb 441static int read_unbuffered(int fd, char *buf, size_t len)
8d9dc9f9 442{
6fe25398 443 static size_t remaining;
909ce14f 444 int tag, ret = 0;
8d9dc9f9
AT
445 char line[1024];
446
7a55d06e 447 if (!io_multiplexing_in || fd != multiplex_in_fd)
4c36ddbe 448 return read_timeout(fd, buf, len);
8d9dc9f9
AT
449
450 while (ret == 0) {
451 if (remaining) {
452 len = MIN(len, remaining);
453 read_loop(fd, buf, len);
454 remaining -= len;
455 ret = len;
456 continue;
457 }
458
909ce14f 459 read_loop(fd, line, 4);
ff41a59f 460 tag = IVAL(line, 0);
679e7657 461
8d9dc9f9
AT
462 remaining = tag & 0xFFFFFF;
463 tag = tag >> 24;
464
909ce14f
MP
465 if (tag == MPLEX_BASE)
466 continue;
8d9dc9f9
AT
467
468 tag -= MPLEX_BASE;
469
470 if (tag != FERROR && tag != FINFO) {
909ce14f 471 rprintf(FERROR, "unexpected tag %d\n", tag);
65417579 472 exit_cleanup(RERR_STREAMIO);
8d9dc9f9
AT
473 }
474
909ce14f 475 if (remaining > sizeof(line) - 1) {
8801138b
WD
476 rprintf(FERROR, "multiplexing overflow %ld\n\n",
477 (long)remaining);
65417579 478 exit_cleanup(RERR_STREAMIO);
8d9dc9f9
AT
479 }
480
481 read_loop(fd, line, remaining);
482 line[remaining] = 0;
483
909ce14f 484 rprintf((enum logcode) tag, "%s", line);
8d9dc9f9
AT
485 remaining = 0;
486 }
487
488 return ret;
489}
490
491
909ce14f 492
880da007
MP
493/**
494 * Do a buffered read from @p fd. Don't return until all @p n bytes
495 * have been read. If all @p n can't be read then exit with an
496 * error.
497 **/
9dd891bb 498static void readfd (int fd, char *buffer, size_t N)
720b47f2 499{
6ba9279f 500 int ret;
06ce139f 501 size_t total=0;
6ba9279f 502
6ba9279f 503 while (total < N) {
8d9dc9f9
AT
504 io_flush();
505
7a55d06e 506 ret = read_unbuffered (fd, buffer + total, N-total);
6ba9279f 507 total += ret;
7f28dbee 508 }
1b7c47cb
AT
509
510 stats.total_read += total;
720b47f2
AT
511}
512
513
b7922338 514int32 read_int(int f)
720b47f2 515{
4c36ddbe 516 char b[4];
d730b113
AT
517 int32 ret;
518
4c36ddbe 519 readfd(f,b,4);
d730b113
AT
520 ret = IVAL(b,0);
521 if (ret == (int32)0xffffffff) return -1;
522 return ret;
720b47f2
AT
523}
524
71c46176 525int64 read_longint(int f)
3a6a366f 526{
71c46176 527 int64 ret;
3a6a366f
AT
528 char b[8];
529 ret = read_int(f);
71c46176 530
8de330a3
AT
531 if ((int32)ret != (int32)0xffffffff) {
532 return ret;
533 }
71c46176 534
3bee6733 535#ifdef NO_INT64
9486289c 536 rprintf(FERROR,"Integer overflow - attempted 64 bit offset\n");
65417579 537 exit_cleanup(RERR_UNSUPPORTED);
71c46176 538#else
91c4da3f
S
539 readfd(f,b,8);
540 ret = IVAL(b,0) | (((int64)IVAL(b,4))<<32);
71c46176
AT
541#endif
542
3a6a366f
AT
543 return ret;
544}
545
9dd891bb 546void read_buf(int f,char *buf,size_t len)
720b47f2 547{
4c36ddbe 548 readfd(f,buf,len);
720b47f2
AT
549}
550
9dd891bb 551void read_sbuf(int f,char *buf,size_t len)
575f2fca 552{
7a55d06e 553 read_buf (f,buf,len);
575f2fca
AT
554 buf[len] = 0;
555}
556
182dca5c
AT
557unsigned char read_byte(int f)
558{
4c36ddbe 559 unsigned char c;
7a55d06e 560 read_buf (f, (char *)&c, 1);
4c36ddbe 561 return c;
182dca5c 562}
720b47f2 563
880da007 564
08571358
MP
565/**
566 * Sleep after writing to limit I/O bandwidth usage.
567 *
568 * @todo Rather than sleeping after each write, it might be better to
569 * use some kind of averaging. The current algorithm seems to always
570 * use a bit less bandwidth than specified, because it doesn't make up
571 * for slow periods. But arguably this is a feature. In addition, we
572 * ought to take the time used to write the data into account.
573 **/
574static void sleep_for_bwlimit(int bytes_written)
575{
576 struct timeval tv;
577
578 if (!bwlimit)
579 return;
e681e820
MP
580
581 assert(bytes_written > 0);
582 assert(bwlimit > 0);
08571358 583
08571358 584 tv.tv_usec = bytes_written * 1000 / bwlimit;
e681e820
MP
585 tv.tv_sec = tv.tv_usec / 1000000;
586 tv.tv_usec = tv.tv_usec % 1000000;
08571358 587
98b332ed 588 select(0, NULL, NULL, NULL, &tv);
08571358
MP
589}
590
591
880da007
MP
592/**
593 * Write len bytes to the file descriptor @p fd.
594 *
595 * This function underlies the multiplexing system. The body of the
596 * application never calls this function directly.
597 **/
9dd891bb 598static void writefd_unbuffered(int fd,char *buf,size_t len)
720b47f2 599{
06ce139f 600 size_t total = 0;
8d9dc9f9 601 fd_set w_fds, r_fds;
4c36ddbe 602 int fd_count, count;
8d9dc9f9 603 struct timeval tv;
720b47f2 604
90ba34e2
AT
605 err_list_push();
606
e44f9a12
AT
607 no_flush++;
608
4c36ddbe 609 while (total < len) {
8d9dc9f9 610 FD_ZERO(&w_fds);
8d9dc9f9 611 FD_SET(fd,&w_fds);
554e0a8d 612 fd_count = fd;
4c36ddbe 613
554e0a8d 614 if (io_error_fd != -1) {
56014c8c 615 FD_ZERO(&r_fds);
554e0a8d
AT
616 FD_SET(io_error_fd,&r_fds);
617 if (io_error_fd > fd_count)
618 fd_count = io_error_fd;
8d9dc9f9
AT
619 }
620
8cd9fd4e 621 tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT;
8d9dc9f9 622 tv.tv_usec = 0;
4c36ddbe 623
554e0a8d
AT
624 errno = 0;
625
626 count = select(fd_count+1,
08f15335 627 io_error_fd != -1?&r_fds:NULL,
4c36ddbe 628 &w_fds,NULL,
8cd9fd4e 629 &tv);
4c36ddbe 630
a57873b7
AT
631 if (count == 0) {
632 check_timeout();
633 }
634
4c36ddbe 635 if (count <= 0) {
554e0a8d
AT
636 if (errno == EBADF) {
637 exit_cleanup(RERR_SOCKETIO);
638 }
8d9dc9f9
AT
639 continue;
640 }
4c36ddbe 641
554e0a8d
AT
642 if (io_error_fd != -1 && FD_ISSET(io_error_fd, &r_fds)) {
643 read_error_fd();
644 }
645
8d9dc9f9 646 if (FD_ISSET(fd, &w_fds)) {
06ce139f
MP
647 int ret;
648 size_t n = len-total;
f0359dd0 649 ret = write(fd,buf+total,n);
4c36ddbe
AT
650
651 if (ret == -1 && errno == EINTR) {
652 continue;
653 }
654
f0359dd0
AT
655 if (ret == -1 &&
656 (errno == EWOULDBLOCK || errno == EAGAIN)) {
e92ee128 657 msleep(1);
f0359dd0
AT
658 continue;
659 }
660
4c36ddbe 661 if (ret <= 0) {
befbfe61
MP
662 /* Don't try to write errors back
663 * across the stream */
664 io_multiplexing_close();
3ce0f9a6 665 rprintf(FERROR, RSYNC_NAME
98b332ed 666 ": writefd_unbuffered failed to write %ld bytes: phase \"%s\": %s\n",
eca2adb4 667 (long) len, io_write_phase,
ce6c7c63 668 strerror(errno));
65417579 669 exit_cleanup(RERR_STREAMIO);
4c36ddbe
AT
670 }
671
08571358 672 sleep_for_bwlimit(ret);
ef5d23eb 673
4c36ddbe 674 total += ret;
a800434a 675
4c36ddbe
AT
676 if (io_timeout)
677 last_io = time(NULL);
8d9dc9f9 678 }
4c36ddbe 679 }
e44f9a12
AT
680
681 no_flush--;
720b47f2
AT
682}
683
8d9dc9f9 684
d6dead6b
AT
685static char *io_buffer;
686static int io_buffer_count;
687
688void io_start_buffering(int fd)
689{
8d9dc9f9 690 if (io_buffer) return;
679e7657 691 multiplex_out_fd = fd;
ff41a59f 692 io_buffer = (char *)malloc(IO_BUFFER_SIZE);
d6dead6b
AT
693 if (!io_buffer) out_of_memory("writefd");
694 io_buffer_count = 0;
ff41a59f
AT
695}
696
880da007
MP
697/**
698 * Write an message to a multiplexed stream. If this fails then rsync
699 * exits.
700 **/
9dd891bb 701static void mplex_write(int fd, enum logcode code, char *buf, size_t len)
ff41a59f
AT
702{
703 char buffer[4096];
06ce139f 704 size_t n = len;
8d9dc9f9 705
ff41a59f
AT
706 SIVAL(buffer, 0, ((MPLEX_BASE + (int)code)<<24) + len);
707
6d7b6081
AT
708 if (n > (sizeof(buffer)-4)) {
709 n = sizeof(buffer)-4;
ff41a59f
AT
710 }
711
712 memcpy(&buffer[4], buf, n);
713 writefd_unbuffered(fd, buffer, n+4);
714
715 len -= n;
716 buf += n;
717
6d7b6081
AT
718 if (len) {
719 writefd_unbuffered(fd, buf, len);
720 }
d6dead6b
AT
721}
722
ff41a59f 723
8d9dc9f9 724void io_flush(void)
d6dead6b 725{
679e7657 726 int fd = multiplex_out_fd;
90ba34e2
AT
727
728 err_list_push();
729
e44f9a12 730 if (!io_buffer_count || no_flush) return;
8d9dc9f9
AT
731
732 if (io_multiplexing_out) {
0f3203c3 733 mplex_write(fd, FNONE, io_buffer, io_buffer_count);
8d9dc9f9 734 } else {
4c36ddbe 735 writefd_unbuffered(fd, io_buffer, io_buffer_count);
d6dead6b 736 }
8d9dc9f9
AT
737 io_buffer_count = 0;
738}
739
0ba48136 740
7b5c3eb0 741void io_end_buffering(void)
8d9dc9f9
AT
742{
743 io_flush();
744 if (!io_multiplexing_out) {
ff41a59f 745 free(io_buffer);
8d9dc9f9
AT
746 io_buffer = NULL;
747 }
d6dead6b
AT
748}
749
9dd891bb 750static void writefd(int fd,char *buf,size_t len)
d6dead6b 751{
1b7c47cb
AT
752 stats.total_written += len;
753
90ba34e2
AT
754 err_list_push();
755
554e0a8d 756 if (!io_buffer || fd != multiplex_out_fd) {
4c36ddbe
AT
757 writefd_unbuffered(fd, buf, len);
758 return;
759 }
d6dead6b
AT
760
761 while (len) {
7b5c3eb0 762 int n = MIN((int) len, IO_BUFFER_SIZE-io_buffer_count);
d6dead6b
AT
763 if (n > 0) {
764 memcpy(io_buffer+io_buffer_count, buf, n);
765 buf += n;
766 len -= n;
767 io_buffer_count += n;
768 }
769
8d9dc9f9 770 if (io_buffer_count == IO_BUFFER_SIZE) io_flush();
d6dead6b 771 }
d6dead6b 772}
720b47f2
AT
773
774
b7922338 775void write_int(int f,int32 x)
720b47f2 776{
8d9dc9f9
AT
777 char b[4];
778 SIVAL(b,0,x);
4c36ddbe 779 writefd(f,b,4);
720b47f2
AT
780}
781
7a24c346 782
805edf9d
MP
783void write_int_named(int f, int32 x, const char *phase)
784{
785 io_write_phase = phase;
786 write_int(f, x);
787 io_write_phase = phase_unknown;
788}
789
790
7a24c346
MP
791/*
792 * Note: int64 may actually be a 32-bit type if ./configure couldn't find any
793 * 64-bit types on this platform.
794 */
71c46176 795void write_longint(int f, int64 x)
3a6a366f 796{
3a6a366f 797 char b[8];
3a6a366f 798
91c4da3f 799 if (x <= 0x7FFFFFFF) {
3a6a366f
AT
800 write_int(f, (int)x);
801 return;
802 }
803
67863f46
S
804#ifdef NO_INT64
805 rprintf(FERROR,"Integer overflow - attempted 64 bit offset\n");
806 exit_cleanup(RERR_UNSUPPORTED);
807#else
8de330a3 808 write_int(f, (int32)0xFFFFFFFF);
3a6a366f
AT
809 SIVAL(b,0,(x&0xFFFFFFFF));
810 SIVAL(b,4,((x>>32)&0xFFFFFFFF));
811
4c36ddbe 812 writefd(f,b,8);
67863f46 813#endif
3a6a366f
AT
814}
815
9dd891bb 816void write_buf(int f,char *buf,size_t len)
720b47f2 817{
4c36ddbe 818 writefd(f,buf,len);
720b47f2
AT
819}
820
880da007 821/** Write a string to the connection */
6e4fb64e 822static void write_sbuf(int f,char *buf)
f0fca04e
AT
823{
824 write_buf(f, buf, strlen(buf));
825}
826
720b47f2 827
182dca5c
AT
828void write_byte(int f,unsigned char c)
829{
f0fca04e 830 write_buf(f,(char *)&c,1);
182dca5c
AT
831}
832
7a55d06e
MP
833
834
914cc65c
MP
835/**
836 * Read a line of up to @p maxlen characters into @p buf. Does not
837 * contain a trailing newline or carriage return.
838 *
839 * @return 1 for success; 0 for io error or truncation.
840 **/
9dd891bb 841int read_line(int f, char *buf, size_t maxlen)
f0fca04e
AT
842{
843 while (maxlen) {
528bfcd7 844 buf[0] = 0;
f0fca04e 845 read_buf(f, buf, 1);
914cc65c
MP
846 if (buf[0] == 0)
847 return 0;
f0fca04e
AT
848 if (buf[0] == '\n') {
849 buf[0] = 0;
850 break;
851 }
852 if (buf[0] != '\r') {
853 buf++;
854 maxlen--;
855 }
856 }
857 if (maxlen == 0) {
858 *buf = 0;
859 return 0;
860 }
528bfcd7 861
f0fca04e
AT
862 return 1;
863}
864
865
866void io_printf(int fd, const char *format, ...)
867{
868 va_list ap;
869 char buf[1024];
870 int len;
871
872 va_start(ap, format);
8950ac03 873 len = vsnprintf(buf, sizeof(buf), format, ap);
f0fca04e
AT
874 va_end(ap);
875
65417579 876 if (len < 0) exit_cleanup(RERR_STREAMIO);
f0fca04e
AT
877
878 write_sbuf(fd, buf);
879}
8d9dc9f9
AT
880
881
880da007 882/** Setup for multiplexing an error stream with the data stream */
8d9dc9f9
AT
883void io_start_multiplex_out(int fd)
884{
679e7657
AT
885 multiplex_out_fd = fd;
886 io_flush();
8d9dc9f9
AT
887 io_start_buffering(fd);
888 io_multiplexing_out = 1;
889}
890
880da007 891/** Setup for multiplexing an error stream with the data stream */
8d9dc9f9
AT
892void io_start_multiplex_in(int fd)
893{
679e7657
AT
894 multiplex_in_fd = fd;
895 io_flush();
8d9dc9f9
AT
896 io_multiplexing_in = 1;
897}
898
880da007 899/** Write an message to the multiplexed error stream */
9dd891bb 900int io_multiplex_write(enum logcode code, char *buf, size_t len)
8d9dc9f9
AT
901{
902 if (!io_multiplexing_out) return 0;
903
904 io_flush();
1b7c47cb 905 stats.total_written += (len+4);
ff41a59f 906 mplex_write(multiplex_out_fd, code, buf, len);
8d9dc9f9
AT
907 return 1;
908}
909
880da007 910/** Stop output multiplexing */
554e0a8d
AT
911void io_multiplexing_close(void)
912{
913 io_multiplexing_out = 0;
914}
915