Added /support/savetransfer to .gitignore.
[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_recv;
57#endif
58extern char curr_dir[MAXPATHLEN];
59extern char *module_dir;
60extern unsigned int module_dirlen;
61extern char sender_file_sum[MAX_DIGEST_LEN];
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/attrs were not transferred (see previous errors)" },
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 > 0 && code != FLOG)
262 goto output_msg;
263
264 if (am_server && msg_fd_out >= 0) {
265 assert(!is_utf8);
266 /* Pass the message to our sibling in native charset. */
267 send_msg((enum msgcode)code, buf, len, 0);
268 return;
269 }
270
271 if (code == FERROR_SOCKET) /* This gets simplified for a non-sibling. */
272 code = FERROR;
273 else if (code == FERROR_UTF8) {
274 is_utf8 = 1;
275 code = FERROR;
276 }
277
278 if (code == FCLIENT)
279 code = FINFO;
280 else if (am_daemon || logfile_name) {
281 static int in_block;
282 char msg[2048];
283 int priority = code == FINFO || code == FLOG ? LOG_INFO : LOG_WARNING;
284
285 if (in_block)
286 return;
287 in_block = 1;
288 if (!log_initialised)
289 log_init(0);
290 strlcpy(msg, buf, MIN((int)sizeof msg, len + 1));
291 logit(priority, msg);
292 in_block = 0;
293
294 if (code == FLOG || (am_daemon && !am_server))
295 return;
296 } else if (code == FLOG)
297 return;
298
299 if (quiet && code == FINFO)
300 return;
301
302 if (am_server) {
303 enum msgcode msg = (enum msgcode)code;
304 if (protocol_version < 30) {
305 if (msg == MSG_ERROR)
306 msg = MSG_ERROR_XFER;
307 else if (msg == MSG_WARNING)
308 msg = MSG_INFO;
309 }
310 /* Pass the message to the non-server side. */
311 if (send_msg(msg, buf, len, !is_utf8))
312 return;
313 if (am_daemon) {
314 /* TODO: can we send the error to the user somehow? */
315 return;
316 }
317 msgs2stderr = -1;
318 }
319
320output_msg:
321 switch (code) {
322 case FERROR_XFER:
323 got_xfer_error = 1;
324 /* FALL THROUGH */
325 case FERROR:
326 case FWARNING:
327 f = stderr;
328 break;
329 case FINFO:
330 case FCLIENT:
331 f = msgs2stderr ? stderr : stdout;
332 break;
333 default:
334 exit_cleanup(RERR_MESSAGEIO);
335 }
336
337 if (output_needs_newline) {
338 fputc('\n', f);
339 output_needs_newline = 0;
340 }
341
342 trailing_CR_or_NL = len && (buf[len-1] == '\n' || buf[len-1] == '\r')
343 ? buf[--len] : 0;
344
345 if (len && buf[0] == '\r') {
346 fputc('\r', f);
347 buf++;
348 len--;
349 }
350
351#ifdef ICONV_CONST
352 if (ic != (iconv_t)-1) {
353 xbuf outbuf, inbuf;
354 char convbuf[1024];
355 int ierrno;
356
357 INIT_CONST_XBUF(outbuf, convbuf);
358 INIT_XBUF(inbuf, (char*)buf, len, (size_t)-1);
359
360 while (inbuf.len) {
361 iconvbufs(ic, &inbuf, &outbuf, 0);
362 ierrno = errno;
363 if (outbuf.len) {
364 filtered_fwrite(f, convbuf, outbuf.len, 0);
365 outbuf.len = 0;
366 }
367 if (!ierrno || ierrno == E2BIG)
368 continue;
369 fprintf(f, "\\#%03o", CVAL(inbuf.buf, inbuf.pos++));
370 inbuf.len--;
371 }
372 } else
373#endif
374 filtered_fwrite(f, buf, len, !allow_8bit_chars);
375
376 if (trailing_CR_or_NL) {
377 fputc(trailing_CR_or_NL, f);
378 fflush(f);
379 }
380}
381
382/* This is the rsync debugging function. Call it with FINFO, FERROR_*,
383 * FWARNING, FLOG, or FCLIENT. */
384void rprintf(enum logcode code, const char *format, ...)
385{
386 va_list ap;
387 char buf[BIGPATHBUFLEN];
388 size_t len;
389
390 va_start(ap, format);
391 len = vsnprintf(buf, sizeof buf, format, ap);
392 va_end(ap);
393
394 /* Deal with buffer overruns. Instead of panicking, just
395 * truncate the resulting string. (Note that configure ensures
396 * that we have a vsnprintf() that doesn't ever return -1.) */
397 if (len > sizeof buf - 1) {
398 static const char ellipsis[] = "[...]";
399
400 /* Reset length, and zero-terminate the end of our buffer */
401 len = sizeof buf - 1;
402 buf[len] = '\0';
403
404 /* Copy the ellipsis to the end of the string, but give
405 * us one extra character:
406 *
407 * v--- null byte at buf[sizeof buf - 1]
408 * abcdefghij0
409 * -> abcd[...]00 <-- now two null bytes at end
410 *
411 * If the input format string has a trailing newline,
412 * we copy it into that extra null; if it doesn't, well,
413 * all we lose is one byte. */
414 memcpy(buf+len-sizeof ellipsis, ellipsis, sizeof ellipsis);
415 if (format[strlen(format)-1] == '\n') {
416 buf[len-1] = '\n';
417 }
418 }
419
420 rwrite(code, buf, len, 0);
421}
422
423/* This is like rprintf, but it also tries to print some
424 * representation of the error code. Normally errcode = errno.
425 *
426 * Unlike rprintf, this always adds a newline and there should not be
427 * one in the format string.
428 *
429 * Note that since strerror might involve dynamically loading a
430 * message catalog we need to call it once before chroot-ing. */
431void rsyserr(enum logcode code, int errcode, const char *format, ...)
432{
433 va_list ap;
434 char buf[BIGPATHBUFLEN];
435 size_t len;
436
437 strlcpy(buf, RSYNC_NAME ": ", sizeof buf);
438 len = (sizeof RSYNC_NAME ": ") - 1;
439
440 va_start(ap, format);
441 len += vsnprintf(buf + len, sizeof buf - len, format, ap);
442 va_end(ap);
443
444 if (len < sizeof buf) {
445 len += snprintf(buf + len, sizeof buf - len,
446 ": %s (%d)\n", strerror(errcode), errcode);
447 }
448 if (len >= sizeof buf)
449 exit_cleanup(RERR_MESSAGEIO);
450
451 rwrite(code, buf, len, 0);
452}
453
454void rflush(enum logcode code)
455{
456 FILE *f = NULL;
457
458 if (am_daemon || code == FLOG)
459 return;
460
461 if (!am_server && (code == FINFO || code == FCLIENT))
462 f = stdout;
463 else
464 f = stderr;
465
466 fflush(f);
467}
468
469/* A generic logging routine for send/recv, with parameter substitiution. */
470static void log_formatted(enum logcode code, const char *format, const char *op,
471 struct file_struct *file, const char *fname,
472 struct stats *initial_stats, int iflags,
473 const char *hlink)
474{
475 char buf[MAXPATHLEN+1024], buf2[MAXPATHLEN], fmt[32];
476 char *p, *s, *c;
477 const char *n;
478 size_t len, total;
479 int64 b;
480
481 *fmt = '%';
482
483 /* We expand % codes one by one in place in buf. We don't
484 * copy in the terminating null of the inserted strings, but
485 * rather keep going until we reach the null of the format. */
486 total = strlcpy(buf, format, sizeof buf);
487 if (total > MAXPATHLEN) {
488 rprintf(FERROR, "log-format string is WAY too long!\n");
489 exit_cleanup(RERR_MESSAGEIO);
490 }
491 buf[total++] = '\n';
492 buf[total] = '\0';
493
494 for (p = buf; (p = strchr(p, '%')) != NULL; ) {
495 s = p++;
496 c = fmt + 1;
497 if (*p == '-')
498 *c++ = *p++;
499 while (isDigit(p) && c - fmt < (int)(sizeof fmt) - 8)
500 *c++ = *p++;
501 if (!*p)
502 break;
503 *c = '\0';
504 n = NULL;
505
506 switch (*p) {
507 case 'h':
508 if (am_daemon)
509 n = client_name(0);
510 break;
511 case 'a':
512 if (am_daemon)
513 n = client_addr(0);
514 break;
515 case 'l':
516 strlcat(fmt, "s", sizeof fmt);
517 snprintf(buf2, sizeof buf2, fmt,
518 big_num(F_LENGTH(file), 0));
519 n = buf2;
520 break;
521 case 'U':
522 strlcat(fmt, "u", sizeof fmt);
523 snprintf(buf2, sizeof buf2, fmt,
524 uid_ndx ? F_OWNER(file) : 0);
525 n = buf2;
526 break;
527 case 'G':
528 if (!gid_ndx || file->flags & FLAG_SKIP_GROUP)
529 n = "DEFAULT";
530 else {
531 strlcat(fmt, "u", sizeof fmt);
532 snprintf(buf2, sizeof buf2, fmt,
533 F_GROUP(file));
534 n = buf2;
535 }
536 break;
537 case 'p':
538 strlcat(fmt, "ld", sizeof fmt);
539 snprintf(buf2, sizeof buf2, fmt,
540 (long)getpid());
541 n = buf2;
542 break;
543 case 'M':
544 n = c = timestring(file->modtime);
545 while ((c = strchr(c, ' ')) != NULL)
546 *c = '-';
547 break;
548 case 'B':
549 c = buf2 + MAXPATHLEN - PERMSTRING_SIZE - 1;
550 permstring(c, file->mode);
551 n = c + 1; /* skip the type char */
552 break;
553 case 'o':
554 n = op;
555 break;
556 case 'f':
557 if (fname) {
558 c = f_name_buf();
559 strlcpy(c, fname, MAXPATHLEN);
560 } else
561 c = f_name(file, NULL);
562 if (am_sender && F_PATHNAME(file)) {
563 pathjoin(buf2, sizeof buf2,
564 F_PATHNAME(file), c);
565 clean_fname(buf2, 0);
566 if (fmt[1]) {
567 strlcpy(c, buf2, MAXPATHLEN);
568 n = c;
569 } else
570 n = buf2;
571 } else if (am_daemon && *c != '/') {
572 pathjoin(buf2, sizeof buf2,
573 curr_dir + module_dirlen, c);
574 clean_fname(buf2, 0);
575 if (fmt[1]) {
576 strlcpy(c, buf2, MAXPATHLEN);
577 n = c;
578 } else
579 n = buf2;
580 } else {
581 clean_fname(c, 0);
582 n = c;
583 }
584 if (*n == '/')
585 n++;
586 break;
587 case 'n':
588 if (fname) {
589 c = f_name_buf();
590 strlcpy(c, fname, MAXPATHLEN);
591 } else
592 c = f_name(file, NULL);
593 if (S_ISDIR(file->mode))
594 strlcat(c, "/", MAXPATHLEN);
595 n = c;
596 break;
597 case 'L':
598 if (hlink && *hlink) {
599 n = hlink;
600 strlcpy(buf2, " => ", sizeof buf2);
601 } else if (S_ISLNK(file->mode) && !fname) {
602 n = F_SYMLINK(file);
603 strlcpy(buf2, " -> ", sizeof buf2);
604 } else {
605 n = "";
606 if (!fmt[1])
607 break;
608 strlcpy(buf2, " ", sizeof buf2);
609 }
610 strlcat(fmt, "s", sizeof fmt);
611 snprintf(buf2 + 4, sizeof buf2 - 4, fmt, n);
612 n = buf2;
613 break;
614 case 'm':
615 n = lp_name(module_id);
616 break;
617 case 't':
618 n = timestring(time(NULL));
619 break;
620 case 'P':
621 n = module_dir;
622 break;
623 case 'u':
624 n = auth_user;
625 break;
626 case 'b':
627 if (am_sender) {
628 b = stats.total_written -
629 initial_stats->total_written;
630 } else {
631 b = stats.total_read -
632 initial_stats->total_read;
633 }
634 strlcat(fmt, "s", sizeof fmt);
635 snprintf(buf2, sizeof buf2, fmt, big_num(b, 0));
636 n = buf2;
637 break;
638 case 'c':
639 if (!am_sender) {
640 b = stats.total_written -
641 initial_stats->total_written;
642 } else {
643 b = stats.total_read -
644 initial_stats->total_read;
645 }
646 strlcat(fmt, "s", sizeof fmt);
647 snprintf(buf2, sizeof buf2, fmt, big_num(b, 0));
648 n = buf2;
649 break;
650 case 'C':
651 if (protocol_version >= 30
652 && (iflags & ITEM_TRANSFER
653 || (always_checksum && S_ISREG(file->mode)))) {
654 int i, x1, x2;
655 const char *sum = iflags & ITEM_TRANSFER
656 ? sender_file_sum : F_SUM(file);
657 c = buf2 + checksum_len*2;
658 *c = '\0';
659 for (i = checksum_len; --i >= 0; ) {
660 x1 = CVAL(sum, i);
661 x2 = x1 >> 4;
662 x1 &= 0xF;
663 *--c = x1 <= 9 ? x1 + '0' : x1 + 'a' - 10;
664 *--c = x2 <= 9 ? x2 + '0' : x2 + 'a' - 10;
665 }
666 } else {
667 memset(buf2, ' ', checksum_len*2);
668 buf2[checksum_len*2] = '\0';
669 }
670 n = buf2;
671 break;
672 case 'i':
673 if (iflags & ITEM_DELETED) {
674 n = "*deleting ";
675 break;
676 }
677 n = c = buf2 + MAXPATHLEN - 32;
678 c[0] = iflags & ITEM_LOCAL_CHANGE
679 ? iflags & ITEM_XNAME_FOLLOWS ? 'h' : 'c'
680 : !(iflags & ITEM_TRANSFER) ? '.'
681 : !local_server && *op == 's' ? '<' : '>';
682 if (S_ISLNK(file->mode)) {
683 c[1] = 'L';
684 c[3] = '.';
685 c[4] = !(iflags & ITEM_REPORT_TIME) ? '.'
686 : !preserve_times || !receiver_symlink_times
687 || (iflags & ITEM_REPORT_TIMEFAIL) ? 'T' : 't';
688 } else {
689 c[1] = S_ISDIR(file->mode) ? 'd'
690 : IS_SPECIAL(file->mode) ? 'S'
691 : IS_DEVICE(file->mode) ? 'D' : 'f';
692 c[3] = !(iflags & ITEM_REPORT_SIZE) ? '.' : 's';
693 c[4] = !(iflags & ITEM_REPORT_TIME) ? '.'
694 : !preserve_times ? 'T' : 't';
695 }
696 c[2] = !(iflags & ITEM_REPORT_CHANGE) ? '.' : 'c';
697 c[5] = !(iflags & ITEM_REPORT_PERMS) ? '.' : 'p';
698 c[6] = !(iflags & ITEM_REPORT_OWNER) ? '.' : 'o';
699 c[7] = !(iflags & ITEM_REPORT_GROUP) ? '.' : 'g';
700 c[8] = !(iflags & ITEM_REPORT_ATIME) ? '.' : 'u';
701 c[9] = !(iflags & ITEM_REPORT_ACL) ? '.' : 'a';
702 c[10] = !(iflags & ITEM_REPORT_XATTR) ? '.' : 'x';
703 c[11] = '\0';
704
705 if (iflags & (ITEM_IS_NEW|ITEM_MISSING_DATA)) {
706 char ch = iflags & ITEM_IS_NEW ? '+' : '?';
707 int i;
708 for (i = 2; c[i]; i++)
709 c[i] = ch;
710 } else if (c[0] == '.' || c[0] == 'h' || c[0] == 'c') {
711 int i;
712 for (i = 2; c[i]; i++) {
713 if (c[i] != '.')
714 break;
715 }
716 if (!c[i]) {
717 for (i = 2; c[i]; i++)
718 c[i] = ' ';
719 }
720 }
721 break;
722 }
723
724 /* "n" is the string to be inserted in place of this % code. */
725 if (!n)
726 continue;
727 if (n != buf2 && fmt[1]) {
728 strlcat(fmt, "s", sizeof fmt);
729 snprintf(buf2, sizeof buf2, fmt, n);
730 n = buf2;
731 }
732 len = strlen(n);
733
734 /* Subtract the length of the escape from the string's size. */
735 total -= p - s + 1;
736
737 if (len + total >= (size_t)sizeof buf) {
738 rprintf(FERROR,
739 "buffer overflow expanding %%%c -- exiting\n",
740 p[0]);
741 exit_cleanup(RERR_MESSAGEIO);
742 }
743
744 /* Shuffle the rest of the string along to make space for n */
745 if (len != (size_t)(p - s + 1))
746 memmove(s + len, p + 1, total - (s - buf) + 1);
747 total += len;
748
749 /* Insert the contents of string "n", but NOT its null. */
750 if (len)
751 memcpy(s, n, len);
752
753 /* Skip over inserted string; continue looking */
754 p = s + len;
755 }
756
757 rwrite(code, buf, total, 0);
758}
759
760/* Return 1 if the format escape is in the log-format string (e.g. look for
761 * the 'b' in the "%9b" format escape). */
762int log_format_has(const char *format, char esc)
763{
764 const char *p;
765
766 if (!format)
767 return 0;
768
769 for (p = format; (p = strchr(p, '%')) != NULL; ) {
770 if (*++p == '-')
771 p++;
772 while (isDigit(p))
773 p++;
774 if (!*p)
775 break;
776 if (*p == esc)
777 return 1;
778 }
779 return 0;
780}
781
782/* Log the transfer of a file. If the code is FCLIENT, the output just goes
783 * to stdout. If it is FLOG, it just goes to the log file. Otherwise we
784 * output to both. */
785void log_item(enum logcode code, struct file_struct *file,
786 struct stats *initial_stats, int iflags, const char *hlink)
787{
788 const char *s_or_r = am_sender ? "send" : "recv";
789
790 if (code != FLOG && stdout_format && !am_server) {
791 log_formatted(FCLIENT, stdout_format, s_or_r,
792 file, NULL, initial_stats, iflags, hlink);
793 }
794 if (code != FCLIENT && logfile_format && *logfile_format) {
795 log_formatted(FLOG, logfile_format, s_or_r,
796 file, NULL, initial_stats, iflags, hlink);
797 }
798}
799
800void maybe_log_item(struct file_struct *file, int iflags, int itemizing,
801 const char *buf)
802{
803 int significant_flags = iflags & SIGNIFICANT_ITEM_FLAGS;
804 int see_item = itemizing && (significant_flags || *buf
805 || stdout_format_has_i > 1 || (INFO_GTE(NAME, 2) && stdout_format_has_i));
806 int local_change = iflags & ITEM_LOCAL_CHANGE && significant_flags;
807 if (am_server) {
808 if (logfile_name && !dry_run && see_item
809 && (significant_flags || logfile_format_has_i))
810 log_item(FLOG, file, &stats, iflags, buf);
811 } else if (see_item || local_change || *buf
812 || (S_ISDIR(file->mode) && significant_flags)) {
813 enum logcode code = significant_flags || logfile_format_has_i ? FINFO : FCLIENT;
814 log_item(code, file, &stats, iflags, buf);
815 }
816}
817
818void log_delete(const char *fname, int mode)
819{
820 static struct {
821 union file_extras ex[4]; /* just in case... */
822 struct file_struct file;
823 } x;
824 int len = strlen(fname);
825 const char *fmt;
826
827 x.file.mode = mode;
828
829 if (!INFO_GTE(DEL, 1) && !stdout_format)
830 ;
831 else if (am_server && protocol_version >= 29 && len < MAXPATHLEN) {
832 if (S_ISDIR(mode))
833 len++; /* directories include trailing null */
834 send_msg(MSG_DELETED, fname, len, am_generator);
835 } else {
836 fmt = stdout_format_has_o_or_i ? stdout_format : "deleting %n";
837 log_formatted(FCLIENT, fmt, "del.", &x.file, fname, &stats,
838 ITEM_DELETED, NULL);
839 }
840
841 if (!logfile_name || dry_run || !logfile_format)
842 return;
843
844 fmt = logfile_format_has_o_or_i ? logfile_format : "deleting %n";
845 log_formatted(FLOG, fmt, "del.", &x.file, fname, &stats, ITEM_DELETED, NULL);
846}
847
848/*
849 * Called when the transfer is interrupted for some reason.
850 *
851 * Code is one of the RERR_* codes from errcode.h, or terminating
852 * successfully.
853 */
854void log_exit(int code, const char *file, int line)
855{
856 if (code == 0) {
857 rprintf(FLOG,"sent %s bytes received %s bytes total size %s\n",
858 big_num(stats.total_written, 0),
859 big_num(stats.total_read, 0),
860 big_num(stats.total_size, 0));
861 } else if (am_server != 2) {
862 const char *name;
863
864 name = rerr_name(code);
865 if (!name)
866 name = "unexplained error";
867
868 /* VANISHED is not an error, only a warning */
869 if (code == RERR_VANISHED) {
870 rprintf(FWARNING, "rsync warning: %s (code %d) at %s(%d) [%s=%s]\n",
871 name, code, file, line, who_am_i(), RSYNC_VERSION);
872 } else {
873 rprintf(FERROR, "rsync error: %s (code %d) at %s(%d) [%s=%s]\n",
874 name, code, file, line, who_am_i(), RSYNC_VERSION);
875 }
876 }
877}