Fixed a newly-introduced problem in read_timeout() where FD_ZERO(&w_fds)
[rsync/rsync.git] / io.c
diff --git a/io.c b/io.c
index f34a899..421571a 100644 (file)
--- a/io.c
+++ b/io.c
@@ -380,6 +380,7 @@ static int read_timeout(int fd, char *buf, size_t len)
                int count;
 
                FD_ZERO(&r_fds);
+               FD_ZERO(&w_fds);
                FD_SET(fd, &r_fds);
                if (msg_fd_in >= 0) {
                        FD_SET(msg_fd_in, &r_fds);
@@ -401,7 +402,6 @@ static int read_timeout(int fd, char *buf, size_t len)
                                        new_fd = -1;
                                }
                        } else {
-                               FD_ZERO(&w_fds);
                                FD_SET(io_filesfrom_f_out, &w_fds);
                                new_fd = io_filesfrom_f_out;
                        }
@@ -414,14 +414,12 @@ static int read_timeout(int fd, char *buf, size_t len)
 
                errno = 0;
 
-               count = select(maxfd + 1, &r_fds,
-                              io_filesfrom_buflen? &w_fds : NULL,
-                              NULL, &tv);
+               count = select(maxfd + 1, &r_fds, &w_fds, NULL, &tv);
 
                if (count <= 0) {
-                       check_timeout();
                        if (errno == EBADF)
                                exit_cleanup(RERR_SOCKETIO);
+                       check_timeout();
                        continue;
                }
 
@@ -586,7 +584,7 @@ static void read_loop(int fd, char *buf, size_t len)
  *
  * Never returns <= 0.
  */
-static int read_unbuffered(int fd, char *buf, size_t len)
+static int readfd_unbuffered(int fd, char *buf, size_t len)
 {
        static size_t remaining;
        int tag, ret = 0;
@@ -603,7 +601,7 @@ static int read_unbuffered(int fd, char *buf, size_t len)
                        bufferSz = 2 * IO_BUFFER_SIZE;
                        buffer   = new_array(char, bufferSz);
                        if (!buffer)
-                               out_of_memory("read_unbuffered");
+                               out_of_memory("readfd_unbuffered");
                }
                remaining = read_timeout(fd, buffer, bufferSz);
                bufferIdx = 0;
@@ -630,7 +628,7 @@ static int read_unbuffered(int fd, char *buf, size_t len)
                        if (!buffer || remaining > bufferSz) {
                                buffer = realloc_array(buffer, char, remaining);
                                if (!buffer)
-                                       out_of_memory("read_unbuffered");
+                                       out_of_memory("readfd_unbuffered");
                                bufferSz = remaining;
                        }
                        read_loop(fd, buffer, remaining);
@@ -672,7 +670,7 @@ static void readfd(int fd, char *buffer, size_t N)
        size_t total = 0;
 
        while (total < N) {
-               ret = read_unbuffered(fd, buffer + total, N-total);
+               ret = readfd_unbuffered(fd, buffer + total, N-total);
                total += ret;
        }
 
@@ -827,9 +825,9 @@ static void writefd_unbuffered(int fd,char *buf,size_t len)
                               &w_fds, NULL, &tv);
 
                if (count <= 0) {
-                       check_timeout();
                        if (count < 0 && errno == EBADF)
                                exit_cleanup(RERR_SOCKETIO);
+                       check_timeout();
                        continue;
                }
 
@@ -904,9 +902,8 @@ static void mplex_write(int fd, enum msgcode code, char *buf, size_t len)
 
        SIVAL(buffer, 0, ((MPLEX_BASE + (int)code)<<24) + len);
 
-       if (n > (sizeof buffer - 4)) {
+       if (n > sizeof buffer - 4)
                n = sizeof buffer - 4;
-       }
 
        memcpy(&buffer[4], buf, n);
        writefd_unbuffered(fd, buffer, n+4);