+static void die_from_readerr(int err)
+{
+ /* this prevents us trying to write errors on a dead socket */
+ io_multiplexing_close();
+
+ rprintf(FERROR, "%s: read error: %s\n",
+ RSYNC_NAME, strerror(err));
+ exit_cleanup(RERR_STREAMIO);
+}
+
+
+/**
+ * Read from a socket with IO 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, ret=0;
+
+ io_flush();
+
+ while (ret == 0) {
+ /* until we manage to read *something* */
+ fd_set r_fds, w_fds;
+ struct timeval tv;
+ int fd_count = fd+1;
+ int count;
+
+ FD_ZERO(&r_fds);
+ FD_SET(fd, &r_fds);
+ if (io_error_fd >= 0) {
+ FD_SET(io_error_fd, &r_fds);
+ if (io_error_fd >= fd_count) fd_count = io_error_fd+1;
+ }
+ if (io_filesfrom_f_out >= 0) {
+ int new_fd;
+ if (io_filesfrom_buflen == 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_ZERO(&w_fds);
+ FD_SET(io_filesfrom_f_out, &w_fds);
+ new_fd = io_filesfrom_f_out;
+ }
+ if (new_fd >= fd_count) fd_count = new_fd+1;
+ }
+
+ tv.tv_sec = io_timeout?io_timeout:SELECT_TIMEOUT;
+ tv.tv_usec = 0;
+
+ errno = 0;
+
+ count = select(fd_count, &r_fds,
+ io_filesfrom_buflen? &w_fds : NULL,
+ NULL, &tv);
+
+ if (count == 0) {
+ check_timeout();
+ }
+
+ if (count <= 0) {
+ if (errno == EBADF) {
+ exit_cleanup(RERR_SOCKETIO);
+ }
+ continue;
+ }
+
+ if (io_error_fd >= 0 && FD_ISSET(io_error_fd, &r_fds)) {
+ read_error_fd();
+ }
+
+ if (io_filesfrom_f_out >= 0) {
+ if (io_filesfrom_buflen) {
+ if (FD_ISSET(io_filesfrom_f_out, &w_fds)) {
+ int l = write(io_filesfrom_f_out,
+ io_filesfrom_bp,
+ io_filesfrom_buflen);
+ if (l > 0) {
+ if (!(io_filesfrom_buflen -= l))
+ io_filesfrom_bp = io_filesfrom_buf;
+ else
+ io_filesfrom_bp += l;
+ } else {
+ /* XXX should we complain? */
+ io_filesfrom_f_out = -1;
+ }
+ }
+ } else if (io_filesfrom_f_in >= 0) {
+ if (FD_ISSET(io_filesfrom_f_in, &r_fds)) {
+ int l = read(io_filesfrom_f_in,
+ io_filesfrom_buf,
+ sizeof io_filesfrom_buf);
+ if (l <= 0) {
+ /* Send end-of-file marker */
+ io_filesfrom_buf[0] = '\0';
+ io_filesfrom_buf[1] = '\0';
+ io_filesfrom_buflen = io_filesfrom_lastchar? 2 : 1;
+ io_filesfrom_f_in = -1;
+ } else {
+ extern int eol_nulls;
+ if (!eol_nulls) {
+ char *s = io_filesfrom_buf + l;
+ /* Transform CR and/or LF into '\0' */
+ while (s-- > io_filesfrom_buf) {
+ if (*s == '\n' || *s == '\r')
+ *s = '\0';
+ }
+ }
+ if (!io_filesfrom_lastchar) {
+ /* Last buf ended with a '\0', so don't
+ * let this buf start with one. */
+ while (l && !*io_filesfrom_bp)
+ io_filesfrom_bp++, l--;
+ }
+ if (!l)
+ io_filesfrom_bp = io_filesfrom_buf;
+ else {
+ char *f = io_filesfrom_bp;
+ char *t = f;
+ char *eob = f + l;
+ /* Eliminate any multi-'\0' runs. */
+ while (f != eob) {
+ if (!(*t++ = *f++)) {
+ while (f != eob && !*f)
+ f++, l--;
+ }
+ }
+ io_filesfrom_lastchar = f[-1];
+ }
+ io_filesfrom_buflen = l;
+ }
+ }
+ }
+ }
+
+ if (!FD_ISSET(fd, &r_fds)) continue;
+
+ n = read(fd, buf, len);
+
+ if (n > 0) {
+ buf += n;
+ len -= n;
+ ret += n;
+ if (io_timeout)
+ last_io = time(NULL);
+ continue;
+ } else if (n == 0) {
+ whine_about_eof();
+ return -1; /* doesn't return */
+ } else if (n < 0) {
+ if (errno == EINTR || errno == EWOULDBLOCK ||
+ errno == EAGAIN)
+ continue;
+ die_from_readerr(errno);
+ }
+ }
+
+ return ret;
+}
+
+/**
+ * Read a line into the "fname" buffer (which must be at least MAXPATHLEN
+ * characters long).
+ */
+int read_filesfrom_line(int fd, char *fname)
+{
+ char ch, *s, *eob = fname + MAXPATHLEN - 1;
+ int cnt;
+ extern int io_timeout;
+ extern int eol_nulls;
+ extern char *remote_filesfrom_file;
+ int reading_remotely = remote_filesfrom_file != NULL;
+ int nulls = eol_nulls || reading_remotely;
+
+ start:
+ s = fname;
+ while (1) {
+ cnt = read(fd, &ch, 1);
+ if (cnt < 0 && (errno == EWOULDBLOCK
+ || errno == EINTR || errno == EAGAIN)) {
+ struct timeval tv;
+ fd_set fds;
+ FD_ZERO(&fds);
+ FD_SET(fd, &fds);
+ tv.tv_sec = io_timeout? io_timeout : SELECT_TIMEOUT;
+ tv.tv_usec = 0;
+ if (!select(fd+1, &fds, NULL, NULL, &tv))
+ check_timeout();
+ continue;
+ }
+ if (cnt != 1)
+ break;
+ if (nulls? !ch : (ch == '\r' || ch == '\n')) {
+ /* Skip empty lines if reading locally. */
+ if (!reading_remotely && s == fname)
+ continue;
+ break;
+ }
+ if (s < eob)
+ *s++ = ch;
+ }
+ *s = '\0';
+
+ /* Dump comments. */
+ if (*fname == '#' || *fname == ';')
+ goto start;
+
+ return s - fname;
+}
+
+
+/**
+ * Continue trying to read len bytes - don't return until len has been
+ * read.
+ **/
+static void read_loop(int fd, char *buf, size_t len)
+{
+ while (len) {
+ int n = read_timeout(fd, buf, len);
+
+ buf += n;
+ len -= n;
+ }
+}
+
+
+/**
+ * Read from the file descriptor handling multiplexing - return number
+ * of bytes read.
+ *
+ * Never returns <= 0.
+ */
+static int read_unbuffered(int fd, char *buf, size_t len)
+{
+ static size_t remaining;
+ int tag, ret = 0;
+ char line[1024];
+ static char *buffer;
+ static size_t bufferIdx = 0;
+ static size_t bufferSz;
+
+ if (fd != multiplex_in_fd)
+ return read_timeout(fd, buf, len);
+
+ if (!io_multiplexing_in && remaining == 0) {
+ if (!buffer) {
+ bufferSz = 2 * IO_BUFFER_SIZE;
+ buffer = new_array(char, bufferSz);
+ if (!buffer) out_of_memory("read_unbuffered");
+ }
+ remaining = read_timeout(fd, buffer, bufferSz);
+ bufferIdx = 0;
+ }
+
+ while (ret == 0) {
+ if (remaining) {
+ len = MIN(len, remaining);
+ memcpy(buf, buffer + bufferIdx, len);
+ bufferIdx += len;
+ remaining -= len;
+ ret = len;
+ break;
+ }
+
+ read_loop(fd, line, 4);
+ tag = IVAL(line, 0);
+
+ remaining = tag & 0xFFFFFF;
+ tag = tag >> 24;
+
+ if (tag == MPLEX_BASE) {
+ if (!buffer || remaining > bufferSz) {
+ buffer = realloc_array(buffer, char, remaining);
+ if (!buffer) out_of_memory("read_unbuffered");
+ bufferSz = remaining;
+ }
+ read_loop(fd, buffer, remaining);
+ bufferIdx = 0;
+ continue;
+ }
+
+ tag -= MPLEX_BASE;
+
+ if (tag != FERROR && tag != FINFO) {
+ rprintf(FERROR, "unexpected tag %d\n", tag);
+ exit_cleanup(RERR_STREAMIO);
+ }
+
+ if (remaining > sizeof line - 1) {
+ rprintf(FERROR, "multiplexing overflow %ld\n\n",
+ (long)remaining);
+ exit_cleanup(RERR_STREAMIO);
+ }
+
+ read_loop(fd, line, remaining);
+ line[remaining] = 0;
+
+ rprintf((enum logcode) tag, "%s", line);
+ remaining = 0;
+ }
+
+ if (remaining == 0)
+ io_flush();
+
+ return ret;
+}
+
+
+
+/**
+ * Do a buffered read from @p fd. Don't return until all @p n bytes
+ * have been read. If all @p n can't be read then exit with an
+ * error.
+ **/
+static void readfd(int fd, char *buffer, size_t N)
+{
+ int ret;
+ size_t total=0;
+
+ while (total < N) {
+ ret = read_unbuffered(fd, buffer + total, N-total);
+ total += ret;
+ }
+
+ stats.total_read += total;
+}
+
+
+int32 read_int(int f)
+{
+ char b[4];
+ int32 ret;
+
+ readfd(f,b,4);
+ ret = IVAL(b,0);
+ if (ret == (int32)0xffffffff) return -1;
+ return ret;
+}
+
+int64 read_longint(int f)
+{
+ int64 ret;
+ char b[8];
+ ret = read_int(f);
+
+ if ((int32)ret != (int32)0xffffffff) {
+ return ret;
+ }
+
+#ifdef NO_INT64
+ rprintf(FERROR,"Integer overflow - attempted 64 bit offset\n");
+ exit_cleanup(RERR_UNSUPPORTED);
+#else
+ readfd(f,b,8);
+ ret = IVAL(b,0) | (((int64)IVAL(b,4))<<32);
+#endif
+
+ return ret;
+}
+
+void read_buf(int f,char *buf,size_t len)
+{
+ readfd(f,buf,len);
+}