Added an am_receiver variable.
[rsync/rsync.git] / io.c
diff --git a/io.c b/io.c
index c22b65f..1ae25ce 100644 (file)
--- a/io.c
+++ b/io.c
@@ -489,23 +489,25 @@ void restore_iobuf_size(xbuf *out)
        }
 }
 
-/* Perform buffered input and output until specified conditions are met.  When
- * given a "needed" read requirement, we'll return without doing any I/O if the
- * iobuf.in bytes are already available.  When reading, we'll read as many
- * bytes as we can into the buffer, and return as soon as we meet the minimum
- * read requirement.  When given a "needed" write requirement, we'll return
- * without doing any I/O if that many bytes will fit in the output buffer (we
- * check either iobuf.out or iobuf.msg, depending on the flags).  When writing,
- * we write out as much as we can, and return as soon as the given free-space
- * requirement is available.
+/* Perform buffered input and/or output until specified conditions are met.
+ * When given a "needed" read or write request, this returns without doing any
+ * I/O if the needed input bytes or write space is already available.  Once I/O
+ * is needed, this will try to do whatever reading and/or writing is currently
+ * possible, up to the maximum buffer allowances, no matter if this is a read
+ * or write request.  However, the I/O stops as soon as the required input
+ * bytes or output space is available.  If this is not a read request, the
+ * routine may also do some advantageous reading of messages from a multiplexed
+ * input source (which ensures that we don't jam up with everyone in their
+ * "need to write" code and nobody reading the accumulated data that would make
+ * writing possible).
  *
- * The iobuf.out and iobuf.msg buffers are circular, so some writes into them
- * will need to be split when the data needs to wrap around to the start.  In
- * order to help make this easier for some operations (such as the use of
- * SIVAL() into the buffer) a buffer may be temporarily shortened, but the
- * original size will be automatically restored.  The iobuf.in buffer is also
- * circular, so callers may need to split their reading of the data if it spans
- * the end.  See also the 3 raw_* iobuf vars that are used in the handling of
+ * The iobuf.in, .out and .msg buffers are all circular.  Callers need to be
+ * aware that some data copies will need to be split when the bytes wrap around
+ * from the end to the start.  In order to help make writing into the output
+ * buffers easier for some operations (such as the use of SIVAL() into the
+ * buffer) a buffer may be temporarily shortened by a small amount, but the
+ * original size will be automatically restored when the .pos wraps to the
+ * start.  See also the 3 raw_* iobuf vars that are used in the handling of
  * MSG_DATA bytes as they are read-from/written-into the buffers.
  *
  * When writing, we flush data in the following priority order:
@@ -896,7 +898,7 @@ void noop_io_until_death(void)
                read_buf(iobuf.in_fd, buf, sizeof buf);
 }
 
-/* Buffer a message for the multiplexed output stream.  Is never used for MSG_DATA. */
+/* Buffer a message for the multiplexed output stream.  Is not used for (normal) MSG_DATA. */
 int send_msg(enum msgcode code, const char *buf, size_t len, int convert)
 {
        char *hdr;
@@ -958,8 +960,8 @@ int send_msg(enum msgcode code, const char *buf, size_t len, int convert)
        {
                size_t siz;
 
-               if ((pos += 4) >= iobuf.msg.size)
-                       pos -= iobuf.msg.size;
+               if ((pos += 4) == iobuf.msg.size)
+                       pos = 0;
 
                /* Handle a split copy if we wrap around the end of the circular buffer. */
                if (pos >= iobuf.msg.pos && (siz = iobuf.msg.size - pos) < len) {
@@ -1313,18 +1315,16 @@ void maybe_flush_socket(int important)
                io_flush(NORMAL_FLUSH);
 }
 
+/* This never adds new non-msg-buffer data, since we don't know the state
+ * of the raw-data buffer. */
 void maybe_send_keepalive(void)
 {
        if (time(NULL) - last_io_out >= allowed_lull) {
                if (!iobuf.msg.len && iobuf.out.len == iobuf.out_empty_len) {
-                       if (protocol_version < 29)
-                               return; /* there's nothing we can do */
                        if (protocol_version >= 30)
                                send_msg(MSG_NOOP, "", 0, 0);
-                       else {
-                               write_int(iobuf.out_fd, cur_flist->used);
-                               write_shortint(iobuf.out_fd, ITEM_IS_NEW);
-                       }
+                       else
+                               send_msg(MSG_DATA, "", 0, 0);
                }
                if (iobuf.msg.len)
                        perform_io(iobuf.msg.size - iobuf.msg.len + 1, PIO_NEED_MSGROOM);
@@ -1372,8 +1372,9 @@ static void read_a_msg(void)
                 * the buffer the msg data will end once it is read.  It is
                 * possible that this points off the end of the buffer, in
                 * which case the gradual reading of the input stream will
-                * cause this value to decrease and eventually become real. */
-               iobuf.raw_input_ends_before = iobuf.in.pos + msg_bytes;
+                * cause this value to wrap around and eventually become real. */
+               if (msg_bytes)
+                       iobuf.raw_input_ends_before = iobuf.in.pos + msg_bytes;
                iobuf.in_multiplexed = 1;
                break;
        case MSG_STATS:
@@ -1410,9 +1411,11 @@ static void read_a_msg(void)
                }
                break;
        case MSG_NOOP:
+               if (msg_bytes != 0)
+                       goto invalid_msg;
+               iobuf.in_multiplexed = 1;
                if (am_sender)
                        maybe_send_keepalive();
-               iobuf.in_multiplexed = 1;
                break;
        case MSG_DELETED:
                if (msg_bytes >= sizeof data)
@@ -1543,23 +1546,19 @@ static void read_a_msg(void)
                                send_msg(MSG_ERROR_EXIT, "", 0, 0);
                                io_flush(FULL_FLUSH);
                        }
-                       val = 0;
-               } else {
-                       val = raw_read_int();
-                       if (protocol_version >= 31) {
-                               if (am_generator) {
-                                       if (DEBUG_GTE(EXIT, 3)) {
-                                               rprintf(FINFO, "[%s] sending MSG_ERROR_EXIT with exit_code %d\n",
-                                                       who_am_i(), val);
-                                       }
-                                       send_msg_int(MSG_ERROR_EXIT, val);
-                               } else {
-                                       if (DEBUG_GTE(EXIT, 3)) {
-                                               rprintf(FINFO, "[%s] sending MSG_ERROR_EXIT (len 0)\n",
-                                                       who_am_i());
-                                       }
-                                       send_msg(MSG_ERROR_EXIT, "", 0, 0);
+               } else if (protocol_version >= 31) {
+                       if (am_generator) {
+                               if (DEBUG_GTE(EXIT, 3)) {
+                                       rprintf(FINFO, "[%s] sending MSG_ERROR_EXIT with exit_code %d\n",
+                                               who_am_i(), val);
+                               }
+                               send_msg_int(MSG_ERROR_EXIT, val);
+                       } else {
+                               if (DEBUG_GTE(EXIT, 3)) {
+                                       rprintf(FINFO, "[%s] sending MSG_ERROR_EXIT (len 0)\n",
+                                               who_am_i());
                                }
+                               send_msg(MSG_ERROR_EXIT, "", 0, 0);
                        }
                }
                /* Send a negative linenum so that we don't end up