Save first filename and linenum in case exit_cleanup() recurses.
[rsync/rsync.git] / io.c
diff --git a/io.c b/io.c
index b174862..b5fd377 100644 (file)
--- a/io.c
+++ b/io.c
@@ -76,6 +76,9 @@ int kluge_around_eof = 0;
 int sock_f_in = -1;
 int sock_f_out = -1;
 
+int64 total_data_read = 0;
+int64 total_data_written = 0;
+
 static struct {
        xbuf in, out, msg;
        int in_fd;
@@ -749,7 +752,7 @@ static char *perform_io(size_t needed, int flags)
                                        /* Don't write errors on a dead socket. */
                                        msgs2stderr = 1;
                                        out->len = iobuf.raw_flushing_ends_before = out->pos = 0;
-                                       rsyserr(FERROR_SOCKET, errno, "write error");
+                                       rsyserr(FERROR_SOCKET, errno, "[%s] write error", who_am_i());
                                        exit_cleanup(RERR_STREAMIO);
                                }
                        }
@@ -799,6 +802,17 @@ static char *perform_io(size_t needed, int flags)
        return data;
 }
 
+void noop_io_until_death(void)
+{
+       char buf[1024];
+
+       kluge_around_eof = 1;
+       set_io_timeout(10);
+
+       while (1)
+               read_buf(iobuf.in_fd, buf, sizeof buf);
+}
+
 /* Buffer a message for the multiplexed output stream.  Is never used for MSG_DATA. */
 int send_msg(enum msgcode code, const char *buf, size_t len, int convert)
 {
@@ -1261,7 +1275,7 @@ void stop_flist_forward(void)
 static void read_a_msg(void)
 {
        char *data, line[BIGPATHBUFLEN];
-       int tag;
+       int tag, val;
        size_t msg_bytes;
 
        data = perform_io(4, PIO_INPUT_AND_CONSUME);
@@ -1303,6 +1317,17 @@ static void read_a_msg(void)
                if (!am_generator)
                        send_msg(MSG_IO_ERROR, data, 4, 0);
                break;
+       case MSG_IO_TIMEOUT:
+               if (msg_bytes != 4 || am_server || am_generator)
+                       goto invalid_msg;
+               data = perform_io(4, PIO_INPUT_AND_CONSUME);
+               val = IVAL(data, 0);
+               if (!io_timeout || io_timeout > val) {
+                       if (INFO_GTE(MISC, 2))
+                               rprintf(FINFO, "Setting --timeout=%d to match server\n", val);
+                       set_io_timeout(val);
+               }
+               break;
        case MSG_NOOP:
                if (am_sender)
                        maybe_send_keepalive();
@@ -1412,6 +1437,16 @@ static void read_a_msg(void)
                        first_message = 0;
                }
                break;
+       case MSG_ERROR_EXIT:
+               if (msg_bytes != 4)
+                       goto invalid_msg;
+               data = perform_io(4, PIO_INPUT_AND_CONSUME);
+               val = IVAL(data, 0);
+               if (am_generator && protocol_version >= 31)
+                       send_msg_int(MSG_ERROR_EXIT, val);
+               if (am_generator)
+                       val = RERR_RCVR_ERROR; /* avoids duplicate errors */
+               exit_cleanup(val);
        default:
                rprintf(FERROR, "unexpected tag %d [%s%s]\n",
                        tag, who_am_i(), inc_recurse ? "/inc" : "");
@@ -1578,6 +1613,7 @@ void read_buf(int f, char *buf, size_t len)
 
        if (!IN_MULTIPLEXED) {
                memcpy(buf, perform_io(len, PIO_INPUT_AND_CONSUME), len);
+               total_data_read += len;
                if (forward_flist_data)
                        write_buf(iobuf.out_fd, buf, len);
          batch_copy:
@@ -1601,6 +1637,7 @@ void read_buf(int f, char *buf, size_t len)
                /* The bytes at the "data" pointer will survive long
                 * enough to make a copy, but not past future I/O. */
                memcpy(buf, data, siz);
+               total_data_read += siz;
 
                if (forward_flist_data)
                        write_buf(iobuf.out_fd, buf, siz);
@@ -1873,6 +1910,7 @@ void write_buf(int f, const char *buf, size_t len)
                memcpy(iobuf.out.buf + pos, buf, len);
 
        iobuf.out.len += len;
+       total_data_written += len;
 
   batch_copy:
        if (f == write_batch_monitor_out)