+/* This is used by the generator to limit how many file transfers can
+ * be active at once when --remove-source-files is specified. Without
+ * this, sender-side deletions were mostly happening at the end. */
+void increment_active_files(int ndx, int itemizing, enum logcode code)
+{
+ /* TODO: tune these limits? */
+ while (active_filecnt >= (active_bytecnt >= 128*1024 ? 10 : 50)) {
+ check_for_finished_files(itemizing, code, 0);
+ if (iobuf_out_cnt)
+ io_flush(NORMAL_FLUSH);
+ else
+ read_msg_fd();
+ }
+
+ active_filecnt++;
+ active_bytecnt += F_LENGTH(cur_flist->files[ndx - cur_flist->ndx_start]);
+}
+
+/* Write an message to a multiplexed stream. If this fails, rsync exits. */
+static void mplex_write(int fd, enum msgcode code, const char *buf, size_t len, int convert)
+{
+ char buffer[BIGPATHBUFLEN]; /* Oversized for use by iconv code. */
+ size_t n = len;
+
+ SIVAL(buffer, 0, ((MPLEX_BASE + (int)code)<<24) + len);
+
+#ifdef ICONV_OPTION
+ if (convert && ic_send == (iconv_t)-1)
+#endif
+ convert = 0;
+
+ if (convert || n > 1024 - 4) /* BIGPATHBUFLEN can handle 1024 bytes */
+ n = 0;
+ else
+ memcpy(buffer + 4, buf, n);
+
+ defer_forwarding_keep = 1; /* defer_forwarding_messages++ on return */
+ writefd_unbuffered(fd, buffer, n+4);
+ defer_forwarding_keep = 0;
+
+ len -= n;
+ buf += n;
+
+#ifdef ICONV_OPTION
+ if (convert) {
+ xbuf outbuf, inbuf;
+
+ INIT_CONST_XBUF(outbuf, buffer);
+ INIT_XBUF(inbuf, (char*)buf, len, -1);
+
+ do {
+ iconvbufs(ic_send, &inbuf, &outbuf,
+ ICB_INCLUDE_BAD | ICB_INCLUDE_INCOMPLETE);
+ writefd_unbuffered(fd, outbuf.buf, outbuf.len);
+ } while (inbuf.len);
+ } else
+#endif
+ if (len)
+ writefd_unbuffered(fd, buf, len);
+
+ if (!--defer_forwarding_messages && !no_flush)
+ msg_flush();
+}
+
+int send_msg(enum msgcode code, const char *buf, int len, int convert)
+{
+ if (msg_fd_out < 0) {
+ if (!defer_forwarding_messages)
+ return io_multiplex_write(code, buf, len, convert);
+ if (!io_multiplexing_out)
+ return 0;
+ msg_list_add(&msg_queue, code, buf, len, convert);
+ return 1;
+ }
+ if (flist_forward_from >= 0)
+ msg_list_add(&msg_queue, code, buf, len, convert);
+ else
+ mplex_write(msg_fd_out, code, buf, len, convert);
+ return 1;
+}
+
+void send_msg_int(enum msgcode code, int num)
+{
+ char numbuf[4];
+ SIVAL(numbuf, 0, num);
+ send_msg(code, numbuf, 4, 0);
+}
+
+void wait_for_receiver(void)
+{
+ if (iobuf_out_cnt)
+ io_flush(NORMAL_FLUSH);
+ else
+ read_msg_fd();
+}
+
+int get_redo_num(void)
+{
+ return flist_ndx_pop(&redo_list);
+}
+
+int get_hlink_num(void)
+{
+ return flist_ndx_pop(&hlink_list);
+}
+
+/**
+ * When we're the receiver and we have a local --files-from list of names
+ * that needs to be sent over the socket to the sender, we have to do two
+ * things at the same time: send the sender a list of what files we're
+ * processing and read the incoming file+info list from the sender. We do
+ * this by augmenting the read_timeout() function to copy this data. It
+ * uses ff_buf to read a block of data from f_in (when it is ready, since
+ * it might be a pipe) and then blast it out f_out (when it is ready to
+ * receive more data).
+ */
+void io_set_filesfrom_fds(int f_in, int f_out)
+{
+ io_filesfrom_f_in = f_in;
+ io_filesfrom_f_out = f_out;
+ alloc_xbuf(&ff_buf, 2048);
+#ifdef ICONV_OPTION
+ if (protect_args)
+ alloc_xbuf(&iconv_buf, 1024);
+#endif
+}
+
+/* It's almost always an error to get an EOF when we're trying to read from the
+ * network, because the protocol is (for the most part) self-terminating.
+ *
+ * There is one case for the receiver when it is at the end of the transfer
+ * (hanging around reading any keep-alive packets that might come its way): if
+ * the sender dies before the generator's kill-signal comes through, we can end
+ * up here needing to loop until the kill-signal arrives. In this situation,
+ * kluge_around_eof will be < 0.
+ *
+ * There is another case for older protocol versions (< 24) where the module
+ * listing was not terminated, so we must ignore an EOF error in that case and
+ * exit. In this situation, kluge_around_eof will be > 0. */
+static void whine_about_eof(int fd)
+{
+ if (kluge_around_eof && fd == sock_f_in) {
+ int i;
+ if (kluge_around_eof > 0)
+ exit_cleanup(0);
+ /* If we're still here after 10 seconds, exit with an error. */
+ for (i = 10*1000/20; i--; )
+ msleep(20);
+ }
+
+ rprintf(FERROR, RSYNC_NAME ": connection unexpectedly closed "
+ "(%.0f bytes received so far) [%s]\n",
+ (double)stats.total_read, who_am_i());
+
+ exit_cleanup(RERR_STREAMIO);
+}
+
+/**
+ * Read from a socket with I/O timeout. return the number of bytes
+ * read. If no bytes can be read then exit, never return a number <= 0.
+ *
+ * TODO: If the remote shell connection fails, then current versions
+ * actually report an "unexpected EOF" error here. Since it's a
+ * fairly common mistake to try to use rsh when ssh is required, we
+ * should trap that: if we fail to read any data at all, we should
+ * give a better explanation. We can tell whether the connection has
+ * started by looking e.g. at whether the remote version is known yet.
+ */
+static int read_timeout(int fd, char *buf, size_t len)
+{
+ int n, cnt = 0;
+
+ io_flush(FULL_FLUSH);
+
+ while (cnt == 0) {
+ /* until we manage to read *something* */
+ fd_set r_fds, w_fds;
+ struct timeval tv;
+ int maxfd = fd;
+ int count;
+
+ FD_ZERO(&r_fds);
+ FD_ZERO(&w_fds);
+ FD_SET(fd, &r_fds);
+ if (io_filesfrom_f_out >= 0) {
+ int new_fd;
+ if (ff_buf.len == 0) {
+ if (io_filesfrom_f_in >= 0) {
+ FD_SET(io_filesfrom_f_in, &r_fds);
+ new_fd = io_filesfrom_f_in;
+ } else {
+ io_filesfrom_f_out = -1;
+ new_fd = -1;
+ }
+ } else {
+ FD_SET(io_filesfrom_f_out, &w_fds);
+ new_fd = io_filesfrom_f_out;
+ }
+ if (new_fd > maxfd)
+ maxfd = new_fd;