+
+
+/* a generic logging routine for send/recv, with parameter
+ * substitiution */
+static void log_formatted(enum logcode code,
+ char *format, char *op, struct file_struct *file,
+ struct stats *initial_stats)
+{
+ char buf[1024];
+ char buf2[1024];
+ char *p, *s, *n;
+ size_t l;
+ int64 b;
+
+ /* We expand % codes one by one in place in buf. We don't
+ * copy in the terminating nul of the inserted strings, but
+ * rather keep going until we reach the nul of the format.
+ * Just to make sure we don't clobber that nul and therefore
+ * accidentally keep going, we zero the buffer now. */
+ l = strlcpy(buf, format, sizeof buf);
+ if (l < sizeof buf)
+ memset(buf + l, 0, sizeof buf - l);
+
+ for (s = &buf[0]; s && (p = strchr(s,'%')); ) {
+ n = NULL;
+ s = p + 1;
+
+ switch (p[1]) {
+ case 'h': if (am_daemon) n = client_name(0); break;
+ case 'a': if (am_daemon) n = client_addr(0); break;
+ case 'l':
+ snprintf(buf2,sizeof(buf2),"%.0f",
+ (double)file->length);
+ n = buf2;
+ break;
+ case 'p':
+ snprintf(buf2,sizeof(buf2),"%d",
+ (int)getpid());
+ n = buf2;
+ break;
+ case 'o': n = op; break;
+ case 'f':
+ pathjoin(buf2, sizeof buf2,
+ am_sender && file->dir.root ? file->dir.root : "",
+ safe_fname(f_name(file)));
+ clean_fname(buf2, 0);
+ n = buf2;
+ if (*n == '/') n++;
+ break;
+ case 'm': n = lp_name(module_id); break;
+ case 't': n = timestring(time(NULL)); break;
+ case 'P': n = lp_path(module_id); break;
+ case 'u': n = auth_user; break;
+ case 'b':
+ if (am_sender) {
+ b = stats.total_written -
+ initial_stats->total_written;
+ } else {
+ b = stats.total_read -
+ initial_stats->total_read;
+ }
+ snprintf(buf2,sizeof(buf2),"%.0f", (double)b);
+ n = buf2;
+ break;
+ case 'c':
+ if (!am_sender) {
+ b = stats.total_written -
+ initial_stats->total_written;
+ } else {
+ b = stats.total_read -
+ initial_stats->total_read;
+ }
+ snprintf(buf2,sizeof(buf2),"%.0f", (double)b);
+ n = buf2;
+ break;
+ }
+
+ /* n is the string to be inserted in place of this %
+ * code; l is its length not including the trailing
+ * NUL */
+ if (!n)
+ continue;
+
+ l = strlen(n);
+
+ if (l + ((int)(s - &buf[0])) >= sizeof(buf)) {
+ rprintf(FERROR,
+ "buffer overflow expanding %%%c -- exiting\n",
+ p[0]);
+ exit_cleanup(RERR_MESSAGEIO);
+ }
+
+ /* Shuffle the rest of the string along to make space for n */
+ if (l != 2) {
+ memmove(s+(l-1), s+1, strlen(s+1)+1);
+ }
+
+ /* Copy in n but NOT its nul, because the format string
+ * probably continues after this. */
+ memcpy(p, n, l);
+
+ /* Skip over inserted string; continue looking */
+ s = p+l;
+ }
+
+ rprintf(code,"%s\n", buf);
+}
+
+/* log the outgoing transfer of a file */
+void log_send(struct file_struct *file, struct stats *initial_stats)
+{
+ if (lp_transfer_logging(module_id)) {
+ log_formatted(FLOG, lp_log_format(module_id), "send", file, initial_stats);
+ } else if (log_format && !am_server) {
+ log_formatted(FINFO, log_format, "send", file, initial_stats);
+ }
+}
+
+/* log the incoming transfer of a file */
+void log_recv(struct file_struct *file, struct stats *initial_stats)
+{
+ if (lp_transfer_logging(module_id)) {
+ log_formatted(FLOG, lp_log_format(module_id), "recv", file, initial_stats);
+ } else if (log_format && !am_server) {
+ log_formatted(FINFO, log_format, "recv", file, initial_stats);
+ }
+}
+
+
+
+
+/*
+ * Called when the transfer is interrupted for some reason.
+ *
+ * Code is one of the RERR_* codes from errcode.h, or terminating
+ * successfully.
+ */
+void log_exit(int code, const char *file, int line)
+{
+ if (code == 0) {
+ rprintf(FLOG,"wrote %.0f bytes read %.0f bytes total size %.0f\n",
+ (double)stats.total_written,
+ (double)stats.total_read,
+ (double)stats.total_size);
+ } else {
+ const char *name;
+
+ name = rerr_name(code);
+ if (!name)
+ name = "unexplained error";
+
+ /* VANISHED is not an error, only a warning */
+ if (code == RERR_VANISHED) {
+ rprintf(FINFO, "rsync warning: %s (code %d) at %s(%d)\n",
+ name, code, file, line);
+ } else {
+ rprintf(FERROR, "rsync error: %s (code %d) at %s(%d)\n",
+ name, code, file, line);
+ }
+ }
+}