Added a debug-helping option, --msgs2stderr, than should help all
[rsync/rsync.git] / log.c
... / ...
CommitLineData
1/*
2 * Logging and utility functions.
3 *
4 * Copyright (C) 1998-2001 Andrew Tridgell <tridge@samba.org>
5 * Copyright (C) 2000-2001 Martin Pool <mbp@samba.org>
6 * Copyright (C) 2003-2008 Wayne Davison
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, visit the http://fsf.org website.
20 */
21
22#include "rsync.h"
23#include "ifuncs.h"
24
25extern int dry_run;
26extern int am_daemon;
27extern int am_server;
28extern int am_sender;
29extern int am_generator;
30extern int local_server;
31extern int quiet;
32extern int module_id;
33extern int msg_fd_out;
34extern int checksum_len;
35extern int allow_8bit_chars;
36extern int protocol_version;
37extern int always_checksum;
38extern int preserve_times;
39extern int msgs2stderr;
40extern int uid_ndx;
41extern int gid_ndx;
42extern int stdout_format_has_i;
43extern int stdout_format_has_o_or_i;
44extern int logfile_format_has_i;
45extern int logfile_format_has_o_or_i;
46extern int receiver_symlink_times;
47extern mode_t orig_umask;
48extern char *auth_user;
49extern char *stdout_format;
50extern char *logfile_format;
51extern char *logfile_name;
52#ifdef ICONV_CONST
53extern iconv_t ic_chck;
54#endif
55#ifdef ICONV_OPTION
56extern iconv_t ic_send, ic_recv;
57#endif
58extern char curr_dir[];
59extern char *module_dir;
60extern unsigned int module_dirlen;
61extern char sender_file_sum[];
62
63static int log_initialised;
64static int logfile_was_closed;
65static FILE *logfile_fp;
66struct stats stats;
67
68int got_xfer_error = 0;
69int output_needs_newline = 0;
70
71struct {
72 int code;
73 char const *name;
74} const rerr_names[] = {
75 { RERR_SYNTAX , "syntax or usage error" },
76 { RERR_PROTOCOL , "protocol incompatibility" },
77 { RERR_FILESELECT , "errors selecting input/output files, dirs" },
78 { RERR_UNSUPPORTED, "requested action not supported" },
79 { RERR_STARTCLIENT, "error starting client-server protocol" },
80 { RERR_SOCKETIO , "error in socket IO" },
81 { RERR_FILEIO , "error in file IO" },
82 { RERR_STREAMIO , "error in rsync protocol data stream" },
83 { RERR_MESSAGEIO , "errors with program diagnostics" },
84 { RERR_IPC , "error in IPC code" },
85 { RERR_CRASHED , "sibling process crashed" },
86 { RERR_TERMINATED , "sibling process terminated abnormally" },
87 { RERR_SIGNAL1 , "received SIGUSR1" },
88 { RERR_SIGNAL , "received SIGINT, SIGTERM, or SIGHUP" },
89 { RERR_WAITCHILD , "waitpid() failed" },
90 { RERR_MALLOC , "error allocating core memory buffers" },
91 { RERR_PARTIAL , "some files could not be transferred" },
92 { RERR_VANISHED , "some files vanished before they could be transferred" },
93 { RERR_TIMEOUT , "timeout in data send/receive" },
94 { RERR_CONTIMEOUT , "timeout waiting for daemon connection" },
95 { RERR_CMD_FAILED , "remote shell failed" },
96 { RERR_CMD_KILLED , "remote shell killed" },
97 { RERR_CMD_RUN , "remote command could not be run" },
98 { RERR_CMD_NOTFOUND,"remote command not found" },
99 { RERR_DEL_LIMIT , "the --max-delete limit stopped deletions" },
100 { 0, NULL }
101};
102
103/*
104 * Map from rsync error code to name, or return NULL.
105 */
106static char const *rerr_name(int code)
107{
108 int i;
109 for (i = 0; rerr_names[i].name; i++) {
110 if (rerr_names[i].code == code)
111 return rerr_names[i].name;
112 }
113 return NULL;
114}
115
116static void logit(int priority, const char *buf)
117{
118 if (logfile_was_closed)
119 logfile_reopen();
120 if (logfile_fp) {
121 fprintf(logfile_fp, "%s [%d] %s",
122 timestring(time(NULL)), (int)getpid(), buf);
123 fflush(logfile_fp);
124 } else {
125 syslog(priority, "%s", buf);
126 }
127}
128
129static void syslog_init()
130{
131 static int been_here = 0;
132 int options = LOG_PID;
133
134 if (been_here)
135 return;
136 been_here = 1;
137
138#ifdef LOG_NDELAY
139 options |= LOG_NDELAY;
140#endif
141
142#ifdef LOG_DAEMON
143 openlog("rsyncd", options, lp_syslog_facility(module_id));
144#else
145 openlog("rsyncd", options);
146#endif
147
148#ifndef LOG_NDELAY
149 logit(LOG_INFO, "rsyncd started\n");
150#endif
151}
152
153static void logfile_open(void)
154{
155 mode_t old_umask = umask(022 | orig_umask);
156 logfile_fp = fopen(logfile_name, "a");
157 umask(old_umask);
158 if (!logfile_fp) {
159 int fopen_errno = errno;
160 /* Rsync falls back to using syslog on failure. */
161 syslog_init();
162 rsyserr(FERROR, fopen_errno,
163 "failed to open log-file %s", logfile_name);
164 rprintf(FINFO, "Ignoring \"log file\" setting.\n");
165 }
166}
167
168void log_init(int restart)
169{
170 if (log_initialised) {
171 if (!restart)
172 return;
173 if (strcmp(logfile_name, lp_log_file(module_id)) != 0) {
174 if (logfile_fp) {
175 fclose(logfile_fp);
176 logfile_fp = NULL;
177 } else
178 closelog();
179 logfile_name = NULL;
180 } else if (*logfile_name)
181 return; /* unchanged, non-empty "log file" names */
182 else if (lp_syslog_facility(-1) != lp_syslog_facility(module_id))
183 closelog();
184 else
185 return; /* unchanged syslog settings */
186 } else
187 log_initialised = 1;
188
189 /* This looks pointless, but it is needed in order for the
190 * C library on some systems to fetch the timezone info
191 * before the chroot. */
192 timestring(time(NULL));
193
194 /* Optionally use a log file instead of syslog. (Non-daemon
195 * rsyncs will have already set logfile_name, as needed.) */
196 if (am_daemon && !logfile_name)
197 logfile_name = lp_log_file(module_id);
198 if (logfile_name && *logfile_name)
199 logfile_open();
200 else
201 syslog_init();
202}
203
204void logfile_close(void)
205{
206 if (logfile_fp) {
207 logfile_was_closed = 1;
208 fclose(logfile_fp);
209 logfile_fp = NULL;
210 }
211}
212
213void logfile_reopen(void)
214{
215 if (logfile_was_closed) {
216 logfile_was_closed = 0;
217 logfile_open();
218 }
219}
220
221static void filtered_fwrite(FILE *f, const char *buf, int len, int use_isprint)
222{
223 const char *s, *end = buf + len;
224 for (s = buf; s < end; s++) {
225 if ((s < end - 4
226 && *s == '\\' && s[1] == '#'
227 && isDigit(s + 2)
228 && isDigit(s + 3)
229 && isDigit(s + 4))
230 || (*s != '\t'
231 && ((use_isprint && !isPrint(s))
232 || *(uchar*)s < ' '))) {
233 if (s != buf && fwrite(buf, s - buf, 1, f) != 1)
234 exit_cleanup(RERR_MESSAGEIO);
235 fprintf(f, "\\#%03o", *(uchar*)s);
236 buf = s + 1;
237 }
238 }
239 if (buf != end && fwrite(buf, end - buf, 1, f) != 1)
240 exit_cleanup(RERR_MESSAGEIO);
241}
242
243/* this is the underlying (unformatted) rsync debugging function. Call
244 * it with FINFO, FERROR_*, FWARNING, FLOG, or FCLIENT. Note: recursion
245 * can happen with certain fatal conditions. */
246void rwrite(enum logcode code, const char *buf, int len, int is_utf8)
247{
248 int trailing_CR_or_NL;
249 FILE *f = NULL;
250#ifdef ICONV_OPTION
251 iconv_t ic = is_utf8 && ic_recv != (iconv_t)-1 ? ic_recv : ic_chck;
252#else
253#ifdef ICONV_CONST
254 iconv_t ic = ic_chck;
255#endif
256#endif
257
258 if (len < 0)
259 exit_cleanup(RERR_MESSAGEIO);
260
261 if (msgs2stderr && code != FLOG) {
262 f = stderr;
263 goto output_msg;
264 }
265
266 if (am_server && msg_fd_out >= 0) {
267 assert(!is_utf8);
268 /* Pass the message to our sibling. */
269 send_msg((enum msgcode)code, buf, len, 0);
270 return;
271 }
272
273 if (code == FERROR_SOCKET) /* This gets simplified for a non-sibling. */
274 code = FERROR;
275
276 if (code == FCLIENT)
277 code = FINFO;
278 else if (am_daemon || logfile_name) {
279 static int in_block;
280 char msg[2048];
281 int priority = code == FINFO || code == FLOG ? LOG_INFO : LOG_WARNING;
282
283 if (in_block)
284 return;
285 in_block = 1;
286 if (!log_initialised)
287 log_init(0);
288 strlcpy(msg, buf, MIN((int)sizeof msg, len + 1));
289 logit(priority, msg);
290 in_block = 0;
291
292 if (code == FLOG || (am_daemon && !am_server))
293 return;
294 } else if (code == FLOG)
295 return;
296
297 if (quiet && code == FINFO)
298 return;
299
300 if (am_server) {
301 enum msgcode msg = (enum msgcode)code;
302 if (protocol_version < 30) {
303 if (msg == MSG_ERROR)
304 msg = MSG_ERROR_XFER;
305 else if (msg == MSG_WARNING)
306 msg = MSG_INFO;
307 }
308 /* Pass the message to the non-server side. */
309 if (send_msg(msg, buf, len, !is_utf8))
310 return;
311 if (am_daemon) {
312 /* TODO: can we send the error to the user somehow? */
313 return;
314 }
315 }
316
317 switch (code) {
318 case FERROR_XFER:
319 got_xfer_error = 1;
320 /* FALL THROUGH */
321 case FERROR:
322 case FWARNING:
323 f = stderr;
324 break;
325 case FINFO:
326 f = am_server ? stderr : stdout;
327 break;
328 default:
329 exit_cleanup(RERR_MESSAGEIO);
330 }
331
332output_msg:
333 if (output_needs_newline) {
334 fputc('\n', f);
335 output_needs_newline = 0;
336 }
337
338 trailing_CR_or_NL = len && (buf[len-1] == '\n' || buf[len-1] == '\r')
339 ? buf[--len] : 0;
340
341 if (len && buf[0] == '\r') {
342 fputc('\r', f);
343 buf++;
344 len--;
345 }
346
347#ifdef ICONV_CONST
348 if (ic != (iconv_t)-1) {
349 xbuf outbuf, inbuf;
350 char convbuf[1024];
351 int ierrno;
352
353 INIT_CONST_XBUF(outbuf, convbuf);
354 INIT_XBUF(inbuf, (char*)buf, len, -1);
355
356 while (inbuf.len) {
357 iconvbufs(ic, &inbuf, &outbuf, 0);
358 ierrno = errno;
359 if (outbuf.len) {
360 filtered_fwrite(f, convbuf, outbuf.len, 0);
361 outbuf.len = 0;
362 }
363 if (!ierrno || ierrno == E2BIG)
364 continue;
365 fprintf(f, "\\#%03o", CVAL(inbuf.buf, inbuf.pos++));
366 inbuf.len--;
367 }
368 } else
369#endif
370 filtered_fwrite(f, buf, len, !allow_8bit_chars);
371
372 if (trailing_CR_or_NL) {
373 fputc(trailing_CR_or_NL, f);
374 fflush(f);
375 }
376}
377
378/* This is the rsync debugging function. Call it with FINFO, FERROR_*,
379 * FWARNING, FLOG, or FCLIENT. */
380void rprintf(enum logcode code, const char *format, ...)
381{
382 va_list ap;
383 char buf[BIGPATHBUFLEN];
384 size_t len;
385
386 va_start(ap, format);
387 len = vsnprintf(buf, sizeof buf, format, ap);
388 va_end(ap);
389
390 /* Deal with buffer overruns. Instead of panicking, just
391 * truncate the resulting string. (Note that configure ensures
392 * that we have a vsnprintf() that doesn't ever return -1.) */
393 if (len > sizeof buf - 1) {
394 static const char ellipsis[] = "[...]";
395
396 /* Reset length, and zero-terminate the end of our buffer */
397 len = sizeof buf - 1;
398 buf[len] = '\0';
399
400 /* Copy the ellipsis to the end of the string, but give
401 * us one extra character:
402 *
403 * v--- null byte at buf[sizeof buf - 1]
404 * abcdefghij0
405 * -> abcd[...]00 <-- now two null bytes at end
406 *
407 * If the input format string has a trailing newline,
408 * we copy it into that extra null; if it doesn't, well,
409 * all we lose is one byte. */
410 memcpy(buf+len-sizeof ellipsis, ellipsis, sizeof ellipsis);
411 if (format[strlen(format)-1] == '\n') {
412 buf[len-1] = '\n';
413 }
414 }
415
416 rwrite(code, buf, len, 0);
417}
418
419/* This is like rprintf, but it also tries to print some
420 * representation of the error code. Normally errcode = errno.
421 *
422 * Unlike rprintf, this always adds a newline and there should not be
423 * one in the format string.
424 *
425 * Note that since strerror might involve dynamically loading a
426 * message catalog we need to call it once before chroot-ing. */
427void rsyserr(enum logcode code, int errcode, const char *format, ...)
428{
429 va_list ap;
430 char buf[BIGPATHBUFLEN];
431 size_t len;
432
433 strlcpy(buf, RSYNC_NAME ": ", sizeof buf);
434 len = (sizeof RSYNC_NAME ": ") - 1;
435
436 va_start(ap, format);
437 len += vsnprintf(buf + len, sizeof buf - len, format, ap);
438 va_end(ap);
439
440 if (len < sizeof buf) {
441 len += snprintf(buf + len, sizeof buf - len,
442 ": %s (%d)\n", strerror(errcode), errcode);
443 }
444 if (len >= sizeof buf)
445 exit_cleanup(RERR_MESSAGEIO);
446
447 rwrite(code, buf, len, 0);
448}
449
450void rflush(enum logcode code)
451{
452 FILE *f = NULL;
453
454 if (am_daemon || code == FLOG)
455 return;
456
457 if (!am_server && (code == FINFO || code == FCLIENT))
458 f = stdout;
459 else
460 f = stderr;
461
462 fflush(f);
463}
464
465/* A generic logging routine for send/recv, with parameter substitiution. */
466static void log_formatted(enum logcode code, const char *format, const char *op,
467 struct file_struct *file, const char *fname,
468 struct stats *initial_stats, int iflags,
469 const char *hlink)
470{
471 char buf[MAXPATHLEN+1024], buf2[MAXPATHLEN], fmt[32];
472 char *p, *s, *c;
473 const char *n;
474 size_t len, total;
475 int64 b;
476
477 *fmt = '%';
478
479 /* We expand % codes one by one in place in buf. We don't
480 * copy in the terminating null of the inserted strings, but
481 * rather keep going until we reach the null of the format. */
482 total = strlcpy(buf, format, sizeof buf);
483 if (total > MAXPATHLEN) {
484 rprintf(FERROR, "log-format string is WAY too long!\n");
485 exit_cleanup(RERR_MESSAGEIO);
486 }
487 buf[total++] = '\n';
488 buf[total] = '\0';
489
490 for (p = buf; (p = strchr(p, '%')) != NULL; ) {
491 s = p++;
492 c = fmt + 1;
493 if (*p == '-')
494 *c++ = *p++;
495 while (isDigit(p) && c - fmt < (int)(sizeof fmt) - 8)
496 *c++ = *p++;
497 if (!*p)
498 break;
499 *c = '\0';
500 n = NULL;
501
502 switch (*p) {
503 case 'h':
504 if (am_daemon)
505 n = client_name(0);
506 break;
507 case 'a':
508 if (am_daemon)
509 n = client_addr(0);
510 break;
511 case 'l':
512 strlcat(fmt, "s", sizeof fmt);
513 snprintf(buf2, sizeof buf2, fmt,
514 big_num(F_LENGTH(file), 0));
515 n = buf2;
516 break;
517 case 'U':
518 strlcat(fmt, "u", sizeof fmt);
519 snprintf(buf2, sizeof buf2, fmt,
520 uid_ndx ? F_OWNER(file) : 0);
521 n = buf2;
522 break;
523 case 'G':
524 if (!gid_ndx || file->flags & FLAG_SKIP_GROUP)
525 n = "DEFAULT";
526 else {
527 strlcat(fmt, "u", sizeof fmt);
528 snprintf(buf2, sizeof buf2, fmt,
529 F_GROUP(file));
530 n = buf2;
531 }
532 break;
533 case 'p':
534 strlcat(fmt, "ld", sizeof fmt);
535 snprintf(buf2, sizeof buf2, fmt,
536 (long)getpid());
537 n = buf2;
538 break;
539 case 'M':
540 n = c = timestring(file->modtime);
541 while ((c = strchr(c, ' ')) != NULL)
542 *c = '-';
543 break;
544 case 'B':
545 c = buf2 + MAXPATHLEN - PERMSTRING_SIZE - 1;
546 permstring(c, file->mode);
547 n = c + 1; /* skip the type char */
548 break;
549 case 'o':
550 n = op;
551 break;
552 case 'f':
553 if (fname) {
554 c = f_name_buf();
555 strlcpy(c, fname, MAXPATHLEN);
556 } else
557 c = f_name(file, NULL);
558 if (am_sender && F_PATHNAME(file)) {
559 pathjoin(buf2, sizeof buf2,
560 F_PATHNAME(file), c);
561 clean_fname(buf2, 0);
562 if (fmt[1]) {
563 strlcpy(c, buf2, MAXPATHLEN);
564 n = c;
565 } else
566 n = buf2;
567 } else if (am_daemon && *c != '/') {
568 pathjoin(buf2, sizeof buf2,
569 curr_dir + module_dirlen, c);
570 clean_fname(buf2, 0);
571 if (fmt[1]) {
572 strlcpy(c, buf2, MAXPATHLEN);
573 n = c;
574 } else
575 n = buf2;
576 } else {
577 clean_fname(c, 0);
578 n = c;
579 }
580 if (*n == '/')
581 n++;
582 break;
583 case 'n':
584 if (fname) {
585 c = f_name_buf();
586 strlcpy(c, fname, MAXPATHLEN);
587 } else
588 c = f_name(file, NULL);
589 if (S_ISDIR(file->mode))
590 strlcat(c, "/", MAXPATHLEN);
591 n = c;
592 break;
593 case 'L':
594 if (hlink && *hlink) {
595 n = hlink;
596 strlcpy(buf2, " => ", sizeof buf2);
597 } else if (S_ISLNK(file->mode) && !fname) {
598 n = F_SYMLINK(file);
599 strlcpy(buf2, " -> ", sizeof buf2);
600 } else {
601 n = "";
602 if (!fmt[1])
603 break;
604 strlcpy(buf2, " ", sizeof buf2);
605 }
606 strlcat(fmt, "s", sizeof fmt);
607 snprintf(buf2 + 4, sizeof buf2 - 4, fmt, n);
608 n = buf2;
609 break;
610 case 'm':
611 n = lp_name(module_id);
612 break;
613 case 't':
614 n = timestring(time(NULL));
615 break;
616 case 'P':
617 n = module_dir;
618 break;
619 case 'u':
620 n = auth_user;
621 break;
622 case 'b':
623 if (am_sender) {
624 b = stats.total_written -
625 initial_stats->total_written;
626 } else {
627 b = stats.total_read -
628 initial_stats->total_read;
629 }
630 strlcat(fmt, "s", sizeof fmt);
631 snprintf(buf2, sizeof buf2, fmt, big_num(b, 0));
632 n = buf2;
633 break;
634 case 'c':
635 if (!am_sender) {
636 b = stats.total_written -
637 initial_stats->total_written;
638 } else {
639 b = stats.total_read -
640 initial_stats->total_read;
641 }
642 strlcat(fmt, "s", sizeof fmt);
643 snprintf(buf2, sizeof buf2, fmt, big_num(b, 0));
644 n = buf2;
645 break;
646 case 'C':
647 if (protocol_version >= 30
648 && (iflags & ITEM_TRANSFER
649 || (always_checksum && S_ISREG(file->mode)))) {
650 int i, x1, x2;
651 const char *sum = iflags & ITEM_TRANSFER
652 ? sender_file_sum : F_SUM(file);
653 c = buf2 + checksum_len*2;
654 *c = '\0';
655 for (i = checksum_len; --i >= 0; ) {
656 x1 = CVAL(sum, i);
657 x2 = x1 >> 4;
658 x1 &= 0xF;
659 *--c = x1 <= 9 ? x1 + '0' : x1 + 'a' - 10;
660 *--c = x2 <= 9 ? x2 + '0' : x2 + 'a' - 10;
661 }
662 } else {
663 memset(buf2, ' ', checksum_len*2);
664 buf2[checksum_len*2] = '\0';
665 }
666 n = buf2;
667 break;
668 case 'i':
669 if (iflags & ITEM_DELETED) {
670 n = "*deleting ";
671 break;
672 }
673 n = c = buf2 + MAXPATHLEN - 32;
674 c[0] = iflags & ITEM_LOCAL_CHANGE
675 ? iflags & ITEM_XNAME_FOLLOWS ? 'h' : 'c'
676 : !(iflags & ITEM_TRANSFER) ? '.'
677 : !local_server && *op == 's' ? '<' : '>';
678 if (S_ISLNK(file->mode)) {
679 c[1] = 'L';
680 c[3] = '.';
681 c[4] = !(iflags & ITEM_REPORT_TIME) ? '.'
682 : !preserve_times || !receiver_symlink_times
683 || (iflags & ITEM_REPORT_TIMEFAIL) ? 'T' : 't';
684 } else {
685 c[1] = S_ISDIR(file->mode) ? 'd'
686 : IS_SPECIAL(file->mode) ? 'S'
687 : IS_DEVICE(file->mode) ? 'D' : 'f';
688 c[3] = !(iflags & ITEM_REPORT_SIZE) ? '.' : 's';
689 c[4] = !(iflags & ITEM_REPORT_TIME) ? '.'
690 : !preserve_times ? 'T' : 't';
691 }
692 c[2] = !(iflags & ITEM_REPORT_CHANGE) ? '.' : 'c';
693 c[5] = !(iflags & ITEM_REPORT_PERMS) ? '.' : 'p';
694 c[6] = !(iflags & ITEM_REPORT_OWNER) ? '.' : 'o';
695 c[7] = !(iflags & ITEM_REPORT_GROUP) ? '.' : 'g';
696 c[8] = !(iflags & ITEM_REPORT_ATIME) ? '.' : 'u';
697 c[9] = !(iflags & ITEM_REPORT_ACL) ? '.' : 'a';
698 c[10] = !(iflags & ITEM_REPORT_XATTR) ? '.' : 'x';
699 c[11] = '\0';
700
701 if (iflags & (ITEM_IS_NEW|ITEM_MISSING_DATA)) {
702 char ch = iflags & ITEM_IS_NEW ? '+' : '?';
703 int i;
704 for (i = 2; c[i]; i++)
705 c[i] = ch;
706 } else if (c[0] == '.' || c[0] == 'h' || c[0] == 'c') {
707 int i;
708 for (i = 2; c[i]; i++) {
709 if (c[i] != '.')
710 break;
711 }
712 if (!c[i]) {
713 for (i = 2; c[i]; i++)
714 c[i] = ' ';
715 }
716 }
717 break;
718 }
719
720 /* "n" is the string to be inserted in place of this % code. */
721 if (!n)
722 continue;
723 if (n != buf2 && fmt[1]) {
724 strlcat(fmt, "s", sizeof fmt);
725 snprintf(buf2, sizeof buf2, fmt, n);
726 n = buf2;
727 }
728 len = strlen(n);
729
730 /* Subtract the length of the escape from the string's size. */
731 total -= p - s + 1;
732
733 if (len + total >= (size_t)sizeof buf) {
734 rprintf(FERROR,
735 "buffer overflow expanding %%%c -- exiting\n",
736 p[0]);
737 exit_cleanup(RERR_MESSAGEIO);
738 }
739
740 /* Shuffle the rest of the string along to make space for n */
741 if (len != (size_t)(p - s + 1))
742 memmove(s + len, p + 1, total - (s - buf) + 1);
743 total += len;
744
745 /* Insert the contents of string "n", but NOT its null. */
746 if (len)
747 memcpy(s, n, len);
748
749 /* Skip over inserted string; continue looking */
750 p = s + len;
751 }
752
753 rwrite(code, buf, total, 0);
754}
755
756/* Return 1 if the format escape is in the log-format string (e.g. look for
757 * the 'b' in the "%9b" format escape). */
758int log_format_has(const char *format, char esc)
759{
760 const char *p;
761
762 if (!format)
763 return 0;
764
765 for (p = format; (p = strchr(p, '%')) != NULL; ) {
766 if (*++p == '-')
767 p++;
768 while (isDigit(p))
769 p++;
770 if (!*p)
771 break;
772 if (*p == esc)
773 return 1;
774 }
775 return 0;
776}
777
778/* Log the transfer of a file. If the code is FCLIENT, the output just goes
779 * to stdout. If it is FLOG, it just goes to the log file. Otherwise we
780 * output to both. */
781void log_item(enum logcode code, struct file_struct *file,
782 struct stats *initial_stats, int iflags, const char *hlink)
783{
784 const char *s_or_r = am_sender ? "send" : "recv";
785
786 if (code != FLOG && stdout_format && !am_server) {
787 log_formatted(FCLIENT, stdout_format, s_or_r,
788 file, NULL, initial_stats, iflags, hlink);
789 }
790 if (code != FCLIENT && logfile_format && *logfile_format) {
791 log_formatted(FLOG, logfile_format, s_or_r,
792 file, NULL, initial_stats, iflags, hlink);
793 }
794}
795
796void maybe_log_item(struct file_struct *file, int iflags, int itemizing,
797 const char *buf)
798{
799 int significant_flags = iflags & SIGNIFICANT_ITEM_FLAGS;
800 int see_item = itemizing && (significant_flags || *buf
801 || stdout_format_has_i > 1 || (INFO_GTE(NAME, 2) && stdout_format_has_i));
802 int local_change = iflags & ITEM_LOCAL_CHANGE && significant_flags;
803 if (am_server) {
804 if (logfile_name && !dry_run && see_item
805 && (significant_flags || logfile_format_has_i))
806 log_item(FLOG, file, &stats, iflags, buf);
807 } else if (see_item || local_change || *buf
808 || (S_ISDIR(file->mode) && significant_flags)) {
809 enum logcode code = significant_flags || logfile_format_has_i ? FINFO : FCLIENT;
810 log_item(code, file, &stats, iflags, buf);
811 }
812}
813
814void log_delete(const char *fname, int mode)
815{
816 static struct {
817 union file_extras ex[4]; /* just in case... */
818 struct file_struct file;
819 } x;
820 int len = strlen(fname);
821 const char *fmt;
822
823 x.file.mode = mode;
824
825 if (!INFO_GTE(DEL, 1) && !stdout_format)
826 ;
827 else if (am_server && protocol_version >= 29 && len < MAXPATHLEN) {
828 if (S_ISDIR(mode))
829 len++; /* directories include trailing null */
830 send_msg(MSG_DELETED, fname, len, am_generator);
831 } else {
832 fmt = stdout_format_has_o_or_i ? stdout_format : "deleting %n";
833 log_formatted(FCLIENT, fmt, "del.", &x.file, fname, &stats,
834 ITEM_DELETED, NULL);
835 }
836
837 if (!logfile_name || dry_run || !logfile_format)
838 return;
839
840 fmt = logfile_format_has_o_or_i ? logfile_format : "deleting %n";
841 log_formatted(FLOG, fmt, "del.", &x.file, fname, &stats, ITEM_DELETED, NULL);
842}
843
844/*
845 * Called when the transfer is interrupted for some reason.
846 *
847 * Code is one of the RERR_* codes from errcode.h, or terminating
848 * successfully.
849 */
850void log_exit(int code, const char *file, int line)
851{
852 if (code == 0) {
853 rprintf(FLOG,"sent %s bytes received %s bytes total size %s\n",
854 big_num(stats.total_written, 0),
855 big_num(stats.total_read, 0),
856 big_num(stats.total_size, 0));
857 } else if (am_server != 2) {
858 const char *name;
859
860 name = rerr_name(code);
861 if (!name)
862 name = "unexplained error";
863
864 /* VANISHED is not an error, only a warning */
865 if (code == RERR_VANISHED) {
866 rprintf(FWARNING, "rsync warning: %s (code %d) at %s(%d) [%s=%s]\n",
867 name, code, file, line, who_am_i(), RSYNC_VERSION);
868 } else {
869 rprintf(FERROR, "rsync error: %s (code %d) at %s(%d) [%s=%s]\n",
870 name, code, file, line, who_am_i(), RSYNC_VERSION);
871 }
872 }
873}