Changed dup-detection code to keep the first item in the user's args.
[rsync/rsync.git] / io.c
CommitLineData
0f78b815
WD
1/*
2 * Socket and pipe I/O utilities used in rsync.
d62bcc17 3 *
0f78b815
WD
4 * Copyright (C) 1996-2001 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org>
ba2133d6 7 * Copyright (C) 2003-2007 Wayne Davison
d62bcc17 8 *
880da007 9 * This program is free software; you can redistribute it and/or modify
ba2133d6
WD
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
d62bcc17 12 *
880da007
MP
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
d62bcc17 17 *
e7c67065
WD
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
880da007 21 */
720b47f2 22
0f78b815
WD
23/* Rsync provides its own multiplexing system, which is used to send
24 * stderr and stdout over a single socket.
87ee2481
MP
25 *
26 * For historical reasons this is off during the start of the
27 * connection, but it's switched on quite early using
0f78b815 28 * io_start_multiplex_out() and io_start_multiplex_in(). */
720b47f2 29
720b47f2
AT
30#include "rsync.h"
31
880da007 32/** If no timeout is specified then use a 60 second select timeout */
8cd9fd4e
AT
33#define SELECT_TIMEOUT 60
34
7a55d06e 35extern int bwlimit;
71e58630 36extern size_t bwlimit_writemax;
6ba9279f 37extern int io_timeout;
cdf236aa 38extern int allowed_lull;
d17e1dd2
WD
39extern int am_server;
40extern int am_daemon;
41extern int am_sender;
98f8c9a5 42extern int am_generator;
3ea6e0e7 43extern int inc_recurse;
8ef246e0 44extern int io_error;
e626b29e 45extern int eol_nulls;
8ef246e0 46extern int flist_eof;
3b0a30eb 47extern int read_batch;
188fed95 48extern int csum_length;
b9f592fb
WD
49extern int checksum_seed;
50extern int protocol_version;
47c11975 51extern int remove_source_files;
cdf236aa 52extern int preserve_hard_links;
d19320fd 53extern char *filesfrom_host;
a800434a 54extern struct stats stats;
8ef246e0 55extern struct file_list *cur_flist, *first_flist;
332cf6df
WD
56#ifdef ICONV_OPTION
57extern iconv_t ic_send, ic_recv;
58#endif
720b47f2 59
a86179f4 60const char phase_unknown[] = "unknown";
9e2409ab 61int ignore_timeout = 0;
b9f592fb 62int batch_fd = -1;
04c722d5 63int msgdone_cnt = 0;
805edf9d 64
cb2e1f18 65/* Ignore an EOF error if non-zero. See whine_about_eof(). */
574c2500 66int kluge_around_eof = 0;
7a55d06e 67
d17e1dd2
WD
68int msg_fd_in = -1;
69int msg_fd_out = -1;
cdf236aa
WD
70int sock_f_in = -1;
71int sock_f_out = -1;
7a55d06e 72
8ef246e0
WD
73static int iobuf_f_in = -1;
74static char *iobuf_in;
75static size_t iobuf_in_siz;
76static size_t iobuf_in_ndx;
77static size_t iobuf_in_remaining;
78
79static int iobuf_f_out = -1;
80static char *iobuf_out;
81static int iobuf_out_cnt;
82
83int flist_forward_from = -1;
84
1f75bb10
WD
85static int io_multiplexing_out;
86static int io_multiplexing_in;
3b0a30eb
WD
87static time_t last_io_in;
88static time_t last_io_out;
1f75bb10
WD
89static int no_flush;
90
b9f592fb
WD
91static int write_batch_monitor_in = -1;
92static int write_batch_monitor_out = -1;
93
56014c8c
WD
94static int io_filesfrom_f_in = -1;
95static int io_filesfrom_f_out = -1;
96static char io_filesfrom_buf[2048];
332cf6df
WD
97#ifdef ICONV_OPTION
98static char iconv_buf[sizeof io_filesfrom_buf / 2];
99#endif
56014c8c
WD
100static char *io_filesfrom_bp;
101static char io_filesfrom_lastchar;
102static int io_filesfrom_buflen;
954bbed8 103static int defer_forwarding_messages = 0;
3b0a30eb 104static int select_timeout = SELECT_TIMEOUT;
d6081c82
WD
105static int active_filecnt = 0;
106static OFF_T active_bytecnt = 0;
720b47f2 107
351e23ad
WD
108static char int_byte_extra[64] = {
109 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* (00 - 3F)/4 */
110 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* (40 - 7F)/4 */
111 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* (80 - BF)/4 */
112 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 6, /* (C0 - FF)/4 */
482f48cc
WD
113};
114
dde5b772 115static void readfd(int fd, char *buffer, size_t N);
8ef246e0
WD
116static void writefd(int fd, const char *buf, size_t len);
117static void writefd_unbuffered(int fd, const char *buf, size_t len);
118static void decrement_active_files(int ndx);
119static void decrement_flist_in_progress(int ndx, int redo);
332cf6df 120static void mplex_write(int fd, enum msgcode code, const char *buf, size_t len, int convert);
ff41a59f 121
c6816b94
WD
122struct flist_ndx_item {
123 struct flist_ndx_item *next;
124 int ndx;
d17e1dd2
WD
125};
126
c6816b94
WD
127struct flist_ndx_list {
128 struct flist_ndx_item *head, *tail;
129};
130
cdf236aa 131static struct flist_ndx_list redo_list, hlink_list;
d17e1dd2 132
08c88178
WD
133struct msg_list_item {
134 struct msg_list_item *next;
332cf6df 135 char convert;
954bbed8 136 char buf[1];
d17e1dd2
WD
137};
138
08c88178
WD
139struct msg_list {
140 struct msg_list_item *head, *tail;
141};
142
e4c877cf 143static struct msg_list msg_queue;
d17e1dd2 144
c6816b94 145static void flist_ndx_push(struct flist_ndx_list *lp, int ndx)
d17e1dd2 146{
c6816b94
WD
147 struct flist_ndx_item *item;
148
149 if (!(item = new(struct flist_ndx_item)))
150 out_of_memory("flist_ndx_push");
151 item->next = NULL;
152 item->ndx = ndx;
153 if (lp->tail)
154 lp->tail->next = item;
d17e1dd2 155 else
c6816b94
WD
156 lp->head = item;
157 lp->tail = item;
158}
159
160static int flist_ndx_pop(struct flist_ndx_list *lp)
161{
162 struct flist_ndx_item *next;
163 int ndx;
164
165 if (!lp->head)
166 return -1;
167
168 ndx = lp->head->ndx;
169 next = lp->head->next;
170 free(lp->head);
171 lp->head = next;
172 if (!next)
173 lp->tail = NULL;
174
175 return ndx;
d17e1dd2
WD
176}
177
8d9dc9f9
AT
178static void check_timeout(void)
179{
180 time_t t;
90ba34e2 181
9e2409ab 182 if (!io_timeout || ignore_timeout)
d17e1dd2 183 return;
8d9dc9f9 184
3b0a30eb
WD
185 if (!last_io_in) {
186 last_io_in = time(NULL);
8d9dc9f9
AT
187 return;
188 }
189
190 t = time(NULL);
191
3b0a30eb 192 if (t - last_io_in >= io_timeout) {
0adb99b9 193 if (!am_server && !am_daemon) {
4ccfd96c 194 rprintf(FERROR, "io timeout after %d seconds -- exiting\n",
3b0a30eb 195 (int)(t-last_io_in));
0adb99b9 196 }
65417579 197 exit_cleanup(RERR_TIMEOUT);
8d9dc9f9
AT
198 }
199}
200
1f75bb10
WD
201/* Note the fds used for the main socket (which might really be a pipe
202 * for a local transfer, but we can ignore that). */
203void io_set_sock_fds(int f_in, int f_out)
204{
205 sock_f_in = f_in;
206 sock_f_out = f_out;
207}
208
3b0a30eb
WD
209void set_io_timeout(int secs)
210{
211 io_timeout = secs;
212
213 if (!io_timeout || io_timeout > SELECT_TIMEOUT)
214 select_timeout = SELECT_TIMEOUT;
215 else
216 select_timeout = io_timeout;
217
218 allowed_lull = read_batch ? 0 : (io_timeout + 1) / 2;
219}
220
98f8c9a5
WD
221/* Setup the fd used to receive MSG_* messages. Only needed during the
222 * early stages of being a local sender (up through the sending of the
223 * file list) or when we're the generator (to fetch the messages from
224 * the receiver). */
d17e1dd2
WD
225void set_msg_fd_in(int fd)
226{
227 msg_fd_in = fd;
228}
229
98f8c9a5
WD
230/* Setup the fd used to send our MSG_* messages. Only needed when
231 * we're the receiver (to send our messages to the generator). */
d17e1dd2
WD
232void set_msg_fd_out(int fd)
233{
234 msg_fd_out = fd;
235 set_nonblocking(msg_fd_out);
236}
237
238/* Add a message to the pending MSG_* list. */
332cf6df 239static void msg_list_add(struct msg_list *lst, int code, const char *buf, int len, int convert)
554e0a8d 240{
954bbed8
WD
241 struct msg_list_item *m;
242 int sz = len + 4 + sizeof m[0] - 1;
d17e1dd2 243
954bbed8 244 if (!(m = (struct msg_list_item *)new_array(char, sz)))
c6816b94 245 out_of_memory("msg_list_add");
954bbed8 246 m->next = NULL;
332cf6df 247 m->convert = convert;
954bbed8
WD
248 SIVAL(m->buf, 0, ((code+MPLEX_BASE)<<24) | len);
249 memcpy(m->buf + 4, buf, len);
250 if (lst->tail)
251 lst->tail->next = m;
d17e1dd2 252 else
954bbed8
WD
253 lst->head = m;
254 lst->tail = m;
554e0a8d
AT
255}
256
e4c877cf 257static void msg_flush(void)
3be97bf9 258{
e4c877cf
WD
259 if (am_generator) {
260 while (msg_queue.head && io_multiplexing_out) {
261 struct msg_list_item *m = msg_queue.head;
332cf6df
WD
262 int len = IVAL(m->buf, 0) & 0xFFFFFF;
263 int tag = *((uchar*)m->buf+3) - MPLEX_BASE;
e4c877cf
WD
264 if (!(msg_queue.head = m->next))
265 msg_queue.tail = NULL;
332cf6df 266 stats.total_written += len + 4;
e4c877cf 267 defer_forwarding_messages++;
332cf6df 268 mplex_write(sock_f_out, tag, m->buf + 4, len, m->convert);
e4c877cf
WD
269 defer_forwarding_messages--;
270 free(m);
271 }
272 } else {
273 while (msg_queue.head) {
274 struct msg_list_item *m = msg_queue.head;
332cf6df
WD
275 int len = IVAL(m->buf, 0) & 0xFFFFFF;
276 int tag = *((uchar*)m->buf+3) - MPLEX_BASE;
e4c877cf
WD
277 if (!(msg_queue.head = m->next))
278 msg_queue.tail = NULL;
279 defer_forwarding_messages++;
332cf6df 280 mplex_write(msg_fd_out, tag, m->buf + 4, len, m->convert);
e4c877cf
WD
281 defer_forwarding_messages--;
282 free(m);
283 }
3be97bf9
WD
284 }
285}
286
98f8c9a5
WD
287/* Read a message from the MSG_* fd and handle it. This is called either
288 * during the early stages of being a local sender (up through the sending
289 * of the file list) or when we're the generator (to fetch the messages
290 * from the receiver). */
d17e1dd2 291static void read_msg_fd(void)
554e0a8d 292{
f9c6b3e7 293 char buf[2048];
06ce139f 294 size_t n;
8ef246e0 295 struct file_list *flist;
d17e1dd2 296 int fd = msg_fd_in;
ff41a59f
AT
297 int tag, len;
298
00bdf899 299 /* Temporarily disable msg_fd_in. This is needed to avoid looping back
af436313 300 * to this routine from writefd_unbuffered(). */
3be97bf9 301 no_flush++;
d17e1dd2 302 msg_fd_in = -1;
be21e29c 303 defer_forwarding_messages++;
554e0a8d 304
dde5b772 305 readfd(fd, buf, 4);
ff41a59f
AT
306 tag = IVAL(buf, 0);
307
308 len = tag & 0xFFFFFF;
d17e1dd2 309 tag = (tag >> 24) - MPLEX_BASE;
ff41a59f 310
d17e1dd2
WD
311 switch (tag) {
312 case MSG_DONE:
cc7b86bf 313 if (len < 0 || len > 1 || !am_generator) {
8ef246e0
WD
314 invalid_msg:
315 rprintf(FERROR, "invalid message %d:%d [%s%s]\n",
316 tag, len, who_am_i(),
3ea6e0e7 317 inc_recurse ? "/inc" : "");
d17e1dd2 318 exit_cleanup(RERR_STREAMIO);
13c7bcbb 319 }
cc7b86bf
WD
320 if (len) {
321 readfd(fd, buf, len);
351e23ad 322 stats.total_read = read_varlong(fd, 3);
cc7b86bf 323 }
04c722d5 324 msgdone_cnt++;
d17e1dd2
WD
325 break;
326 case MSG_REDO:
8ef246e0
WD
327 if (len != 4 || !am_generator)
328 goto invalid_msg;
dde5b772 329 readfd(fd, buf, 4);
47c11975 330 if (remove_source_files)
d6081c82 331 decrement_active_files(IVAL(buf,0));
c6816b94 332 flist_ndx_push(&redo_list, IVAL(buf,0));
3ea6e0e7 333 if (inc_recurse)
8ef246e0
WD
334 decrement_flist_in_progress(IVAL(buf,0), 1);
335 break;
336 case MSG_FLIST:
3ea6e0e7 337 if (len != 4 || !am_generator || !inc_recurse)
8ef246e0 338 goto invalid_msg;
dde5b772 339 readfd(fd, buf, 4);
8ef246e0
WD
340 /* Read extra file list from receiver. */
341 assert(iobuf_in != NULL);
342 assert(iobuf_f_in == fd);
1faa1a6d
WD
343 if (verbose > 3) {
344 rprintf(FINFO, "[%s] receiving flist for dir %d\n",
345 who_am_i(), IVAL(buf,0));
346 }
8ef246e0
WD
347 flist = recv_file_list(fd);
348 flist->parent_ndx = IVAL(buf,0);
349 break;
350 case MSG_FLIST_EOF:
3ea6e0e7 351 if (len != 0 || !am_generator || !inc_recurse)
8ef246e0
WD
352 goto invalid_msg;
353 flist_eof = 1;
d17e1dd2 354 break;
0d67e00a 355 case MSG_DELETED:
8ef246e0
WD
356 if (len >= (int)sizeof buf || !am_generator)
357 goto invalid_msg;
dde5b772 358 readfd(fd, buf, len);
332cf6df 359 send_msg(MSG_DELETED, buf, len, 1);
0d67e00a 360 break;
9981c27e 361 case MSG_SUCCESS:
8ef246e0
WD
362 if (len != 4 || !am_generator)
363 goto invalid_msg;
dde5b772 364 readfd(fd, buf, len);
47c11975 365 if (remove_source_files) {
d6081c82 366 decrement_active_files(IVAL(buf,0));
332cf6df 367 send_msg(MSG_SUCCESS, buf, len, 0);
d6081c82 368 }
cdf236aa
WD
369 if (preserve_hard_links)
370 flist_ndx_push(&hlink_list, IVAL(buf,0));
3ea6e0e7 371 if (inc_recurse)
8ef246e0
WD
372 decrement_flist_in_progress(IVAL(buf,0), 0);
373 break;
374 case MSG_NO_SEND:
375 if (len != 4 || !am_generator)
376 goto invalid_msg;
dde5b772 377 readfd(fd, buf, len);
3ea6e0e7 378 if (inc_recurse)
8ef246e0 379 decrement_flist_in_progress(IVAL(buf,0), 0);
9981c27e 380 break;
72f2d1b3 381 case MSG_SOCKERR:
64119c79 382 case MSG_CLIENT:
8ef246e0
WD
383 if (!am_generator)
384 goto invalid_msg;
64119c79 385 if (tag == MSG_SOCKERR)
8ef246e0 386 io_end_multiplex_out();
72f2d1b3 387 /* FALL THROUGH */
d17e1dd2
WD
388 case MSG_INFO:
389 case MSG_ERROR:
390 case MSG_LOG:
391 while (len) {
392 n = len;
393 if (n >= sizeof buf)
394 n = sizeof buf - 1;
dde5b772 395 readfd(fd, buf, n);
332cf6df 396 rwrite((enum logcode)tag, buf, n, !am_generator);
d17e1dd2
WD
397 len -= n;
398 }
399 break;
400 default:
f8b9da1a
WD
401 rprintf(FERROR, "unknown message %d:%d [%s]\n",
402 tag, len, who_am_i());
d17e1dd2
WD
403 exit_cleanup(RERR_STREAMIO);
404 }
405
3be97bf9 406 no_flush--;
d17e1dd2 407 msg_fd_in = fd;
3be97bf9 408 if (!--defer_forwarding_messages)
e4c877cf 409 msg_flush();
d17e1dd2
WD
410}
411
d6081c82 412/* This is used by the generator to limit how many file transfers can
47c11975 413 * be active at once when --remove-source-files is specified. Without
d6081c82
WD
414 * this, sender-side deletions were mostly happening at the end. */
415void increment_active_files(int ndx, int itemizing, enum logcode code)
416{
417 /* TODO: tune these limits? */
5b986297 418 while (active_filecnt >= (active_bytecnt >= 128*1024 ? 10 : 50)) {
04c722d5
WD
419 check_for_finished_files(itemizing, code, 0);
420 if (iobuf_out_cnt)
421 io_flush(NORMAL_FLUSH);
422 else
423 read_msg_fd();
d6081c82
WD
424 }
425
426 active_filecnt++;
121bfb2b 427 active_bytecnt += F_LENGTH(cur_flist->files[ndx - cur_flist->ndx_start]);
d6081c82
WD
428}
429
8ef246e0 430static void decrement_active_files(int ndx)
d6081c82 431{
8ef246e0
WD
432 struct file_list *flist = flist_for_ndx(ndx);
433 assert(flist != NULL);
d6081c82 434 active_filecnt--;
8ef246e0
WD
435 active_bytecnt -= F_LENGTH(flist->files[ndx - flist->ndx_start]);
436}
437
438static void decrement_flist_in_progress(int ndx, int redo)
439{
440 struct file_list *flist = cur_flist ? cur_flist : first_flist;
441
442 while (ndx < flist->ndx_start) {
443 if (flist == first_flist) {
444 invalid_ndx:
445 rprintf(FERROR,
446 "Invalid file index: %d (%d - %d) [%s]\n",
447 ndx, first_flist->ndx_start,
448 first_flist->prev->ndx_start + first_flist->prev->count - 1,
449 who_am_i());
450 exit_cleanup(RERR_PROTOCOL);
451 }
452 flist = flist->prev;
453 }
454 while (ndx >= flist->ndx_start + flist->count) {
455 if (!(flist = flist->next))
456 goto invalid_ndx;
457 }
458
459 flist->in_progress--;
460 if (redo)
461 flist->to_redo++;
d6081c82
WD
462}
463
3be97bf9 464/* Write an message to a multiplexed stream. If this fails, rsync exits. */
332cf6df 465static void mplex_write(int fd, enum msgcode code, const char *buf, size_t len, int convert)
d17e1dd2 466{
332cf6df 467 char buffer[BIGPATHBUFLEN]; /* Oversized for use by iconv code. */
3be97bf9 468 size_t n = len;
d17e1dd2 469
3be97bf9
WD
470 SIVAL(buffer, 0, ((MPLEX_BASE + (int)code)<<24) + len);
471
332cf6df
WD
472#ifdef ICONV_OPTION
473 if (convert && ic_send == (iconv_t)-1)
474#endif
475 convert = 0;
476
477 if (convert || n > 1024 - 4) /* BIGPATHBUFLEN can handle 1024 bytes */
3be97bf9
WD
478 n = 0;
479 else
480 memcpy(buffer + 4, buf, n);
481
482 writefd_unbuffered(fd, buffer, n+4);
483
484 len -= n;
485 buf += n;
486
332cf6df
WD
487#ifdef ICONV_OPTION
488 if (convert) {
489 iconv(ic_send, NULL, 0, NULL, 0);
490 defer_forwarding_messages++;
491 while (len) {
492 ICONV_CONST char *ibuf = (ICONV_CONST char *)buf;
493 char *obuf = buffer;
494 size_t ocnt = sizeof buffer;
495 while (len && iconv(ic_send, &ibuf,&len,
496 &obuf,&ocnt) == (size_t)-1) {
497 if (errno == E2BIG || !ocnt)
498 break;
499 *obuf++ = *ibuf++;
500 ocnt--, len--;
501 }
502 n = obuf - buffer;
503 writefd_unbuffered(fd, buffer, n);
504 }
505 if (!--defer_forwarding_messages)
506 msg_flush();
507 } else
508#endif
3be97bf9
WD
509 if (len) {
510 defer_forwarding_messages++;
511 writefd_unbuffered(fd, buf, len);
512 if (!--defer_forwarding_messages)
e4c877cf 513 msg_flush();
8ef246e0 514 }
d17e1dd2
WD
515}
516
332cf6df 517int send_msg(enum msgcode code, const char *buf, int len, int convert)
37f35d89
WD
518{
519 if (msg_fd_out < 0) {
12ccc73a 520 if (!defer_forwarding_messages)
332cf6df 521 return io_multiplex_write(code, buf, len, convert);
12ccc73a
WD
522 if (!io_multiplexing_out)
523 return 0;
332cf6df 524 msg_list_add(&msg_queue, code, buf, len, convert);
12ccc73a 525 return 1;
37f35d89 526 }
e4c877cf 527 if (flist_forward_from >= 0)
332cf6df 528 msg_list_add(&msg_queue, code, buf, len, convert);
e4c877cf 529 else
332cf6df 530 mplex_write(msg_fd_out, code, buf, len, convert);
12ccc73a 531 return 1;
37f35d89
WD
532}
533
155d9206
WD
534void send_msg_int(enum msgcode code, int num)
535{
536 char numbuf[4];
537 SIVAL(numbuf, 0, num);
332cf6df 538 send_msg(code, numbuf, 4, 0);
155d9206
WD
539}
540
8ef246e0 541void wait_for_receiver(void)
d17e1dd2 542{
2a5df862
WD
543 if (iobuf_out_cnt)
544 io_flush(NORMAL_FLUSH);
545 else
546 read_msg_fd();
8ef246e0 547}
554e0a8d 548
8ef246e0
WD
549int get_redo_num(void)
550{
c6816b94 551 return flist_ndx_pop(&redo_list);
554e0a8d
AT
552}
553
cdf236aa
WD
554int get_hlink_num(void)
555{
556 return flist_ndx_pop(&hlink_list);
557}
558
56014c8c
WD
559/**
560 * When we're the receiver and we have a local --files-from list of names
561 * that needs to be sent over the socket to the sender, we have to do two
562 * things at the same time: send the sender a list of what files we're
563 * processing and read the incoming file+info list from the sender. We do
564 * this by augmenting the read_timeout() function to copy this data. It
565 * uses the io_filesfrom_buf to read a block of data from f_in (when it is
566 * ready, since it might be a pipe) and then blast it out f_out (when it
567 * is ready to receive more data).
568 */
569void io_set_filesfrom_fds(int f_in, int f_out)
570{
571 io_filesfrom_f_in = f_in;
572 io_filesfrom_f_out = f_out;
573 io_filesfrom_bp = io_filesfrom_buf;
574 io_filesfrom_lastchar = '\0';
575 io_filesfrom_buflen = 0;
576}
720b47f2 577
cb2e1f18
WD
578/* It's almost always an error to get an EOF when we're trying to read from the
579 * network, because the protocol is (for the most part) self-terminating.
880da007 580 *
cb2e1f18
WD
581 * There is one case for the receiver when it is at the end of the transfer
582 * (hanging around reading any keep-alive packets that might come its way): if
583 * the sender dies before the generator's kill-signal comes through, we can end
584 * up here needing to loop until the kill-signal arrives. In this situation,
585 * kluge_around_eof will be < 0.
586 *
587 * There is another case for older protocol versions (< 24) where the module
588 * listing was not terminated, so we must ignore an EOF error in that case and
589 * exit. In this situation, kluge_around_eof will be > 0. */
1f75bb10 590static void whine_about_eof(int fd)
7a55d06e 591{
574c2500 592 if (kluge_around_eof && fd == sock_f_in) {
55bb7fff 593 int i;
574c2500
WD
594 if (kluge_around_eof > 0)
595 exit_cleanup(0);
55bb7fff
WD
596 /* If we're still here after 10 seconds, exit with an error. */
597 for (i = 10*1000/20; i--; )
574c2500
WD
598 msleep(20);
599 }
3151cbae 600
00bdf899 601 rprintf(FERROR, RSYNC_NAME ": connection unexpectedly closed "
dca68b0a
WD
602 "(%.0f bytes received so far) [%s]\n",
603 (double)stats.total_read, who_am_i());
00bdf899
WD
604
605 exit_cleanup(RERR_STREAMIO);
7a55d06e 606}
720b47f2 607
880da007 608/**
6ed6d7f5 609 * Read from a socket with I/O timeout. return the number of bytes
c3563c46
MP
610 * read. If no bytes can be read then exit, never return a number <= 0.
611 *
8886f8d0
MP
612 * TODO: If the remote shell connection fails, then current versions
613 * actually report an "unexpected EOF" error here. Since it's a
614 * fairly common mistake to try to use rsh when ssh is required, we
615 * should trap that: if we fail to read any data at all, we should
616 * give a better explanation. We can tell whether the connection has
617 * started by looking e.g. at whether the remote version is known yet.
c3563c46 618 */
3151cbae 619static int read_timeout(int fd, char *buf, size_t len)
8d9dc9f9 620{
d8aeda1e 621 int n, cnt = 0;
4c36ddbe 622
dde5b772 623 io_flush(FULL_FLUSH);
ea2111d1 624
d8aeda1e 625 while (cnt == 0) {
7a55d06e 626 /* until we manage to read *something* */
56014c8c 627 fd_set r_fds, w_fds;
4c36ddbe 628 struct timeval tv;
1ea087a7 629 int maxfd = fd;
a57873b7 630 int count;
4c36ddbe 631
56014c8c 632 FD_ZERO(&r_fds);
a7026ba9 633 FD_ZERO(&w_fds);
56014c8c 634 FD_SET(fd, &r_fds);
3309507d 635 if (io_filesfrom_f_out >= 0) {
56014c8c
WD
636 int new_fd;
637 if (io_filesfrom_buflen == 0) {
3309507d 638 if (io_filesfrom_f_in >= 0) {
56014c8c
WD
639 FD_SET(io_filesfrom_f_in, &r_fds);
640 new_fd = io_filesfrom_f_in;
641 } else {
642 io_filesfrom_f_out = -1;
643 new_fd = -1;
644 }
645 } else {
56014c8c
WD
646 FD_SET(io_filesfrom_f_out, &w_fds);
647 new_fd = io_filesfrom_f_out;
648 }
1ea087a7
WD
649 if (new_fd > maxfd)
650 maxfd = new_fd;
554e0a8d
AT
651 }
652
e626b29e 653 tv.tv_sec = select_timeout;
4c36ddbe
AT
654 tv.tv_usec = 0;
655
554e0a8d
AT
656 errno = 0;
657
a7026ba9 658 count = select(maxfd + 1, &r_fds, &w_fds, NULL, &tv);
a57873b7 659
a57873b7 660 if (count <= 0) {
2a5df862
WD
661 if (errno == EBADF) {
662 defer_forwarding_messages = 0;
554e0a8d 663 exit_cleanup(RERR_SOCKETIO);
2a5df862 664 }
bd717af8 665 check_timeout();
4c36ddbe
AT
666 continue;
667 }
668
3309507d 669 if (io_filesfrom_f_out >= 0) {
56014c8c
WD
670 if (io_filesfrom_buflen) {
671 if (FD_ISSET(io_filesfrom_f_out, &w_fds)) {
672 int l = write(io_filesfrom_f_out,
673 io_filesfrom_bp,
674 io_filesfrom_buflen);
675 if (l > 0) {
676 if (!(io_filesfrom_buflen -= l))
677 io_filesfrom_bp = io_filesfrom_buf;
678 else
679 io_filesfrom_bp += l;
680 } else {
681 /* XXX should we complain? */
682 io_filesfrom_f_out = -1;
683 }
684 }
3309507d 685 } else if (io_filesfrom_f_in >= 0) {
56014c8c
WD
686 if (FD_ISSET(io_filesfrom_f_in, &r_fds)) {
687 int l = read(io_filesfrom_f_in,
688 io_filesfrom_buf,
689 sizeof io_filesfrom_buf);
690 if (l <= 0) {
691 /* Send end-of-file marker */
692 io_filesfrom_buf[0] = '\0';
693 io_filesfrom_buf[1] = '\0';
694 io_filesfrom_buflen = io_filesfrom_lastchar? 2 : 1;
695 io_filesfrom_f_in = -1;
696 } else {
56014c8c
WD
697 if (!eol_nulls) {
698 char *s = io_filesfrom_buf + l;
699 /* Transform CR and/or LF into '\0' */
700 while (s-- > io_filesfrom_buf) {
701 if (*s == '\n' || *s == '\r')
702 *s = '\0';
703 }
704 }
705 if (!io_filesfrom_lastchar) {
706 /* Last buf ended with a '\0', so don't
707 * let this buf start with one. */
708 while (l && !*io_filesfrom_bp)
709 io_filesfrom_bp++, l--;
710 }
711 if (!l)
712 io_filesfrom_bp = io_filesfrom_buf;
713 else {
714 char *f = io_filesfrom_bp;
715 char *t = f;
716 char *eob = f + l;
717 /* Eliminate any multi-'\0' runs. */
718 while (f != eob) {
719 if (!(*t++ = *f++)) {
720 while (f != eob && !*f)
721 f++, l--;
722 }
723 }
724 io_filesfrom_lastchar = f[-1];
725 }
726 io_filesfrom_buflen = l;
727 }
728 }
729 }
730 }
731
f89b9368
WD
732 if (!FD_ISSET(fd, &r_fds))
733 continue;
554e0a8d 734
4c36ddbe
AT
735 n = read(fd, buf, len);
736
1ea087a7
WD
737 if (n <= 0) {
738 if (n == 0)
1f75bb10 739 whine_about_eof(fd); /* Doesn't return. */
d62bcc17
WD
740 if (errno == EINTR || errno == EWOULDBLOCK
741 || errno == EAGAIN)
7a55d06e 742 continue;
1f75bb10
WD
743
744 /* Don't write errors on a dead socket. */
72f2d1b3 745 if (fd == sock_f_in) {
8ef246e0 746 io_end_multiplex_out();
72f2d1b3
WD
747 rsyserr(FSOCKERR, errno, "read error");
748 } else
749 rsyserr(FERROR, errno, "read error");
1f75bb10 750 exit_cleanup(RERR_STREAMIO);
8d9dc9f9 751 }
00bdf899
WD
752
753 buf += n;
754 len -= n;
d8aeda1e 755 cnt += n;
1f75bb10 756
3b0a30eb
WD
757 if (fd == sock_f_in && io_timeout)
758 last_io_in = time(NULL);
4c36ddbe 759 }
8d9dc9f9 760
d8aeda1e 761 return cnt;
4c36ddbe 762}
8d9dc9f9 763
332cf6df
WD
764/* Read a line into the "fname" buffer (which must be at least MAXPATHLEN
765 * characters long). */
56014c8c
WD
766int read_filesfrom_line(int fd, char *fname)
767{
768 char ch, *s, *eob = fname + MAXPATHLEN - 1;
769 int cnt;
d19320fd 770 int reading_remotely = filesfrom_host != NULL;
56014c8c
WD
771 int nulls = eol_nulls || reading_remotely;
772
773 start:
774 s = fname;
775 while (1) {
776 cnt = read(fd, &ch, 1);
777 if (cnt < 0 && (errno == EWOULDBLOCK
778 || errno == EINTR || errno == EAGAIN)) {
779 struct timeval tv;
6c850772
WD
780 fd_set r_fds, e_fds;
781 FD_ZERO(&r_fds);
782 FD_SET(fd, &r_fds);
783 FD_ZERO(&e_fds);
784 FD_SET(fd, &e_fds);
e626b29e 785 tv.tv_sec = select_timeout;
56014c8c 786 tv.tv_usec = 0;
6c850772 787 if (!select(fd+1, &r_fds, NULL, &e_fds, &tv))
56014c8c 788 check_timeout();
6c850772
WD
789 if (FD_ISSET(fd, &e_fds)) {
790 rsyserr(FINFO, errno,
791 "select exception on fd %d", fd);
792 }
56014c8c
WD
793 continue;
794 }
795 if (cnt != 1)
796 break;
797 if (nulls? !ch : (ch == '\r' || ch == '\n')) {
798 /* Skip empty lines if reading locally. */
799 if (!reading_remotely && s == fname)
800 continue;
801 break;
802 }
803 if (s < eob)
804 *s++ = ch;
805 }
806 *s = '\0';
7a55d06e 807
6b45fcf1
WD
808 /* Dump comments. */
809 if (*fname == '#' || *fname == ';')
56014c8c
WD
810 goto start;
811
812 return s - fname;
813}
7a55d06e 814
8ef246e0 815int io_start_buffering_out(int f_out)
1f75bb10 816{
8ef246e0
WD
817 if (iobuf_out) {
818 assert(f_out == iobuf_f_out);
819 return 0;
820 }
1f75bb10
WD
821 if (!(iobuf_out = new_array(char, IO_BUFFER_SIZE)))
822 out_of_memory("io_start_buffering_out");
823 iobuf_out_cnt = 0;
8ef246e0
WD
824 iobuf_f_out = f_out;
825 return 1;
1f75bb10
WD
826}
827
8ef246e0 828int io_start_buffering_in(int f_in)
1f75bb10 829{
8ef246e0
WD
830 if (iobuf_in) {
831 assert(f_in == iobuf_f_in);
832 return 0;
833 }
1f75bb10
WD
834 iobuf_in_siz = 2 * IO_BUFFER_SIZE;
835 if (!(iobuf_in = new_array(char, iobuf_in_siz)))
836 out_of_memory("io_start_buffering_in");
8ef246e0
WD
837 iobuf_f_in = f_in;
838 return 1;
1f75bb10
WD
839}
840
8ef246e0 841void io_end_buffering_in(void)
1f75bb10 842{
8ef246e0
WD
843 if (!iobuf_in)
844 return;
845 free(iobuf_in);
846 iobuf_in = NULL;
847 iobuf_in_ndx = 0;
848 iobuf_in_remaining = 0;
849 iobuf_f_in = -1;
850}
851
852void io_end_buffering_out(void)
853{
854 if (!iobuf_out)
855 return;
856 io_flush(FULL_FLUSH);
857 free(iobuf_out);
858 iobuf_out = NULL;
859 iobuf_f_out = -1;
1f75bb10
WD
860}
861
be91bd81 862void maybe_flush_socket(int important)
626bec8e 863{
be91bd81
WD
864 if (iobuf_out && iobuf_out_cnt
865 && (important || time(NULL) - last_io_out >= 5))
626bec8e
WD
866 io_flush(NORMAL_FLUSH);
867}
868
cdf236aa 869void maybe_send_keepalive(void)
9e2409ab 870{
3b0a30eb 871 if (time(NULL) - last_io_out >= allowed_lull) {
9e2409ab 872 if (!iobuf_out || !iobuf_out_cnt) {
3221f451
WD
873 if (protocol_version < 29)
874 return; /* there's nothing we can do */
8ef246e0 875 if (protocol_version >= 30)
332cf6df 876 send_msg(MSG_NOOP, "", 0, 0);
8ef246e0
WD
877 else {
878 write_int(sock_f_out, cur_flist->count);
879 write_shortint(sock_f_out, ITEM_IS_NEW);
880 }
9e2409ab
WD
881 }
882 if (iobuf_out)
883 io_flush(NORMAL_FLUSH);
884 }
885}
886
8ef246e0
WD
887void start_flist_forward(int f_in)
888{
889 assert(iobuf_out != NULL);
890 assert(iobuf_f_out == msg_fd_out);
891 flist_forward_from = f_in;
892}
893
894void stop_flist_forward()
895{
8ef246e0 896 flist_forward_from = -1;
dde5b772 897 io_flush(FULL_FLUSH);
8ef246e0
WD
898}
899
880da007
MP
900/**
901 * Continue trying to read len bytes - don't return until len has been
902 * read.
903 **/
3151cbae 904static void read_loop(int fd, char *buf, size_t len)
4c36ddbe
AT
905{
906 while (len) {
907 int n = read_timeout(fd, buf, len);
908
909 buf += n;
910 len -= n;
8d9dc9f9
AT
911 }
912}
913
7a55d06e
MP
914/**
915 * Read from the file descriptor handling multiplexing - return number
916 * of bytes read.
d62bcc17
WD
917 *
918 * Never returns <= 0.
7a55d06e 919 */
c399d22a 920static int readfd_unbuffered(int fd, char *buf, size_t len)
8d9dc9f9 921{
f2a4853c 922 size_t msg_bytes;
d8aeda1e 923 int tag, cnt = 0;
33544bf4 924 char line[BIGPATHBUFLEN];
8d9dc9f9 925
8ef246e0 926 if (!iobuf_in || fd != iobuf_f_in)
4c36ddbe 927 return read_timeout(fd, buf, len);
8d9dc9f9 928
8ef246e0
WD
929 if (!io_multiplexing_in && iobuf_in_remaining == 0) {
930 iobuf_in_remaining = read_timeout(fd, iobuf_in, iobuf_in_siz);
1f75bb10 931 iobuf_in_ndx = 0;
76c21947
WD
932 }
933
d8aeda1e 934 while (cnt == 0) {
8ef246e0
WD
935 if (iobuf_in_remaining) {
936 len = MIN(len, iobuf_in_remaining);
1f75bb10
WD
937 memcpy(buf, iobuf_in + iobuf_in_ndx, len);
938 iobuf_in_ndx += len;
8ef246e0 939 iobuf_in_remaining -= len;
d8aeda1e 940 cnt = len;
76c21947 941 break;
8d9dc9f9
AT
942 }
943
909ce14f 944 read_loop(fd, line, 4);
ff41a59f 945 tag = IVAL(line, 0);
679e7657 946
f2a4853c 947 msg_bytes = tag & 0xFFFFFF;
d17e1dd2 948 tag = (tag >> 24) - MPLEX_BASE;
8d9dc9f9 949
d17e1dd2
WD
950 switch (tag) {
951 case MSG_DATA:
f2a4853c 952 if (msg_bytes > iobuf_in_siz) {
1f75bb10 953 if (!(iobuf_in = realloc_array(iobuf_in, char,
f2a4853c 954 msg_bytes)))
c399d22a 955 out_of_memory("readfd_unbuffered");
f2a4853c 956 iobuf_in_siz = msg_bytes;
76c21947 957 }
f2a4853c 958 read_loop(fd, iobuf_in, msg_bytes);
8ef246e0 959 iobuf_in_remaining = msg_bytes;
1f75bb10 960 iobuf_in_ndx = 0;
d17e1dd2 961 break;
8ef246e0
WD
962 case MSG_NOOP:
963 if (am_sender)
964 maybe_send_keepalive();
965 break;
966 case MSG_IO_ERROR:
967 if (msg_bytes != 4)
968 goto invalid_msg;
969 read_loop(fd, line, msg_bytes);
970 io_error |= IVAL(line, 0);
971 break;
0d67e00a 972 case MSG_DELETED:
f2a4853c
WD
973 if (msg_bytes >= sizeof line)
974 goto overflow;
332cf6df
WD
975#ifdef ICONV_OPTION
976 if (ic_recv != (iconv_t)-1) {
977 ICONV_CONST char *ibuf;
978 char *obuf = line;
979 size_t icnt, ocnt = sizeof line - 1;
980 int add_null = 0;
981 iconv(ic_send, NULL, 0, NULL, 0);
982 while (msg_bytes) {
983 icnt = msg_bytes > sizeof iconv_buf
984 ? sizeof iconv_buf : msg_bytes;
985 read_loop(fd, iconv_buf, icnt);
986 if (!(msg_bytes -= icnt) && !iconv_buf[icnt-1])
987 icnt--, add_null = 1;
988 ibuf = (ICONV_CONST char *)iconv_buf;
989 if (iconv(ic_send, &ibuf,&icnt,
990 &obuf,&ocnt) == (size_t)-1)
991 goto overflow; // XXX
992 }
993 if (add_null)
994 *obuf++ = '\0';
995 msg_bytes = obuf - line;
996 } else
997#endif
998 read_loop(fd, line, msg_bytes);
0d67e00a 999 /* A directory name was sent with the trailing null */
f2a4853c 1000 if (msg_bytes > 0 && !line[msg_bytes-1])
0d67e00a 1001 log_delete(line, S_IFDIR);
12bda6f7
WD
1002 else {
1003 line[msg_bytes] = '\0';
0d67e00a 1004 log_delete(line, S_IFREG);
12bda6f7 1005 }
0d67e00a 1006 break;
9981c27e 1007 case MSG_SUCCESS:
f2a4853c 1008 if (msg_bytes != 4) {
8ef246e0 1009 invalid_msg:
cdf236aa 1010 rprintf(FERROR, "invalid multi-message %d:%ld [%s]\n",
f2a4853c 1011 tag, (long)msg_bytes, who_am_i());
9981c27e
WD
1012 exit_cleanup(RERR_STREAMIO);
1013 }
f2a4853c 1014 read_loop(fd, line, msg_bytes);
9981c27e 1015 successful_send(IVAL(line, 0));
9981c27e 1016 break;
8ef246e0
WD
1017 case MSG_NO_SEND:
1018 if (msg_bytes != 4)
1019 goto invalid_msg;
1020 read_loop(fd, line, msg_bytes);
1021 send_msg_int(MSG_NO_SEND, IVAL(line, 0));
1022 break;
d17e1dd2
WD
1023 case MSG_INFO:
1024 case MSG_ERROR:
f2a4853c
WD
1025 if (msg_bytes >= sizeof line) {
1026 overflow:
bd9fca47 1027 rprintf(FERROR,
cdf236aa 1028 "multiplexing overflow %d:%ld [%s]\n",
f2a4853c 1029 tag, (long)msg_bytes, who_am_i());
d17e1dd2
WD
1030 exit_cleanup(RERR_STREAMIO);
1031 }
f2a4853c 1032 read_loop(fd, line, msg_bytes);
332cf6df 1033 rwrite((enum logcode)tag, line, msg_bytes, 1);
d17e1dd2
WD
1034 break;
1035 default:
cdf236aa
WD
1036 rprintf(FERROR, "unexpected tag %d [%s]\n",
1037 tag, who_am_i());
65417579 1038 exit_cleanup(RERR_STREAMIO);
8d9dc9f9 1039 }
8d9dc9f9
AT
1040 }
1041
8ef246e0 1042 if (iobuf_in_remaining == 0)
d17e1dd2 1043 io_flush(NORMAL_FLUSH);
76c21947 1044
d8aeda1e 1045 return cnt;
8d9dc9f9
AT
1046}
1047
dde5b772
WD
1048/* Do a buffered read from fd. Don't return until all N bytes have
1049 * been read. If all N can't be read then exit with an error. */
3151cbae 1050static void readfd(int fd, char *buffer, size_t N)
720b47f2 1051{
d8aeda1e 1052 int cnt;
d62bcc17 1053 size_t total = 0;
3151cbae 1054
6ba9279f 1055 while (total < N) {
d8aeda1e
WD
1056 cnt = readfd_unbuffered(fd, buffer + total, N-total);
1057 total += cnt;
7f28dbee 1058 }
1b7c47cb 1059
b9f592fb
WD
1060 if (fd == write_batch_monitor_in) {
1061 if ((size_t)write(batch_fd, buffer, total) != total)
1062 exit_cleanup(RERR_FILEIO);
1063 }
1f75bb10 1064
8ef246e0
WD
1065 if (fd == flist_forward_from)
1066 writefd(iobuf_f_out, buffer, total);
1067
1f75bb10
WD
1068 if (fd == sock_f_in)
1069 stats.total_read += total;
720b47f2
AT
1070}
1071
3a993aa4 1072unsigned short read_shortint(int f)
9361f839 1073{
482f48cc
WD
1074 char b[2];
1075 readfd(f, b, 2);
1076 return (UVAL(b, 1) << 8) + UVAL(b, 0);
9361f839
WD
1077}
1078
b7922338 1079int32 read_int(int f)
720b47f2 1080{
4c36ddbe 1081 char b[4];
d8aeda1e 1082 int32 num;
d730b113 1083
482f48cc
WD
1084 readfd(f, b, 4);
1085 num = IVAL(b, 0);
1086#if SIZEOF_INT32 > 4
1087 if (num & (int32)0x80000000)
1088 num |= ~(int32)0xffffffff;
1089#endif
d8aeda1e 1090 return num;
720b47f2
AT
1091}
1092
351e23ad 1093int32 read_varint(int f)
3a6a366f 1094{
351e23ad
WD
1095 union {
1096 char b[5];
1097 int32 x;
1098 } u;
1099 uchar ch;
1100 int extra;
1101
1102 u.x = 0;
1103 readfd(f, (char*)&ch, 1);
1104 extra = int_byte_extra[ch / 4];
1105 if (extra) {
1106 uchar bit = ((uchar)1<<(8-extra));
1107 if (extra >= (int)sizeof u.b) {
1108 rprintf(FERROR, "Overflow in read_varint()\n");
1109 exit_cleanup(RERR_STREAMIO);
1110 }
1111 readfd(f, u.b, extra);
1112 u.b[extra] = ch & (bit-1);
1113 } else
1114 u.b[0] = ch;
1115#if CAREFUL_ALIGNMENT
1116 u.x = IVAL(u.b,0);
1117#endif
1118#if SIZEOF_INT32 > 4
1119 if (u.x & (int32)0x80000000)
1120 u.x |= ~(int32)0xffffffff;
1121#endif
1122 return u.x;
1123}
71c46176 1124
351e23ad
WD
1125int64 read_varlong(int f, uchar min_bytes)
1126{
1127 union {
1128 char b[9];
1129 int64 x;
1130 } u;
1131 char b2[8];
1132 int extra;
71c46176 1133
031fa9ad 1134#if SIZEOF_INT64 < 8
351e23ad 1135 memset(u.b, 0, 8);
031fa9ad 1136#else
351e23ad 1137 u.x = 0;
031fa9ad 1138#endif
351e23ad
WD
1139 readfd(f, b2, min_bytes);
1140 memcpy(u.b, b2+1, min_bytes-1);
1141 extra = int_byte_extra[CVAL(b2, 0) / 4];
1142 if (extra) {
1143 uchar bit = ((uchar)1<<(8-extra));
1144 if (min_bytes + extra > (int)sizeof u.b) {
1145 rprintf(FERROR, "Overflow in read_varlong()\n");
1146 exit_cleanup(RERR_STREAMIO);
1147 }
1148 readfd(f, u.b + min_bytes - 1, extra);
1149 u.b[min_bytes + extra - 1] = CVAL(b2, 0) & (bit-1);
4ea4acf1 1150#if SIZEOF_INT64 < 8
351e23ad 1151 if (min_bytes + extra > 5 || u.b[4] || CVAL(u.b,3) & 0x80) {
4ea4acf1
WD
1152 rprintf(FERROR, "Integer overflow: attempted 64-bit offset\n");
1153 exit_cleanup(RERR_UNSUPPORTED);
1154 }
1155#endif
351e23ad
WD
1156 } else
1157 u.b[min_bytes + extra - 1] = CVAL(b2, 0);
1158#if SIZEOF_INT64 < 8
1159 u.x = IVAL(u.b,0);
1160#elif CAREFUL_ALIGNMENT
1161 u.x = IVAL(u.b,0) | (((int64)IVAL(u.b,4))<<32);
1162#endif
1163 return u.x;
1164}
1165
1166int64 read_longint(int f)
1167{
4ea4acf1 1168#if SIZEOF_INT64 >= 8
351e23ad 1169 char b[9];
4ea4acf1 1170#endif
351e23ad 1171 int32 num = read_int(f);
71c46176 1172
351e23ad
WD
1173 if (num != (int32)0xffffffff)
1174 return num;
1175
1176#if SIZEOF_INT64 < 8
1177 rprintf(FERROR, "Integer overflow: attempted 64-bit offset\n");
1178 exit_cleanup(RERR_UNSUPPORTED);
1179#else
1180 readfd(f, b, 8);
1181 return IVAL(b,0) | (((int64)IVAL(b,4))<<32);
1182#endif
3a6a366f
AT
1183}
1184
4a19c3b2 1185void read_buf(int f, char *buf, size_t len)
720b47f2 1186{
4c36ddbe 1187 readfd(f,buf,len);
720b47f2
AT
1188}
1189
4a19c3b2 1190void read_sbuf(int f, char *buf, size_t len)
575f2fca 1191{
93095cbe 1192 readfd(f, buf, len);
af436313 1193 buf[len] = '\0';
575f2fca
AT
1194}
1195
9361f839 1196uchar read_byte(int f)
182dca5c 1197{
9361f839 1198 uchar c;
93095cbe 1199 readfd(f, (char *)&c, 1);
4c36ddbe 1200 return c;
182dca5c 1201}
720b47f2 1202
46e99b09
WD
1203int read_vstring(int f, char *buf, int bufsize)
1204{
1205 int len = read_byte(f);
1206
1207 if (len & 0x80)
1208 len = (len & ~0x80) * 0x100 + read_byte(f);
1209
1210 if (len >= bufsize) {
1211 rprintf(FERROR, "over-long vstring received (%d > %d)\n",
1212 len, bufsize - 1);
9a6ed83f 1213 return -1;
46e99b09
WD
1214 }
1215
1216 if (len)
1217 readfd(f, buf, len);
1218 buf[len] = '\0';
1219 return len;
1220}
1221
188fed95
WD
1222/* Populate a sum_struct with values from the socket. This is
1223 * called by both the sender and the receiver. */
1224void read_sum_head(int f, struct sum_struct *sum)
1225{
1226 sum->count = read_int(f);
c638caa6
WD
1227 if (sum->count < 0) {
1228 rprintf(FERROR, "Invalid checksum count %ld [%s]\n",
1229 (long)sum->count, who_am_i());
1230 exit_cleanup(RERR_PROTOCOL);
1231 }
188fed95
WD
1232 sum->blength = read_int(f);
1233 if (sum->blength < 0 || sum->blength > MAX_BLOCK_SIZE) {
cdf236aa
WD
1234 rprintf(FERROR, "Invalid block length %ld [%s]\n",
1235 (long)sum->blength, who_am_i());
188fed95
WD
1236 exit_cleanup(RERR_PROTOCOL);
1237 }
1238 sum->s2length = protocol_version < 27 ? csum_length : (int)read_int(f);
a0456b9c 1239 if (sum->s2length < 0 || sum->s2length > MAX_DIGEST_LEN) {
cdf236aa
WD
1240 rprintf(FERROR, "Invalid checksum length %d [%s]\n",
1241 sum->s2length, who_am_i());
188fed95
WD
1242 exit_cleanup(RERR_PROTOCOL);
1243 }
1244 sum->remainder = read_int(f);
1245 if (sum->remainder < 0 || sum->remainder > sum->blength) {
cdf236aa
WD
1246 rprintf(FERROR, "Invalid remainder length %ld [%s]\n",
1247 (long)sum->remainder, who_am_i());
188fed95
WD
1248 exit_cleanup(RERR_PROTOCOL);
1249 }
1250}
1251
c207d7ec
WD
1252/* Send the values from a sum_struct over the socket. Set sum to
1253 * NULL if there are no checksums to send. This is called by both
1254 * the generator and the sender. */
1255void write_sum_head(int f, struct sum_struct *sum)
1256{
1257 static struct sum_struct null_sum;
1258
1259 if (sum == NULL)
1260 sum = &null_sum;
1261
1262 write_int(f, sum->count);
1263 write_int(f, sum->blength);
1264 if (protocol_version >= 27)
1265 write_int(f, sum->s2length);
1266 write_int(f, sum->remainder);
1267}
1268
08571358
MP
1269/**
1270 * Sleep after writing to limit I/O bandwidth usage.
1271 *
1272 * @todo Rather than sleeping after each write, it might be better to
1273 * use some kind of averaging. The current algorithm seems to always
1274 * use a bit less bandwidth than specified, because it doesn't make up
1275 * for slow periods. But arguably this is a feature. In addition, we
1276 * ought to take the time used to write the data into account.
71e58630
WD
1277 *
1278 * During some phases of big transfers (file FOO is uptodate) this is
1279 * called with a small bytes_written every time. As the kernel has to
1280 * round small waits up to guarantee that we actually wait at least the
1281 * requested number of microseconds, this can become grossly inaccurate.
1282 * We therefore keep track of the bytes we've written over time and only
1283 * sleep when the accumulated delay is at least 1 tenth of a second.
08571358
MP
1284 **/
1285static void sleep_for_bwlimit(int bytes_written)
1286{
71e58630 1287 static struct timeval prior_tv;
0f78b815 1288 static long total_written = 0;
71e58630
WD
1289 struct timeval tv, start_tv;
1290 long elapsed_usec, sleep_usec;
1291
1292#define ONE_SEC 1000000L /* # of microseconds in a second */
08571358 1293
9a0cfff5 1294 if (!bwlimit_writemax)
08571358 1295 return;
e681e820 1296
0f78b815 1297 total_written += bytes_written;
71e58630
WD
1298
1299 gettimeofday(&start_tv, NULL);
1300 if (prior_tv.tv_sec) {
1301 elapsed_usec = (start_tv.tv_sec - prior_tv.tv_sec) * ONE_SEC
1302 + (start_tv.tv_usec - prior_tv.tv_usec);
1303 total_written -= elapsed_usec * bwlimit / (ONE_SEC/1024);
1304 if (total_written < 0)
1305 total_written = 0;
1306 }
3151cbae 1307
71e58630
WD
1308 sleep_usec = total_written * (ONE_SEC/1024) / bwlimit;
1309 if (sleep_usec < ONE_SEC / 10) {
1310 prior_tv = start_tv;
1311 return;
1312 }
08571358 1313
71e58630
WD
1314 tv.tv_sec = sleep_usec / ONE_SEC;
1315 tv.tv_usec = sleep_usec % ONE_SEC;
98b332ed 1316 select(0, NULL, NULL, NULL, &tv);
71e58630
WD
1317
1318 gettimeofday(&prior_tv, NULL);
1319 elapsed_usec = (prior_tv.tv_sec - start_tv.tv_sec) * ONE_SEC
1320 + (prior_tv.tv_usec - start_tv.tv_usec);
1321 total_written = (sleep_usec - elapsed_usec) * bwlimit / (ONE_SEC/1024);
08571358
MP
1322}
1323
1f75bb10 1324/* Write len bytes to the file descriptor fd, looping as necessary to get
0fb2fc4a 1325 * the job done and also (in certain circumstances) reading any data on
af436313 1326 * msg_fd_in to avoid deadlock.
880da007
MP
1327 *
1328 * This function underlies the multiplexing system. The body of the
1f75bb10 1329 * application never calls this function directly. */
4a19c3b2 1330static void writefd_unbuffered(int fd, const char *buf, size_t len)
720b47f2 1331{
1ea087a7 1332 size_t n, total = 0;
6c850772 1333 fd_set w_fds, r_fds, e_fds;
d8aeda1e 1334 int maxfd, count, cnt, using_r_fds;
e4c877cf 1335 int defer_inc = 0;
8d9dc9f9 1336 struct timeval tv;
720b47f2 1337
3be97bf9 1338 if (no_flush++)
e4c877cf 1339 defer_forwarding_messages++, defer_inc++;
e44f9a12 1340
4c36ddbe 1341 while (total < len) {
8d9dc9f9 1342 FD_ZERO(&w_fds);
6c850772
WD
1343 FD_SET(fd, &w_fds);
1344 FD_ZERO(&e_fds);
1345 FD_SET(fd, &e_fds);
1ea087a7 1346 maxfd = fd;
4c36ddbe 1347
954bbed8 1348 if (msg_fd_in >= 0) {
56014c8c 1349 FD_ZERO(&r_fds);
6c850772 1350 FD_SET(msg_fd_in, &r_fds);
1ea087a7
WD
1351 if (msg_fd_in > maxfd)
1352 maxfd = msg_fd_in;
3eeac9bc
WD
1353 using_r_fds = 1;
1354 } else
1355 using_r_fds = 0;
8d9dc9f9 1356
e626b29e 1357 tv.tv_sec = select_timeout;
8d9dc9f9 1358 tv.tv_usec = 0;
4c36ddbe 1359
554e0a8d 1360 errno = 0;
3eeac9bc 1361 count = select(maxfd + 1, using_r_fds ? &r_fds : NULL,
6c850772 1362 &w_fds, &e_fds, &tv);
4c36ddbe
AT
1363
1364 if (count <= 0) {
1ea087a7 1365 if (count < 0 && errno == EBADF)
554e0a8d 1366 exit_cleanup(RERR_SOCKETIO);
bd717af8 1367 check_timeout();
8d9dc9f9
AT
1368 continue;
1369 }
4c36ddbe 1370
6c850772
WD
1371 if (FD_ISSET(fd, &e_fds)) {
1372 rsyserr(FINFO, errno,
1373 "select exception on fd %d", fd);
1374 }
1375
3eeac9bc 1376 if (using_r_fds && FD_ISSET(msg_fd_in, &r_fds))
d17e1dd2 1377 read_msg_fd();
554e0a8d 1378
9a6ed83f 1379 if (!FD_ISSET(fd, &w_fds))
1ea087a7 1380 continue;
4c36ddbe 1381
1ea087a7 1382 n = len - total;
9a0cfff5 1383 if (bwlimit_writemax && n > bwlimit_writemax)
1ea087a7 1384 n = bwlimit_writemax;
d8aeda1e 1385 cnt = write(fd, buf + total, n);
1ea087a7 1386
d8aeda1e
WD
1387 if (cnt <= 0) {
1388 if (cnt < 0) {
3309507d
WD
1389 if (errno == EINTR)
1390 continue;
1391 if (errno == EWOULDBLOCK || errno == EAGAIN) {
1392 msleep(1);
1393 continue;
1394 }
f0359dd0
AT
1395 }
1396
1ea087a7 1397 /* Don't try to write errors back across the stream. */
1f75bb10 1398 if (fd == sock_f_out)
8ef246e0 1399 io_end_multiplex_out();
2a5df862
WD
1400 /* Don't try to write errors down a failing msg pipe. */
1401 if (am_server && fd == msg_fd_out)
1402 exit_cleanup(RERR_STREAMIO);
1ea087a7 1403 rsyserr(FERROR, errno,
b88c2e8f
WD
1404 "writefd_unbuffered failed to write %ld bytes [%s]",
1405 (long)len, who_am_i());
d1b31da7
WD
1406 /* If the other side is sending us error messages, try
1407 * to grab any messages they sent before they died. */
7f459268 1408 while (fd == sock_f_out && io_multiplexing_in) {
3b0a30eb 1409 set_io_timeout(30);
9e2409ab 1410 ignore_timeout = 0;
d1b31da7
WD
1411 readfd_unbuffered(sock_f_in, io_filesfrom_buf,
1412 sizeof io_filesfrom_buf);
1413 }
1ea087a7
WD
1414 exit_cleanup(RERR_STREAMIO);
1415 }
4c36ddbe 1416
d8aeda1e 1417 total += cnt;
e4c877cf 1418 defer_forwarding_messages++, defer_inc++;
a800434a 1419
1f75bb10 1420 if (fd == sock_f_out) {
626bec8e 1421 if (io_timeout || am_generator)
3b0a30eb 1422 last_io_out = time(NULL);
d8aeda1e 1423 sleep_for_bwlimit(cnt);
1f75bb10 1424 }
4c36ddbe 1425 }
e44f9a12
AT
1426
1427 no_flush--;
e4c877cf
WD
1428 if (!(defer_forwarding_messages -= defer_inc))
1429 msg_flush();
d6dead6b
AT
1430}
1431
dde5b772 1432void io_flush(int flush_it_all)
d6dead6b 1433{
1f75bb10 1434 if (!iobuf_out_cnt || no_flush)
d17e1dd2 1435 return;
8d9dc9f9 1436
d17e1dd2 1437 if (io_multiplexing_out)
332cf6df 1438 mplex_write(sock_f_out, MSG_DATA, iobuf_out, iobuf_out_cnt, 0);
d17e1dd2 1439 else
8ef246e0 1440 writefd_unbuffered(iobuf_f_out, iobuf_out, iobuf_out_cnt);
1f75bb10 1441 iobuf_out_cnt = 0;
e4c877cf
WD
1442
1443 if (flush_it_all && !defer_forwarding_messages)
1444 msg_flush();
8d9dc9f9
AT
1445}
1446
4a19c3b2 1447static void writefd(int fd, const char *buf, size_t len)
d6dead6b 1448{
1f75bb10
WD
1449 if (fd == sock_f_out)
1450 stats.total_written += len;
1451
b9f592fb
WD
1452 if (fd == write_batch_monitor_out) {
1453 if ((size_t)write(batch_fd, buf, len) != len)
1454 exit_cleanup(RERR_FILEIO);
1455 }
1456
8ef246e0 1457 if (!iobuf_out || fd != iobuf_f_out) {
4c36ddbe
AT
1458 writefd_unbuffered(fd, buf, len);
1459 return;
1460 }
d6dead6b
AT
1461
1462 while (len) {
1f75bb10 1463 int n = MIN((int)len, IO_BUFFER_SIZE - iobuf_out_cnt);
d6dead6b 1464 if (n > 0) {
1f75bb10 1465 memcpy(iobuf_out+iobuf_out_cnt, buf, n);
d6dead6b
AT
1466 buf += n;
1467 len -= n;
1f75bb10 1468 iobuf_out_cnt += n;
d6dead6b 1469 }
3151cbae 1470
1f75bb10 1471 if (iobuf_out_cnt == IO_BUFFER_SIZE)
d17e1dd2 1472 io_flush(NORMAL_FLUSH);
d6dead6b 1473 }
d6dead6b 1474}
720b47f2 1475
3a993aa4 1476void write_shortint(int f, unsigned short x)
9361f839 1477{
3a993aa4
WD
1478 char b[2];
1479 b[0] = (char)x;
1480 b[1] = (char)(x >> 8);
1481 writefd(f, b, 2);
9361f839
WD
1482}
1483
351e23ad
WD
1484void write_int(int f, int32 x)
1485{
1486 char b[4];
1487 SIVAL(b, 0, x);
1488 writefd(f, b, 4);
1489}
1490
f31514ad 1491void write_varint(int f, int32 x)
1c3344a1
WD
1492{
1493 char b[5];
351e23ad
WD
1494 uchar bit;
1495 int cnt = 4;
1496
1497 SIVAL(b, 1, x);
1498
1499 while (cnt > 1 && b[cnt] == 0)
1500 cnt--;
1501 bit = ((uchar)1<<(7-cnt+1));
1502 if (CVAL(b, cnt) >= bit) {
1503 cnt++;
1504 *b = ~(bit-1);
1505 } else if (cnt > 1)
1506 *b = b[cnt] | ~(bit*2-1);
1507 else
1508 *b = b[cnt];
1509
1510 writefd(f, b, cnt);
1c3344a1
WD
1511}
1512
351e23ad 1513void write_varlong(int f, int64 x, uchar min_bytes)
720b47f2 1514{
351e23ad
WD
1515 char b[9];
1516 uchar bit;
1517 int cnt = 8;
1518
1519 SIVAL(b, 1, x);
1520#if SIZEOF_INT64 >= 8
1521 SIVAL(b, 5, x >> 32);
1522#else
1523 if (x <= 0x7FFFFFFF && x >= 0)
1524 memset(b + 5, 0, 4);
1525 else {
1526 rprintf(FERROR, "Integer overflow: attempted 64-bit offset\n");
1527 exit_cleanup(RERR_UNSUPPORTED);
1528 }
1529#endif
1530
1531 while (cnt > min_bytes && b[cnt] == 0)
1532 cnt--;
1533 bit = ((uchar)1<<(7-cnt+min_bytes));
1534 if (CVAL(b, cnt) >= bit) {
1535 cnt++;
1536 *b = ~(bit-1);
1537 } else if (cnt > min_bytes)
1538 *b = b[cnt] | ~(bit*2-1);
1539 else
1540 *b = b[cnt];
1541
1542 writefd(f, b, cnt);
720b47f2
AT
1543}
1544
7a24c346
MP
1545/*
1546 * Note: int64 may actually be a 32-bit type if ./configure couldn't find any
1547 * 64-bit types on this platform.
1548 */
71c46176 1549void write_longint(int f, int64 x)
3a6a366f 1550{
351e23ad 1551 char b[12], * const s = b+4;
3a6a366f 1552
351e23ad
WD
1553 SIVAL(s, 0, x);
1554 if (x <= 0x7FFFFFFF && x >= 0) {
482f48cc 1555 writefd(f, s, 4);
351e23ad
WD
1556 return;
1557 }
3a6a366f 1558
4ea4acf1 1559#if SIZEOF_INT64 < 8
351e23ad
WD
1560 rprintf(FERROR, "Integer overflow: attempted 64-bit offset\n");
1561 exit_cleanup(RERR_UNSUPPORTED);
4ea4acf1 1562#else
351e23ad
WD
1563 memset(b, 0xFF, 4);
1564 SIVAL(s, 4, x >> 32);
1565 writefd(f, b, 12);
4ea4acf1 1566#endif
3a6a366f
AT
1567}
1568
4a19c3b2 1569void write_buf(int f, const char *buf, size_t len)
720b47f2 1570{
4c36ddbe 1571 writefd(f,buf,len);
720b47f2
AT
1572}
1573
880da007 1574/** Write a string to the connection */
4a19c3b2 1575void write_sbuf(int f, const char *buf)
f0fca04e 1576{
93095cbe 1577 writefd(f, buf, strlen(buf));
f0fca04e
AT
1578}
1579
9361f839 1580void write_byte(int f, uchar c)
182dca5c 1581{
93095cbe 1582 writefd(f, (char *)&c, 1);
182dca5c
AT
1583}
1584
4a19c3b2 1585void write_vstring(int f, const char *str, int len)
46e99b09
WD
1586{
1587 uchar lenbuf[3], *lb = lenbuf;
1588
1589 if (len > 0x7F) {
1590 if (len > 0x7FFF) {
1591 rprintf(FERROR,
1592 "attempting to send over-long vstring (%d > %d)\n",
1593 len, 0x7FFF);
1594 exit_cleanup(RERR_PROTOCOL);
1595 }
1596 *lb++ = len / 0x100 + 0x80;
1597 }
1598 *lb = len;
1599
1600 writefd(f, (char*)lenbuf, lb - lenbuf + 1);
1601 if (len)
1602 writefd(f, str, len);
1603}
1604
8a65e0ce
WD
1605/* Send a file-list index using a byte-reduction method. */
1606void write_ndx(int f, int32 ndx)
1607{
1608 static int32 prev_positive = -1, prev_negative = 1;
1609 int32 diff, cnt = 0;
1610 char b[6];
1611
1612 if (protocol_version < 30 || read_batch) {
1613 write_int(f, ndx);
1614 return;
1615 }
1616
1617 /* Send NDX_DONE as a single-byte 0 with no side effects. Send
1618 * negative nums as a positive after sending a leading 0xFF. */
1619 if (ndx >= 0) {
1620 diff = ndx - prev_positive;
1621 prev_positive = ndx;
1622 } else if (ndx == NDX_DONE) {
1623 *b = 0;
1624 writefd(f, b, 1);
1625 return;
1626 } else {
1627 b[cnt++] = (char)0xFF;
1628 ndx = -ndx;
1629 diff = ndx - prev_negative;
1630 prev_negative = ndx;
1631 }
1632
1633 /* A diff of 1 - 253 is sent as a one-byte diff; a diff of 254 - 32767
1634 * or 0 is sent as a 0xFE + a two-byte diff; otherwise we send 0xFE
1635 * & all 4 bytes of the (non-negative) num with the high-bit set. */
1636 if (diff < 0xFE && diff > 0)
1637 b[cnt++] = (char)diff;
1638 else if (diff < 0 || diff > 0x7FFF) {
1639 b[cnt++] = (char)0xFE;
1640 b[cnt++] = (char)((ndx >> 24) | 0x80);
8a65e0ce 1641 b[cnt++] = (char)ndx;
351e23ad
WD
1642 b[cnt++] = (char)(ndx >> 8);
1643 b[cnt++] = (char)(ndx >> 16);
8a65e0ce
WD
1644 } else {
1645 b[cnt++] = (char)0xFE;
1646 b[cnt++] = (char)(diff >> 8);
1647 b[cnt++] = (char)diff;
1648 }
1649 writefd(f, b, cnt);
1650}
1651
1652/* Receive a file-list index using a byte-reduction method. */
1653int32 read_ndx(int f)
1654{
1655 static int32 prev_positive = -1, prev_negative = 1;
1656 int32 *prev_ptr, num;
1657 char b[4];
1658
1659 if (protocol_version < 30)
1660 return read_int(f);
1661
1662 readfd(f, b, 1);
1663 if (CVAL(b, 0) == 0xFF) {
1664 readfd(f, b, 1);
1665 prev_ptr = &prev_negative;
1666 } else if (CVAL(b, 0) == 0)
1667 return NDX_DONE;
1668 else
1669 prev_ptr = &prev_positive;
1670 if (CVAL(b, 0) == 0xFE) {
1671 readfd(f, b, 2);
1672 if (CVAL(b, 0) & 0x80) {
351e23ad
WD
1673 b[3] = CVAL(b, 0) & ~0x80;
1674 b[0] = b[1];
1675 readfd(f, b+1, 2);
1676 num = IVAL(b, 0);
8a65e0ce 1677 } else
351e23ad 1678 num = (UVAL(b,0)<<8) + UVAL(b,1) + *prev_ptr;
8a65e0ce 1679 } else
351e23ad 1680 num = UVAL(b, 0) + *prev_ptr;
8a65e0ce
WD
1681 *prev_ptr = num;
1682 if (prev_ptr == &prev_negative)
1683 num = -num;
1684 return num;
1685}
1686
914cc65c 1687/**
6ed6d7f5
WD
1688 * Read a line of up to @p maxlen characters into @p buf (not counting
1689 * the trailing null). Strips the (required) trailing newline and all
1690 * carriage returns.
914cc65c 1691 *
6ed6d7f5 1692 * @return 1 for success; 0 for I/O error or truncation.
914cc65c 1693 **/
9dd891bb 1694int read_line(int f, char *buf, size_t maxlen)
f0fca04e
AT
1695{
1696 while (maxlen) {
528bfcd7 1697 buf[0] = 0;
f0fca04e 1698 read_buf(f, buf, 1);
914cc65c
MP
1699 if (buf[0] == 0)
1700 return 0;
6ed6d7f5 1701 if (buf[0] == '\n')
f0fca04e 1702 break;
f0fca04e
AT
1703 if (buf[0] != '\r') {
1704 buf++;
1705 maxlen--;
1706 }
1707 }
6ed6d7f5
WD
1708 *buf = '\0';
1709 return maxlen > 0;
f0fca04e
AT
1710}
1711
f0fca04e
AT
1712void io_printf(int fd, const char *format, ...)
1713{
d62bcc17 1714 va_list ap;
33544bf4 1715 char buf[BIGPATHBUFLEN];
f0fca04e 1716 int len;
3151cbae 1717
f0fca04e 1718 va_start(ap, format);
3151cbae 1719 len = vsnprintf(buf, sizeof buf, format, ap);
f0fca04e
AT
1720 va_end(ap);
1721
f89b9368
WD
1722 if (len < 0)
1723 exit_cleanup(RERR_STREAMIO);
f0fca04e 1724
33544bf4
WD
1725 if (len > (int)sizeof buf) {
1726 rprintf(FERROR, "io_printf() was too long for the buffer.\n");
1727 exit_cleanup(RERR_STREAMIO);
1728 }
1729
f0fca04e
AT
1730 write_sbuf(fd, buf);
1731}
8d9dc9f9 1732
d17e1dd2 1733/** Setup for multiplexing a MSG_* stream with the data stream. */
1f75bb10 1734void io_start_multiplex_out(void)
8d9dc9f9 1735{
d17e1dd2 1736 io_flush(NORMAL_FLUSH);
8ef246e0 1737 io_start_buffering_out(sock_f_out);
8d9dc9f9
AT
1738 io_multiplexing_out = 1;
1739}
1740
d17e1dd2 1741/** Setup for multiplexing a MSG_* stream with the data stream. */
1f75bb10 1742void io_start_multiplex_in(void)
8d9dc9f9 1743{
d17e1dd2 1744 io_flush(NORMAL_FLUSH);
8ef246e0 1745 io_start_buffering_in(sock_f_in);
8d9dc9f9
AT
1746 io_multiplexing_in = 1;
1747}
1748
d17e1dd2 1749/** Write an message to the multiplexed data stream. */
332cf6df 1750int io_multiplex_write(enum msgcode code, const char *buf, size_t len, int convert)
8d9dc9f9 1751{
f89b9368
WD
1752 if (!io_multiplexing_out)
1753 return 0;
d17e1dd2 1754 io_flush(NORMAL_FLUSH);
1b7c47cb 1755 stats.total_written += (len+4);
332cf6df 1756 mplex_write(sock_f_out, code, buf, len, convert);
8d9dc9f9
AT
1757 return 1;
1758}
1759
8ef246e0 1760void io_end_multiplex_in(void)
7f459268
WD
1761{
1762 io_multiplexing_in = 0;
8ef246e0 1763 io_end_buffering_in();
7f459268
WD
1764}
1765
d17e1dd2 1766/** Stop output multiplexing. */
8ef246e0 1767void io_end_multiplex_out(void)
554e0a8d
AT
1768{
1769 io_multiplexing_out = 0;
8ef246e0 1770 io_end_buffering_out();
554e0a8d
AT
1771}
1772
b9f592fb
WD
1773void start_write_batch(int fd)
1774{
1775 /* Some communication has already taken place, but we don't
1776 * enable batch writing until here so that we can write a
1777 * canonical record of the communication even though the
1778 * actual communication so far depends on whether a daemon
1779 * is involved. */
1780 write_int(batch_fd, protocol_version);
1781 write_int(batch_fd, checksum_seed);
b9f592fb
WD
1782
1783 if (am_sender)
1784 write_batch_monitor_out = fd;
1785 else
1786 write_batch_monitor_in = fd;
1787}
1788
1789void stop_write_batch(void)
1790{
1791 write_batch_monitor_out = -1;
1792 write_batch_monitor_in = -1;
1793}