version 1.1, 2012/02/17 15:09:30
|
version 1.1.1.4, 2021/03/17 00:32:36
|
Line 4
|
Line 4
|
* Copyright (C) 1996-2001 Andrew Tridgell |
* Copyright (C) 1996-2001 Andrew Tridgell |
* Copyright (C) 1996 Paul Mackerras |
* Copyright (C) 1996 Paul Mackerras |
* Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org> |
* Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org> |
* Copyright (C) 2003-2009 Wayne Davison | * Copyright (C) 2003-2020 Wayne Davison |
* |
* |
* This program is free software; you can redistribute it and/or modify |
* This program is free software; you can redistribute it and/or modify |
* it under the terms of the GNU General Public License as published by |
* it under the terms of the GNU General Public License as published by |
Line 29
|
Line 29
|
|
|
#include "rsync.h" |
#include "rsync.h" |
#include "ifuncs.h" |
#include "ifuncs.h" |
|
#include "inums.h" |
|
|
/** If no timeout is specified then use a 60 second select timeout */ |
/** If no timeout is specified then use a 60 second select timeout */ |
#define SELECT_TIMEOUT 60 |
#define SELECT_TIMEOUT 60 |
Line 37 extern int bwlimit;
|
Line 38 extern int bwlimit;
|
extern size_t bwlimit_writemax; |
extern size_t bwlimit_writemax; |
extern int io_timeout; |
extern int io_timeout; |
extern int am_server; |
extern int am_server; |
extern int am_daemon; |
|
extern int am_sender; |
extern int am_sender; |
|
extern int am_receiver; |
extern int am_generator; |
extern int am_generator; |
|
extern int local_server; |
|
extern int msgs2stderr; |
extern int inc_recurse; |
extern int inc_recurse; |
|
extern int same_db; |
extern int io_error; |
extern int io_error; |
|
extern int batch_fd; |
extern int eol_nulls; |
extern int eol_nulls; |
extern int flist_eof; |
extern int flist_eof; |
|
extern int file_total; |
|
extern int file_old_total; |
extern int list_only; |
extern int list_only; |
extern int read_batch; |
extern int read_batch; |
extern int compat_flags; |
extern int compat_flags; |
extern int protect_args; |
extern int protect_args; |
extern int checksum_seed; |
extern int checksum_seed; |
|
extern int checksum_files; |
|
extern int daemon_connection; |
extern int protocol_version; |
extern int protocol_version; |
extern int remove_source_files; |
extern int remove_source_files; |
extern int preserve_hard_links; |
extern int preserve_hard_links; |
|
extern BOOL extra_flist_sending_enabled; |
|
extern BOOL flush_ok_after_signal; |
extern struct stats stats; |
extern struct stats stats; |
|
extern time_t stop_at_utime; |
extern struct file_list *cur_flist; |
extern struct file_list *cur_flist; |
#ifdef ICONV_OPTION |
#ifdef ICONV_OPTION |
extern int filesfrom_convert; |
extern int filesfrom_convert; |
Line 61 extern iconv_t ic_send, ic_recv;
|
Line 73 extern iconv_t ic_send, ic_recv;
|
|
|
int csum_length = SHORT_SUM_LENGTH; /* initial value */ |
int csum_length = SHORT_SUM_LENGTH; /* initial value */ |
int allowed_lull = 0; |
int allowed_lull = 0; |
int ignore_timeout = 0; |
|
int batch_fd = -1; |
|
int msgdone_cnt = 0; |
int msgdone_cnt = 0; |
|
int forward_flist_data = 0; |
|
BOOL flist_receiving_enabled = False; |
|
|
/* Ignore an EOF error if non-zero. See whine_about_eof(). */ |
/* Ignore an EOF error if non-zero. See whine_about_eof(). */ |
int kluge_around_eof = 0; |
int kluge_around_eof = 0; |
|
int got_kill_signal = -1; /* is set to 0 only after multiplexed I/O starts */ |
|
|
int msg_fd_in = -1; |
|
int msg_fd_out = -1; |
|
int sock_f_in = -1; |
int sock_f_in = -1; |
int sock_f_out = -1; |
int sock_f_out = -1; |
|
|
static int iobuf_f_in = -1; | int64 total_data_read = 0; |
static char *iobuf_in; | int64 total_data_written = 0; |
static size_t iobuf_in_siz; | |
static size_t iobuf_in_ndx; | |
static size_t iobuf_in_remaining; | |
|
|
static int iobuf_f_out = -1; | static struct { |
static char *iobuf_out; | xbuf in, out, msg; |
static int iobuf_out_cnt; | int in_fd; |
| int out_fd; /* Both "out" and "msg" go to this fd. */ |
| int in_multiplexed; |
| unsigned out_empty_len; |
| size_t raw_data_header_pos; /* in the out xbuf */ |
| size_t raw_flushing_ends_before; /* in the out xbuf */ |
| size_t raw_input_ends_before; /* in the in xbuf */ |
| } iobuf = { .in_fd = -1, .out_fd = -1 }; |
|
|
int flist_forward_from = -1; |
|
|
|
static int io_multiplexing_out; |
|
static int io_multiplexing_in; |
|
static time_t last_io_in; |
static time_t last_io_in; |
static time_t last_io_out; |
static time_t last_io_out; |
static int no_flush; |
|
|
|
static int write_batch_monitor_in = -1; |
static int write_batch_monitor_in = -1; |
static int write_batch_monitor_out = -1; |
static int write_batch_monitor_out = -1; |
|
|
static int io_filesfrom_f_in = -1; | static int ff_forward_fd = -1; |
static int io_filesfrom_f_out = -1; | static int ff_reenable_multiplex = -1; |
static xbuf ff_buf = EMPTY_XBUF; | static char ff_lastchar = '\0'; |
static char ff_lastchar; | static xbuf ff_xb = EMPTY_XBUF; |
#ifdef ICONV_OPTION |
#ifdef ICONV_OPTION |
static xbuf iconv_buf = EMPTY_XBUF; |
static xbuf iconv_buf = EMPTY_XBUF; |
#endif |
#endif |
static int defer_forwarding_messages = 0, keep_defer_forwarding = 0; |
|
static int select_timeout = SELECT_TIMEOUT; |
static int select_timeout = SELECT_TIMEOUT; |
static int active_filecnt = 0; |
static int active_filecnt = 0; |
static OFF_T active_bytecnt = 0; |
static OFF_T active_bytecnt = 0; |
Line 114 static char int_byte_extra[64] = {
|
Line 123 static char int_byte_extra[64] = {
|
2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 6, /* (C0 - FF)/4 */ |
2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 5, 6, /* (C0 - FF)/4 */ |
}; |
}; |
|
|
|
/* Our I/O buffers are sized with no bits on in the lowest byte of the "size" |
|
* (indeed, our rounding of sizes in 1024-byte units assures more than this). |
|
* This allows the code that is storing bytes near the physical end of a |
|
* circular buffer to temporarily reduce the buffer's size (in order to make |
|
* some storing idioms easier), while also making it simple to restore the |
|
* buffer's actual size when the buffer's "pos" wraps around to the start (we |
|
* just round the buffer's size up again). */ |
|
|
|
#define IOBUF_WAS_REDUCED(siz) ((siz) & 0xFF) |
|
#define IOBUF_RESTORE_SIZE(siz) (((siz) | 0xFF) + 1) |
|
|
|
#define IN_MULTIPLEXED (iobuf.in_multiplexed != 0) |
|
#define IN_MULTIPLEXED_AND_READY (iobuf.in_multiplexed > 0) |
|
#define OUT_MULTIPLEXED (iobuf.out_empty_len != 0) |
|
|
|
#define PIO_NEED_INPUT (1<<0) /* The *_NEED_* flags are mutually exclusive. */ |
|
#define PIO_NEED_OUTROOM (1<<1) |
|
#define PIO_NEED_MSGROOM (1<<2) |
|
|
|
#define PIO_CONSUME_INPUT (1<<4) /* Must becombined with PIO_NEED_INPUT. */ |
|
|
|
#define PIO_INPUT_AND_CONSUME (PIO_NEED_INPUT | PIO_CONSUME_INPUT) |
|
#define PIO_NEED_FLAGS (PIO_NEED_INPUT | PIO_NEED_OUTROOM | PIO_NEED_MSGROOM) |
|
|
#define REMOTE_OPTION_ERROR "rsync: on remote machine: -" |
#define REMOTE_OPTION_ERROR "rsync: on remote machine: -" |
#define REMOTE_OPTION_ERROR2 ": unknown option" |
#define REMOTE_OPTION_ERROR2 ": unknown option" |
|
|
|
#define FILESFROM_BUFLEN 2048 |
|
|
enum festatus { FES_SUCCESS, FES_REDO, FES_NO_SEND }; |
enum festatus { FES_SUCCESS, FES_REDO, FES_NO_SEND }; |
|
|
static void check_timeout(void) | static flist_ndx_list redo_list, hlink_list; |
| |
| static void read_a_msg(void); |
| static void drain_multiplex_messages(void); |
| static void sleep_for_bwlimit(int bytes_written); |
| |
| static void check_timeout(BOOL allow_keepalive, int keepalive_flags) |
{ |
{ |
time_t t, chk; |
time_t t, chk; |
|
|
if (!io_timeout || ignore_timeout) | /* On the receiving side, the generator is now the one that decides |
| * when a timeout has occurred. When it is sifting through a lot of |
| * files looking for work, it will be sending keep-alive messages to |
| * the sender, and even though the receiver won't be sending/receiving |
| * anything (not even keep-alive messages), the successful writes to |
| * the sender will keep things going. If the receiver is actively |
| * receiving data, it will ensure that the generator knows that it is |
| * not idle by sending the generator keep-alive messages (since the |
| * generator might be blocked trying to send checksums, it needs to |
| * know that the receiver is active). Thus, as long as one or the |
| * other is successfully doing work, the generator will not timeout. */ |
| if (!io_timeout) |
return; |
return; |
|
|
t = time(NULL); |
t = time(NULL); |
|
|
|
if (allow_keepalive) { |
|
/* This may put data into iobuf.msg w/o flushing. */ |
|
maybe_send_keepalive(t, keepalive_flags); |
|
} |
|
|
if (!last_io_in) |
if (!last_io_in) |
last_io_in = t; |
last_io_in = t; |
|
|
|
if (am_receiver) |
|
return; |
|
|
chk = MAX(last_io_out, last_io_in); |
chk = MAX(last_io_out, last_io_in); |
if (t - chk >= io_timeout) { |
if (t - chk >= io_timeout) { |
if (am_server || am_daemon) | if (am_server) |
exit_cleanup(RERR_TIMEOUT); | msgs2stderr = 1; |
rprintf(FERROR, "[%s] io timeout after %d seconds -- exiting\n", |
rprintf(FERROR, "[%s] io timeout after %d seconds -- exiting\n", |
who_am_i(), (int)(t-chk)); |
who_am_i(), (int)(t-chk)); |
exit_cleanup(RERR_TIMEOUT); |
exit_cleanup(RERR_TIMEOUT); |
} |
} |
} |
} |
|
|
static void readfd(int fd, char *buffer, size_t N); | /* It's almost always an error to get an EOF when we're trying to read from the |
static void writefd(int fd, const char *buf, size_t len); | * network, because the protocol is (for the most part) self-terminating. |
static void writefd_unbuffered(int fd, const char *buf, size_t len); | * |
static void mplex_write(int fd, enum msgcode code, const char *buf, size_t len, int convert); | * There is one case for the receiver when it is at the end of the transfer |
| * (hanging around reading any keep-alive packets that might come its way): if |
| * the sender dies before the generator's kill-signal comes through, we can end |
| * up here needing to loop until the kill-signal arrives. In this situation, |
| * kluge_around_eof will be < 0. |
| * |
| * There is another case for older protocol versions (< 24) where the module |
| * listing was not terminated, so we must ignore an EOF error in that case and |
| * exit. In this situation, kluge_around_eof will be > 0. */ |
| static NORETURN void whine_about_eof(BOOL allow_kluge) |
| { |
| if (kluge_around_eof && allow_kluge) { |
| int i; |
| if (kluge_around_eof > 0) |
| exit_cleanup(0); |
| /* If we're still here after 10 seconds, exit with an error. */ |
| for (i = 10*1000/20; i--; ) |
| msleep(20); |
| } |
|
|
static flist_ndx_list redo_list, hlink_list; | rprintf(FERROR, RSYNC_NAME ": connection unexpectedly closed " |
| "(%s bytes received so far) [%s]\n", |
| big_num(stats.total_read), who_am_i()); |
|
|
struct msg_list_item { | exit_cleanup(RERR_STREAMIO); |
struct msg_list_item *next; | } |
char convert; | |
char buf[1]; | |
}; | |
|
|
struct msg_list { | /* Do a safe read, handling any needed looping and error handling. |
struct msg_list_item *head, *tail; | * Returns the count of the bytes read, which will only be different |
}; | * from "len" if we encountered an EOF. This routine is not used on |
| * the socket except very early in the transfer. */ |
| static size_t safe_read(int fd, char *buf, size_t len) |
| { |
| size_t got = 0; |
|
|
static struct msg_list msg_queue; | assert(fd != iobuf.in_fd); |
|
|
static void got_flist_entry_status(enum festatus status, const char *buf) | while (1) { |
| struct timeval tv; |
| fd_set r_fds, e_fds; |
| int cnt; |
| |
| FD_ZERO(&r_fds); |
| FD_SET(fd, &r_fds); |
| FD_ZERO(&e_fds); |
| FD_SET(fd, &e_fds); |
| tv.tv_sec = select_timeout; |
| tv.tv_usec = 0; |
| |
| cnt = select(fd+1, &r_fds, NULL, &e_fds, &tv); |
| if (cnt <= 0) { |
| if (cnt < 0 && errno == EBADF) { |
| rsyserr(FERROR, errno, "safe_read select failed"); |
| exit_cleanup(RERR_FILEIO); |
| } |
| check_timeout(1, MSK_ALLOW_FLUSH); |
| continue; |
| } |
| |
| /*if (FD_ISSET(fd, &e_fds)) |
| rprintf(FINFO, "select exception on fd %d\n", fd); */ |
| |
| if (FD_ISSET(fd, &r_fds)) { |
| int n = read(fd, buf + got, len - got); |
| if (DEBUG_GTE(IO, 2)) |
| rprintf(FINFO, "[%s] safe_read(%d)=%ld\n", who_am_i(), fd, (long)n); |
| if (n == 0) |
| break; |
| if (n < 0) { |
| if (errno == EINTR) |
| continue; |
| rsyserr(FERROR, errno, "safe_read failed to read %ld bytes", (long)len); |
| exit_cleanup(RERR_STREAMIO); |
| } |
| if ((got += (size_t)n) == len) |
| break; |
| } |
| } |
| |
| return got; |
| } |
| |
| static const char *what_fd_is(int fd) |
{ |
{ |
int ndx = IVAL(buf, 0); | static char buf[20]; |
| |
| if (fd == sock_f_out) |
| return "socket"; |
| else if (fd == iobuf.out_fd) |
| return "message fd"; |
| else if (fd == batch_fd) |
| return "batch file"; |
| else { |
| snprintf(buf, sizeof buf, "fd %d", fd); |
| return buf; |
| } |
| } |
| |
| /* Do a safe write, handling any needed looping and error handling. |
| * Returns only if everything was successfully written. This routine |
| * is not used on the socket except very early in the transfer. */ |
| static void safe_write(int fd, const char *buf, size_t len) |
| { |
| int n; |
| |
| assert(fd != iobuf.out_fd); |
| |
| n = write(fd, buf, len); |
| if ((size_t)n == len) |
| return; |
| if (n < 0) { |
| if (errno != EINTR && errno != EWOULDBLOCK && errno != EAGAIN) { |
| write_failed: |
| rsyserr(FERROR, errno, |
| "safe_write failed to write %ld bytes to %s", |
| (long)len, what_fd_is(fd)); |
| exit_cleanup(RERR_STREAMIO); |
| } |
| } else { |
| buf += n; |
| len -= n; |
| } |
| |
| while (len) { |
| struct timeval tv; |
| fd_set w_fds; |
| int cnt; |
| |
| FD_ZERO(&w_fds); |
| FD_SET(fd, &w_fds); |
| tv.tv_sec = select_timeout; |
| tv.tv_usec = 0; |
| |
| cnt = select(fd + 1, NULL, &w_fds, NULL, &tv); |
| if (cnt <= 0) { |
| if (cnt < 0 && errno == EBADF) { |
| rsyserr(FERROR, errno, "safe_write select failed on %s", what_fd_is(fd)); |
| exit_cleanup(RERR_FILEIO); |
| } |
| if (io_timeout) |
| maybe_send_keepalive(time(NULL), MSK_ALLOW_FLUSH); |
| continue; |
| } |
| |
| if (FD_ISSET(fd, &w_fds)) { |
| n = write(fd, buf, len); |
| if (n < 0) { |
| if (errno == EINTR) |
| continue; |
| goto write_failed; |
| } |
| buf += n; |
| len -= n; |
| } |
| } |
| } |
| |
| /* This is only called when files-from data is known to be available. We read |
| * a chunk of data and put it into the output buffer. */ |
| static void forward_filesfrom_data(void) |
| { |
| int len; |
| |
| 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 */ |
| ff_forward_fd = -1; |
| write_buf(iobuf.out_fd, "\0\0", ff_lastchar ? 2 : 1); |
| free_xbuf(&ff_xb); |
| if (ff_reenable_multiplex >= 0) |
| io_start_multiplex_out(ff_reenable_multiplex); |
| } |
| return; |
| } |
| |
| 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 = ff_xb.buf + len; |
| /* Transform CR and/or LF into '\0' */ |
| while (s-- > ff_xb.buf) { |
| if (*s == '\n' || *s == '\r') |
| *s = '\0'; |
| } |
| } |
| |
| if (ff_lastchar) |
| ff_xb.pos = 0; |
| else { |
| 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--; |
| 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 = 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 == '\0') |
| f++; |
| } |
| } |
| ff_lastchar = f[-1]; |
| 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); |
| } |
| } |
| } |
| |
| void reduce_iobuf_size(xbuf *out, size_t new_size) |
| { |
| if (new_size < out->size) { |
| /* Avoid weird buffer interactions by only outputting this to stderr. */ |
| if (msgs2stderr == 1 && DEBUG_GTE(IO, 4)) { |
| const char *name = out == &iobuf.out ? "iobuf.out" |
| : out == &iobuf.msg ? "iobuf.msg" |
| : NULL; |
| if (name) { |
| rprintf(FINFO, "[%s] reduced size of %s (-%d)\n", |
| who_am_i(), name, (int)(out->size - new_size)); |
| } |
| } |
| out->size = new_size; |
| } |
| } |
| |
| void restore_iobuf_size(xbuf *out) |
| { |
| if (IOBUF_WAS_REDUCED(out->size)) { |
| size_t new_size = IOBUF_RESTORE_SIZE(out->size); |
| /* Avoid weird buffer interactions by only outputting this to stderr. */ |
| if (msgs2stderr == 1 && DEBUG_GTE(IO, 4)) { |
| const char *name = out == &iobuf.out ? "iobuf.out" |
| : out == &iobuf.msg ? "iobuf.msg" |
| : NULL; |
| if (name) { |
| rprintf(FINFO, "[%s] restored size of %s (+%d)\n", |
| who_am_i(), name, (int)(new_size - out->size)); |
| } |
| } |
| out->size = new_size; |
| } |
| } |
| |
| static void handle_kill_signal(BOOL flush_ok) |
| { |
| got_kill_signal = -1; |
| flush_ok_after_signal = flush_ok; |
| exit_cleanup(RERR_SIGNAL); |
| } |
| |
| /* Perform buffered input and/or output until specified conditions are met. |
| * When given a "needed" read or write request, this returns without doing any |
| * I/O if the needed input bytes or write space is already available. Once I/O |
| * is needed, this will try to do whatever reading and/or writing is currently |
| * possible, up to the maximum buffer allowances, no matter if this is a read |
| * or write request. However, the I/O stops as soon as the required input |
| * bytes or output space is available. If this is not a read request, the |
| * routine may also do some advantageous reading of messages from a multiplexed |
| * input source (which ensures that we don't jam up with everyone in their |
| * "need to write" code and nobody reading the accumulated data that would make |
| * writing possible). |
| * |
| * The iobuf.in, .out and .msg buffers are all circular. Callers need to be |
| * aware that some data copies will need to be split when the bytes wrap around |
| * from the end to the start. In order to help make writing into the output |
| * buffers easier for some operations (such as the use of SIVAL() into the |
| * buffer) a buffer may be temporarily shortened by a small amount, but the |
| * original size will be automatically restored when the .pos wraps to the |
| * start. See also the 3 raw_* iobuf vars that are used in the handling of |
| * MSG_DATA bytes as they are read-from/written-into the buffers. |
| * |
| * When writing, we flush data in the following priority order: |
| * |
| * 1. Finish writing any in-progress MSG_DATA sequence from iobuf.out. |
| * |
| * 2. Write out all the messages from the message buf (if iobuf.msg is active). |
| * Yes, this means that a PIO_NEED_OUTROOM call will completely flush any |
| * messages before getting to the iobuf.out flushing (except for rule 1). |
| * |
| * 3. Write out the raw data from iobuf.out, possibly filling in the multiplexed |
| * MSG_DATA header that was pre-allocated (when output is multiplexed). |
| * |
| * TODO: items for possible future work: |
| * |
| * - Make this routine able to read the generator-to-receiver batch flow? |
| * |
| * Unlike the old routines that this replaces, it is OK to read ahead as far as |
| * we can because the read_a_msg() routine now reads its bytes out of the input |
| * buffer. In the old days, only raw data was in the input buffer, and any |
| * unused raw data in the buf would prevent the reading of socket data. */ |
| static char *perform_io(size_t needed, int flags) |
| { |
| fd_set r_fds, e_fds, w_fds; |
| struct timeval tv; |
| int cnt, max_fd; |
| size_t empty_buf_len = 0; |
| xbuf *out; |
| char *data; |
| |
| if (iobuf.in.len == 0 && iobuf.in.pos != 0) { |
| if (iobuf.raw_input_ends_before) |
| iobuf.raw_input_ends_before -= iobuf.in.pos; |
| iobuf.in.pos = 0; |
| } |
| |
| switch (flags & PIO_NEED_FLAGS) { |
| case PIO_NEED_INPUT: |
| /* We never resize the circular input buffer. */ |
| if (iobuf.in.size < needed) { |
| rprintf(FERROR, "need to read %ld bytes, iobuf.in.buf is only %ld bytes.\n", |
| (long)needed, (long)iobuf.in.size); |
| exit_cleanup(RERR_PROTOCOL); |
| } |
| |
| if (msgs2stderr == 1 && DEBUG_GTE(IO, 3)) { |
| rprintf(FINFO, "[%s] perform_io(%ld, %sinput)\n", |
| who_am_i(), (long)needed, flags & PIO_CONSUME_INPUT ? "consume&" : ""); |
| } |
| break; |
| |
| case PIO_NEED_OUTROOM: |
| /* We never resize the circular output buffer. */ |
| if (iobuf.out.size - iobuf.out_empty_len < needed) { |
| fprintf(stderr, "need to write %ld bytes, iobuf.out.buf is only %ld bytes.\n", |
| (long)needed, (long)(iobuf.out.size - iobuf.out_empty_len)); |
| exit_cleanup(RERR_PROTOCOL); |
| } |
| |
| if (msgs2stderr == 1 && DEBUG_GTE(IO, 3)) { |
| rprintf(FINFO, "[%s] perform_io(%ld, outroom) needs to flush %ld\n", |
| who_am_i(), (long)needed, |
| iobuf.out.len + needed > iobuf.out.size |
| ? (long)(iobuf.out.len + needed - iobuf.out.size) : 0L); |
| } |
| break; |
| |
| case PIO_NEED_MSGROOM: |
| /* We never resize the circular message buffer. */ |
| if (iobuf.msg.size < needed) { |
| fprintf(stderr, "need to write %ld bytes, iobuf.msg.buf is only %ld bytes.\n", |
| (long)needed, (long)iobuf.msg.size); |
| exit_cleanup(RERR_PROTOCOL); |
| } |
| |
| if (msgs2stderr == 1 && DEBUG_GTE(IO, 3)) { |
| rprintf(FINFO, "[%s] perform_io(%ld, msgroom) needs to flush %ld\n", |
| who_am_i(), (long)needed, |
| iobuf.msg.len + needed > iobuf.msg.size |
| ? (long)(iobuf.msg.len + needed - iobuf.msg.size) : 0L); |
| } |
| break; |
| |
| case 0: |
| if (msgs2stderr == 1 && DEBUG_GTE(IO, 3)) |
| rprintf(FINFO, "[%s] perform_io(%ld, %d)\n", who_am_i(), (long)needed, flags); |
| break; |
| |
| default: |
| exit_cleanup(RERR_UNSUPPORTED); |
| } |
| |
| while (1) { |
| switch (flags & PIO_NEED_FLAGS) { |
| case PIO_NEED_INPUT: |
| if (iobuf.in.len >= needed) |
| goto double_break; |
| break; |
| case PIO_NEED_OUTROOM: |
| /* Note that iobuf.out_empty_len doesn't factor into this check |
| * because iobuf.out.len already holds any needed header len. */ |
| if (iobuf.out.len + needed <= iobuf.out.size) |
| goto double_break; |
| break; |
| case PIO_NEED_MSGROOM: |
| if (iobuf.msg.len + needed <= iobuf.msg.size) |
| goto double_break; |
| break; |
| } |
| |
| max_fd = -1; |
| |
| FD_ZERO(&r_fds); |
| FD_ZERO(&e_fds); |
| if (iobuf.in_fd >= 0 && iobuf.in.size - iobuf.in.len) { |
| if (!read_batch || batch_fd >= 0) { |
| FD_SET(iobuf.in_fd, &r_fds); |
| FD_SET(iobuf.in_fd, &e_fds); |
| } |
| if (iobuf.in_fd > max_fd) |
| max_fd = iobuf.in_fd; |
| } |
| |
| /* Only do more filesfrom processing if there is enough room in the out buffer. */ |
| if (ff_forward_fd >= 0 && iobuf.out.size - iobuf.out.len > FILESFROM_BUFLEN*2) { |
| FD_SET(ff_forward_fd, &r_fds); |
| if (ff_forward_fd > max_fd) |
| max_fd = ff_forward_fd; |
| } |
| |
| FD_ZERO(&w_fds); |
| if (iobuf.out_fd >= 0) { |
| if (iobuf.raw_flushing_ends_before |
| || (!iobuf.msg.len && iobuf.out.len > iobuf.out_empty_len && !(flags & PIO_NEED_MSGROOM))) { |
| if (OUT_MULTIPLEXED && !iobuf.raw_flushing_ends_before) { |
| /* The iobuf.raw_flushing_ends_before value can point off the end |
| * of the iobuf.out buffer for a while, for easier subtracting. */ |
| iobuf.raw_flushing_ends_before = iobuf.out.pos + iobuf.out.len; |
| |
| SIVAL(iobuf.out.buf + iobuf.raw_data_header_pos, 0, |
| ((MPLEX_BASE + (int)MSG_DATA)<<24) + iobuf.out.len - 4); |
| |
| if (msgs2stderr == 1 && DEBUG_GTE(IO, 1)) { |
| rprintf(FINFO, "[%s] send_msg(%d, %ld)\n", |
| who_am_i(), (int)MSG_DATA, (long)iobuf.out.len - 4); |
| } |
| |
| /* reserve room for the next MSG_DATA header */ |
| iobuf.raw_data_header_pos = iobuf.raw_flushing_ends_before; |
| if (iobuf.raw_data_header_pos >= iobuf.out.size) |
| iobuf.raw_data_header_pos -= iobuf.out.size; |
| else if (iobuf.raw_data_header_pos + 4 > iobuf.out.size) { |
| /* The 4-byte header won't fit at the end of the buffer, |
| * so we'll temporarily reduce the output buffer's size |
| * and put the header at the start of the buffer. */ |
| reduce_iobuf_size(&iobuf.out, iobuf.raw_data_header_pos); |
| iobuf.raw_data_header_pos = 0; |
| } |
| /* Yes, it is possible for this to make len > size for a while. */ |
| iobuf.out.len += 4; |
| } |
| |
| empty_buf_len = iobuf.out_empty_len; |
| out = &iobuf.out; |
| } else if (iobuf.msg.len) { |
| empty_buf_len = 0; |
| out = &iobuf.msg; |
| } else |
| out = NULL; |
| if (out) { |
| FD_SET(iobuf.out_fd, &w_fds); |
| if (iobuf.out_fd > max_fd) |
| max_fd = iobuf.out_fd; |
| } |
| } else |
| out = NULL; |
| |
| if (max_fd < 0) { |
| switch (flags & PIO_NEED_FLAGS) { |
| case PIO_NEED_INPUT: |
| iobuf.in.len = 0; |
| if (kluge_around_eof == 2) |
| exit_cleanup(0); |
| if (iobuf.in_fd == -2) |
| whine_about_eof(True); |
| rprintf(FERROR, "error in perform_io: no fd for input.\n"); |
| exit_cleanup(RERR_PROTOCOL); |
| case PIO_NEED_OUTROOM: |
| case PIO_NEED_MSGROOM: |
| msgs2stderr = 1; |
| drain_multiplex_messages(); |
| if (iobuf.out_fd == -2) |
| whine_about_eof(True); |
| rprintf(FERROR, "error in perform_io: no fd for output.\n"); |
| exit_cleanup(RERR_PROTOCOL); |
| default: |
| /* No stated needs, so I guess this is OK. */ |
| break; |
| } |
| break; |
| } |
| |
| if (got_kill_signal > 0) |
| handle_kill_signal(True); |
| |
| if (extra_flist_sending_enabled) { |
| if (file_total - file_old_total < MAX_FILECNT_LOOKAHEAD && IN_MULTIPLEXED_AND_READY) |
| tv.tv_sec = 0; |
| else { |
| extra_flist_sending_enabled = False; |
| tv.tv_sec = select_timeout; |
| } |
| } else |
| tv.tv_sec = select_timeout; |
| tv.tv_usec = 0; |
| |
| cnt = select(max_fd + 1, &r_fds, &w_fds, &e_fds, &tv); |
| |
| if (cnt <= 0) { |
| if (cnt < 0 && errno == EBADF) { |
| msgs2stderr = 1; |
| exit_cleanup(RERR_SOCKETIO); |
| } |
| if (extra_flist_sending_enabled) { |
| extra_flist_sending_enabled = False; |
| send_extra_file_list(sock_f_out, -1); |
| extra_flist_sending_enabled = !flist_eof; |
| } else |
| check_timeout((flags & PIO_NEED_INPUT) != 0, 0); |
| FD_ZERO(&r_fds); /* Just in case... */ |
| FD_ZERO(&w_fds); |
| } |
| |
| if (iobuf.in_fd >= 0 && FD_ISSET(iobuf.in_fd, &r_fds)) { |
| size_t len, pos = iobuf.in.pos + iobuf.in.len; |
| int n; |
| if (pos >= iobuf.in.size) { |
| pos -= iobuf.in.size; |
| len = iobuf.in.size - iobuf.in.len; |
| } else |
| len = iobuf.in.size - pos; |
| if ((n = read(iobuf.in_fd, iobuf.in.buf + pos, len)) <= 0) { |
| if (n == 0) { |
| /* Signal that input has become invalid. */ |
| if (!read_batch || batch_fd < 0 || am_generator) |
| iobuf.in_fd = -2; |
| batch_fd = -1; |
| continue; |
| } |
| if (errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN) |
| n = 0; |
| else { |
| /* Don't write errors on a dead socket. */ |
| if (iobuf.in_fd == sock_f_in) { |
| if (am_sender) |
| msgs2stderr = 1; |
| rsyserr(FERROR_SOCKET, errno, "read error"); |
| } else |
| rsyserr(FERROR, errno, "read error"); |
| exit_cleanup(RERR_SOCKETIO); |
| } |
| } |
| if (msgs2stderr == 1 && DEBUG_GTE(IO, 2)) |
| rprintf(FINFO, "[%s] recv=%ld\n", who_am_i(), (long)n); |
| |
| if (io_timeout || stop_at_utime) { |
| last_io_in = time(NULL); |
| if (stop_at_utime && last_io_in >= stop_at_utime) { |
| rprintf(FERROR, "stopping at requested limit\n"); |
| exit_cleanup(RERR_TIMEOUT); |
| } |
| if (io_timeout && flags & PIO_NEED_INPUT) |
| maybe_send_keepalive(last_io_in, 0); |
| } |
| stats.total_read += n; |
| |
| iobuf.in.len += n; |
| } |
| |
| if (out && FD_ISSET(iobuf.out_fd, &w_fds)) { |
| size_t len = iobuf.raw_flushing_ends_before ? iobuf.raw_flushing_ends_before - out->pos : out->len; |
| int n; |
| |
| if (bwlimit_writemax && len > bwlimit_writemax) |
| len = bwlimit_writemax; |
| |
| if (out->pos + len > out->size) |
| len = out->size - out->pos; |
| if ((n = write(iobuf.out_fd, out->buf + out->pos, len)) <= 0) { |
| if (errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN) |
| n = 0; |
| else { |
| /* Don't write errors on a dead socket. */ |
| msgs2stderr = 1; |
| iobuf.out_fd = -2; |
| iobuf.out.len = iobuf.msg.len = iobuf.raw_flushing_ends_before = 0; |
| rsyserr(FERROR_SOCKET, errno, "write error"); |
| drain_multiplex_messages(); |
| exit_cleanup(RERR_SOCKETIO); |
| } |
| } |
| if (msgs2stderr == 1 && DEBUG_GTE(IO, 2)) { |
| rprintf(FINFO, "[%s] %s sent=%ld\n", |
| who_am_i(), out == &iobuf.out ? "out" : "msg", (long)n); |
| } |
| |
| if (io_timeout) |
| last_io_out = time(NULL); |
| stats.total_written += n; |
| |
| if (bwlimit_writemax) |
| sleep_for_bwlimit(n); |
| |
| if ((out->pos += n) == out->size) { |
| if (iobuf.raw_flushing_ends_before) |
| iobuf.raw_flushing_ends_before -= out->size; |
| out->pos = 0; |
| restore_iobuf_size(out); |
| } else if (out->pos == iobuf.raw_flushing_ends_before) |
| iobuf.raw_flushing_ends_before = 0; |
| if ((out->len -= n) == empty_buf_len) { |
| out->pos = 0; |
| restore_iobuf_size(out); |
| if (empty_buf_len) |
| iobuf.raw_data_header_pos = 0; |
| } |
| } |
| |
| if (got_kill_signal > 0) |
| handle_kill_signal(True); |
| |
| /* We need to help prevent deadlock by doing what reading |
| * we can whenever we are here trying to write. */ |
| if (IN_MULTIPLEXED_AND_READY && !(flags & PIO_NEED_INPUT)) { |
| while (!iobuf.raw_input_ends_before && iobuf.in.len > 512) |
| read_a_msg(); |
| if (flist_receiving_enabled && iobuf.in.len > 512) |
| wait_for_receiver(); /* generator only */ |
| } |
| |
| if (ff_forward_fd >= 0 && FD_ISSET(ff_forward_fd, &r_fds)) { |
| /* This can potentially flush all output and enable |
| * multiplexed output, so keep this last in the loop |
| * and be sure to not cache anything that would break |
| * such a change. */ |
| forward_filesfrom_data(); |
| } |
| } |
| double_break: |
| |
| if (got_kill_signal > 0) |
| handle_kill_signal(True); |
| |
| data = iobuf.in.buf + iobuf.in.pos; |
| |
| if (flags & PIO_CONSUME_INPUT) { |
| iobuf.in.len -= needed; |
| iobuf.in.pos += needed; |
| if (iobuf.in.pos == iobuf.raw_input_ends_before) |
| iobuf.raw_input_ends_before = 0; |
| if (iobuf.in.pos >= iobuf.in.size) { |
| iobuf.in.pos -= iobuf.in.size; |
| if (iobuf.raw_input_ends_before) |
| iobuf.raw_input_ends_before -= iobuf.in.size; |
| } |
| } |
| |
| return data; |
| } |
| |
| static void raw_read_buf(char *buf, size_t len) |
| { |
| size_t pos = iobuf.in.pos; |
| char *data = perform_io(len, PIO_INPUT_AND_CONSUME); |
| if (iobuf.in.pos <= pos && len) { |
| size_t siz = len - iobuf.in.pos; |
| memcpy(buf, data, siz); |
| memcpy(buf + siz, iobuf.in.buf, iobuf.in.pos); |
| } else |
| memcpy(buf, data, len); |
| } |
| |
| static int32 raw_read_int(void) |
| { |
| char *data, buf[4]; |
| if (iobuf.in.size - iobuf.in.pos >= 4) |
| data = perform_io(4, PIO_INPUT_AND_CONSUME); |
| else |
| raw_read_buf(data = buf, 4); |
| return IVAL(data, 0); |
| } |
| |
| void noop_io_until_death(void) |
| { |
| char buf[1024]; |
| |
| if (!iobuf.in.buf || !iobuf.out.buf || iobuf.in_fd < 0 || iobuf.out_fd < 0 || kluge_around_eof) |
| return; |
| |
| /* If we're talking to a daemon over a socket, don't short-circuit this logic */ |
| if (msgs2stderr && daemon_connection >= 0) |
| return; |
| |
| kluge_around_eof = 2; |
| /* Setting an I/O timeout ensures that if something inexplicably weird |
| * happens, we won't hang around forever. */ |
| if (!io_timeout) |
| set_io_timeout(60); |
| |
| while (1) |
| read_buf(iobuf.in_fd, buf, sizeof buf); |
| } |
| |
| /* Buffer a message for the multiplexed output stream. Is not used for (normal) MSG_DATA. */ |
| int send_msg(enum msgcode code, const char *buf, size_t len, int convert) |
| { |
| char *hdr; |
| size_t needed, pos; |
| BOOL want_debug = DEBUG_GTE(IO, 1) && convert >= 0 && (msgs2stderr == 1 || code != MSG_INFO); |
| |
| if (!OUT_MULTIPLEXED) |
| return 0; |
| |
| if (want_debug) |
| rprintf(FINFO, "[%s] send_msg(%d, %ld)\n", who_am_i(), (int)code, (long)len); |
| |
| /* When checking for enough free space for this message, we need to |
| * make sure that there is space for the 4-byte header, plus we'll |
| * assume that we may waste up to 3 bytes (if the header doesn't fit |
| * at the physical end of the buffer). */ |
| #ifdef ICONV_OPTION |
| if (convert > 0 && ic_send == (iconv_t)-1) |
| convert = 0; |
| if (convert > 0) { |
| /* Ensuring double-size room leaves space for maximal conversion expansion. */ |
| needed = len*2 + 4 + 3; |
| } else |
| #endif |
| needed = len + 4 + 3; |
| if (iobuf.msg.len + needed > iobuf.msg.size) { |
| if (!am_receiver) |
| perform_io(needed, PIO_NEED_MSGROOM); |
| else { /* We allow the receiver to increase their iobuf.msg size to avoid a deadlock. */ |
| size_t old_size = iobuf.msg.size; |
| restore_iobuf_size(&iobuf.msg); |
| realloc_xbuf(&iobuf.msg, iobuf.msg.size * 2); |
| if (iobuf.msg.pos + iobuf.msg.len > old_size) |
| memcpy(iobuf.msg.buf + old_size, iobuf.msg.buf, iobuf.msg.pos + iobuf.msg.len - old_size); |
| } |
| } |
| |
| pos = iobuf.msg.pos + iobuf.msg.len; /* Must be set after any flushing. */ |
| if (pos >= iobuf.msg.size) |
| pos -= iobuf.msg.size; |
| else if (pos + 4 > iobuf.msg.size) { |
| /* The 4-byte header won't fit at the end of the buffer, |
| * so we'll temporarily reduce the message buffer's size |
| * and put the header at the start of the buffer. */ |
| reduce_iobuf_size(&iobuf.msg, pos); |
| pos = 0; |
| } |
| hdr = iobuf.msg.buf + pos; |
| |
| iobuf.msg.len += 4; /* Allocate room for the coming header bytes. */ |
| |
| #ifdef ICONV_OPTION |
| if (convert > 0) { |
| xbuf inbuf; |
| |
| INIT_XBUF(inbuf, (char*)buf, len, (size_t)-1); |
| |
| len = iobuf.msg.len; |
| iconvbufs(ic_send, &inbuf, &iobuf.msg, |
| 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); |
| } |
| len = iobuf.msg.len - len; |
| } else |
| #endif |
| { |
| size_t siz; |
| |
| if ((pos += 4) == iobuf.msg.size) |
| pos = 0; |
| |
| /* Handle a split copy if we wrap around the end of the circular buffer. */ |
| if (pos >= iobuf.msg.pos && (siz = iobuf.msg.size - pos) < len) { |
| memcpy(iobuf.msg.buf + pos, buf, siz); |
| memcpy(iobuf.msg.buf, buf + siz, len - siz); |
| } else |
| memcpy(iobuf.msg.buf + pos, buf, len); |
| |
| iobuf.msg.len += len; |
| } |
| |
| SIVAL(hdr, 0, ((MPLEX_BASE + (int)code)<<24) + len); |
| |
| if (want_debug && convert > 0) |
| rprintf(FINFO, "[%s] converted msg len=%ld\n", who_am_i(), (long)len); |
| |
| return 1; |
| } |
| |
| void send_msg_int(enum msgcode code, int num) |
| { |
| char numbuf[4]; |
| |
| if (DEBUG_GTE(IO, 1)) |
| rprintf(FINFO, "[%s] send_msg_int(%d, %d)\n", who_am_i(), (int)code, num); |
| |
| SIVAL(numbuf, 0, num); |
| send_msg(code, numbuf, 4, -1); |
| } |
| |
| static void got_flist_entry_status(enum festatus status, int ndx) |
| { |
struct file_list *flist = flist_for_ndx(ndx, "got_flist_entry_status"); |
struct file_list *flist = flist_for_ndx(ndx, "got_flist_entry_status"); |
|
|
if (remove_source_files) { |
if (remove_source_files) { |
Line 176 static void got_flist_entry_status(enum festatus statu
|
Line 1056 static void got_flist_entry_status(enum festatus statu
|
switch (status) { |
switch (status) { |
case FES_SUCCESS: |
case FES_SUCCESS: |
if (remove_source_files) |
if (remove_source_files) |
send_msg(MSG_SUCCESS, buf, 4, 0); | send_msg_int(MSG_SUCCESS, ndx); |
/* FALL THROUGH */ |
/* FALL THROUGH */ |
case FES_NO_SEND: |
case FES_NO_SEND: |
#ifdef SUPPORT_HARD_LINKS |
#ifdef SUPPORT_HARD_LINKS |
Line 186 static void got_flist_entry_status(enum festatus statu
|
Line 1066 static void got_flist_entry_status(enum festatus statu
|
if (status == FES_NO_SEND) |
if (status == FES_NO_SEND) |
flist_ndx_push(&hlink_list, -2); /* indicates a failure follows */ |
flist_ndx_push(&hlink_list, -2); /* indicates a failure follows */ |
flist_ndx_push(&hlink_list, ndx); |
flist_ndx_push(&hlink_list, ndx); |
flist->in_progress++; | if (inc_recurse) |
| flist->in_progress++; |
} |
} |
|
} else if (checksum_files & CSF_UPDATE) { |
|
struct file_struct *file = flist->files[ndx - flist->ndx_start]; |
|
set_cached_checksum(flist, file); |
} |
} |
#endif |
#endif |
break; |
break; |
Line 226 void set_io_timeout(int secs)
|
Line 1110 void set_io_timeout(int secs)
|
allowed_lull = 0; |
allowed_lull = 0; |
} |
} |
|
|
/* Setup the fd used to receive MSG_* messages. Only needed during the |
|
* early stages of being a local sender (up through the sending of the |
|
* file list) or when we're the generator (to fetch the messages from |
|
* the receiver). */ |
|
void set_msg_fd_in(int fd) |
|
{ |
|
msg_fd_in = fd; |
|
} |
|
|
|
/* Setup the fd used to send our MSG_* messages. Only needed when |
|
* we're the receiver (to send our messages to the generator). */ |
|
void set_msg_fd_out(int fd) |
|
{ |
|
msg_fd_out = fd; |
|
set_nonblocking(msg_fd_out); |
|
} |
|
|
|
/* Add a message to the pending MSG_* list. */ |
|
static void msg_list_add(struct msg_list *lst, int code, const char *buf, int len, int convert) |
|
{ |
|
struct msg_list_item *m; |
|
int sz = len + 4 + sizeof m[0] - 1; |
|
|
|
if (!(m = (struct msg_list_item *)new_array(char, sz))) |
|
out_of_memory("msg_list_add"); |
|
m->next = NULL; |
|
m->convert = convert; |
|
SIVAL(m->buf, 0, ((code+MPLEX_BASE)<<24) | len); |
|
memcpy(m->buf + 4, buf, len); |
|
if (lst->tail) |
|
lst->tail->next = m; |
|
else |
|
lst->head = m; |
|
lst->tail = m; |
|
} |
|
|
|
static inline int flush_a_msg(int fd) |
|
{ |
|
struct msg_list_item *m = msg_queue.head; |
|
int len = IVAL(m->buf, 0) & 0xFFFFFF; |
|
int tag = *((uchar*)m->buf+3) - MPLEX_BASE; |
|
|
|
if (!(msg_queue.head = m->next)) |
|
msg_queue.tail = NULL; |
|
|
|
defer_forwarding_messages++; |
|
mplex_write(fd, tag, m->buf + 4, len, m->convert); |
|
defer_forwarding_messages--; |
|
|
|
free(m); |
|
|
|
return len; |
|
} |
|
|
|
static void msg_flush(void) |
|
{ |
|
if (am_generator) { |
|
while (msg_queue.head && io_multiplexing_out) |
|
stats.total_written += flush_a_msg(sock_f_out) + 4; |
|
} else { |
|
while (msg_queue.head) |
|
(void)flush_a_msg(msg_fd_out); |
|
} |
|
} |
|
|
|
static void check_for_d_option_error(const char *msg) |
static void check_for_d_option_error(const char *msg) |
{ |
{ |
static char rsync263_opts[] = "BCDHIKLPRSTWabceghlnopqrtuvxz"; |
static char rsync263_opts[] = "BCDHIKLPRSTWabceghlnopqrtuvxz"; |
Line 316 static void check_for_d_option_error(const char *msg)
|
Line 1135 static void check_for_d_option_error(const char *msg)
|
} |
} |
|
|
if (saw_d) { |
if (saw_d) { |
rprintf(FWARNING, | rprintf(FWARNING, "*** Try using \"--old-d\" if remote rsync is <= 2.6.3 ***\n"); |
"*** Try using \"--old-d\" if remote rsync is <= 2.6.3 ***\n"); | |
} |
} |
} |
} |
|
|
/* Read a message from the MSG_* fd and handle it. This is called either |
|
* during the early stages of being a local sender (up through the sending |
|
* of the file list) or when we're the generator (to fetch the messages |
|
* from the receiver). */ |
|
static void read_msg_fd(void) |
|
{ |
|
char buf[2048]; |
|
size_t n; |
|
struct file_list *flist; |
|
int fd = msg_fd_in; |
|
int tag, len; |
|
|
|
/* Temporarily disable msg_fd_in. This is needed to avoid looping back |
|
* to this routine from writefd_unbuffered(). */ |
|
no_flush++; |
|
msg_fd_in = -1; |
|
defer_forwarding_messages++; |
|
|
|
readfd(fd, buf, 4); |
|
tag = IVAL(buf, 0); |
|
|
|
len = tag & 0xFFFFFF; |
|
tag = (tag >> 24) - MPLEX_BASE; |
|
|
|
switch (tag) { |
|
case MSG_DONE: |
|
if (len < 0 || len > 1 || !am_generator) { |
|
invalid_msg: |
|
rprintf(FERROR, "invalid message %d:%d [%s%s]\n", |
|
tag, len, who_am_i(), |
|
inc_recurse ? "/inc" : ""); |
|
exit_cleanup(RERR_STREAMIO); |
|
} |
|
if (len) { |
|
readfd(fd, buf, len); |
|
stats.total_read = read_varlong(fd, 3); |
|
} |
|
msgdone_cnt++; |
|
break; |
|
case MSG_REDO: |
|
if (len != 4 || !am_generator) |
|
goto invalid_msg; |
|
readfd(fd, buf, 4); |
|
got_flist_entry_status(FES_REDO, buf); |
|
break; |
|
case MSG_FLIST: |
|
if (len != 4 || !am_generator || !inc_recurse) |
|
goto invalid_msg; |
|
readfd(fd, buf, 4); |
|
/* Read extra file list from receiver. */ |
|
assert(iobuf_in != NULL); |
|
assert(iobuf_f_in == fd); |
|
if (verbose > 3) { |
|
rprintf(FINFO, "[%s] receiving flist for dir %d\n", |
|
who_am_i(), IVAL(buf,0)); |
|
} |
|
flist = recv_file_list(fd); |
|
flist->parent_ndx = IVAL(buf,0); |
|
#ifdef SUPPORT_HARD_LINKS |
|
if (preserve_hard_links) |
|
match_hard_links(flist); |
|
#endif |
|
break; |
|
case MSG_FLIST_EOF: |
|
if (len != 0 || !am_generator || !inc_recurse) |
|
goto invalid_msg; |
|
flist_eof = 1; |
|
break; |
|
case MSG_IO_ERROR: |
|
if (len != 4) |
|
goto invalid_msg; |
|
readfd(fd, buf, len); |
|
io_error |= IVAL(buf, 0); |
|
break; |
|
case MSG_DELETED: |
|
if (len >= (int)sizeof buf || !am_generator) |
|
goto invalid_msg; |
|
readfd(fd, buf, len); |
|
send_msg(MSG_DELETED, buf, len, 1); |
|
break; |
|
case MSG_SUCCESS: |
|
if (len != 4 || !am_generator) |
|
goto invalid_msg; |
|
readfd(fd, buf, 4); |
|
got_flist_entry_status(FES_SUCCESS, buf); |
|
break; |
|
case MSG_NO_SEND: |
|
if (len != 4 || !am_generator) |
|
goto invalid_msg; |
|
readfd(fd, buf, 4); |
|
got_flist_entry_status(FES_NO_SEND, buf); |
|
break; |
|
case MSG_ERROR_SOCKET: |
|
case MSG_ERROR_UTF8: |
|
case MSG_CLIENT: |
|
if (!am_generator) |
|
goto invalid_msg; |
|
if (tag == MSG_ERROR_SOCKET) |
|
io_end_multiplex_out(); |
|
/* FALL THROUGH */ |
|
case MSG_INFO: |
|
case MSG_ERROR: |
|
case MSG_ERROR_XFER: |
|
case MSG_WARNING: |
|
case MSG_LOG: |
|
while (len) { |
|
n = len; |
|
if (n >= sizeof buf) |
|
n = sizeof buf - 1; |
|
readfd(fd, buf, n); |
|
rwrite((enum logcode)tag, buf, n, !am_generator); |
|
len -= n; |
|
} |
|
break; |
|
default: |
|
rprintf(FERROR, "unknown message %d:%d [%s]\n", |
|
tag, len, who_am_i()); |
|
exit_cleanup(RERR_STREAMIO); |
|
} |
|
|
|
no_flush--; |
|
msg_fd_in = fd; |
|
if (!--defer_forwarding_messages && !no_flush) |
|
msg_flush(); |
|
} |
|
|
|
/* This is used by the generator to limit how many file transfers can |
/* This is used by the generator to limit how many file transfers can |
* be active at once when --remove-source-files is specified. Without |
* be active at once when --remove-source-files is specified. Without |
* this, sender-side deletions were mostly happening at the end. */ |
* this, sender-side deletions were mostly happening at the end. */ |
Line 460 void increment_active_files(int ndx, int itemizing, en
|
Line 1152 void increment_active_files(int ndx, int itemizing, en
|
check_for_finished_files(itemizing, code, 0); |
check_for_finished_files(itemizing, code, 0); |
if (active_filecnt < limit) |
if (active_filecnt < limit) |
break; |
break; |
if (iobuf_out_cnt) | wait_for_receiver(); |
io_flush(NORMAL_FLUSH); | |
else | |
read_msg_fd(); | |
} |
} |
|
|
active_filecnt++; |
active_filecnt++; |
active_bytecnt += F_LENGTH(cur_flist->files[ndx - cur_flist->ndx_start]); |
active_bytecnt += F_LENGTH(cur_flist->files[ndx - cur_flist->ndx_start]); |
} |
} |
|
|
/* Write an message to a multiplexed stream. If this fails, rsync exits. */ |
|
static void mplex_write(int fd, enum msgcode code, const char *buf, size_t len, int convert) |
|
{ |
|
char buffer[BIGPATHBUFLEN]; /* Oversized for use by iconv code. */ |
|
size_t n = len; |
|
|
|
#ifdef ICONV_OPTION |
|
/* We need to convert buf before doing anything else so that we |
|
* can include the (converted) byte length in the message header. */ |
|
if (convert && ic_send != (iconv_t)-1) { |
|
xbuf outbuf, inbuf; |
|
|
|
INIT_XBUF(outbuf, buffer + 4, 0, sizeof buffer - 4); |
|
INIT_XBUF(inbuf, (char*)buf, len, -1); |
|
|
|
iconvbufs(ic_send, &inbuf, &outbuf, |
|
ICB_INCLUDE_BAD | ICB_INCLUDE_INCOMPLETE); |
|
if (inbuf.len > 0) { |
|
rprintf(FERROR, "overflowed conversion buffer in mplex_write"); |
|
exit_cleanup(RERR_UNSUPPORTED); |
|
} |
|
|
|
n = len = outbuf.len; |
|
} else |
|
#endif |
|
if (n > 1024 - 4) /* BIGPATHBUFLEN can handle 1024 bytes */ |
|
n = 0; /* We'd rather do 2 writes than too much memcpy(). */ |
|
else |
|
memcpy(buffer + 4, buf, n); |
|
|
|
SIVAL(buffer, 0, ((MPLEX_BASE + (int)code)<<24) + len); |
|
|
|
keep_defer_forwarding++; /* defer_forwarding_messages++ on return */ |
|
writefd_unbuffered(fd, buffer, n+4); |
|
keep_defer_forwarding--; |
|
|
|
if (len > n) |
|
writefd_unbuffered(fd, buf+n, len-n); |
|
|
|
if (!--defer_forwarding_messages && !no_flush) |
|
msg_flush(); |
|
} |
|
|
|
int send_msg(enum msgcode code, const char *buf, int len, int convert) |
|
{ |
|
if (msg_fd_out < 0) { |
|
if (!defer_forwarding_messages) |
|
return io_multiplex_write(code, buf, len, convert); |
|
if (!io_multiplexing_out) |
|
return 0; |
|
msg_list_add(&msg_queue, code, buf, len, convert); |
|
return 1; |
|
} |
|
if (flist_forward_from >= 0) |
|
msg_list_add(&msg_queue, code, buf, len, convert); |
|
else |
|
mplex_write(msg_fd_out, code, buf, len, convert); |
|
return 1; |
|
} |
|
|
|
void send_msg_int(enum msgcode code, int num) |
|
{ |
|
char numbuf[4]; |
|
SIVAL(numbuf, 0, num); |
|
send_msg(code, numbuf, 4, 0); |
|
} |
|
|
|
void wait_for_receiver(void) |
|
{ |
|
if (io_flush(NORMAL_FLUSH)) |
|
return; |
|
read_msg_fd(); |
|
} |
|
|
|
int get_redo_num(void) |
int get_redo_num(void) |
{ |
{ |
return flist_ndx_pop(&redo_list); |
return flist_ndx_pop(&redo_list); |
Line 554 int get_hlink_num(void)
|
Line 1169 int get_hlink_num(void)
|
return flist_ndx_pop(&hlink_list); |
return flist_ndx_pop(&hlink_list); |
} |
} |
|
|
/** | /* When we're the receiver and we have a local --files-from list of names |
* When we're the receiver and we have a local --files-from list of names | |
* that needs to be sent over the socket to the sender, we have to do two |
* that needs to be sent over the socket to the sender, we have to do two |
* things at the same time: send the sender a list of what files we're |
* things at the same time: send the sender a list of what files we're |
* processing and read the incoming file+info list from the sender. We do |
* processing and read the incoming file+info list from the sender. We do |
* this by augmenting the read_timeout() function to copy this data. It | * this by making recv_file_list() call forward_filesfrom_data(), which |
* uses ff_buf to read a block of data from f_in (when it is ready, since | * will ensure that we forward data to the sender until we get some data |
* it might be a pipe) and then blast it out f_out (when it is ready to | * for recv_file_list() to use. */ |
* receive more data). | void start_filesfrom_forwarding(int fd) |
*/ | |
void io_set_filesfrom_fds(int f_in, int f_out) | |
{ |
{ |
io_filesfrom_f_in = f_in; | if (protocol_version < 31 && OUT_MULTIPLEXED) { |
io_filesfrom_f_out = f_out; | /* Older protocols send the files-from data w/o packaging |
alloc_xbuf(&ff_buf, 2048); | * it in multiplexed I/O packets, so temporarily switch |
#ifdef ICONV_OPTION | * to buffered I/O to match this behavior. */ |
if (protect_args) | iobuf.msg.pos = iobuf.msg.len = 0; /* Be extra sure no messages go out. */ |
alloc_xbuf(&iconv_buf, 1024); | ff_reenable_multiplex = io_end_multiplex_out(MPLX_TO_BUFFERED); |
#endif | |
} | |
| |
/* It's almost always an error to get an EOF when we're trying to read from the | |
* network, because the protocol is (for the most part) self-terminating. | |
* | |
* There is one case for the receiver when it is at the end of the transfer | |
* (hanging around reading any keep-alive packets that might come its way): if | |
* the sender dies before the generator's kill-signal comes through, we can end | |
* up here needing to loop until the kill-signal arrives. In this situation, | |
* kluge_around_eof will be < 0. | |
* | |
* There is another case for older protocol versions (< 24) where the module | |
* listing was not terminated, so we must ignore an EOF error in that case and | |
* exit. In this situation, kluge_around_eof will be > 0. */ | |
static void whine_about_eof(int fd) | |
{ | |
if (kluge_around_eof && fd == sock_f_in) { | |
int i; | |
if (kluge_around_eof > 0) | |
exit_cleanup(0); | |
/* If we're still here after 10 seconds, exit with an error. */ | |
for (i = 10*1000/20; i--; ) | |
msleep(20); | |
} |
} |
|
ff_forward_fd = fd; |
|
|
rprintf(FERROR, RSYNC_NAME ": connection unexpectedly closed " | alloc_xbuf(&ff_xb, FILESFROM_BUFLEN); |
"(%.0f bytes received so far) [%s]\n", | |
(double)stats.total_read, who_am_i()); | |
| |
exit_cleanup(RERR_STREAMIO); | |
} |
} |
|
|
/** |
|
* Read from a socket with I/O 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, cnt = 0; |
|
|
|
io_flush(FULL_FLUSH); |
|
|
|
while (cnt == 0) { |
|
/* until we manage to read *something* */ |
|
fd_set r_fds, w_fds; |
|
struct timeval tv; |
|
int maxfd = fd; |
|
int count; |
|
|
|
FD_ZERO(&r_fds); |
|
FD_ZERO(&w_fds); |
|
FD_SET(fd, &r_fds); |
|
if (io_filesfrom_f_out >= 0) { |
|
int new_fd; |
|
if (ff_buf.len == 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_SET(io_filesfrom_f_out, &w_fds); |
|
new_fd = io_filesfrom_f_out; |
|
} |
|
if (new_fd > maxfd) |
|
maxfd = new_fd; |
|
} |
|
|
|
tv.tv_sec = select_timeout; |
|
tv.tv_usec = 0; |
|
|
|
errno = 0; |
|
|
|
count = select(maxfd + 1, &r_fds, &w_fds, NULL, &tv); |
|
|
|
if (count <= 0) { |
|
if (errno == EBADF) { |
|
defer_forwarding_messages = 0; |
|
exit_cleanup(RERR_SOCKETIO); |
|
} |
|
check_timeout(); |
|
continue; |
|
} |
|
|
|
if (io_filesfrom_f_out >= 0) { |
|
if (ff_buf.len) { |
|
if (FD_ISSET(io_filesfrom_f_out, &w_fds)) { |
|
int l = write(io_filesfrom_f_out, |
|
ff_buf.buf + ff_buf.pos, |
|
ff_buf.len); |
|
if (l > 0) { |
|
if (!(ff_buf.len -= l)) |
|
ff_buf.pos = 0; |
|
else |
|
ff_buf.pos += l; |
|
} else if (errno != EINTR) { |
|
/* 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)) { |
|
#ifdef ICONV_OPTION |
|
xbuf *ibuf = filesfrom_convert ? &iconv_buf : &ff_buf; |
|
#else |
|
xbuf *ibuf = &ff_buf; |
|
#endif |
|
int l = read(io_filesfrom_f_in, ibuf->buf, ibuf->size); |
|
if (l <= 0) { |
|
if (l == 0 || errno != EINTR) { |
|
/* Send end-of-file marker */ |
|
memcpy(ff_buf.buf, "\0\0", 2); |
|
ff_buf.len = ff_lastchar? 2 : 1; |
|
ff_buf.pos = 0; |
|
io_filesfrom_f_in = -1; |
|
} |
|
} else { |
|
#ifdef ICONV_OPTION |
|
if (filesfrom_convert) { |
|
iconv_buf.pos = 0; |
|
iconv_buf.len = l; |
|
iconvbufs(ic_send, &iconv_buf, &ff_buf, |
|
ICB_EXPAND_OUT|ICB_INCLUDE_BAD|ICB_INCLUDE_INCOMPLETE); |
|
l = ff_buf.len; |
|
} |
|
#endif |
|
if (!eol_nulls) { |
|
char *s = ff_buf.buf + l; |
|
/* Transform CR and/or LF into '\0' */ |
|
while (s-- > ff_buf.buf) { |
|
if (*s == '\n' || *s == '\r') |
|
*s = '\0'; |
|
} |
|
} |
|
if (!ff_lastchar) { |
|
/* Last buf ended with a '\0', so don't |
|
* let this buf start with one. */ |
|
while (l && ff_buf.buf[ff_buf.pos] == '\0') |
|
ff_buf.pos++, l--; |
|
} |
|
if (!l) |
|
ff_buf.pos = 0; |
|
else { |
|
char *f = ff_buf.buf + ff_buf.pos; |
|
char *t = f; |
|
char *eob = f + l; |
|
/* Eliminate any multi-'\0' runs. */ |
|
while (f != eob) { |
|
if (!(*t++ = *f++)) { |
|
while (f != eob && !*f) |
|
f++, l--; |
|
} |
|
} |
|
ff_lastchar = f[-1]; |
|
} |
|
ff_buf.len = l; |
|
} |
|
} |
|
} |
|
} |
|
|
|
if (!FD_ISSET(fd, &r_fds)) |
|
continue; |
|
|
|
n = read(fd, buf, len); |
|
|
|
if (n <= 0) { |
|
if (n == 0) |
|
whine_about_eof(fd); /* Doesn't return. */ |
|
if (errno == EINTR || errno == EWOULDBLOCK |
|
|| errno == EAGAIN) |
|
continue; |
|
|
|
/* Don't write errors on a dead socket. */ |
|
if (fd == sock_f_in) { |
|
io_end_multiplex_out(); |
|
rsyserr(FERROR_SOCKET, errno, "read error"); |
|
} else |
|
rsyserr(FERROR, errno, "read error"); |
|
exit_cleanup(RERR_STREAMIO); |
|
} |
|
|
|
buf += n; |
|
len -= n; |
|
cnt += n; |
|
|
|
if (fd == sock_f_in && io_timeout) |
|
last_io_in = time(NULL); |
|
} |
|
|
|
return cnt; |
|
} |
|
|
|
/* Read a line into the "buf" buffer. */ |
/* Read a line into the "buf" buffer. */ |
int read_line(int fd, char *buf, size_t bufsiz, int flags) |
int read_line(int fd, char *buf, size_t bufsiz, int flags) |
{ |
{ |
char ch, *s, *eob; |
char ch, *s, *eob; |
int cnt; |
|
|
|
#ifdef ICONV_OPTION |
#ifdef ICONV_OPTION |
if (flags & RL_CONVERT && iconv_buf.size < bufsiz) |
if (flags & RL_CONVERT && iconv_buf.size < bufsiz) |
realloc_xbuf(&iconv_buf, bufsiz + 1024); | realloc_xbuf(&iconv_buf, ROUND_UP_1024(bufsiz) + 1024); |
#endif |
#endif |
|
|
start: |
start: |
Line 794 int read_line(int fd, char *buf, size_t bufsiz, int fl
|
Line 1208 int read_line(int fd, char *buf, size_t bufsiz, int fl
|
#endif |
#endif |
eob = s + bufsiz - 1; |
eob = s + bufsiz - 1; |
while (1) { |
while (1) { |
cnt = read(fd, &ch, 1); | /* We avoid read_byte() for files because files can return an EOF. */ |
if (cnt < 0 && (errno == EWOULDBLOCK | if (fd == iobuf.in_fd) |
|| errno == EINTR || errno == EAGAIN)) { | ch = read_byte(fd); |
struct timeval tv; | else if (safe_read(fd, &ch, 1) == 0) |
fd_set r_fds, e_fds; | |
FD_ZERO(&r_fds); | |
FD_SET(fd, &r_fds); | |
FD_ZERO(&e_fds); | |
FD_SET(fd, &e_fds); | |
tv.tv_sec = select_timeout; | |
tv.tv_usec = 0; | |
if (!select(fd+1, &r_fds, NULL, &e_fds, &tv)) | |
check_timeout(); | |
/*if (FD_ISSET(fd, &e_fds)) | |
rprintf(FINFO, "select exception on fd %d\n", fd); */ | |
continue; | |
} | |
if (cnt != 1) | |
break; |
break; |
if (flags & RL_EOL_NULLS ? ch == '\0' : (ch == '\r' || ch == '\n')) { |
if (flags & RL_EOL_NULLS ? ch == '\0' : (ch == '\r' || ch == '\n')) { |
/* Skip empty lines if dumping comments. */ |
/* Skip empty lines if dumping comments. */ |
Line 834 int read_line(int fd, char *buf, size_t bufsiz, int fl
|
Line 1234 int read_line(int fd, char *buf, size_t bufsiz, int fl
|
iconv_buf.pos = 0; |
iconv_buf.pos = 0; |
iconv_buf.len = s - iconv_buf.buf; |
iconv_buf.len = s - iconv_buf.buf; |
iconvbufs(ic_recv, &iconv_buf, &outbuf, |
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'; |
outbuf.buf[outbuf.len] = '\0'; |
return outbuf.len; |
return outbuf.len; |
} |
} |
Line 847 void read_args(int f_in, char *mod_name, char *buf, si
|
Line 1247 void read_args(int f_in, char *mod_name, char *buf, si
|
char ***argv_p, int *argc_p, char **request_p) |
char ***argv_p, int *argc_p, char **request_p) |
{ |
{ |
int maxargs = MAX_ARGS; |
int maxargs = MAX_ARGS; |
int dot_pos = 0; | int dot_pos = 0, argc = 0, request_len = 0; |
int argc = 0; | |
char **argv, *p; |
char **argv, *p; |
int rl_flags = (rl_nulls ? RL_EOL_NULLS : 0); |
int rl_flags = (rl_nulls ? RL_EOL_NULLS : 0); |
|
|
Line 856 void read_args(int f_in, char *mod_name, char *buf, si
|
Line 1255 void read_args(int f_in, char *mod_name, char *buf, si
|
rl_flags |= (protect_args && ic_recv != (iconv_t)-1 ? RL_CONVERT : 0); |
rl_flags |= (protect_args && ic_recv != (iconv_t)-1 ? RL_CONVERT : 0); |
#endif |
#endif |
|
|
if (!(argv = new_array(char *, maxargs))) | argv = new_array(char *, maxargs); |
out_of_memory("read_args"); | |
if (mod_name && !protect_args) |
if (mod_name && !protect_args) |
argv[argc++] = "rsyncd"; |
argv[argc++] = "rsyncd"; |
|
|
|
if (request_p) |
|
*request_p = NULL; |
|
|
while (1) { |
while (1) { |
if (read_line(f_in, buf, bufsiz, rl_flags) == 0) |
if (read_line(f_in, buf, bufsiz, rl_flags) == 0) |
break; |
break; |
|
|
if (argc == maxargs-1) { |
if (argc == maxargs-1) { |
maxargs += MAX_ARGS; |
maxargs += MAX_ARGS; |
if (!(argv = realloc_array(argv, char *, maxargs))) | argv = realloc_array(argv, char *, maxargs); |
out_of_memory("read_args"); | |
} |
} |
|
|
if (dot_pos) { |
if (dot_pos) { |
if (request_p) { | if (request_p && request_len < 1024) { |
*request_p = strdup(buf); | int len = strlen(buf); |
request_p = NULL; | if (request_len) |
| request_p[0][request_len++] = ' '; |
| *request_p = realloc_array(*request_p, char, request_len + len + 1); |
| memcpy(*request_p + request_len, buf, len + 1); |
| request_len += len; |
} |
} |
if (mod_name) |
if (mod_name) |
glob_expand_module(mod_name, buf, &argv, &argc, &maxargs); |
glob_expand_module(mod_name, buf, &argv, &argc, &maxargs); |
else |
else |
glob_expand(buf, &argv, &argc, &maxargs); |
glob_expand(buf, &argv, &argc, &maxargs); |
} else { |
} else { |
if (!(p = strdup(buf))) | p = strdup(buf); |
out_of_memory("read_args"); | |
argv[argc++] = p; |
argv[argc++] = p; |
if (*p == '.' && p[1] == '\0') |
if (*p == '.' && p[1] == '\0') |
dot_pos = argc; |
dot_pos = argc; |
Line 896 void read_args(int f_in, char *mod_name, char *buf, si
|
Line 1299 void read_args(int f_in, char *mod_name, char *buf, si
|
*argv_p = argv; |
*argv_p = argv; |
} |
} |
|
|
int io_start_buffering_out(int f_out) | BOOL io_start_buffering_out(int f_out) |
{ |
{ |
if (iobuf_out) { | if (msgs2stderr == 1 && DEBUG_GTE(IO, 2)) |
assert(f_out == iobuf_f_out); | rprintf(FINFO, "[%s] io_start_buffering_out(%d)\n", who_am_i(), f_out); |
return 0; | |
| if (iobuf.out.buf) { |
| if (iobuf.out_fd == -1) |
| iobuf.out_fd = f_out; |
| else |
| assert(f_out == iobuf.out_fd); |
| return False; |
} |
} |
if (!(iobuf_out = new_array(char, IO_BUFFER_SIZE))) | |
out_of_memory("io_start_buffering_out"); | alloc_xbuf(&iobuf.out, ROUND_UP_1024(IO_BUFFER_SIZE * 2)); |
iobuf_out_cnt = 0; | iobuf.out_fd = f_out; |
iobuf_f_out = f_out; | |
return 1; | return True; |
} |
} |
|
|
int io_start_buffering_in(int f_in) | BOOL io_start_buffering_in(int f_in) |
{ |
{ |
if (iobuf_in) { | if (msgs2stderr == 1 && DEBUG_GTE(IO, 2)) |
assert(f_in == iobuf_f_in); | rprintf(FINFO, "[%s] io_start_buffering_in(%d)\n", who_am_i(), f_in); |
return 0; | |
| if (iobuf.in.buf) { |
| if (iobuf.in_fd == -1) |
| iobuf.in_fd = f_in; |
| else |
| assert(f_in == iobuf.in_fd); |
| return False; |
} |
} |
iobuf_in_siz = 2 * IO_BUFFER_SIZE; | |
if (!(iobuf_in = new_array(char, iobuf_in_siz))) | alloc_xbuf(&iobuf.in, ROUND_UP_1024(IO_BUFFER_SIZE)); |
out_of_memory("io_start_buffering_in"); | iobuf.in_fd = f_in; |
iobuf_f_in = f_in; | |
return 1; | return True; |
} |
} |
|
|
void io_end_buffering_in(void) | void io_end_buffering_in(BOOL free_buffers) |
{ |
{ |
if (!iobuf_in) | if (msgs2stderr == 1 && DEBUG_GTE(IO, 2)) { |
return; | rprintf(FINFO, "[%s] io_end_buffering_in(IOBUF_%s_BUFS)\n", |
free(iobuf_in); | who_am_i(), free_buffers ? "FREE" : "KEEP"); |
iobuf_in = NULL; | } |
iobuf_in_ndx = 0; | |
iobuf_in_remaining = 0; | if (free_buffers) |
iobuf_f_in = -1; | free_xbuf(&iobuf.in); |
| else |
| iobuf.in.pos = iobuf.in.len = 0; |
| |
| iobuf.in_fd = -1; |
} |
} |
|
|
void io_end_buffering_out(void) | void io_end_buffering_out(BOOL free_buffers) |
{ |
{ |
if (!iobuf_out) | if (msgs2stderr == 1 && DEBUG_GTE(IO, 2)) { |
return; | rprintf(FINFO, "[%s] io_end_buffering_out(IOBUF_%s_BUFS)\n", |
| who_am_i(), free_buffers ? "FREE" : "KEEP"); |
| } |
| |
io_flush(FULL_FLUSH); |
io_flush(FULL_FLUSH); |
free(iobuf_out); | |
iobuf_out = NULL; | if (free_buffers) { |
iobuf_f_out = -1; | free_xbuf(&iobuf.out); |
| free_xbuf(&iobuf.msg); |
| } |
| |
| iobuf.out_fd = -1; |
} |
} |
|
|
void maybe_flush_socket(int important) |
void maybe_flush_socket(int important) |
{ |
{ |
if (iobuf_out && iobuf_out_cnt | if (flist_eof && iobuf.out.buf && iobuf.out.len > iobuf.out_empty_len |
&& (important || time(NULL) - last_io_out >= 5)) |
&& (important || time(NULL) - last_io_out >= 5)) |
io_flush(NORMAL_FLUSH); |
io_flush(NORMAL_FLUSH); |
} |
} |
|
|
void maybe_send_keepalive(void) | /* Older rsync versions used to send either a MSG_NOOP (protocol 30) or a |
| * raw-data-based keep-alive (protocol 29), both of which implied forwarding of |
| * the message through the sender. Since the new timeout method does not need |
| * any forwarding, we just send an empty MSG_DATA message, which works with all |
| * rsync versions. This avoids any message forwarding, and leaves the raw-data |
| * stream alone (since we can never be quite sure if that stream is in the |
| * right state for a keep-alive message). */ |
| void maybe_send_keepalive(time_t now, int flags) |
{ |
{ |
if (time(NULL) - last_io_out >= allowed_lull) { | if (flags & MSK_ACTIVE_RECEIVER) |
if (!iobuf_out || !iobuf_out_cnt) { | last_io_in = now; /* Fudge things when we're working hard on the files. */ |
if (protocol_version < 29) | |
send_msg(MSG_DATA, "", 0, 0); | /* Early in the transfer (before the receiver forks) the receiving side doesn't |
else if (protocol_version >= 30) | * care if it hasn't sent data in a while as long as it is receiving data (in |
send_msg(MSG_NOOP, "", 0, 0); | * fact, a pre-3.1.0 rsync would die if we tried to send it a keep alive during |
else { | * this time). So, if we're an early-receiving proc, just return and let the |
write_int(sock_f_out, cur_flist->used); | * incoming data determine if we timeout. */ |
write_shortint(sock_f_out, ITEM_IS_NEW); | if (!am_sender && !am_receiver && !am_generator) |
} | return; |
} | |
if (iobuf_out) | if (now - last_io_out >= allowed_lull) { |
| /* The receiver is special: it only sends keep-alive messages if it is |
| * actively receiving data. Otherwise, it lets the generator timeout. */ |
| if (am_receiver && now - last_io_in >= io_timeout) |
| return; |
| |
| if (!iobuf.msg.len && iobuf.out.len == iobuf.out_empty_len) |
| send_msg(MSG_DATA, "", 0, 0); |
| if (!(flags & MSK_ALLOW_FLUSH)) { |
| /* Let the caller worry about writing out the data. */ |
| } else if (iobuf.msg.len) |
| perform_io(iobuf.msg.size - iobuf.msg.len + 1, PIO_NEED_MSGROOM); |
| else if (iobuf.out.len > iobuf.out_empty_len) |
io_flush(NORMAL_FLUSH); |
io_flush(NORMAL_FLUSH); |
} |
} |
} |
} |
|
|
void start_flist_forward(int f_in) | void start_flist_forward(int ndx) |
{ |
{ |
assert(iobuf_out != NULL); | write_int(iobuf.out_fd, ndx); |
assert(iobuf_f_out == msg_fd_out); | forward_flist_data = 1; |
flist_forward_from = f_in; | |
defer_forwarding_messages++; | |
} |
} |
|
|
void stop_flist_forward(void) |
void stop_flist_forward(void) |
{ |
{ |
flist_forward_from = -1; | forward_flist_data = 0; |
defer_forwarding_messages--; | |
io_flush(FULL_FLUSH); | |
} |
} |
|
|
/** | /* Read a message from a multiplexed source. */ |
* Continue trying to read len bytes - don't return until len has been | static void read_a_msg(void) |
* read. | |
**/ | |
static void read_loop(int fd, char *buf, size_t len) | |
{ |
{ |
while (len) { | char data[BIGPATHBUFLEN]; |
int n = read_timeout(fd, buf, len); | int tag, val; |
| |
buf += n; | |
len -= n; | |
} | |
} | |
| |
/** | |
* Read from the file descriptor handling multiplexing - return number | |
* of bytes read. | |
* | |
* Never returns <= 0. | |
*/ | |
static int readfd_unbuffered(int fd, char *buf, size_t len) | |
{ | |
size_t msg_bytes; |
size_t msg_bytes; |
int tag, cnt = 0; |
|
char line[BIGPATHBUFLEN]; |
|
|
|
if (!iobuf_in || fd != iobuf_f_in) | /* This ensures that perform_io() does not try to do any message reading |
return read_timeout(fd, buf, len); | * until we've read all of the data for this message. We should also |
| * try to avoid calling things that will cause data to be written via |
| * perform_io() prior to this being reset to 1. */ |
| iobuf.in_multiplexed = -1; |
|
|
if (!io_multiplexing_in && iobuf_in_remaining == 0) { | tag = raw_read_int(); |
iobuf_in_remaining = read_timeout(fd, iobuf_in, iobuf_in_siz); | |
iobuf_in_ndx = 0; | |
} | |
|
|
while (cnt == 0) { | msg_bytes = tag & 0xFFFFFF; |
if (iobuf_in_remaining) { | tag = (tag >> 24) - MPLEX_BASE; |
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); | if (msgs2stderr == 1 && DEBUG_GTE(IO, 1)) |
tag = IVAL(line, 0); | rprintf(FINFO, "[%s] got msg=%d, len=%ld\n", who_am_i(), (int)tag, (long)msg_bytes); |
|
|
msg_bytes = tag & 0xFFFFFF; | switch (tag) { |
tag = (tag >> 24) - MPLEX_BASE; | case MSG_DATA: |
| assert(iobuf.raw_input_ends_before == 0); |
switch (tag) { | /* Though this does not yet read the data, we do mark where in |
case MSG_DATA: | * the buffer the msg data will end once it is read. It is |
if (msg_bytes > iobuf_in_siz) { | * possible that this points off the end of the buffer, in |
if (!(iobuf_in = realloc_array(iobuf_in, char, | * which case the gradual reading of the input stream will |
msg_bytes))) | * cause this value to wrap around and eventually become real. */ |
out_of_memory("readfd_unbuffered"); | if (msg_bytes) |
iobuf_in_siz = msg_bytes; | iobuf.raw_input_ends_before = iobuf.in.pos + msg_bytes; |
} | iobuf.in_multiplexed = 1; |
read_loop(fd, iobuf_in, msg_bytes); | break; |
iobuf_in_remaining = msg_bytes; | case MSG_STATS: |
iobuf_in_ndx = 0; | if (msg_bytes != sizeof stats.total_read || !am_generator) |
| goto invalid_msg; |
| raw_read_buf((char*)&stats.total_read, sizeof stats.total_read); |
| iobuf.in_multiplexed = 1; |
| break; |
| case MSG_REDO: |
| if (msg_bytes != 4 || !am_generator) |
| goto invalid_msg; |
| val = raw_read_int(); |
| iobuf.in_multiplexed = 1; |
| got_flist_entry_status(FES_REDO, val); |
| break; |
| case MSG_IO_ERROR: |
| if (msg_bytes != 4) |
| goto invalid_msg; |
| val = raw_read_int(); |
| iobuf.in_multiplexed = 1; |
| io_error |= val; |
| if (am_receiver) |
| send_msg_int(MSG_IO_ERROR, val); |
| break; |
| case MSG_IO_TIMEOUT: |
| if (msg_bytes != 4 || am_server || am_generator) |
| goto invalid_msg; |
| val = raw_read_int(); |
| iobuf.in_multiplexed = 1; |
| 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: |
| /* Support protocol-30 keep-alive method. */ |
| if (msg_bytes != 0) |
| goto invalid_msg; |
| iobuf.in_multiplexed = 1; |
| if (am_sender) |
| maybe_send_keepalive(time(NULL), MSK_ALLOW_FLUSH); |
| break; |
| case MSG_CHECKSUM: |
| /* This receives some checksum info that we want to make a note of |
| * (which allows a single process to do all the writing to the db). */ |
| if (msg_bytes != MSG_CHECKSUM_LEN) |
| goto overflow; |
| raw_read_buf(data, MSG_CHECKSUM_LEN); |
| if (am_generator && same_db) { |
| iobuf.in_multiplexed = 1; |
| send_msg(MSG_CHECKSUM, data, MSG_CHECKSUM_LEN, 0); |
| } if (am_receiver || (am_sender && !local_server)) |
| goto unexpected; |
| else { |
| /* The received data is a set of numbers followed by the checksum. */ |
| STRUCT_STAT st; |
| st.st_dev = IVAL64(data, 0); |
| st.st_ino = IVAL64(data, 8); |
| st.st_size = IVAL64(data, 16); |
| st.st_mtime = IVAL64(data, 24); |
| st.st_ctime = IVAL64(data, 32); |
| #if MSG_CHECKSUM_LONGS != 5 |
| #error Fix the parsing of checksum long values |
| #endif |
| iobuf.in_multiplexed = 1; |
| db_set_checksum(IVAL(data, MSG_CHECKSUM_LONGS*8), &st, data + MSG_CHECKSUM_LONGS*8 + 4); |
| } |
| break; |
| case MSG_DELETED: |
| if (msg_bytes >= sizeof data) |
| goto overflow; |
| if (am_generator) { |
| raw_read_buf(data, msg_bytes); |
| iobuf.in_multiplexed = 1; |
| send_msg(MSG_DELETED, data, msg_bytes, 1); |
break; |
break; |
case MSG_NOOP: | } |
if (msg_bytes != 0) | |
goto invalid_msg; | |
if (am_sender) | |
maybe_send_keepalive(); | |
break; | |
case MSG_IO_ERROR: | |
if (msg_bytes != 4) | |
goto invalid_msg; | |
read_loop(fd, line, msg_bytes); | |
send_msg_int(MSG_IO_ERROR, IVAL(line, 0)); | |
io_error |= IVAL(line, 0); | |
break; | |
case MSG_DELETED: | |
if (msg_bytes >= sizeof line) | |
goto overflow; | |
#ifdef ICONV_OPTION |
#ifdef ICONV_OPTION |
if (ic_recv != (iconv_t)-1) { | if (ic_recv != (iconv_t)-1) { |
xbuf outbuf, inbuf; | xbuf outbuf, inbuf; |
char ibuf[512]; | char ibuf[512]; |
int add_null = 0; | int add_null = 0; |
| int flags = ICB_INCLUDE_BAD | ICB_INIT; |
|
|
INIT_CONST_XBUF(outbuf, line); | INIT_CONST_XBUF(outbuf, data); |
INIT_XBUF(inbuf, ibuf, 0, -1); | INIT_XBUF(inbuf, ibuf, 0, (size_t)-1); |
|
|
while (msg_bytes) { | while (msg_bytes) { |
inbuf.len = msg_bytes > sizeof ibuf | size_t len = msg_bytes > sizeof ibuf - inbuf.len ? sizeof ibuf - inbuf.len : msg_bytes; |
? sizeof ibuf : msg_bytes; | raw_read_buf(ibuf + inbuf.len, len); |
read_loop(fd, inbuf.buf, inbuf.len); | inbuf.pos = 0; |
if (!(msg_bytes -= inbuf.len) | inbuf.len += len; |
&& !ibuf[inbuf.len-1]) | if (!(msg_bytes -= len) && !ibuf[inbuf.len-1]) |
inbuf.len--, add_null = 1; | inbuf.len--, add_null = 1; |
if (iconvbufs(ic_send, &inbuf, &outbuf, | if (iconvbufs(ic_send, &inbuf, &outbuf, flags) < 0) { |
ICB_INCLUDE_BAD | ICB_INCLUDE_INCOMPLETE) < 0) | if (errno == E2BIG) |
goto overflow; |
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); |
} |
} |
if (add_null) { | flags &= ~ICB_INIT; |
if (outbuf.len == outbuf.size) | |
goto overflow; | |
outbuf.buf[outbuf.len++] = '\0'; | |
} | |
msg_bytes = outbuf.len; | |
} 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; | if (add_null) { |
case MSG_SUCCESS: | if (outbuf.len == outbuf.size) |
if (msg_bytes != 4) { | goto overflow; |
invalid_msg: | outbuf.buf[outbuf.len++] = '\0'; |
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); | msg_bytes = outbuf.len; |
successful_send(IVAL(line, 0)); | } else |
break; | #endif |
case MSG_NO_SEND: | raw_read_buf(data, msg_bytes); |
if (msg_bytes != 4) | iobuf.in_multiplexed = 1; |
goto invalid_msg; | /* A directory name was sent with the trailing null */ |
read_loop(fd, line, msg_bytes); | if (msg_bytes > 0 && !data[msg_bytes-1]) |
send_msg_int(MSG_NO_SEND, IVAL(line, 0)); | log_delete(data, S_IFDIR); |
break; | else { |
case MSG_INFO: | data[msg_bytes] = '\0'; |
case MSG_ERROR: | log_delete(data, S_IFREG); |
case MSG_ERROR_XFER: | } |
case MSG_WARNING: | break; |
if (msg_bytes >= sizeof line) { | case MSG_SUCCESS: |
overflow: | if (msg_bytes != 4) { |
rprintf(FERROR, | invalid_msg: |
"multiplexing overflow %d:%ld [%s]\n", | rprintf(FERROR, "invalid multi-message %d:%lu [%s%s]\n", |
tag, (long)msg_bytes, who_am_i()); | tag, (unsigned long)msg_bytes, who_am_i(), |
exit_cleanup(RERR_STREAMIO); | inc_recurse ? "/inc" : ""); |
| exit_cleanup(RERR_STREAMIO); |
| } |
| val = raw_read_int(); |
| iobuf.in_multiplexed = 1; |
| if (am_generator) |
| got_flist_entry_status(FES_SUCCESS, val); |
| else |
| successful_send(val); |
| break; |
| case MSG_NO_SEND: |
| if (msg_bytes != 4) |
| goto invalid_msg; |
| val = raw_read_int(); |
| iobuf.in_multiplexed = 1; |
| if (am_generator) |
| got_flist_entry_status(FES_NO_SEND, val); |
| else |
| send_msg_int(MSG_NO_SEND, val); |
| break; |
| case MSG_ERROR_SOCKET: |
| case MSG_ERROR_UTF8: |
| case MSG_CLIENT: |
| case MSG_LOG: |
| if (!am_generator) |
| goto invalid_msg; |
| if (tag == MSG_ERROR_SOCKET) |
| msgs2stderr = 1; |
| /* FALL THROUGH */ |
| case MSG_INFO: |
| case MSG_ERROR: |
| case MSG_ERROR_XFER: |
| case MSG_WARNING: |
| if (msg_bytes >= sizeof data) { |
| overflow: |
| rprintf(FERROR, |
| "multiplexing overflow %d:%lu [%s%s]\n", |
| tag, (unsigned long)msg_bytes, who_am_i(), |
| inc_recurse ? "/inc" : ""); |
| exit_cleanup(RERR_STREAMIO); |
| } |
| raw_read_buf(data, msg_bytes); |
| /* We don't set in_multiplexed value back to 1 before writing this message |
| * because the write might loop back and read yet another message, over and |
| * over again, while waiting for room to put the message in the msg buffer. */ |
| rwrite((enum logcode)tag, data, msg_bytes, !am_generator); |
| iobuf.in_multiplexed = 1; |
| if (first_message) { |
| if (list_only && !am_sender && tag == 1 && msg_bytes < sizeof data) { |
| data[msg_bytes] = '\0'; |
| check_for_d_option_error(data); |
} |
} |
read_loop(fd, line, msg_bytes); | first_message = 0; |
rwrite((enum logcode)tag, line, msg_bytes, 1); | } |
if (first_message) { | break; |
if (list_only && !am_sender && tag == 1) { | case MSG_ERROR_EXIT: |
line[msg_bytes] = '\0'; | if (msg_bytes == 4) |
check_for_d_option_error(line); | val = raw_read_int(); |
| else if (msg_bytes == 0) |
| val = 0; |
| else |
| goto invalid_msg; |
| iobuf.in_multiplexed = 1; |
| if (DEBUG_GTE(EXIT, 3)) |
| rprintf(FINFO, "[%s] got MSG_ERROR_EXIT with %ld bytes\n", who_am_i(), (long)msg_bytes); |
| if (msg_bytes == 0) { |
| if (!am_sender && !am_generator) { |
| if (DEBUG_GTE(EXIT, 3)) { |
| rprintf(FINFO, "[%s] sending MSG_ERROR_EXIT (len 0)\n", |
| who_am_i()); |
} |
} |
first_message = 0; | send_msg(MSG_ERROR_EXIT, "", 0, 0); |
| io_flush(FULL_FLUSH); |
} |
} |
break; | } else if (protocol_version >= 31) { |
default: | if (am_generator || am_receiver) { |
rprintf(FERROR, "unexpected tag %d [%s]\n", | if (DEBUG_GTE(EXIT, 3)) { |
tag, who_am_i()); | rprintf(FINFO, "[%s] sending MSG_ERROR_EXIT with exit_code %d\n", |
exit_cleanup(RERR_STREAMIO); | who_am_i(), val); |
| } |
| send_msg_int(MSG_ERROR_EXIT, val); |
| } else { |
| if (DEBUG_GTE(EXIT, 3)) { |
| rprintf(FINFO, "[%s] sending MSG_ERROR_EXIT (len 0)\n", |
| who_am_i()); |
| } |
| send_msg(MSG_ERROR_EXIT, "", 0, 0); |
| } |
} |
} |
|
/* Send a negative linenum so that we don't end up |
|
* with a duplicate exit message. */ |
|
_exit_cleanup(val, __FILE__, 0 - __LINE__); |
|
default: |
|
unexpected: |
|
rprintf(FERROR, "unexpected tag %d [%s%s]\n", |
|
tag, who_am_i(), inc_recurse ? "/inc" : ""); |
|
exit_cleanup(RERR_STREAMIO); |
} |
} |
|
|
if (iobuf_in_remaining == 0) | assert(iobuf.in_multiplexed > 0); |
io_flush(NORMAL_FLUSH); | |
| |
return cnt; | |
} |
} |
|
|
/* Do a buffered read from fd. Don't return until all N bytes have | static void drain_multiplex_messages(void) |
* been read. If all N can't be read then exit with an error. */ | |
static void readfd(int fd, char *buffer, size_t N) | |
{ |
{ |
int cnt; | while (IN_MULTIPLEXED_AND_READY && iobuf.in.len) { |
size_t total = 0; | if (iobuf.raw_input_ends_before) { |
| size_t raw_len = iobuf.raw_input_ends_before - iobuf.in.pos; |
while (total < N) { | iobuf.raw_input_ends_before = 0; |
cnt = readfd_unbuffered(fd, buffer + total, N-total); | if (raw_len >= iobuf.in.len) { |
total += cnt; | iobuf.in.len = 0; |
| break; |
| } |
| iobuf.in.len -= raw_len; |
| if ((iobuf.in.pos += raw_len) >= iobuf.in.size) |
| iobuf.in.pos -= iobuf.in.size; |
| } |
| read_a_msg(); |
} |
} |
|
} |
|
|
if (fd == write_batch_monitor_in) { | void wait_for_receiver(void) |
if ((size_t)write(batch_fd, buffer, total) != total) | { |
exit_cleanup(RERR_FILEIO); | if (!iobuf.raw_input_ends_before) |
} | read_a_msg(); |
|
|
if (fd == flist_forward_from) | if (iobuf.raw_input_ends_before) { |
writefd(iobuf_f_out, buffer, total); | int ndx = read_int(iobuf.in_fd); |
| if (ndx < 0) { |
if (fd == sock_f_in) | switch (ndx) { |
stats.total_read += total; | case NDX_FLIST_EOF: |
| flist_eof = 1; |
| if (DEBUG_GTE(FLIST, 3)) |
| rprintf(FINFO, "[%s] flist_eof=1\n", who_am_i()); |
| break; |
| case NDX_DONE: |
| msgdone_cnt++; |
| break; |
| default: |
| exit_cleanup(RERR_STREAMIO); |
| } |
| } else { |
| struct file_list *flist; |
| flist_receiving_enabled = False; |
| if (DEBUG_GTE(FLIST, 2)) { |
| rprintf(FINFO, "[%s] receiving flist for dir %d\n", |
| who_am_i(), ndx); |
| } |
| flist = recv_file_list(iobuf.in_fd, ndx); |
| flist->parent_ndx = ndx; |
| #ifdef SUPPORT_HARD_LINKS |
| if (preserve_hard_links) |
| match_hard_links(flist); |
| #endif |
| flist_receiving_enabled = True; |
| } |
| } |
} |
} |
|
|
unsigned short read_shortint(int f) |
unsigned short read_shortint(int f) |
{ |
{ |
char b[2]; |
char b[2]; |
readfd(f, b, 2); | read_buf(f, b, 2); |
return (UVAL(b, 1) << 8) + UVAL(b, 0); |
return (UVAL(b, 1) << 8) + UVAL(b, 0); |
} |
} |
|
|
Line 1184 int32 read_int(int f)
|
Line 1752 int32 read_int(int f)
|
char b[4]; |
char b[4]; |
int32 num; |
int32 num; |
|
|
readfd(f, b, 4); | read_buf(f, b, 4); |
num = IVAL(b, 0); |
num = IVAL(b, 0); |
#if SIZEOF_INT32 > 4 |
#if SIZEOF_INT32 > 4 |
if (num & (int32)0x80000000) |
if (num & (int32)0x80000000) |
Line 1196 int32 read_int(int f)
|
Line 1764 int32 read_int(int f)
|
int32 read_varint(int f) |
int32 read_varint(int f) |
{ |
{ |
union { |
union { |
char b[5]; | char b[5]; |
int32 x; | int32 x; |
} u; |
} u; |
uchar ch; |
uchar ch; |
int extra; |
int extra; |
|
|
u.x = 0; |
u.x = 0; |
readfd(f, (char*)&ch, 1); | ch = read_byte(f); |
extra = int_byte_extra[ch / 4]; |
extra = int_byte_extra[ch / 4]; |
if (extra) { |
if (extra) { |
uchar bit = ((uchar)1<<(8-extra)); |
uchar bit = ((uchar)1<<(8-extra)); |
Line 1211 int32 read_varint(int f)
|
Line 1779 int32 read_varint(int f)
|
rprintf(FERROR, "Overflow in read_varint()\n"); |
rprintf(FERROR, "Overflow in read_varint()\n"); |
exit_cleanup(RERR_STREAMIO); |
exit_cleanup(RERR_STREAMIO); |
} |
} |
readfd(f, u.b, extra); | read_buf(f, u.b, extra); |
u.b[extra] = ch & (bit-1); |
u.b[extra] = ch & (bit-1); |
} else |
} else |
u.b[0] = ch; |
u.b[0] = ch; |
Line 1228 int32 read_varint(int f)
|
Line 1796 int32 read_varint(int f)
|
int64 read_varlong(int f, uchar min_bytes) |
int64 read_varlong(int f, uchar min_bytes) |
{ |
{ |
union { |
union { |
char b[9]; | char b[9]; |
int64 x; | int64 x; |
} u; |
} u; |
char b2[8]; |
char b2[8]; |
int extra; |
int extra; |
Line 1239 int64 read_varlong(int f, uchar min_bytes)
|
Line 1807 int64 read_varlong(int f, uchar min_bytes)
|
#else |
#else |
u.x = 0; |
u.x = 0; |
#endif |
#endif |
readfd(f, b2, min_bytes); | read_buf(f, b2, min_bytes); |
memcpy(u.b, b2+1, min_bytes-1); |
memcpy(u.b, b2+1, min_bytes-1); |
extra = int_byte_extra[CVAL(b2, 0) / 4]; |
extra = int_byte_extra[CVAL(b2, 0) / 4]; |
if (extra) { |
if (extra) { |
Line 1248 int64 read_varlong(int f, uchar min_bytes)
|
Line 1816 int64 read_varlong(int f, uchar min_bytes)
|
rprintf(FERROR, "Overflow in read_varlong()\n"); |
rprintf(FERROR, "Overflow in read_varlong()\n"); |
exit_cleanup(RERR_STREAMIO); |
exit_cleanup(RERR_STREAMIO); |
} |
} |
readfd(f, u.b + min_bytes - 1, extra); | read_buf(f, u.b + min_bytes - 1, extra); |
u.b[min_bytes + extra - 1] = CVAL(b2, 0) & (bit-1); |
u.b[min_bytes + extra - 1] = CVAL(b2, 0) & (bit-1); |
#if SIZEOF_INT64 < 8 |
#if SIZEOF_INT64 < 8 |
if (min_bytes + extra > 5 || u.b[4] || CVAL(u.b,3) & 0x80) { |
if (min_bytes + extra > 5 || u.b[4] || CVAL(u.b,3) & 0x80) { |
Line 1261 int64 read_varlong(int f, uchar min_bytes)
|
Line 1829 int64 read_varlong(int f, uchar min_bytes)
|
#if SIZEOF_INT64 < 8 |
#if SIZEOF_INT64 < 8 |
u.x = IVAL(u.b,0); |
u.x = IVAL(u.b,0); |
#elif CAREFUL_ALIGNMENT |
#elif CAREFUL_ALIGNMENT |
u.x = IVAL(u.b,0) | (((int64)IVAL(u.b,4))<<32); | u.x = IVAL64(u.b,0); |
#endif |
#endif |
return u.x; |
return u.x; |
} |
} |
Line 1280 int64 read_longint(int f)
|
Line 1848 int64 read_longint(int f)
|
rprintf(FERROR, "Integer overflow: attempted 64-bit offset\n"); |
rprintf(FERROR, "Integer overflow: attempted 64-bit offset\n"); |
exit_cleanup(RERR_UNSUPPORTED); |
exit_cleanup(RERR_UNSUPPORTED); |
#else |
#else |
readfd(f, b, 8); | read_buf(f, b, 8); |
return IVAL(b,0) | (((int64)IVAL(b,4))<<32); |
return IVAL(b,0) | (((int64)IVAL(b,4))<<32); |
#endif |
#endif |
} |
} |
|
|
void read_buf(int f, char *buf, size_t len) |
void read_buf(int f, char *buf, size_t len) |
{ |
{ |
readfd(f,buf,len); | if (f != iobuf.in_fd) { |
| if (safe_read(f, buf, len) != len) |
| whine_about_eof(False); /* Doesn't return. */ |
| goto batch_copy; |
| } |
| |
| if (!IN_MULTIPLEXED) { |
| raw_read_buf(buf, len); |
| total_data_read += len; |
| if (forward_flist_data) |
| write_buf(iobuf.out_fd, buf, len); |
| batch_copy: |
| if (f == write_batch_monitor_in) |
| safe_write(batch_fd, buf, len); |
| return; |
| } |
| |
| while (1) { |
| size_t siz; |
| |
| while (!iobuf.raw_input_ends_before) |
| read_a_msg(); |
| |
| siz = MIN(len, iobuf.raw_input_ends_before - iobuf.in.pos); |
| if (siz >= iobuf.in.size) |
| siz = iobuf.in.size; |
| raw_read_buf(buf, siz); |
| total_data_read += siz; |
| |
| if (forward_flist_data) |
| write_buf(iobuf.out_fd, buf, siz); |
| |
| if (f == write_batch_monitor_in) |
| safe_write(batch_fd, buf, siz); |
| |
| if ((len -= siz) == 0) |
| break; |
| buf += siz; |
| } |
} |
} |
|
|
void read_sbuf(int f, char *buf, size_t len) |
void read_sbuf(int f, char *buf, size_t len) |
{ |
{ |
readfd(f, buf, len); | read_buf(f, buf, len); |
buf[len] = '\0'; |
buf[len] = '\0'; |
} |
} |
|
|
uchar read_byte(int f) |
uchar read_byte(int f) |
{ |
{ |
uchar c; |
uchar c; |
readfd(f, (char *)&c, 1); | read_buf(f, (char*)&c, 1); |
return c; |
return c; |
} |
} |
|
|
Line 1317 int read_vstring(int f, char *buf, int bufsize)
|
Line 1923 int read_vstring(int f, char *buf, int bufsize)
|
} |
} |
|
|
if (len) |
if (len) |
readfd(f, buf, len); | read_buf(f, buf, len); |
buf[len] = '\0'; |
buf[len] = '\0'; |
return len; |
return len; |
} |
} |
Line 1370 void write_sum_head(int f, struct sum_struct *sum)
|
Line 1976 void write_sum_head(int f, struct sum_struct *sum)
|
write_int(f, sum->remainder); |
write_int(f, sum->remainder); |
} |
} |
|
|
/** | /* Sleep after writing to limit I/O bandwidth usage. |
* Sleep after writing to limit I/O bandwidth usage. | |
* |
* |
* @todo Rather than sleeping after each write, it might be better to |
* @todo Rather than sleeping after each write, it might be better to |
* use some kind of averaging. The current algorithm seems to always |
* use some kind of averaging. The current algorithm seems to always |
Line 1384 void write_sum_head(int f, struct sum_struct *sum)
|
Line 1989 void write_sum_head(int f, struct sum_struct *sum)
|
* round small waits up to guarantee that we actually wait at least the |
* round small waits up to guarantee that we actually wait at least the |
* requested number of microseconds, this can become grossly inaccurate. |
* requested number of microseconds, this can become grossly inaccurate. |
* We therefore keep track of the bytes we've written over time and only |
* We therefore keep track of the bytes we've written over time and only |
* sleep when the accumulated delay is at least 1 tenth of a second. | * sleep when the accumulated delay is at least 1 tenth of a second. */ |
**/ | |
static void sleep_for_bwlimit(int bytes_written) |
static void sleep_for_bwlimit(int bytes_written) |
{ |
{ |
static struct timeval prior_tv; |
static struct timeval prior_tv; |
Line 1395 static void sleep_for_bwlimit(int bytes_written)
|
Line 1999 static void sleep_for_bwlimit(int bytes_written)
|
|
|
#define ONE_SEC 1000000L /* # of microseconds in a second */ |
#define ONE_SEC 1000000L /* # of microseconds in a second */ |
|
|
if (!bwlimit_writemax) |
|
return; |
|
|
|
total_written += bytes_written; |
total_written += bytes_written; |
|
|
gettimeofday(&start_tv, NULL); |
gettimeofday(&start_tv, NULL); |
Line 1425 static void sleep_for_bwlimit(int bytes_written)
|
Line 2026 static void sleep_for_bwlimit(int bytes_written)
|
total_written = (sleep_usec - elapsed_usec) * bwlimit / (ONE_SEC/1024); |
total_written = (sleep_usec - elapsed_usec) * bwlimit / (ONE_SEC/1024); |
} |
} |
|
|
static const char *what_fd_is(int fd) | void io_flush(int flush_type) |
{ |
{ |
static char buf[20]; | if (iobuf.out.len > iobuf.out_empty_len) { |
| if (flush_type == FULL_FLUSH) /* flush everything in the output buffers */ |
if (fd == sock_f_out) | perform_io(iobuf.out.size - iobuf.out_empty_len, PIO_NEED_OUTROOM); |
return "socket"; | else if (flush_type == NORMAL_FLUSH) /* flush at least 1 byte */ |
else if (fd == msg_fd_out) | perform_io(iobuf.out.size - iobuf.out.len + 1, PIO_NEED_OUTROOM); |
return "message fd"; | /* MSG_FLUSH: flush iobuf.msg only */ |
else if (fd == batch_fd) | |
return "batch file"; | |
else { | |
snprintf(buf, sizeof buf, "fd %d", fd); | |
return buf; | |
} |
} |
|
if (iobuf.msg.len) |
|
perform_io(iobuf.msg.size, PIO_NEED_MSGROOM); |
} |
} |
|
|
/* Write len bytes to the file descriptor fd, looping as necessary to get |
|
* the job done and also (in certain circumstances) reading any data on |
|
* msg_fd_in to avoid deadlock. |
|
* |
|
* This function underlies the multiplexing system. The body of the |
|
* application never calls this function directly. */ |
|
static void writefd_unbuffered(int fd, const char *buf, size_t len) |
|
{ |
|
size_t n, total = 0; |
|
fd_set w_fds, r_fds, e_fds; |
|
int maxfd, count, cnt, using_r_fds; |
|
int defer_inc = 0; |
|
struct timeval tv; |
|
|
|
if (no_flush++) |
|
defer_forwarding_messages++, defer_inc++; |
|
|
|
while (total < len) { |
|
FD_ZERO(&w_fds); |
|
FD_SET(fd, &w_fds); |
|
FD_ZERO(&e_fds); |
|
FD_SET(fd, &e_fds); |
|
maxfd = fd; |
|
|
|
if (msg_fd_in >= 0) { |
|
FD_ZERO(&r_fds); |
|
FD_SET(msg_fd_in, &r_fds); |
|
if (msg_fd_in > maxfd) |
|
maxfd = msg_fd_in; |
|
using_r_fds = 1; |
|
} else |
|
using_r_fds = 0; |
|
|
|
tv.tv_sec = select_timeout; |
|
tv.tv_usec = 0; |
|
|
|
errno = 0; |
|
count = select(maxfd + 1, using_r_fds ? &r_fds : NULL, |
|
&w_fds, &e_fds, &tv); |
|
|
|
if (count <= 0) { |
|
if (count < 0 && errno == EBADF) |
|
exit_cleanup(RERR_SOCKETIO); |
|
check_timeout(); |
|
continue; |
|
} |
|
|
|
/*if (FD_ISSET(fd, &e_fds)) |
|
rprintf(FINFO, "select exception on fd %d\n", fd); */ |
|
|
|
if (using_r_fds && FD_ISSET(msg_fd_in, &r_fds)) |
|
read_msg_fd(); |
|
|
|
if (!FD_ISSET(fd, &w_fds)) |
|
continue; |
|
|
|
n = len - total; |
|
if (bwlimit_writemax && n > bwlimit_writemax) |
|
n = bwlimit_writemax; |
|
cnt = write(fd, buf + total, n); |
|
|
|
if (cnt <= 0) { |
|
if (cnt < 0) { |
|
if (errno == EINTR) |
|
continue; |
|
if (errno == EWOULDBLOCK || errno == EAGAIN) { |
|
msleep(1); |
|
continue; |
|
} |
|
} |
|
|
|
/* Don't try to write errors back across the stream. */ |
|
if (fd == sock_f_out) |
|
io_end_multiplex_out(); |
|
/* Don't try to write errors down a failing msg pipe. */ |
|
if (am_server && fd == msg_fd_out) |
|
exit_cleanup(RERR_STREAMIO); |
|
rsyserr(FERROR, errno, |
|
"writefd_unbuffered failed to write %ld bytes to %s [%s]", |
|
(long)len, what_fd_is(fd), who_am_i()); |
|
/* If the other side is sending us error messages, try |
|
* to grab any messages they sent before they died. */ |
|
while (!am_server && fd == sock_f_out && io_multiplexing_in) { |
|
char buf[1024]; |
|
set_io_timeout(30); |
|
ignore_timeout = 0; |
|
readfd_unbuffered(sock_f_in, buf, sizeof buf); |
|
} |
|
exit_cleanup(RERR_STREAMIO); |
|
} |
|
|
|
total += cnt; |
|
defer_forwarding_messages++, defer_inc++; |
|
|
|
if (fd == sock_f_out) { |
|
if (io_timeout || am_generator) |
|
last_io_out = time(NULL); |
|
sleep_for_bwlimit(cnt); |
|
} |
|
} |
|
|
|
no_flush--; |
|
if (keep_defer_forwarding) |
|
defer_inc--; |
|
if (!(defer_forwarding_messages -= defer_inc) && !no_flush) |
|
msg_flush(); |
|
} |
|
|
|
int io_flush(int flush_it_all) |
|
{ |
|
int flushed_something = 0; |
|
|
|
if (no_flush) |
|
return 0; |
|
|
|
if (iobuf_out_cnt) { |
|
if (io_multiplexing_out) |
|
mplex_write(sock_f_out, MSG_DATA, iobuf_out, iobuf_out_cnt, 0); |
|
else |
|
writefd_unbuffered(iobuf_f_out, iobuf_out, iobuf_out_cnt); |
|
iobuf_out_cnt = 0; |
|
flushed_something = 1; |
|
} |
|
|
|
if (flush_it_all && !defer_forwarding_messages && msg_queue.head) { |
|
msg_flush(); |
|
flushed_something = 1; |
|
} |
|
|
|
return flushed_something; |
|
} |
|
|
|
static void writefd(int fd, const char *buf, size_t len) |
|
{ |
|
if (fd == sock_f_out) |
|
stats.total_written += len; |
|
|
|
if (fd == write_batch_monitor_out) |
|
writefd_unbuffered(batch_fd, buf, len); |
|
|
|
if (!iobuf_out || fd != iobuf_f_out) { |
|
writefd_unbuffered(fd, buf, len); |
|
return; |
|
} |
|
|
|
while (len) { |
|
int n = MIN((int)len, IO_BUFFER_SIZE - iobuf_out_cnt); |
|
if (n > 0) { |
|
memcpy(iobuf_out+iobuf_out_cnt, buf, n); |
|
buf += n; |
|
len -= n; |
|
iobuf_out_cnt += n; |
|
} |
|
|
|
if (iobuf_out_cnt == IO_BUFFER_SIZE) |
|
io_flush(NORMAL_FLUSH); |
|
} |
|
} |
|
|
|
void write_shortint(int f, unsigned short x) |
void write_shortint(int f, unsigned short x) |
{ |
{ |
char b[2]; |
char b[2]; |
b[0] = (char)x; |
b[0] = (char)x; |
b[1] = (char)(x >> 8); |
b[1] = (char)(x >> 8); |
writefd(f, b, 2); | write_buf(f, b, 2); |
} |
} |
|
|
void write_int(int f, int32 x) |
void write_int(int f, int32 x) |
{ |
{ |
char b[4]; |
char b[4]; |
SIVAL(b, 0, x); |
SIVAL(b, 0, x); |
writefd(f, b, 4); | write_buf(f, b, 4); |
} |
} |
|
|
void write_varint(int f, int32 x) |
void write_varint(int f, int32 x) |
{ |
{ |
char b[5]; |
char b[5]; |
uchar bit; |
uchar bit; |
int cnt = 4; | int cnt; |
|
|
SIVAL(b, 1, x); |
SIVAL(b, 1, x); |
|
|
while (cnt > 1 && b[cnt] == 0) | for (cnt = 4; cnt > 1 && b[cnt] == 0; cnt--) {} |
cnt--; | |
bit = ((uchar)1<<(7-cnt+1)); |
bit = ((uchar)1<<(7-cnt+1)); |
|
|
if (CVAL(b, cnt) >= bit) { |
if (CVAL(b, cnt) >= bit) { |
cnt++; |
cnt++; |
*b = ~(bit-1); |
*b = ~(bit-1); |
} else if (cnt > 1) |
} else if (cnt > 1) |
*b = b[cnt] | ~(bit*2-1); |
*b = b[cnt] | ~(bit*2-1); |
else |
else |
*b = b[cnt]; | *b = b[1]; |
|
|
writefd(f, b, cnt); | write_buf(f, b, cnt); |
} |
} |
|
|
void write_varlong(int f, int64 x, uchar min_bytes) |
void write_varlong(int f, int64 x, uchar min_bytes) |
Line 1643 void write_varlong(int f, int64 x, uchar min_bytes)
|
Line 2082 void write_varlong(int f, int64 x, uchar min_bytes)
|
uchar bit; |
uchar bit; |
int cnt = 8; |
int cnt = 8; |
|
|
SIVAL(b, 1, x); |
|
#if SIZEOF_INT64 >= 8 |
#if SIZEOF_INT64 >= 8 |
SIVAL(b, 5, x >> 32); | SIVAL64(b, 1, x); |
#else |
#else |
|
SIVAL(b, 1, x); |
if (x <= 0x7FFFFFFF && x >= 0) |
if (x <= 0x7FFFFFFF && x >= 0) |
memset(b + 5, 0, 4); |
memset(b + 5, 0, 4); |
else { |
else { |
Line 1666 void write_varlong(int f, int64 x, uchar min_bytes)
|
Line 2105 void write_varlong(int f, int64 x, uchar min_bytes)
|
else |
else |
*b = b[cnt]; |
*b = b[cnt]; |
|
|
writefd(f, b, cnt); | write_buf(f, b, cnt); |
} |
} |
|
|
/* |
/* |
Line 1679 void write_longint(int f, int64 x)
|
Line 2118 void write_longint(int f, int64 x)
|
|
|
SIVAL(s, 0, x); |
SIVAL(s, 0, x); |
if (x <= 0x7FFFFFFF && x >= 0) { |
if (x <= 0x7FFFFFFF && x >= 0) { |
writefd(f, s, 4); | write_buf(f, s, 4); |
return; |
return; |
} |
} |
|
|
Line 1689 void write_longint(int f, int64 x)
|
Line 2128 void write_longint(int f, int64 x)
|
#else |
#else |
memset(b, 0xFF, 4); |
memset(b, 0xFF, 4); |
SIVAL(s, 4, x >> 32); |
SIVAL(s, 4, x >> 32); |
writefd(f, b, 12); | write_buf(f, b, 12); |
#endif |
#endif |
} |
} |
|
|
|
void write_bigbuf(int f, const char *buf, size_t len) |
|
{ |
|
size_t half_max = (iobuf.out.size - iobuf.out_empty_len) / 2; |
|
|
|
while (len > half_max + 1024) { |
|
write_buf(f, buf, half_max); |
|
buf += half_max; |
|
len -= half_max; |
|
} |
|
|
|
write_buf(f, buf, len); |
|
} |
|
|
void write_buf(int f, const char *buf, size_t len) |
void write_buf(int f, const char *buf, size_t len) |
{ |
{ |
writefd(f,buf,len); | size_t pos, siz; |
| |
| if (f != iobuf.out_fd) { |
| safe_write(f, buf, len); |
| goto batch_copy; |
| } |
| |
| if (iobuf.out.len + len > iobuf.out.size) |
| perform_io(len, PIO_NEED_OUTROOM); |
| |
| pos = iobuf.out.pos + iobuf.out.len; /* Must be set after any flushing. */ |
| if (pos >= iobuf.out.size) |
| pos -= iobuf.out.size; |
| |
| /* Handle a split copy if we wrap around the end of the circular buffer. */ |
| if (pos >= iobuf.out.pos && (siz = iobuf.out.size - pos) < len) { |
| memcpy(iobuf.out.buf + pos, buf, siz); |
| memcpy(iobuf.out.buf, buf + siz, len - siz); |
| } else |
| memcpy(iobuf.out.buf + pos, buf, len); |
| |
| iobuf.out.len += len; |
| total_data_written += len; |
| |
| batch_copy: |
| if (f == write_batch_monitor_out) |
| safe_write(batch_fd, buf, len); |
} |
} |
|
|
/** Write a string to the connection */ | /* Write a string to the connection */ |
void write_sbuf(int f, const char *buf) |
void write_sbuf(int f, const char *buf) |
{ |
{ |
writefd(f, buf, strlen(buf)); | write_buf(f, buf, strlen(buf)); |
} |
} |
|
|
void write_byte(int f, uchar c) |
void write_byte(int f, uchar c) |
{ |
{ |
writefd(f, (char *)&c, 1); | write_buf(f, (char *)&c, 1); |
} |
} |
|
|
void write_vstring(int f, const char *str, int len) |
void write_vstring(int f, const char *str, int len) |
Line 1724 void write_vstring(int f, const char *str, int len)
|
Line 2202 void write_vstring(int f, const char *str, int len)
|
} |
} |
*lb = len; |
*lb = len; |
|
|
writefd(f, (char*)lenbuf, lb - lenbuf + 1); | write_buf(f, (char*)lenbuf, lb - lenbuf + 1); |
if (len) |
if (len) |
writefd(f, str, len); | write_buf(f, str, len); |
} |
} |
|
|
/* Send a file-list index using a byte-reduction method. */ |
/* Send a file-list index using a byte-reduction method. */ |
Line 1748 void write_ndx(int f, int32 ndx)
|
Line 2226 void write_ndx(int f, int32 ndx)
|
prev_positive = ndx; |
prev_positive = ndx; |
} else if (ndx == NDX_DONE) { |
} else if (ndx == NDX_DONE) { |
*b = 0; |
*b = 0; |
writefd(f, b, 1); | write_buf(f, b, 1); |
return; |
return; |
} else { |
} else { |
b[cnt++] = (char)0xFF; |
b[cnt++] = (char)0xFF; |
Line 1773 void write_ndx(int f, int32 ndx)
|
Line 2251 void write_ndx(int f, int32 ndx)
|
b[cnt++] = (char)(diff >> 8); |
b[cnt++] = (char)(diff >> 8); |
b[cnt++] = (char)diff; |
b[cnt++] = (char)diff; |
} |
} |
writefd(f, b, cnt); | write_buf(f, b, cnt); |
} |
} |
|
|
/* Receive a file-list index using a byte-reduction method. */ |
/* Receive a file-list index using a byte-reduction method. */ |
Line 1786 int32 read_ndx(int f)
|
Line 2264 int32 read_ndx(int f)
|
if (protocol_version < 30) |
if (protocol_version < 30) |
return read_int(f); |
return read_int(f); |
|
|
readfd(f, b, 1); | read_buf(f, b, 1); |
if (CVAL(b, 0) == 0xFF) { |
if (CVAL(b, 0) == 0xFF) { |
readfd(f, b, 1); | read_buf(f, b, 1); |
prev_ptr = &prev_negative; |
prev_ptr = &prev_negative; |
} else if (CVAL(b, 0) == 0) |
} else if (CVAL(b, 0) == 0) |
return NDX_DONE; |
return NDX_DONE; |
else |
else |
prev_ptr = &prev_positive; |
prev_ptr = &prev_positive; |
if (CVAL(b, 0) == 0xFE) { |
if (CVAL(b, 0) == 0xFE) { |
readfd(f, b, 2); | read_buf(f, b, 2); |
if (CVAL(b, 0) & 0x80) { |
if (CVAL(b, 0) & 0x80) { |
b[3] = CVAL(b, 0) & ~0x80; |
b[3] = CVAL(b, 0) & ~0x80; |
b[0] = b[1]; |
b[0] = b[1]; |
readfd(f, b+1, 2); | read_buf(f, b+1, 2); |
num = IVAL(b, 0); |
num = IVAL(b, 0); |
} else |
} else |
num = (UVAL(b,0)<<8) + UVAL(b,1) + *prev_ptr; |
num = (UVAL(b,0)<<8) + UVAL(b,1) + *prev_ptr; |
Line 1814 int32 read_ndx(int f)
|
Line 2292 int32 read_ndx(int f)
|
/* Read a line of up to bufsiz-1 characters into buf. Strips |
/* Read a line of up to bufsiz-1 characters into buf. Strips |
* the (required) trailing newline and all carriage returns. |
* the (required) trailing newline and all carriage returns. |
* Returns 1 for success; 0 for I/O error or truncation. */ |
* Returns 1 for success; 0 for I/O error or truncation. */ |
int read_line_old(int f, char *buf, size_t bufsiz) | int read_line_old(int fd, char *buf, size_t bufsiz, int eof_ok) |
{ |
{ |
|
assert(fd != iobuf.in_fd); |
bufsiz--; /* leave room for the null */ |
bufsiz--; /* leave room for the null */ |
while (bufsiz > 0) { |
while (bufsiz > 0) { |
buf[0] = 0; | if (safe_read(fd, buf, 1) == 0) { |
read_buf(f, buf, 1); | if (eof_ok) |
if (buf[0] == 0) | break; |
return 0; |
return 0; |
if (buf[0] == '\n') | } |
| if (*buf == '\0') |
| return 0; |
| if (*buf == '\n') |
break; |
break; |
if (buf[0] != '\r') { | if (*buf != '\r') { |
buf++; |
buf++; |
bufsiz--; |
bufsiz--; |
} |
} |
Line 1844 void io_printf(int fd, const char *format, ...)
|
Line 2326 void io_printf(int fd, const char *format, ...)
|
va_end(ap); |
va_end(ap); |
|
|
if (len < 0) |
if (len < 0) |
exit_cleanup(RERR_STREAMIO); | exit_cleanup(RERR_PROTOCOL); |
|
|
if (len > (int)sizeof buf) { | if (len >= (int)sizeof buf) { |
rprintf(FERROR, "io_printf() was too long for the buffer.\n"); |
rprintf(FERROR, "io_printf() was too long for the buffer.\n"); |
exit_cleanup(RERR_STREAMIO); | exit_cleanup(RERR_PROTOCOL); |
} |
} |
|
|
write_sbuf(fd, buf); |
write_sbuf(fd, buf); |
} |
} |
|
|
/** Setup for multiplexing a MSG_* stream with the data stream. */ | /* Setup for multiplexing a MSG_* stream with the data stream. */ |
void io_start_multiplex_out(void) | void io_start_multiplex_out(int fd) |
{ |
{ |
io_flush(NORMAL_FLUSH); | io_flush(FULL_FLUSH); |
io_start_buffering_out(sock_f_out); | |
io_multiplexing_out = 1; | |
} | |
|
|
/** Setup for multiplexing a MSG_* stream with the data stream. */ | if (msgs2stderr == 1 && DEBUG_GTE(IO, 2)) |
void io_start_multiplex_in(void) | rprintf(FINFO, "[%s] io_start_multiplex_out(%d)\n", who_am_i(), fd); |
{ | |
io_flush(NORMAL_FLUSH); | if (!iobuf.msg.buf) |
io_start_buffering_in(sock_f_in); | alloc_xbuf(&iobuf.msg, ROUND_UP_1024(IO_BUFFER_SIZE)); |
io_multiplexing_in = 1; | |
| iobuf.out_empty_len = 4; /* See also OUT_MULTIPLEXED */ |
| io_start_buffering_out(fd); |
| got_kill_signal = 0; |
| |
| iobuf.raw_data_header_pos = iobuf.out.pos + iobuf.out.len; |
| iobuf.out.len += 4; |
} |
} |
|
|
/** Write an message to the multiplexed data stream. */ | /* Setup for multiplexing a MSG_* stream with the data stream. */ |
int io_multiplex_write(enum msgcode code, const char *buf, size_t len, int convert) | void io_start_multiplex_in(int fd) |
{ |
{ |
if (!io_multiplexing_out) | if (msgs2stderr == 1 && DEBUG_GTE(IO, 2)) |
return 0; | rprintf(FINFO, "[%s] io_start_multiplex_in(%d)\n", who_am_i(), fd); |
io_flush(NORMAL_FLUSH); | |
stats.total_written += (len+4); | iobuf.in_multiplexed = 1; /* See also IN_MULTIPLEXED */ |
mplex_write(sock_f_out, code, buf, len, convert); | io_start_buffering_in(fd); |
return 1; | |
} |
} |
|
|
void io_end_multiplex_in(void) | int io_end_multiplex_in(int mode) |
{ |
{ |
io_multiplexing_in = 0; | int ret = iobuf.in_multiplexed ? iobuf.in_fd : -1; |
io_end_buffering_in(); | |
| if (msgs2stderr == 1 && DEBUG_GTE(IO, 2)) |
| rprintf(FINFO, "[%s] io_end_multiplex_in(mode=%d)\n", who_am_i(), mode); |
| |
| iobuf.in_multiplexed = 0; |
| if (mode == MPLX_SWITCHING) |
| iobuf.raw_input_ends_before = 0; |
| else |
| assert(iobuf.raw_input_ends_before == 0); |
| if (mode != MPLX_TO_BUFFERED) |
| io_end_buffering_in(mode); |
| |
| return ret; |
} |
} |
|
|
/** Stop output multiplexing. */ | int io_end_multiplex_out(int mode) |
void io_end_multiplex_out(void) | |
{ |
{ |
io_multiplexing_out = 0; | int ret = iobuf.out_empty_len ? iobuf.out_fd : -1; |
io_end_buffering_out(); | |
| if (msgs2stderr == 1 && DEBUG_GTE(IO, 2)) |
| rprintf(FINFO, "[%s] io_end_multiplex_out(mode=%d)\n", who_am_i(), mode); |
| |
| if (mode != MPLX_TO_BUFFERED) |
| io_end_buffering_out(mode); |
| else |
| io_flush(FULL_FLUSH); |
| |
| iobuf.out.len = 0; |
| iobuf.out_empty_len = 0; |
| if (got_kill_signal > 0) /* Just in case... */ |
| handle_kill_signal(False); |
| got_kill_signal = -1; |
| |
| return ret; |
} |
} |
|
|
void start_write_batch(int fd) |
void start_write_batch(int fd) |
Line 1903 void start_write_batch(int fd)
|
Line 2413 void start_write_batch(int fd)
|
* is involved. */ |
* is involved. */ |
write_int(batch_fd, protocol_version); |
write_int(batch_fd, protocol_version); |
if (protocol_version >= 30) |
if (protocol_version >= 30) |
write_byte(batch_fd, compat_flags); | write_varint(batch_fd, compat_flags); |
write_int(batch_fd, checksum_seed); |
write_int(batch_fd, checksum_seed); |
|
|
if (am_sender) |
if (am_sender) |