static int buffer_f_in = -1;
static int io_error_fd = -1;
+static void read_loop(int fd, char *buf, int len);
+
void setup_readbuffer(int f_in)
{
buffer_f_in = f_in;
io_error_fd = fd;
}
-/* read some data from the error fd and write it to FERROR */
+/* read some data from the error fd and write it to the write log code */
static void read_error_fd(void)
{
char buf[200];
int n;
int fd = io_error_fd;
+ int tag, len;
+
io_error_fd = -1;
- n = read(fd, buf, sizeof(buf)-1);
- if (n > 0) {
- rwrite(FERROR, buf, n);
+ read_loop(fd, buf, 4);
+ tag = IVAL(buf, 0);
+
+ len = tag & 0xFFFFFF;
+ tag = tag >> 24;
+ tag -= MPLEX_BASE;
+
+ while (len) {
+ n = len;
+ if (n > (sizeof(buf)-1)) n = sizeof(buf)-1;
+ read_loop(fd, buf, n);
+ rwrite((enum logcode)tag, buf, n);
+ len -= n;
}
io_error_fd = fd;
static int read_unbuffered(int fd, char *buf, int len)
{
static int remaining;
- char ibuf[4];
int tag, ret=0;
char line[1024];
continue;
}
- read_loop(fd, ibuf, 4);
- tag = IVAL(ibuf, 0);
+ read_loop(fd, line, 4);
+ tag = IVAL(line, 0);
remaining = tag & 0xFFFFFF;
tag = tag >> 24;
read_loop(fd, line, remaining);
line[remaining] = 0;
- rprintf(tag,"%s", line);
+ rprintf((enum logcode)tag,"%s", line);
remaining = 0;
}
{
if (io_buffer) return;
multiplex_out_fd = fd;
- io_buffer = (char *)malloc(IO_BUFFER_SIZE+4);
+ io_buffer = (char *)malloc(IO_BUFFER_SIZE);
if (!io_buffer) out_of_memory("writefd");
io_buffer_count = 0;
+}
+
+/* write an message to a multiplexed stream. If this fails then rsync
+ exits */
+static void mplex_write(int fd, enum logcode code, char *buf, int len)
+{
+ char buffer[4096];
+ int n = len;
- /* leave room for the multiplex header in case it's needed */
- io_buffer += 4;
+ SIVAL(buffer, 0, ((MPLEX_BASE + (int)code)<<24) + len);
+
+ if (n > (sizeof(buf)-4)) {
+ n = sizeof(buf)-4;
+ }
+
+ memcpy(&buffer[4], buf, n);
+ writefd_unbuffered(fd, buffer, n+4);
+
+ len -= n;
+ buf += n;
+
+ writefd_unbuffered(fd, buf, len);
}
+
void io_flush(void)
{
int fd = multiplex_out_fd;
if (!io_buffer_count || no_flush) return;
if (io_multiplexing_out) {
- SIVAL(io_buffer-4, 0, (MPLEX_BASE<<24) + io_buffer_count);
- writefd_unbuffered(fd, io_buffer-4, io_buffer_count+4);
+ mplex_write(fd, 0, io_buffer, io_buffer_count);
} else {
writefd_unbuffered(fd, io_buffer, io_buffer_count);
}
{
io_flush();
if (!io_multiplexing_out) {
- free(io_buffer-4);
+ free(io_buffer);
io_buffer = NULL;
}
}
}
/* write an message to the multiplexed error stream */
-int io_multiplex_write(int f, char *buf, int len)
+int io_multiplex_write(enum logcode code, char *buf, int len)
{
if (!io_multiplexing_out) return 0;
io_flush();
-
- SIVAL(io_buffer-4, 0, ((MPLEX_BASE + f)<<24) + len);
- memcpy(io_buffer, buf, len);
-
stats.total_written += (len+4);
-
- writefd_unbuffered(multiplex_out_fd, io_buffer-4, len+4);
+ mplex_write(multiplex_out_fd, code, buf, len);
return 1;
}
/* write a message to the special error fd */
-int io_error_write(int f, char *buf, int len)
+int io_error_write(int f, enum logcode code, char *buf, int len)
{
if (f == -1) return 0;
- writefd_unbuffered(f, buf, len);
+ mplex_write(f, code, buf, len);
return 1;
}
/* this is the underlying (unformatted) rsync debugging function. Call
it with FINFO, FERROR or FLOG */
-void rwrite(int fd, char *buf, int len)
+void rwrite(enum logcode code, char *buf, int len)
{
FILE *f=NULL;
extern int am_daemon;
extern int quiet;
/* recursion can happen with certain fatal conditions */
- if (quiet != 0 && fd == FINFO) return;
+ if (quiet != 0 && code == FINFO) return;
if (len < 0) exit_cleanup(RERR_MESSAGEIO);
buf[len] = 0;
- if (fd == FLOG) {
+ if (code == FLOG) {
if (am_daemon) logit(LOG_INFO, buf);
return;
}
+ if (io_error_write(log_error_fd, code, buf, strlen(buf))) return;
+
if (am_daemon) {
static int depth;
int priority = LOG_INFO;
- if (fd == FERROR) priority = LOG_WARNING;
+ if (code == FERROR) priority = LOG_WARNING;
if (depth) return;
depth++;
log_open();
-
- if (!io_error_write(log_error_fd, buf, strlen(buf)) &&
- !io_multiplex_write(fd, buf, strlen(buf))) {
+ if (!io_multiplex_write(code, buf, strlen(buf))) {
logit(priority, buf);
}
return;
}
- if (fd == FERROR) {
+ if (code == FERROR) {
f = stderr;
}
- if (fd == FINFO) {
+ if (code == FINFO) {
extern int am_server;
if (am_server)
f = stderr;
/* this is the rsync debugging function. Call it with FINFO, FERROR or FLOG */
- void rprintf(int fd, const char *format, ...)
+ void rprintf(enum logcode code, const char *format, ...)
{
va_list ap;
char buf[1024];
if (len > sizeof(buf)-1) exit_cleanup(RERR_MESSAGEIO);
- rwrite(fd, buf, len);
+ rwrite(code, buf, len);
}
-void rflush(int fd)
+void rflush(enum logcode code)
{
FILE *f = NULL;
extern int am_daemon;
return;
}
- if (fd == FLOG) {
+ if (code == FLOG) {
return;
}
- if (fd == FERROR) {
+ if (code == FERROR) {
f = stderr;
}
- if (fd == FINFO) {
+ if (code == FINFO) {
extern int am_server;
if (am_server)
f = stderr;
#define SAME_TIME (1<<7)
/* update this if you make incompatible changes */
-#define PROTOCOL_VERSION 22
-#define MIN_PROTOCOL_VERSION 11
+#define PROTOCOL_VERSION 23
+#define MIN_PROTOCOL_VERSION 15
#define MAX_PROTOCOL_VERSION 30
#define RSYNC_PORT 873
#define MAX_ARGS 1000
#define MPLEX_BASE 7
-#define FERROR 1
-#define FINFO 2
-#define FLOG 3
+
+enum logcode {FERROR=1, FINFO=2, FLOG=3};
#include "errcode.h"
#define NS(s) ((s)?(s):"<NULL>")
/* use magic gcc attributes to catch format errors */
- void rprintf(int , const char *, ...)
+ void rprintf(enum logcode , const char *, ...)
#ifdef __GNUC__
__attribute__ ((format (printf, 2, 3)))
#endif