+ while (cnt == 0) {
+ if (iobuf_in_remaining) {
+ len = MIN(len, iobuf_in_remaining);
+ memcpy(buf, iobuf_in + iobuf_in_ndx, len);
+ iobuf_in_ndx += len;
+ iobuf_in_remaining -= len;
+ cnt = len;
+ break;
+ }
+
+ read_loop(fd, line, 4);
+ tag = IVAL(line, 0);
+
+ msg_bytes = tag & 0xFFFFFF;
+ tag = (tag >> 24) - MPLEX_BASE;
+
+ switch (tag) {
+ case MSG_DATA:
+ if (msg_bytes > iobuf_in_siz) {
+ if (!(iobuf_in = realloc_array(iobuf_in, char,
+ msg_bytes)))
+ out_of_memory("readfd_unbuffered");
+ iobuf_in_siz = msg_bytes;
+ }
+ read_loop(fd, iobuf_in, msg_bytes);
+ iobuf_in_remaining = msg_bytes;
+ iobuf_in_ndx = 0;
+ break;
+ case MSG_NOOP:
+ if (am_sender)
+ maybe_send_keepalive();
+ break;
+ case MSG_IO_ERROR:
+ if (msg_bytes != 4)
+ goto invalid_msg;
+ read_loop(fd, line, msg_bytes);
+ io_error |= IVAL(line, 0);
+ break;
+ case MSG_DELETED:
+ if (msg_bytes >= sizeof line)
+ goto overflow;
+#ifdef ICONV_OPTION
+ if (ic_recv != (iconv_t)-1) {
+ ICONV_CONST char *ibuf;
+ char *obuf = line;
+ size_t icnt, ocnt = sizeof line - 1;
+ int add_null = 0;
+ iconv(ic_send, NULL, 0, NULL, 0);
+ while (msg_bytes) {
+ icnt = msg_bytes > sizeof iconv_buf
+ ? sizeof iconv_buf : msg_bytes;
+ read_loop(fd, iconv_buf, icnt);
+ if (!(msg_bytes -= icnt) && !iconv_buf[icnt-1])
+ icnt--, add_null = 1;
+ ibuf = (ICONV_CONST char *)iconv_buf;
+ if (iconv(ic_send, &ibuf,&icnt,
+ &obuf,&ocnt) == (size_t)-1)
+ goto overflow; // XXX
+ }
+ if (add_null)
+ *obuf++ = '\0';
+ msg_bytes = obuf - line;
+ } else
+#endif
+ read_loop(fd, line, msg_bytes);
+ /* A directory name was sent with the trailing null */
+ if (msg_bytes > 0 && !line[msg_bytes-1])
+ log_delete(line, S_IFDIR);
+ else {
+ line[msg_bytes] = '\0';
+ log_delete(line, S_IFREG);
+ }
+ break;
+ case MSG_SUCCESS:
+ if (msg_bytes != 4) {
+ invalid_msg:
+ rprintf(FERROR, "invalid multi-message %d:%ld [%s]\n",
+ tag, (long)msg_bytes, who_am_i());
+ exit_cleanup(RERR_STREAMIO);
+ }
+ read_loop(fd, line, msg_bytes);
+ successful_send(IVAL(line, 0));
+ break;
+ case MSG_NO_SEND:
+ if (msg_bytes != 4)
+ goto invalid_msg;
+ read_loop(fd, line, msg_bytes);
+ send_msg_int(MSG_NO_SEND, IVAL(line, 0));
+ break;
+ case MSG_INFO:
+ case MSG_ERROR:
+ if (msg_bytes >= sizeof line) {
+ overflow:
+ rprintf(FERROR,
+ "multiplexing overflow %d:%ld [%s]\n",
+ tag, (long)msg_bytes, who_am_i());
+ exit_cleanup(RERR_STREAMIO);
+ }
+ read_loop(fd, line, msg_bytes);
+ rwrite((enum logcode)tag, line, msg_bytes, 1);
+ break;
+ default:
+ rprintf(FERROR, "unexpected tag %d [%s]\n",
+ tag, who_am_i());
+ exit_cleanup(RERR_STREAMIO);
+ }