Improved/fixed some of the entries.
[rsync/rsync.git] / io.c
CommitLineData
7a24c346 1/* -*- c-file-style: "linux" -*-
d62bcc17
WD
2 *
3 * Copyright (C) 1996-2001 by Andrew Tridgell
880da007
MP
4 * Copyright (C) Paul Mackerras 1996
5 * Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org>
d62bcc17 6 *
880da007
MP
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.
d62bcc17 11 *
880da007
MP
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.
d62bcc17 16 *
880da007
MP
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 *
6ed6d7f5 25 * Socket and pipe I/O utilities used in rsync.
87ee2481
MP
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
7a55d06e 42extern int bwlimit;
71e58630 43extern size_t bwlimit_writemax;
720b47f2 44extern int verbose;
6ba9279f 45extern int io_timeout;
cdf236aa 46extern int allowed_lull;
d17e1dd2
WD
47extern int am_server;
48extern int am_daemon;
49extern int am_sender;
98f8c9a5 50extern int am_generator;
e626b29e 51extern int eol_nulls;
3b0a30eb 52extern int read_batch;
188fed95 53extern int csum_length;
b9f592fb
WD
54extern int checksum_seed;
55extern int protocol_version;
cdf236aa
WD
56extern int remove_sent_files;
57extern int preserve_hard_links;
d19320fd 58extern char *filesfrom_host;
a800434a 59extern struct stats stats;
cdf236aa 60extern struct file_list *the_file_list;
720b47f2 61
a86179f4 62const char phase_unknown[] = "unknown";
9e2409ab 63int ignore_timeout = 0;
b9f592fb 64int batch_fd = -1;
b0ad5429 65int batch_gen_fd = -1;
805edf9d 66
98b332ed
MP
67/**
68 * The connection might be dropped at some point; perhaps because the
69 * remote instance crashed. Just giving the offset on the stream is
70 * not very helpful. So instead we try to make io_phase_name point to
71 * something useful.
eca2adb4 72 *
6ed6d7f5 73 * For buffered/multiplexed I/O these names will be somewhat
805edf9d 74 * approximate; perhaps for ease of support we would rather make the
6ed6d7f5 75 * buffer always flush when a single application-level I/O finishes.
805edf9d 76 *
eca2adb4
MP
77 * @todo Perhaps we want some simple stack functionality, but there's
78 * no need to overdo it.
98b332ed 79 **/
805edf9d
MP
80const char *io_write_phase = phase_unknown;
81const char *io_read_phase = phase_unknown;
98b332ed 82
cb2e1f18 83/* Ignore an EOF error if non-zero. See whine_about_eof(). */
574c2500 84int kluge_around_eof = 0;
7a55d06e 85
d17e1dd2
WD
86int msg_fd_in = -1;
87int msg_fd_out = -1;
cdf236aa
WD
88int sock_f_in = -1;
89int sock_f_out = -1;
7a55d06e 90
1f75bb10
WD
91static int io_multiplexing_out;
92static int io_multiplexing_in;
3b0a30eb
WD
93static time_t last_io_in;
94static time_t last_io_out;
1f75bb10
WD
95static int no_flush;
96
b9f592fb
WD
97static int write_batch_monitor_in = -1;
98static int write_batch_monitor_out = -1;
99
56014c8c
WD
100static int io_filesfrom_f_in = -1;
101static int io_filesfrom_f_out = -1;
102static char io_filesfrom_buf[2048];
103static char *io_filesfrom_bp;
104static char io_filesfrom_lastchar;
105static int io_filesfrom_buflen;
af436313 106static size_t contiguous_write_len = 0;
3b0a30eb 107static int select_timeout = SELECT_TIMEOUT;
720b47f2 108
9dd891bb 109static void read_loop(int fd, char *buf, size_t len);
ff41a59f 110
c6816b94
WD
111struct flist_ndx_item {
112 struct flist_ndx_item *next;
113 int ndx;
d17e1dd2
WD
114};
115
c6816b94
WD
116struct flist_ndx_list {
117 struct flist_ndx_item *head, *tail;
118};
119
cdf236aa 120static struct flist_ndx_list redo_list, hlink_list;
d17e1dd2
WD
121
122struct msg_list {
123 struct msg_list *next;
124 char *buf;
125 int len;
126};
127
128static struct msg_list *msg_list_head;
129static struct msg_list *msg_list_tail;
130
c6816b94 131static void flist_ndx_push(struct flist_ndx_list *lp, int ndx)
d17e1dd2 132{
c6816b94
WD
133 struct flist_ndx_item *item;
134
135 if (!(item = new(struct flist_ndx_item)))
136 out_of_memory("flist_ndx_push");
137 item->next = NULL;
138 item->ndx = ndx;
139 if (lp->tail)
140 lp->tail->next = item;
d17e1dd2 141 else
c6816b94
WD
142 lp->head = item;
143 lp->tail = item;
144}
145
146static int flist_ndx_pop(struct flist_ndx_list *lp)
147{
148 struct flist_ndx_item *next;
149 int ndx;
150
151 if (!lp->head)
152 return -1;
153
154 ndx = lp->head->ndx;
155 next = lp->head->next;
156 free(lp->head);
157 lp->head = next;
158 if (!next)
159 lp->tail = NULL;
160
161 return ndx;
d17e1dd2
WD
162}
163
8d9dc9f9
AT
164static void check_timeout(void)
165{
166 time_t t;
90ba34e2 167
9e2409ab 168 if (!io_timeout || ignore_timeout)
d17e1dd2 169 return;
8d9dc9f9 170
3b0a30eb
WD
171 if (!last_io_in) {
172 last_io_in = time(NULL);
8d9dc9f9
AT
173 return;
174 }
175
176 t = time(NULL);
177
3b0a30eb 178 if (t - last_io_in >= io_timeout) {
0adb99b9 179 if (!am_server && !am_daemon) {
4ccfd96c 180 rprintf(FERROR, "io timeout after %d seconds -- exiting\n",
3b0a30eb 181 (int)(t-last_io_in));
0adb99b9 182 }
65417579 183 exit_cleanup(RERR_TIMEOUT);
8d9dc9f9
AT
184 }
185}
186
1f75bb10
WD
187/* Note the fds used for the main socket (which might really be a pipe
188 * for a local transfer, but we can ignore that). */
189void io_set_sock_fds(int f_in, int f_out)
190{
191 sock_f_in = f_in;
192 sock_f_out = f_out;
193}
194
3b0a30eb
WD
195void set_io_timeout(int secs)
196{
197 io_timeout = secs;
198
199 if (!io_timeout || io_timeout > SELECT_TIMEOUT)
200 select_timeout = SELECT_TIMEOUT;
201 else
202 select_timeout = io_timeout;
203
204 allowed_lull = read_batch ? 0 : (io_timeout + 1) / 2;
205}
206
98f8c9a5
WD
207/* Setup the fd used to receive MSG_* messages. Only needed during the
208 * early stages of being a local sender (up through the sending of the
209 * file list) or when we're the generator (to fetch the messages from
210 * the receiver). */
d17e1dd2
WD
211void set_msg_fd_in(int fd)
212{
213 msg_fd_in = fd;
214}
215
98f8c9a5
WD
216/* Setup the fd used to send our MSG_* messages. Only needed when
217 * we're the receiver (to send our messages to the generator). */
d17e1dd2
WD
218void set_msg_fd_out(int fd)
219{
220 msg_fd_out = fd;
221 set_nonblocking(msg_fd_out);
222}
223
224/* Add a message to the pending MSG_* list. */
225static void msg_list_add(int code, char *buf, int len)
554e0a8d 226{
d17e1dd2
WD
227 struct msg_list *ml;
228
229 if (!(ml = new(struct msg_list)))
c6816b94 230 out_of_memory("msg_list_add");
d17e1dd2
WD
231 ml->next = NULL;
232 if (!(ml->buf = new_array(char, len+4)))
c6816b94 233 out_of_memory("msg_list_add");
d17e1dd2
WD
234 SIVAL(ml->buf, 0, ((code+MPLEX_BASE)<<24) | len);
235 memcpy(ml->buf+4, buf, len);
236 ml->len = len+4;
237 if (msg_list_tail)
238 msg_list_tail->next = ml;
239 else
240 msg_list_head = ml;
241 msg_list_tail = ml;
554e0a8d
AT
242}
243
d17e1dd2
WD
244void send_msg(enum msgcode code, char *buf, int len)
245{
0d67e00a
WD
246 if (msg_fd_out < 0) {
247 io_multiplex_write(code, buf, len);
248 return;
249 }
d17e1dd2
WD
250 msg_list_add(code, buf, len);
251 msg_list_push(NORMAL_FLUSH);
252}
253
98f8c9a5
WD
254/* Read a message from the MSG_* fd and handle it. This is called either
255 * during the early stages of being a local sender (up through the sending
256 * of the file list) or when we're the generator (to fetch the messages
257 * from the receiver). */
d17e1dd2 258static void read_msg_fd(void)
554e0a8d 259{
f9c6b3e7 260 char buf[2048];
06ce139f 261 size_t n;
d17e1dd2 262 int fd = msg_fd_in;
ff41a59f
AT
263 int tag, len;
264
00bdf899 265 /* Temporarily disable msg_fd_in. This is needed to avoid looping back
af436313 266 * to this routine from writefd_unbuffered(). */
d17e1dd2 267 msg_fd_in = -1;
554e0a8d 268
ff41a59f
AT
269 read_loop(fd, buf, 4);
270 tag = IVAL(buf, 0);
271
272 len = tag & 0xFFFFFF;
d17e1dd2 273 tag = (tag >> 24) - MPLEX_BASE;
ff41a59f 274
d17e1dd2
WD
275 switch (tag) {
276 case MSG_DONE:
98f8c9a5 277 if (len != 0 || !am_generator) {
13c7bcbb 278 rprintf(FERROR, "invalid message %d:%d\n", tag, len);
d17e1dd2 279 exit_cleanup(RERR_STREAMIO);
13c7bcbb 280 }
c6816b94 281 flist_ndx_push(&redo_list, -1);
d17e1dd2
WD
282 break;
283 case MSG_REDO:
98f8c9a5 284 if (len != 4 || !am_generator) {
13c7bcbb 285 rprintf(FERROR, "invalid message %d:%d\n", tag, len);
d17e1dd2 286 exit_cleanup(RERR_STREAMIO);
13c7bcbb 287 }
d17e1dd2 288 read_loop(fd, buf, 4);
c6816b94 289 flist_ndx_push(&redo_list, IVAL(buf,0));
d17e1dd2 290 break;
0d67e00a
WD
291 case MSG_DELETED:
292 if (len >= (int)sizeof buf || !am_generator) {
293 rprintf(FERROR, "invalid message %d:%d\n", tag, len);
294 exit_cleanup(RERR_STREAMIO);
295 }
296 read_loop(fd, buf, len);
297 io_multiplex_write(MSG_DELETED, buf, len);
298 break;
9981c27e
WD
299 case MSG_SUCCESS:
300 if (len != 4 || !am_generator) {
301 rprintf(FERROR, "invalid message %d:%d\n", tag, len);
302 exit_cleanup(RERR_STREAMIO);
303 }
304 read_loop(fd, buf, len);
cdf236aa
WD
305 if (remove_sent_files)
306 io_multiplex_write(MSG_SUCCESS, buf, len);
307 if (preserve_hard_links)
308 flist_ndx_push(&hlink_list, IVAL(buf,0));
9981c27e 309 break;
d17e1dd2
WD
310 case MSG_INFO:
311 case MSG_ERROR:
312 case MSG_LOG:
313 while (len) {
314 n = len;
315 if (n >= sizeof buf)
316 n = sizeof buf - 1;
317 read_loop(fd, buf, n);
318 rwrite((enum logcode)tag, buf, n);
319 len -= n;
320 }
321 break;
322 default:
13c7bcbb 323 rprintf(FERROR, "unknown message %d:%d\n", tag, len);
d17e1dd2
WD
324 exit_cleanup(RERR_STREAMIO);
325 }
326
327 msg_fd_in = fd;
328}
329
330/* Try to push messages off the list onto the wire. If we leave with more
331 * to do, return 0. On error, return -1. If everything flushed, return 1.
f9c6b3e7 332 * This is only active in the receiver. */
d17e1dd2
WD
333int msg_list_push(int flush_it_all)
334{
335 static int written = 0;
336 struct timeval tv;
337 fd_set fds;
338
339 if (msg_fd_out < 0)
340 return -1;
341
342 while (msg_list_head) {
343 struct msg_list *ml = msg_list_head;
344 int n = write(msg_fd_out, ml->buf + written, ml->len - written);
345 if (n < 0) {
346 if (errno == EINTR)
347 continue;
348 if (errno != EWOULDBLOCK && errno != EAGAIN)
349 return -1;
350 if (!flush_it_all)
351 return 0;
352 FD_ZERO(&fds);
353 FD_SET(msg_fd_out, &fds);
e626b29e 354 tv.tv_sec = select_timeout;
d17e1dd2
WD
355 tv.tv_usec = 0;
356 if (!select(msg_fd_out+1, NULL, &fds, NULL, &tv))
357 check_timeout();
358 } else if ((written += n) == ml->len) {
359 free(ml->buf);
360 msg_list_head = ml->next;
361 if (!msg_list_head)
362 msg_list_tail = NULL;
363 free(ml);
364 written = 0;
365 }
554e0a8d 366 }
d17e1dd2
WD
367 return 1;
368}
369
cdf236aa 370int get_redo_num(int itemizing, enum logcode code)
d17e1dd2 371{
cdf236aa
WD
372 while (1) {
373 if (hlink_list.head)
374 check_for_finished_hlinks(itemizing, code);
375 if (redo_list.head)
376 break;
d17e1dd2 377 read_msg_fd();
c6816b94 378 }
554e0a8d 379
c6816b94 380 return flist_ndx_pop(&redo_list);
554e0a8d
AT
381}
382
cdf236aa
WD
383int get_hlink_num(void)
384{
385 return flist_ndx_pop(&hlink_list);
386}
387
56014c8c
WD
388/**
389 * When we're the receiver and we have a local --files-from list of names
390 * that needs to be sent over the socket to the sender, we have to do two
391 * things at the same time: send the sender a list of what files we're
392 * processing and read the incoming file+info list from the sender. We do
393 * this by augmenting the read_timeout() function to copy this data. It
394 * uses the io_filesfrom_buf to read a block of data from f_in (when it is
395 * ready, since it might be a pipe) and then blast it out f_out (when it
396 * is ready to receive more data).
397 */
398void io_set_filesfrom_fds(int f_in, int f_out)
399{
400 io_filesfrom_f_in = f_in;
401 io_filesfrom_f_out = f_out;
402 io_filesfrom_bp = io_filesfrom_buf;
403 io_filesfrom_lastchar = '\0';
404 io_filesfrom_buflen = 0;
405}
720b47f2 406
cb2e1f18
WD
407/* It's almost always an error to get an EOF when we're trying to read from the
408 * network, because the protocol is (for the most part) self-terminating.
880da007 409 *
cb2e1f18
WD
410 * There is one case for the receiver when it is at the end of the transfer
411 * (hanging around reading any keep-alive packets that might come its way): if
412 * the sender dies before the generator's kill-signal comes through, we can end
413 * up here needing to loop until the kill-signal arrives. In this situation,
414 * kluge_around_eof will be < 0.
415 *
416 * There is another case for older protocol versions (< 24) where the module
417 * listing was not terminated, so we must ignore an EOF error in that case and
418 * exit. In this situation, kluge_around_eof will be > 0. */
1f75bb10 419static void whine_about_eof(int fd)
7a55d06e 420{
574c2500 421 if (kluge_around_eof && fd == sock_f_in) {
55bb7fff 422 int i;
574c2500
WD
423 if (kluge_around_eof > 0)
424 exit_cleanup(0);
55bb7fff
WD
425 /* If we're still here after 10 seconds, exit with an error. */
426 for (i = 10*1000/20; i--; )
574c2500
WD
427 msleep(20);
428 }
3151cbae 429
00bdf899 430 rprintf(FERROR, RSYNC_NAME ": connection unexpectedly closed "
dca68b0a
WD
431 "(%.0f bytes received so far) [%s]\n",
432 (double)stats.total_read, who_am_i());
00bdf899
WD
433
434 exit_cleanup(RERR_STREAMIO);
7a55d06e 435}
720b47f2 436
7a55d06e 437
880da007 438/**
6ed6d7f5 439 * Read from a socket with I/O timeout. return the number of bytes
c3563c46
MP
440 * read. If no bytes can be read then exit, never return a number <= 0.
441 *
8886f8d0
MP
442 * TODO: If the remote shell connection fails, then current versions
443 * actually report an "unexpected EOF" error here. Since it's a
444 * fairly common mistake to try to use rsh when ssh is required, we
445 * should trap that: if we fail to read any data at all, we should
446 * give a better explanation. We can tell whether the connection has
447 * started by looking e.g. at whether the remote version is known yet.
c3563c46 448 */
3151cbae 449static int read_timeout(int fd, char *buf, size_t len)
8d9dc9f9 450{
d62bcc17 451 int n, ret = 0;
4c36ddbe 452
d17e1dd2 453 io_flush(NORMAL_FLUSH);
ea2111d1 454
4c36ddbe 455 while (ret == 0) {
7a55d06e 456 /* until we manage to read *something* */
56014c8c 457 fd_set r_fds, w_fds;
4c36ddbe 458 struct timeval tv;
1ea087a7 459 int maxfd = fd;
a57873b7 460 int count;
4c36ddbe 461
56014c8c 462 FD_ZERO(&r_fds);
a7026ba9 463 FD_ZERO(&w_fds);
56014c8c 464 FD_SET(fd, &r_fds);
af436313 465 if (msg_list_head) {
4033b80b 466 FD_SET(msg_fd_out, &w_fds);
1ea087a7
WD
467 if (msg_fd_out > maxfd)
468 maxfd = msg_fd_out;
56014c8c 469 }
3309507d 470 if (io_filesfrom_f_out >= 0) {
56014c8c
WD
471 int new_fd;
472 if (io_filesfrom_buflen == 0) {
3309507d 473 if (io_filesfrom_f_in >= 0) {
56014c8c
WD
474 FD_SET(io_filesfrom_f_in, &r_fds);
475 new_fd = io_filesfrom_f_in;
476 } else {
477 io_filesfrom_f_out = -1;
478 new_fd = -1;
479 }
480 } else {
56014c8c
WD
481 FD_SET(io_filesfrom_f_out, &w_fds);
482 new_fd = io_filesfrom_f_out;
483 }
1ea087a7
WD
484 if (new_fd > maxfd)
485 maxfd = new_fd;
554e0a8d
AT
486 }
487
e626b29e 488 tv.tv_sec = select_timeout;
4c36ddbe
AT
489 tv.tv_usec = 0;
490
554e0a8d
AT
491 errno = 0;
492
a7026ba9 493 count = select(maxfd + 1, &r_fds, &w_fds, NULL, &tv);
a57873b7 494
a57873b7 495 if (count <= 0) {
f89b9368 496 if (errno == EBADF)
554e0a8d 497 exit_cleanup(RERR_SOCKETIO);
bd717af8 498 check_timeout();
4c36ddbe
AT
499 continue;
500 }
501
af436313 502 if (msg_list_head && FD_ISSET(msg_fd_out, &w_fds))
4033b80b 503 msg_list_push(NORMAL_FLUSH);
554e0a8d 504
3309507d 505 if (io_filesfrom_f_out >= 0) {
56014c8c
WD
506 if (io_filesfrom_buflen) {
507 if (FD_ISSET(io_filesfrom_f_out, &w_fds)) {
508 int l = write(io_filesfrom_f_out,
509 io_filesfrom_bp,
510 io_filesfrom_buflen);
511 if (l > 0) {
512 if (!(io_filesfrom_buflen -= l))
513 io_filesfrom_bp = io_filesfrom_buf;
514 else
515 io_filesfrom_bp += l;
516 } else {
517 /* XXX should we complain? */
518 io_filesfrom_f_out = -1;
519 }
520 }
3309507d 521 } else if (io_filesfrom_f_in >= 0) {
56014c8c
WD
522 if (FD_ISSET(io_filesfrom_f_in, &r_fds)) {
523 int l = read(io_filesfrom_f_in,
524 io_filesfrom_buf,
525 sizeof io_filesfrom_buf);
526 if (l <= 0) {
527 /* Send end-of-file marker */
528 io_filesfrom_buf[0] = '\0';
529 io_filesfrom_buf[1] = '\0';
530 io_filesfrom_buflen = io_filesfrom_lastchar? 2 : 1;
531 io_filesfrom_f_in = -1;
532 } else {
56014c8c
WD
533 if (!eol_nulls) {
534 char *s = io_filesfrom_buf + l;
535 /* Transform CR and/or LF into '\0' */
536 while (s-- > io_filesfrom_buf) {
537 if (*s == '\n' || *s == '\r')
538 *s = '\0';
539 }
540 }
541 if (!io_filesfrom_lastchar) {
542 /* Last buf ended with a '\0', so don't
543 * let this buf start with one. */
544 while (l && !*io_filesfrom_bp)
545 io_filesfrom_bp++, l--;
546 }
547 if (!l)
548 io_filesfrom_bp = io_filesfrom_buf;
549 else {
550 char *f = io_filesfrom_bp;
551 char *t = f;
552 char *eob = f + l;
553 /* Eliminate any multi-'\0' runs. */
554 while (f != eob) {
555 if (!(*t++ = *f++)) {
556 while (f != eob && !*f)
557 f++, l--;
558 }
559 }
560 io_filesfrom_lastchar = f[-1];
561 }
562 io_filesfrom_buflen = l;
563 }
564 }
565 }
566 }
567
f89b9368
WD
568 if (!FD_ISSET(fd, &r_fds))
569 continue;
554e0a8d 570
4c36ddbe
AT
571 n = read(fd, buf, len);
572
1ea087a7
WD
573 if (n <= 0) {
574 if (n == 0)
1f75bb10 575 whine_about_eof(fd); /* Doesn't return. */
d62bcc17
WD
576 if (errno == EINTR || errno == EWOULDBLOCK
577 || errno == EAGAIN)
7a55d06e 578 continue;
1f75bb10
WD
579
580 /* Don't write errors on a dead socket. */
581 if (fd == sock_f_in)
7f459268 582 close_multiplexing_out();
1f75bb10
WD
583 rsyserr(FERROR, errno, "read error");
584 exit_cleanup(RERR_STREAMIO);
8d9dc9f9 585 }
00bdf899
WD
586
587 buf += n;
588 len -= n;
589 ret += n;
1f75bb10 590
3b0a30eb
WD
591 if (fd == sock_f_in && io_timeout)
592 last_io_in = time(NULL);
4c36ddbe 593 }
8d9dc9f9 594
4c36ddbe
AT
595 return ret;
596}
8d9dc9f9 597
56014c8c
WD
598/**
599 * Read a line into the "fname" buffer (which must be at least MAXPATHLEN
600 * characters long).
601 */
602int read_filesfrom_line(int fd, char *fname)
603{
604 char ch, *s, *eob = fname + MAXPATHLEN - 1;
605 int cnt;
d19320fd 606 int reading_remotely = filesfrom_host != NULL;
56014c8c
WD
607 int nulls = eol_nulls || reading_remotely;
608
609 start:
610 s = fname;
611 while (1) {
612 cnt = read(fd, &ch, 1);
613 if (cnt < 0 && (errno == EWOULDBLOCK
614 || errno == EINTR || errno == EAGAIN)) {
615 struct timeval tv;
616 fd_set fds;
617 FD_ZERO(&fds);
618 FD_SET(fd, &fds);
e626b29e 619 tv.tv_sec = select_timeout;
56014c8c
WD
620 tv.tv_usec = 0;
621 if (!select(fd+1, &fds, NULL, NULL, &tv))
622 check_timeout();
623 continue;
624 }
625 if (cnt != 1)
626 break;
627 if (nulls? !ch : (ch == '\r' || ch == '\n')) {
628 /* Skip empty lines if reading locally. */
629 if (!reading_remotely && s == fname)
630 continue;
631 break;
632 }
633 if (s < eob)
634 *s++ = ch;
635 }
636 *s = '\0';
7a55d06e 637
6b45fcf1
WD
638 /* Dump comments. */
639 if (*fname == '#' || *fname == ';')
56014c8c
WD
640 goto start;
641
642 return s - fname;
643}
7a55d06e
MP
644
645
1f75bb10
WD
646static char *iobuf_out;
647static int iobuf_out_cnt;
648
649void io_start_buffering_out(void)
650{
651 if (iobuf_out)
652 return;
653 if (!(iobuf_out = new_array(char, IO_BUFFER_SIZE)))
654 out_of_memory("io_start_buffering_out");
655 iobuf_out_cnt = 0;
656}
657
658
659static char *iobuf_in;
660static size_t iobuf_in_siz;
661
662void io_start_buffering_in(void)
663{
664 if (iobuf_in)
665 return;
666 iobuf_in_siz = 2 * IO_BUFFER_SIZE;
667 if (!(iobuf_in = new_array(char, iobuf_in_siz)))
668 out_of_memory("io_start_buffering_in");
669}
670
671
672void io_end_buffering(void)
673{
674 io_flush(NORMAL_FLUSH);
675 if (!io_multiplexing_out) {
676 free(iobuf_out);
677 iobuf_out = NULL;
678 }
679}
680
681
626bec8e
WD
682void maybe_flush_socket(void)
683{
3b0a30eb 684 if (iobuf_out && iobuf_out_cnt && time(NULL) - last_io_out >= 5)
626bec8e
WD
685 io_flush(NORMAL_FLUSH);
686}
687
688
cdf236aa 689void maybe_send_keepalive(void)
9e2409ab 690{
3b0a30eb 691 if (time(NULL) - last_io_out >= allowed_lull) {
9e2409ab 692 if (!iobuf_out || !iobuf_out_cnt) {
3221f451
WD
693 if (protocol_version < 29)
694 return; /* there's nothing we can do */
cdf236aa 695 write_int(sock_f_out, the_file_list->count);
9e2409ab
WD
696 write_shortint(sock_f_out, ITEM_IS_NEW);
697 }
698 if (iobuf_out)
699 io_flush(NORMAL_FLUSH);
700 }
701}
702
703
880da007
MP
704/**
705 * Continue trying to read len bytes - don't return until len has been
706 * read.
707 **/
3151cbae 708static void read_loop(int fd, char *buf, size_t len)
4c36ddbe
AT
709{
710 while (len) {
711 int n = read_timeout(fd, buf, len);
712
713 buf += n;
714 len -= n;
8d9dc9f9
AT
715 }
716}
717
7a55d06e
MP
718
719/**
720 * Read from the file descriptor handling multiplexing - return number
721 * of bytes read.
d62bcc17
WD
722 *
723 * Never returns <= 0.
7a55d06e 724 */
c399d22a 725static int readfd_unbuffered(int fd, char *buf, size_t len)
8d9dc9f9 726{
6fe25398 727 static size_t remaining;
1f75bb10 728 static size_t iobuf_in_ndx;
f2a4853c 729 size_t msg_bytes;
909ce14f 730 int tag, ret = 0;
33544bf4 731 char line[BIGPATHBUFLEN];
8d9dc9f9 732
1f75bb10 733 if (!iobuf_in || fd != sock_f_in)
4c36ddbe 734 return read_timeout(fd, buf, len);
8d9dc9f9 735
76c21947 736 if (!io_multiplexing_in && remaining == 0) {
1f75bb10
WD
737 remaining = read_timeout(fd, iobuf_in, iobuf_in_siz);
738 iobuf_in_ndx = 0;
76c21947
WD
739 }
740
8d9dc9f9
AT
741 while (ret == 0) {
742 if (remaining) {
743 len = MIN(len, remaining);
1f75bb10
WD
744 memcpy(buf, iobuf_in + iobuf_in_ndx, len);
745 iobuf_in_ndx += len;
8d9dc9f9
AT
746 remaining -= len;
747 ret = len;
76c21947 748 break;
8d9dc9f9
AT
749 }
750
909ce14f 751 read_loop(fd, line, 4);
ff41a59f 752 tag = IVAL(line, 0);
679e7657 753
f2a4853c 754 msg_bytes = tag & 0xFFFFFF;
d17e1dd2 755 tag = (tag >> 24) - MPLEX_BASE;
8d9dc9f9 756
d17e1dd2
WD
757 switch (tag) {
758 case MSG_DATA:
f2a4853c 759 if (msg_bytes > iobuf_in_siz) {
1f75bb10 760 if (!(iobuf_in = realloc_array(iobuf_in, char,
f2a4853c 761 msg_bytes)))
c399d22a 762 out_of_memory("readfd_unbuffered");
f2a4853c 763 iobuf_in_siz = msg_bytes;
76c21947 764 }
f2a4853c
WD
765 read_loop(fd, iobuf_in, msg_bytes);
766 remaining = msg_bytes;
1f75bb10 767 iobuf_in_ndx = 0;
d17e1dd2 768 break;
0d67e00a 769 case MSG_DELETED:
f2a4853c
WD
770 if (msg_bytes >= sizeof line)
771 goto overflow;
772 read_loop(fd, line, msg_bytes);
773 line[msg_bytes] = '\0';
0d67e00a 774 /* A directory name was sent with the trailing null */
f2a4853c 775 if (msg_bytes > 0 && !line[msg_bytes-1])
0d67e00a
WD
776 log_delete(line, S_IFDIR);
777 else
778 log_delete(line, S_IFREG);
0d67e00a 779 break;
9981c27e 780 case MSG_SUCCESS:
f2a4853c 781 if (msg_bytes != 4) {
cdf236aa 782 rprintf(FERROR, "invalid multi-message %d:%ld [%s]\n",
f2a4853c 783 tag, (long)msg_bytes, who_am_i());
9981c27e
WD
784 exit_cleanup(RERR_STREAMIO);
785 }
f2a4853c 786 read_loop(fd, line, msg_bytes);
9981c27e 787 successful_send(IVAL(line, 0));
9981c27e 788 break;
d17e1dd2
WD
789 case MSG_INFO:
790 case MSG_ERROR:
f2a4853c
WD
791 if (msg_bytes >= sizeof line) {
792 overflow:
bd9fca47 793 rprintf(FERROR,
cdf236aa 794 "multiplexing overflow %d:%ld [%s]\n",
f2a4853c 795 tag, (long)msg_bytes, who_am_i());
d17e1dd2
WD
796 exit_cleanup(RERR_STREAMIO);
797 }
f2a4853c
WD
798 read_loop(fd, line, msg_bytes);
799 rwrite((enum logcode)tag, line, msg_bytes);
d17e1dd2
WD
800 break;
801 default:
cdf236aa
WD
802 rprintf(FERROR, "unexpected tag %d [%s]\n",
803 tag, who_am_i());
65417579 804 exit_cleanup(RERR_STREAMIO);
8d9dc9f9 805 }
8d9dc9f9
AT
806 }
807
76c21947 808 if (remaining == 0)
d17e1dd2 809 io_flush(NORMAL_FLUSH);
76c21947 810
8d9dc9f9
AT
811 return ret;
812}
813
814
909ce14f 815
880da007
MP
816/**
817 * Do a buffered read from @p fd. Don't return until all @p n bytes
818 * have been read. If all @p n can't be read then exit with an
819 * error.
820 **/
3151cbae 821static void readfd(int fd, char *buffer, size_t N)
720b47f2 822{
6ba9279f 823 int ret;
d62bcc17 824 size_t total = 0;
3151cbae 825
6ba9279f 826 while (total < N) {
c399d22a 827 ret = readfd_unbuffered(fd, buffer + total, N-total);
6ba9279f 828 total += ret;
7f28dbee 829 }
1b7c47cb 830
b9f592fb
WD
831 if (fd == write_batch_monitor_in) {
832 if ((size_t)write(batch_fd, buffer, total) != total)
833 exit_cleanup(RERR_FILEIO);
834 }
1f75bb10
WD
835
836 if (fd == sock_f_in)
837 stats.total_read += total;
720b47f2
AT
838}
839
840
0d67e00a 841int read_shortint(int f)
9361f839
WD
842{
843 uchar b[2];
844 readfd(f, (char *)b, 2);
845 return (b[1] << 8) + b[0];
846}
847
848
b7922338 849int32 read_int(int f)
720b47f2 850{
4c36ddbe 851 char b[4];
d730b113
AT
852 int32 ret;
853
4c36ddbe 854 readfd(f,b,4);
d730b113 855 ret = IVAL(b,0);
f89b9368
WD
856 if (ret == (int32)0xffffffff)
857 return -1;
d730b113 858 return ret;
720b47f2
AT
859}
860
71c46176 861int64 read_longint(int f)
3a6a366f 862{
71c46176 863 int64 ret;
3a6a366f
AT
864 char b[8];
865 ret = read_int(f);
71c46176 866
f89b9368 867 if ((int32)ret != (int32)0xffffffff)
8de330a3 868 return ret;
71c46176 869
031fa9ad
WD
870#if SIZEOF_INT64 < 8
871 rprintf(FERROR, "Integer overflow: attempted 64-bit offset\n");
872 exit_cleanup(RERR_UNSUPPORTED);
873#else
91c4da3f
S
874 readfd(f,b,8);
875 ret = IVAL(b,0) | (((int64)IVAL(b,4))<<32);
031fa9ad 876#endif
71c46176 877
3a6a366f
AT
878 return ret;
879}
880
9dd891bb 881void read_buf(int f,char *buf,size_t len)
720b47f2 882{
4c36ddbe 883 readfd(f,buf,len);
720b47f2
AT
884}
885
9dd891bb 886void read_sbuf(int f,char *buf,size_t len)
575f2fca 887{
93095cbe 888 readfd(f, buf, len);
af436313 889 buf[len] = '\0';
575f2fca
AT
890}
891
9361f839 892uchar read_byte(int f)
182dca5c 893{
9361f839 894 uchar c;
93095cbe 895 readfd(f, (char *)&c, 1);
4c36ddbe 896 return c;
182dca5c 897}
720b47f2 898
46e99b09
WD
899int read_vstring(int f, char *buf, int bufsize)
900{
901 int len = read_byte(f);
902
903 if (len & 0x80)
904 len = (len & ~0x80) * 0x100 + read_byte(f);
905
906 if (len >= bufsize) {
907 rprintf(FERROR, "over-long vstring received (%d > %d)\n",
908 len, bufsize - 1);
9a6ed83f 909 return -1;
46e99b09
WD
910 }
911
912 if (len)
913 readfd(f, buf, len);
914 buf[len] = '\0';
915 return len;
916}
917
188fed95
WD
918/* Populate a sum_struct with values from the socket. This is
919 * called by both the sender and the receiver. */
920void read_sum_head(int f, struct sum_struct *sum)
921{
922 sum->count = read_int(f);
923 sum->blength = read_int(f);
924 if (sum->blength < 0 || sum->blength > MAX_BLOCK_SIZE) {
cdf236aa
WD
925 rprintf(FERROR, "Invalid block length %ld [%s]\n",
926 (long)sum->blength, who_am_i());
188fed95
WD
927 exit_cleanup(RERR_PROTOCOL);
928 }
929 sum->s2length = protocol_version < 27 ? csum_length : (int)read_int(f);
930 if (sum->s2length < 0 || sum->s2length > MD4_SUM_LENGTH) {
cdf236aa
WD
931 rprintf(FERROR, "Invalid checksum length %d [%s]\n",
932 sum->s2length, who_am_i());
188fed95
WD
933 exit_cleanup(RERR_PROTOCOL);
934 }
935 sum->remainder = read_int(f);
936 if (sum->remainder < 0 || sum->remainder > sum->blength) {
cdf236aa
WD
937 rprintf(FERROR, "Invalid remainder length %ld [%s]\n",
938 (long)sum->remainder, who_am_i());
188fed95
WD
939 exit_cleanup(RERR_PROTOCOL);
940 }
941}
942
c207d7ec
WD
943/* Send the values from a sum_struct over the socket. Set sum to
944 * NULL if there are no checksums to send. This is called by both
945 * the generator and the sender. */
946void write_sum_head(int f, struct sum_struct *sum)
947{
948 static struct sum_struct null_sum;
949
950 if (sum == NULL)
951 sum = &null_sum;
952
953 write_int(f, sum->count);
954 write_int(f, sum->blength);
955 if (protocol_version >= 27)
956 write_int(f, sum->s2length);
957 write_int(f, sum->remainder);
958}
959
880da007 960
08571358
MP
961/**
962 * Sleep after writing to limit I/O bandwidth usage.
963 *
964 * @todo Rather than sleeping after each write, it might be better to
965 * use some kind of averaging. The current algorithm seems to always
966 * use a bit less bandwidth than specified, because it doesn't make up
967 * for slow periods. But arguably this is a feature. In addition, we
968 * ought to take the time used to write the data into account.
71e58630
WD
969 *
970 * During some phases of big transfers (file FOO is uptodate) this is
971 * called with a small bytes_written every time. As the kernel has to
972 * round small waits up to guarantee that we actually wait at least the
973 * requested number of microseconds, this can become grossly inaccurate.
974 * We therefore keep track of the bytes we've written over time and only
975 * sleep when the accumulated delay is at least 1 tenth of a second.
08571358
MP
976 **/
977static void sleep_for_bwlimit(int bytes_written)
978{
71e58630
WD
979 static struct timeval prior_tv;
980 static long total_written = 0;
981 struct timeval tv, start_tv;
982 long elapsed_usec, sleep_usec;
983
984#define ONE_SEC 1000000L /* # of microseconds in a second */
08571358
MP
985
986 if (!bwlimit)
987 return;
e681e820 988
71e58630
WD
989 total_written += bytes_written;
990
991 gettimeofday(&start_tv, NULL);
992 if (prior_tv.tv_sec) {
993 elapsed_usec = (start_tv.tv_sec - prior_tv.tv_sec) * ONE_SEC
994 + (start_tv.tv_usec - prior_tv.tv_usec);
995 total_written -= elapsed_usec * bwlimit / (ONE_SEC/1024);
996 if (total_written < 0)
997 total_written = 0;
998 }
3151cbae 999
71e58630
WD
1000 sleep_usec = total_written * (ONE_SEC/1024) / bwlimit;
1001 if (sleep_usec < ONE_SEC / 10) {
1002 prior_tv = start_tv;
1003 return;
1004 }
08571358 1005
71e58630
WD
1006 tv.tv_sec = sleep_usec / ONE_SEC;
1007 tv.tv_usec = sleep_usec % ONE_SEC;
98b332ed 1008 select(0, NULL, NULL, NULL, &tv);
71e58630
WD
1009
1010 gettimeofday(&prior_tv, NULL);
1011 elapsed_usec = (prior_tv.tv_sec - start_tv.tv_sec) * ONE_SEC
1012 + (prior_tv.tv_usec - start_tv.tv_usec);
1013 total_written = (sleep_usec - elapsed_usec) * bwlimit / (ONE_SEC/1024);
08571358
MP
1014}
1015
1016
1f75bb10 1017/* Write len bytes to the file descriptor fd, looping as necessary to get
af436313
WD
1018 * the job done and also (in certain circumstnces) reading any data on
1019 * msg_fd_in to avoid deadlock.
880da007
MP
1020 *
1021 * This function underlies the multiplexing system. The body of the
1f75bb10 1022 * application never calls this function directly. */
9dd891bb 1023static void writefd_unbuffered(int fd,char *buf,size_t len)
720b47f2 1024{
1ea087a7 1025 size_t n, total = 0;
8d9dc9f9 1026 fd_set w_fds, r_fds;
3eeac9bc 1027 int maxfd, count, ret, using_r_fds;
8d9dc9f9 1028 struct timeval tv;
720b47f2 1029
e44f9a12
AT
1030 no_flush++;
1031
4c36ddbe 1032 while (total < len) {
8d9dc9f9 1033 FD_ZERO(&w_fds);
8d9dc9f9 1034 FD_SET(fd,&w_fds);
1ea087a7 1035 maxfd = fd;
4c36ddbe 1036
af436313 1037 if (msg_fd_in >= 0 && len-total >= contiguous_write_len) {
56014c8c 1038 FD_ZERO(&r_fds);
d17e1dd2 1039 FD_SET(msg_fd_in,&r_fds);
1ea087a7
WD
1040 if (msg_fd_in > maxfd)
1041 maxfd = msg_fd_in;
3eeac9bc
WD
1042 using_r_fds = 1;
1043 } else
1044 using_r_fds = 0;
8d9dc9f9 1045
e626b29e 1046 tv.tv_sec = select_timeout;
8d9dc9f9 1047 tv.tv_usec = 0;
4c36ddbe 1048
554e0a8d 1049 errno = 0;
3eeac9bc 1050 count = select(maxfd + 1, using_r_fds ? &r_fds : NULL,
d17e1dd2 1051 &w_fds, NULL, &tv);
4c36ddbe
AT
1052
1053 if (count <= 0) {
1ea087a7 1054 if (count < 0 && errno == EBADF)
554e0a8d 1055 exit_cleanup(RERR_SOCKETIO);
bd717af8 1056 check_timeout();
8d9dc9f9
AT
1057 continue;
1058 }
4c36ddbe 1059
3eeac9bc 1060 if (using_r_fds && FD_ISSET(msg_fd_in, &r_fds))
d17e1dd2 1061 read_msg_fd();
554e0a8d 1062
9a6ed83f 1063 if (!FD_ISSET(fd, &w_fds))
1ea087a7 1064 continue;
4c36ddbe 1065
1ea087a7
WD
1066 n = len - total;
1067 if (bwlimit && n > bwlimit_writemax)
1068 n = bwlimit_writemax;
1069 ret = write(fd, buf + total, n);
1070
1071 if (ret <= 0) {
3309507d
WD
1072 if (ret < 0) {
1073 if (errno == EINTR)
1074 continue;
1075 if (errno == EWOULDBLOCK || errno == EAGAIN) {
1076 msleep(1);
1077 continue;
1078 }
f0359dd0
AT
1079 }
1080
1ea087a7 1081 /* Don't try to write errors back across the stream. */
1f75bb10 1082 if (fd == sock_f_out)
7f459268 1083 close_multiplexing_out();
1ea087a7 1084 rsyserr(FERROR, errno,
dca68b0a
WD
1085 "writefd_unbuffered failed to write %ld bytes: phase \"%s\" [%s]",
1086 (long)len, io_write_phase, who_am_i());
d1b31da7
WD
1087 /* If the other side is sending us error messages, try
1088 * to grab any messages they sent before they died. */
7f459268 1089 while (fd == sock_f_out && io_multiplexing_in) {
3b0a30eb 1090 set_io_timeout(30);
9e2409ab 1091 ignore_timeout = 0;
d1b31da7
WD
1092 readfd_unbuffered(sock_f_in, io_filesfrom_buf,
1093 sizeof io_filesfrom_buf);
1094 }
1ea087a7
WD
1095 exit_cleanup(RERR_STREAMIO);
1096 }
4c36ddbe 1097
1ea087a7 1098 total += ret;
a800434a 1099
1f75bb10 1100 if (fd == sock_f_out) {
626bec8e 1101 if (io_timeout || am_generator)
3b0a30eb 1102 last_io_out = time(NULL);
1f75bb10
WD
1103 sleep_for_bwlimit(ret);
1104 }
4c36ddbe 1105 }
e44f9a12
AT
1106
1107 no_flush--;
720b47f2
AT
1108}
1109
8d9dc9f9 1110
880da007
MP
1111/**
1112 * Write an message to a multiplexed stream. If this fails then rsync
1113 * exits.
1114 **/
1f75bb10 1115static void mplex_write(enum msgcode code, char *buf, size_t len)
ff41a59f 1116{
33544bf4 1117 char buffer[BIGPATHBUFLEN];
06ce139f 1118 size_t n = len;
8d9dc9f9 1119
ff41a59f
AT
1120 SIVAL(buffer, 0, ((MPLEX_BASE + (int)code)<<24) + len);
1121
af436313
WD
1122 /* When the generator reads messages from the msg_fd_in pipe, it can
1123 * cause output to occur down the socket. Setting contiguous_write_len
1124 * prevents the reading of msg_fd_in once we actually start to write
1125 * this sequence of data (though we might read it before the start). */
1126 if (am_generator && msg_fd_in >= 0)
1127 contiguous_write_len = len + 4;
1128
c399d22a 1129 if (n > sizeof buffer - 4)
3151cbae 1130 n = sizeof buffer - 4;
ff41a59f
AT
1131
1132 memcpy(&buffer[4], buf, n);
1f75bb10 1133 writefd_unbuffered(sock_f_out, buffer, n+4);
ff41a59f
AT
1134
1135 len -= n;
1136 buf += n;
1137
1ea087a7 1138 if (len)
1f75bb10 1139 writefd_unbuffered(sock_f_out, buf, len);
af436313
WD
1140
1141 if (am_generator && msg_fd_in >= 0)
1142 contiguous_write_len = 0;
d6dead6b
AT
1143}
1144
ff41a59f 1145
d17e1dd2 1146void io_flush(int flush_it_all)
d6dead6b 1147{
d17e1dd2 1148 msg_list_push(flush_it_all);
90ba34e2 1149
1f75bb10 1150 if (!iobuf_out_cnt || no_flush)
d17e1dd2 1151 return;
8d9dc9f9 1152
d17e1dd2 1153 if (io_multiplexing_out)
1f75bb10 1154 mplex_write(MSG_DATA, iobuf_out, iobuf_out_cnt);
d17e1dd2 1155 else
1f75bb10
WD
1156 writefd_unbuffered(sock_f_out, iobuf_out, iobuf_out_cnt);
1157 iobuf_out_cnt = 0;
8d9dc9f9
AT
1158}
1159
0ba48136 1160
9dd891bb 1161static void writefd(int fd,char *buf,size_t len)
d6dead6b 1162{
4033b80b
WD
1163 if (fd == msg_fd_out) {
1164 rprintf(FERROR, "Internal error: wrong write used in receiver.\n");
1165 exit_cleanup(RERR_PROTOCOL);
1166 }
90ba34e2 1167
1f75bb10
WD
1168 if (fd == sock_f_out)
1169 stats.total_written += len;
1170
b9f592fb
WD
1171 if (fd == write_batch_monitor_out) {
1172 if ((size_t)write(batch_fd, buf, len) != len)
1173 exit_cleanup(RERR_FILEIO);
1174 }
1175
1f75bb10 1176 if (!iobuf_out || fd != sock_f_out) {
4c36ddbe
AT
1177 writefd_unbuffered(fd, buf, len);
1178 return;
1179 }
d6dead6b
AT
1180
1181 while (len) {
1f75bb10 1182 int n = MIN((int)len, IO_BUFFER_SIZE - iobuf_out_cnt);
d6dead6b 1183 if (n > 0) {
1f75bb10 1184 memcpy(iobuf_out+iobuf_out_cnt, buf, n);
d6dead6b
AT
1185 buf += n;
1186 len -= n;
1f75bb10 1187 iobuf_out_cnt += n;
d6dead6b 1188 }
3151cbae 1189
1f75bb10 1190 if (iobuf_out_cnt == IO_BUFFER_SIZE)
d17e1dd2 1191 io_flush(NORMAL_FLUSH);
d6dead6b 1192 }
d6dead6b 1193}
720b47f2
AT
1194
1195
0d67e00a 1196void write_shortint(int f, int x)
9361f839
WD
1197{
1198 uchar b[2];
1199 b[0] = x;
1200 b[1] = x >> 8;
1201 writefd(f, (char *)b, 2);
1202}
1203
1204
b7922338 1205void write_int(int f,int32 x)
720b47f2 1206{
8d9dc9f9
AT
1207 char b[4];
1208 SIVAL(b,0,x);
4c36ddbe 1209 writefd(f,b,4);
720b47f2
AT
1210}
1211
7a24c346 1212
805edf9d
MP
1213void write_int_named(int f, int32 x, const char *phase)
1214{
1215 io_write_phase = phase;
1216 write_int(f, x);
1217 io_write_phase = phase_unknown;
1218}
1219
1220
7a24c346
MP
1221/*
1222 * Note: int64 may actually be a 32-bit type if ./configure couldn't find any
1223 * 64-bit types on this platform.
1224 */
71c46176 1225void write_longint(int f, int64 x)
3a6a366f 1226{
3a6a366f 1227 char b[8];
3a6a366f 1228
91c4da3f 1229 if (x <= 0x7FFFFFFF) {
3a6a366f
AT
1230 write_int(f, (int)x);
1231 return;
1232 }
1233
031fa9ad
WD
1234#if SIZEOF_INT64 < 8
1235 rprintf(FERROR, "Integer overflow: attempted 64-bit offset\n");
1236 exit_cleanup(RERR_UNSUPPORTED);
1237#else
8de330a3 1238 write_int(f, (int32)0xFFFFFFFF);
3a6a366f
AT
1239 SIVAL(b,0,(x&0xFFFFFFFF));
1240 SIVAL(b,4,((x>>32)&0xFFFFFFFF));
1241
4c36ddbe 1242 writefd(f,b,8);
031fa9ad 1243#endif
3a6a366f
AT
1244}
1245
9dd891bb 1246void write_buf(int f,char *buf,size_t len)
720b47f2 1247{
4c36ddbe 1248 writefd(f,buf,len);
720b47f2
AT
1249}
1250
880da007 1251/** Write a string to the connection */
cf338ab1 1252void write_sbuf(int f, char *buf)
f0fca04e 1253{
93095cbe 1254 writefd(f, buf, strlen(buf));
f0fca04e
AT
1255}
1256
9361f839 1257void write_byte(int f, uchar c)
182dca5c 1258{
93095cbe 1259 writefd(f, (char *)&c, 1);
182dca5c
AT
1260}
1261
46e99b09
WD
1262void write_vstring(int f, char *str, int len)
1263{
1264 uchar lenbuf[3], *lb = lenbuf;
1265
1266 if (len > 0x7F) {
1267 if (len > 0x7FFF) {
1268 rprintf(FERROR,
1269 "attempting to send over-long vstring (%d > %d)\n",
1270 len, 0x7FFF);
1271 exit_cleanup(RERR_PROTOCOL);
1272 }
1273 *lb++ = len / 0x100 + 0x80;
1274 }
1275 *lb = len;
1276
1277 writefd(f, (char*)lenbuf, lb - lenbuf + 1);
1278 if (len)
1279 writefd(f, str, len);
1280}
1281
7a55d06e 1282
914cc65c 1283/**
6ed6d7f5
WD
1284 * Read a line of up to @p maxlen characters into @p buf (not counting
1285 * the trailing null). Strips the (required) trailing newline and all
1286 * carriage returns.
914cc65c 1287 *
6ed6d7f5 1288 * @return 1 for success; 0 for I/O error or truncation.
914cc65c 1289 **/
9dd891bb 1290int read_line(int f, char *buf, size_t maxlen)
f0fca04e
AT
1291{
1292 while (maxlen) {
528bfcd7 1293 buf[0] = 0;
f0fca04e 1294 read_buf(f, buf, 1);
914cc65c
MP
1295 if (buf[0] == 0)
1296 return 0;
6ed6d7f5 1297 if (buf[0] == '\n')
f0fca04e 1298 break;
f0fca04e
AT
1299 if (buf[0] != '\r') {
1300 buf++;
1301 maxlen--;
1302 }
1303 }
6ed6d7f5
WD
1304 *buf = '\0';
1305 return maxlen > 0;
f0fca04e
AT
1306}
1307
1308
1309void io_printf(int fd, const char *format, ...)
1310{
d62bcc17 1311 va_list ap;
33544bf4 1312 char buf[BIGPATHBUFLEN];
f0fca04e 1313 int len;
3151cbae 1314
f0fca04e 1315 va_start(ap, format);
3151cbae 1316 len = vsnprintf(buf, sizeof buf, format, ap);
f0fca04e
AT
1317 va_end(ap);
1318
f89b9368
WD
1319 if (len < 0)
1320 exit_cleanup(RERR_STREAMIO);
f0fca04e 1321
33544bf4
WD
1322 if (len > (int)sizeof buf) {
1323 rprintf(FERROR, "io_printf() was too long for the buffer.\n");
1324 exit_cleanup(RERR_STREAMIO);
1325 }
1326
f0fca04e
AT
1327 write_sbuf(fd, buf);
1328}
8d9dc9f9
AT
1329
1330
d17e1dd2 1331/** Setup for multiplexing a MSG_* stream with the data stream. */
1f75bb10 1332void io_start_multiplex_out(void)
8d9dc9f9 1333{
d17e1dd2 1334 io_flush(NORMAL_FLUSH);
1f75bb10 1335 io_start_buffering_out();
8d9dc9f9
AT
1336 io_multiplexing_out = 1;
1337}
1338
d17e1dd2 1339/** Setup for multiplexing a MSG_* stream with the data stream. */
1f75bb10 1340void io_start_multiplex_in(void)
8d9dc9f9 1341{
d17e1dd2 1342 io_flush(NORMAL_FLUSH);
1f75bb10 1343 io_start_buffering_in();
8d9dc9f9
AT
1344 io_multiplexing_in = 1;
1345}
1346
d17e1dd2
WD
1347/** Write an message to the multiplexed data stream. */
1348int io_multiplex_write(enum msgcode code, char *buf, size_t len)
8d9dc9f9 1349{
f89b9368
WD
1350 if (!io_multiplexing_out)
1351 return 0;
8d9dc9f9 1352
d17e1dd2 1353 io_flush(NORMAL_FLUSH);
1b7c47cb 1354 stats.total_written += (len+4);
1f75bb10 1355 mplex_write(code, buf, len);
8d9dc9f9
AT
1356 return 1;
1357}
1358
7f459268
WD
1359void close_multiplexing_in(void)
1360{
1361 io_multiplexing_in = 0;
1362}
1363
d17e1dd2 1364/** Stop output multiplexing. */
7f459268 1365void close_multiplexing_out(void)
554e0a8d
AT
1366{
1367 io_multiplexing_out = 0;
1368}
1369
b9f592fb
WD
1370void start_write_batch(int fd)
1371{
741d6544
WD
1372 write_stream_flags(batch_fd);
1373
b9f592fb
WD
1374 /* Some communication has already taken place, but we don't
1375 * enable batch writing until here so that we can write a
1376 * canonical record of the communication even though the
1377 * actual communication so far depends on whether a daemon
1378 * is involved. */
1379 write_int(batch_fd, protocol_version);
1380 write_int(batch_fd, checksum_seed);
b9f592fb
WD
1381
1382 if (am_sender)
1383 write_batch_monitor_out = fd;
1384 else
1385 write_batch_monitor_in = fd;
1386}
1387
1388void stop_write_batch(void)
1389{
1390 write_batch_monitor_out = -1;
1391 write_batch_monitor_in = -1;
1392}