X-Git-Url: https://mattmccutchen.net/rsync/rsync.git/blobdiff_plain/20caffd2b361bcad51692998411e4cc566c04b40..cbc63a09e8ea792a9eb39fc2f94536c7a01e525d:/io.c diff --git a/io.c b/io.c index 7c50d5af..0707ca21 100644 --- a/io.c +++ b/io.c @@ -95,6 +95,7 @@ static int write_batch_monitor_out = -1; static int ff_forward_fd = -1; static char ff_lastchar; +static xbuf ff_xb = EMPTY_XBUF; #ifdef ICONV_OPTION static xbuf iconv_buf = EMPTY_XBUF; #endif @@ -338,18 +339,15 @@ static void safe_write(int fd, const char *buf, size_t len) * a chunk of data and put it into the output buffer. */ static void forward_filesfrom_data(void) { - char buf[FILESFROM_BUFLEN]; int len; - xbuf x; - INIT_CONST_XBUF(x, buf); - - len = read(ff_forward_fd, x.buf, x.size); + len = read(ff_forward_fd, ff_xb.buf + ff_xb.len, ff_xb.size - ff_xb.len); if (len <= 0) { if (len == 0 || errno != EINTR) { /* Send end-of-file marker */ - write_buf(iobuf.out_fd, "\0\0", ff_lastchar ? 2 : 1); ff_forward_fd = -1; + write_buf(iobuf.out_fd, "\0\0", ff_lastchar ? 2 : 1); + free_xbuf(&ff_xb); if (protocol_version < 31) io_start_multiplex_out(iobuf.out_fd); } @@ -359,47 +357,84 @@ static void forward_filesfrom_data(void) if (DEBUG_GTE(IO, 2)) rprintf(FINFO, "[%s] files-from read=%ld\n", who_am_i(), (long)len); +#ifdef ICONV_OPTION + len += ff_xb.len; +#endif + if (!eol_nulls) { - char *s = x.buf + len; + char *s = ff_xb.buf + len; /* Transform CR and/or LF into '\0' */ - while (s-- > x.buf) { + while (s-- > ff_xb.buf) { if (*s == '\n' || *s == '\r') *s = '\0'; } } + if (ff_lastchar) - x.pos = 0; + ff_xb.pos = 0; else { - char *s = x.buf; + char *s = ff_xb.buf; /* Last buf ended with a '\0', so don't let this buf start with one. */ while (len && *s == '\0') s++, len--; - x.pos = s - x.buf; + ff_xb.pos = s - ff_xb.buf; } + +#ifdef ICONV_OPTION + if (filesfrom_convert && len) { + char *sob = ff_xb.buf + ff_xb.pos, *s = sob; + char *eob = sob + len; + int flags = ICB_INCLUDE_BAD | ICB_INCLUDE_INCOMPLETE | ICB_CIRCULAR_OUT; + if (ff_lastchar == '\0') + flags |= ICB_INIT; + /* Convert/send each null-terminated string separately, skipping empties. */ + while (s != eob) { + if (*s++ == '\0') { + ff_xb.len = s - sob - 1; + if (iconvbufs(ic_send, &ff_xb, &iobuf.out, flags) < 0) + exit_cleanup(RERR_PROTOCOL); /* impossible? */ + write_buf(iobuf.out_fd, s-1, 1); /* Send the '\0'. */ + while (s != eob && *s == '\0') + s++; + sob = s; + ff_xb.pos = sob - ff_xb.buf; + flags |= ICB_INIT; + } + } + + if ((ff_xb.len = s - sob) == 0) + ff_lastchar = '\0'; + else { + /* Handle a partial string specially, saving any incomplete chars. */ + flags &= ~ICB_INCLUDE_INCOMPLETE; + if (iconvbufs(ic_send, &ff_xb, &iobuf.out, flags) < 0) { + if (errno == E2BIG) + exit_cleanup(RERR_PROTOCOL); /* impossible? */ + if (ff_xb.pos) + memmove(ff_xb.buf, ff_xb.buf + ff_xb.pos, ff_xb.len); + } + ff_lastchar = 'x'; /* Anything non-zero. */ + } + } else +#endif + if (len) { - char *f = x.buf + x.pos; - char *t = f; /* Keep any non-zero offset to avoid iconv reset. */ + char *f = ff_xb.buf + ff_xb.pos; + char *t = ff_xb.buf; char *eob = f + len; /* Eliminate any multi-'\0' runs. */ while (f != eob) { if (!(*t++ = *f++)) { - while (f != eob && !*f) - f++, len--; + while (f != eob && *f == '\0') + f++; } } ff_lastchar = f[-1]; - } -#ifdef ICONV_OPTION - if (filesfrom_convert) { - /* TODO would it help to translate each string between nulls separately? */ - x.len = len; - iconvbufs(ic_send, &x, &iobuf.out, ICB_INCLUDE_BAD|ICB_INCLUDE_INCOMPLETE|ICB_CIRCULAR_OUT); - } else -#endif - if (len) { - /* This will not circle back to perform_io() because we only get - * called when there is plenty of room in the output buffer. */ - write_buf(iobuf.out_fd, x.buf, len); + if ((len = t - ff_xb.buf) != 0) { + /* This will not circle back to perform_io() because we only get + * called when there is plenty of room in the output buffer. */ + write_buf(iobuf.out_fd, ff_xb.buf, len); + } } } @@ -471,8 +506,8 @@ static char *perform_io(size_t needed, int flags) if (!(iobuf.in.buf = realloc_array(iobuf.in.buf, char, needed))) out_of_memory("perform_io"); if (DEBUG_GTE(IO, 4)) { - rprintf(FINFO, "[%s] resized input buffer from %d to %d bytes.\n", - who_am_i(), iobuf.in.size, needed); + rprintf(FINFO, "[%s] resized input buffer from %ld to %ld bytes.\n", + who_am_i(), (long)iobuf.in.size, (long)needed); } iobuf.in.size = needed; } @@ -482,8 +517,8 @@ static char *perform_io(size_t needed, int flags) memmove(iobuf.in.buf, iobuf.in.buf + iobuf.in.pos, iobuf.in.len); if (DEBUG_GTE(IO, 4)) { rprintf(FINFO, - "[%s] moved %d bytes from %d to 0 in the input buffer (size=%d, needed=%d).\n", - who_am_i(), iobuf.in.len, iobuf.in.pos, iobuf.in.size, needed); + "[%s] moved %ld bytes from %ld to 0 in the input buffer (size=%ld, needed=%ld).\n", + who_am_i(), (long)iobuf.in.len, (long)iobuf.in.pos, (long)iobuf.in.size, (long)needed); } if (iobuf.raw_input_ends_before) iobuf.raw_input_ends_before -= iobuf.in.pos; @@ -803,7 +838,7 @@ int send_msg(enum msgcode code, const char *buf, size_t len, int convert) len = iobuf.msg.len; iconvbufs(ic_send, &inbuf, &iobuf.msg, - ICB_INCLUDE_BAD | ICB_INCLUDE_INCOMPLETE | ICB_CIRCULAR_OUT); + ICB_INCLUDE_BAD | ICB_INCLUDE_INCOMPLETE | ICB_CIRCULAR_OUT | ICB_INIT); if (inbuf.len > 0) { rprintf(FERROR, "overflowed iobuf.msg buffer in send_msg"); exit_cleanup(RERR_UNSUPPORTED); @@ -993,6 +1028,8 @@ void start_filesfrom_forwarding(int fd) io_end_multiplex_out(False); iobuf.out_fd = save_fd; } + + alloc_xbuf(&ff_xb, FILESFROM_BUFLEN); } /* Read a line into the "buf" buffer. */ @@ -1039,7 +1076,7 @@ int read_line(int fd, char *buf, size_t bufsiz, int flags) iconv_buf.pos = 0; iconv_buf.len = s - iconv_buf.buf; iconvbufs(ic_recv, &iconv_buf, &outbuf, - ICB_INCLUDE_BAD | ICB_INCLUDE_INCOMPLETE); + ICB_INCLUDE_BAD | ICB_INCLUDE_INCOMPLETE | ICB_INIT); outbuf.buf[outbuf.len] = '\0'; return outbuf.len; } @@ -1153,13 +1190,6 @@ int io_start_buffering_in(int f_in) return 1; } -static void free_xbuf(xbuf *x) -{ - if (x->buf) - free(x->buf); - memset(x, 0, sizeof (xbuf)); -} - void io_end_buffering_in(BOOL free_buffers) { if (DEBUG_GTE(IO, 2)) { @@ -1197,7 +1227,8 @@ void io_end_buffering_out(BOOL free_buffers) void maybe_flush_socket(int important) { - if (iobuf.out.buf && iobuf.out.len && (important || time(NULL) - last_io_out >= 5)) + if (flist_eof && iobuf.out.buf && iobuf.out.len > iobuf.out_empty_len + && (important || time(NULL) - last_io_out >= 5)) io_flush(NORMAL_FLUSH); } @@ -1295,19 +1326,26 @@ static void read_a_msg(void) xbuf outbuf, inbuf; char ibuf[512]; int add_null = 0; + int flags = ICB_INCLUDE_BAD | ICB_INIT; INIT_CONST_XBUF(outbuf, line); INIT_XBUF(inbuf, ibuf, 0, (size_t)-1); while (msg_bytes) { - inbuf.len = msg_bytes > sizeof ibuf ? sizeof ibuf : msg_bytes; - memcpy(inbuf.buf, perform_io(inbuf.len, PIO_INPUT_AND_CONSUME), inbuf.len); - if (!(msg_bytes -= inbuf.len) - && !ibuf[inbuf.len-1]) + size_t len = msg_bytes > sizeof ibuf - inbuf.len ? sizeof ibuf - inbuf.len : msg_bytes; + memcpy(ibuf + inbuf.len, perform_io(len, PIO_INPUT_AND_CONSUME), len); + inbuf.pos = 0; + inbuf.len += len; + if (!(msg_bytes -= len) && !ibuf[inbuf.len-1]) inbuf.len--, add_null = 1; - if (iconvbufs(ic_send, &inbuf, &outbuf, - ICB_INCLUDE_BAD | ICB_INCLUDE_INCOMPLETE) < 0) - goto overflow; + if (iconvbufs(ic_send, &inbuf, &outbuf, flags) < 0) { + if (errno == E2BIG) + goto overflow; + /* Buffer ended with an incomplete char, so move the + * bytes to the start of the buffer and continue. */ + memmove(ibuf, ibuf + inbuf.pos, inbuf.len); + } + flags &= ~ICB_INIT; } if (add_null) { if (outbuf.len == outbuf.size)