Two sparse fixes from Yuxuan Shui.
[rsync.git] / log.c
blob4cd3aa611aabd674f6c78ac968e071e02f3dd7b8
1 /*
2 * Logging and utility functions.
4 * Copyright (C) 1998-2001 Andrew Tridgell <tridge@samba.org>
5 * Copyright (C) 2000-2001 Martin Pool <mbp@samba.org>
6 * Copyright (C) 2003-2020 Wayne Davison
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.
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.
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.
22 #include "rsync.h"
23 #include "itypes.h"
24 #include "inums.h"
26 extern int dry_run;
27 extern int am_daemon;
28 extern int am_server;
29 extern int am_sender;
30 extern int am_generator;
31 extern int local_server;
32 extern int quiet;
33 extern int module_id;
34 extern int allow_8bit_chars;
35 extern int protocol_version;
36 extern int always_checksum;
37 extern int preserve_times;
38 extern int msgs2stderr;
39 extern int xfersum_type;
40 extern int checksum_type;
41 extern int stdout_format_has_i;
42 extern int stdout_format_has_o_or_i;
43 extern int logfile_format_has_i;
44 extern int logfile_format_has_o_or_i;
45 extern int receiver_symlink_times;
46 extern int64 total_data_written;
47 extern int64 total_data_read;
48 extern mode_t orig_umask;
49 extern char *auth_user;
50 extern char *stdout_format;
51 extern char *logfile_format;
52 extern char *logfile_name;
53 #ifdef ICONV_CONST
54 extern iconv_t ic_chck;
55 #endif
56 #ifdef ICONV_OPTION
57 extern iconv_t ic_recv;
58 #endif
59 extern char curr_dir[MAXPATHLEN];
60 extern char *full_module_path;
61 extern unsigned int module_dirlen;
62 extern char sender_file_sum[MAX_DIGEST_LEN];
63 extern const char undetermined_hostname[];
65 static int log_initialised;
66 static int logfile_was_closed;
67 static FILE *logfile_fp;
68 struct stats stats;
70 int got_xfer_error = 0;
71 int output_needs_newline = 0;
72 int send_msgs_to_gen = 0;
74 static int64 initial_data_written;
75 static int64 initial_data_read;
77 struct {
78 int code;
79 char const *name;
80 } const rerr_names[] = {
81 { RERR_SYNTAX , "syntax or usage error" },
82 { RERR_PROTOCOL , "protocol incompatibility" },
83 { RERR_FILESELECT , "errors selecting input/output files, dirs" },
84 { RERR_UNSUPPORTED, "requested action not supported" },
85 { RERR_STARTCLIENT, "error starting client-server protocol" },
86 { RERR_SOCKETIO , "error in socket IO" },
87 { RERR_FILEIO , "error in file IO" },
88 { RERR_STREAMIO , "error in rsync protocol data stream" },
89 { RERR_MESSAGEIO , "errors with program diagnostics" },
90 { RERR_IPC , "error in IPC code" },
91 { RERR_CRASHED , "sibling process crashed" },
92 { RERR_TERMINATED , "sibling process terminated abnormally" },
93 { RERR_SIGNAL1 , "received SIGUSR1" },
94 { RERR_SIGNAL , "received SIGINT, SIGTERM, or SIGHUP" },
95 { RERR_WAITCHILD , "waitpid() failed" },
96 { RERR_MALLOC , "error allocating core memory buffers" },
97 { RERR_PARTIAL , "some files/attrs were not transferred (see previous errors)" },
98 { RERR_VANISHED , "some files vanished before they could be transferred" },
99 { RERR_DEL_LIMIT , "the --max-delete limit stopped deletions" },
100 { RERR_TIMEOUT , "timeout in data send/receive" },
101 { RERR_CONTIMEOUT , "timeout waiting for daemon connection" },
102 { RERR_CMD_FAILED , "remote shell failed" },
103 { RERR_CMD_KILLED , "remote shell killed" },
104 { RERR_CMD_RUN , "remote command could not be run" },
105 { RERR_CMD_NOTFOUND,"remote command not found" },
106 { 0, NULL }
110 * Map from rsync error code to name, or return NULL.
112 static char const *rerr_name(int code)
114 int i;
115 for (i = 0; rerr_names[i].name; i++) {
116 if (rerr_names[i].code == code)
117 return rerr_names[i].name;
119 return NULL;
122 static void logit(int priority, const char *buf)
124 if (logfile_was_closed)
125 logfile_reopen();
126 if (logfile_fp) {
127 fprintf(logfile_fp, "%s [%d] %s", timestring(time(NULL)), (int)getpid(), buf);
128 fflush(logfile_fp);
129 } else {
130 syslog(priority, "%s", buf);
134 static void syslog_init()
136 int options = LOG_PID;
138 #ifdef LOG_NDELAY
139 options |= LOG_NDELAY;
140 #endif
142 #ifdef LOG_DAEMON
143 openlog(lp_syslog_tag(module_id), options, lp_syslog_facility(module_id));
144 #else
145 openlog(lp_syslog_tag(module_id), options);
146 #endif
148 #ifndef LOG_NDELAY
149 logit(LOG_INFO, "rsyncd started\n");
150 #endif
153 static void logfile_open(void)
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 logfile_name = "";
169 void log_init(int restart)
171 if (log_initialised) {
172 if (!restart) /* Note: a restart only happens with am_daemon */
173 return;
174 assert(logfile_name); /* all am_daemon procs got at least an empty string */
175 if (strcmp(logfile_name, lp_log_file(module_id)) != 0) {
176 if (logfile_fp) {
177 fclose(logfile_fp);
178 logfile_fp = NULL;
179 } else
180 closelog();
181 logfile_name = NULL;
182 } else if (*logfile_name)
183 return; /* unchanged, non-empty "log file" names */
184 else if (lp_syslog_facility(-1) != lp_syslog_facility(module_id)
185 || strcmp(lp_syslog_tag(-1), lp_syslog_tag(module_id)) != 0)
186 closelog();
187 else
188 return; /* unchanged syslog settings */
189 } else
190 log_initialised = 1;
192 /* This looks pointless, but it is needed in order for the
193 * C library on some systems to fetch the timezone info
194 * before the chroot. */
195 timestring(time(NULL));
197 /* Optionally use a log file instead of syslog. (Non-daemon
198 * rsyncs will have already set logfile_name, as needed.) */
199 if (am_daemon && !logfile_name)
200 logfile_name = lp_log_file(module_id);
201 if (logfile_name && *logfile_name)
202 logfile_open();
203 else
204 syslog_init();
207 /* Note that this close & reopen idiom intentionally ignores syslog logging. */
208 void logfile_close(void)
210 if (logfile_fp) {
211 logfile_was_closed = 1;
212 fclose(logfile_fp);
213 logfile_fp = NULL;
217 void logfile_reopen(void)
219 if (logfile_was_closed) {
220 logfile_was_closed = 0;
221 logfile_open();
225 static void filtered_fwrite(FILE *f, const char *in_buf, int in_len, int use_isprint, char end_char)
227 char outbuf[1024], *ob = outbuf;
228 const char *end = in_buf + in_len;
229 while (in_buf < end) {
230 if (ob - outbuf >= (int)sizeof outbuf - 10) {
231 if (fwrite(outbuf, ob - outbuf, 1, f) != 1)
232 exit_cleanup(RERR_MESSAGEIO);
233 ob = outbuf;
235 if ((in_buf < end - 4 && *in_buf == '\\' && in_buf[1] == '#'
236 && isDigit(in_buf + 2) && isDigit(in_buf + 3) && isDigit(in_buf + 4))
237 || (*in_buf != '\t' && ((use_isprint && !isPrint(in_buf)) || *(uchar*)in_buf < ' ')))
238 ob += snprintf(ob, 6, "\\#%03o", *(uchar*)in_buf++);
239 else
240 *ob++ = *in_buf++;
242 if (end_char) /* The "- 10" above means that there is always room for one more char here. */
243 *ob++ = end_char;
244 if (ob != outbuf && fwrite(outbuf, ob - outbuf, 1, f) != 1)
245 exit_cleanup(RERR_MESSAGEIO);
248 /* this is the underlying (unformatted) rsync debugging function. Call
249 * it with FINFO, FERROR_*, FWARNING, FLOG, or FCLIENT. Note: recursion
250 * can happen with certain fatal conditions. */
251 void rwrite(enum logcode code, const char *buf, int len, int is_utf8)
253 char trailing_CR_or_NL;
254 FILE *f = msgs2stderr ? stderr : stdout;
255 #ifdef ICONV_OPTION
256 iconv_t ic = is_utf8 && ic_recv != (iconv_t)-1 ? ic_recv : ic_chck;
257 #else
258 #ifdef ICONV_CONST
259 iconv_t ic = ic_chck;
260 #endif
261 #endif
263 if (len < 0)
264 exit_cleanup(RERR_MESSAGEIO);
266 if (msgs2stderr) {
267 if (!am_daemon) {
268 if (code == FLOG)
269 return;
270 goto output_msg;
272 if (code == FCLIENT)
273 return;
274 code = FLOG;
275 } else if (send_msgs_to_gen) {
276 assert(!is_utf8);
277 /* Pass the message to our sibling in native charset. */
278 send_msg((enum msgcode)code, buf, len, 0);
279 return;
282 if (code == FERROR_SOCKET) /* This gets simplified for a non-sibling. */
283 code = FERROR;
284 else if (code == FERROR_UTF8) {
285 is_utf8 = 1;
286 code = FERROR;
289 if (code == FCLIENT)
290 code = FINFO;
291 else if (am_daemon || logfile_name) {
292 static int in_block;
293 char msg[2048];
294 int priority = code == FINFO || code == FLOG ? LOG_INFO : LOG_WARNING;
296 if (in_block)
297 return;
298 in_block = 1;
299 if (!log_initialised)
300 log_init(0);
301 strlcpy(msg, buf, MIN((int)sizeof msg, len + 1));
302 logit(priority, msg);
303 in_block = 0;
305 if (code == FLOG || (am_daemon && !am_server))
306 return;
307 } else if (code == FLOG)
308 return;
310 if (quiet && code == FINFO)
311 return;
313 if (am_server) {
314 enum msgcode msg = (enum msgcode)code;
315 if (protocol_version < 30) {
316 if (msg == MSG_ERROR)
317 msg = MSG_ERROR_XFER;
318 else if (msg == MSG_WARNING)
319 msg = MSG_INFO;
321 /* Pass the message to the non-server side. */
322 if (send_msg(msg, buf, len, !is_utf8))
323 return;
324 if (am_daemon) {
325 /* TODO: can we send the error to the user somehow? */
326 return;
328 f = stderr;
331 output_msg:
332 switch (code) {
333 case FERROR_XFER:
334 got_xfer_error = 1;
335 /* FALL THROUGH */
336 case FERROR:
337 case FERROR_UTF8:
338 case FERROR_SOCKET:
339 case FWARNING:
340 f = stderr;
341 break;
342 case FLOG:
343 case FINFO:
344 case FCLIENT:
345 break;
346 default:
347 fprintf(stderr, "Unknown logcode in rwrite(): %d [%s]\n", (int)code, who_am_i());
348 exit_cleanup(RERR_MESSAGEIO);
351 if (output_needs_newline) {
352 fputc('\n', f);
353 output_needs_newline = 0;
356 trailing_CR_or_NL = len && (buf[len-1] == '\n' || buf[len-1] == '\r')
357 ? buf[--len] : 0;
359 if (len && buf[0] == '\r') {
360 fputc('\r', f);
361 buf++;
362 len--;
365 #ifdef ICONV_CONST
366 if (ic != (iconv_t)-1) {
367 xbuf outbuf, inbuf;
368 char convbuf[1024];
369 int ierrno;
371 INIT_CONST_XBUF(outbuf, convbuf);
372 INIT_XBUF(inbuf, (char*)buf, len, (size_t)-1);
374 while (inbuf.len) {
375 iconvbufs(ic, &inbuf, &outbuf, inbuf.pos ? 0 : ICB_INIT);
376 ierrno = errno;
377 if (outbuf.len) {
378 filtered_fwrite(f, convbuf, outbuf.len, 0, 0);
379 outbuf.len = 0;
381 /* Log one byte of illegal/incomplete sequence and continue with
382 * the next character. Check that the buffer is non-empty for the
383 * sake of robustness. */
384 if ((ierrno == EILSEQ || ierrno == EINVAL) && inbuf.len) {
385 fprintf(f, "\\#%03o", CVAL(inbuf.buf, inbuf.pos++));
386 inbuf.len--;
390 if (trailing_CR_or_NL) {
391 fputc(trailing_CR_or_NL, f);
392 fflush(f);
394 } else
395 #endif
397 filtered_fwrite(f, buf, len, !allow_8bit_chars, trailing_CR_or_NL);
398 if (trailing_CR_or_NL)
399 fflush(f);
403 /* This is the rsync debugging function. Call it with FINFO, FERROR_*,
404 * FWARNING, FLOG, or FCLIENT. */
405 void rprintf(enum logcode code, const char *format, ...)
407 va_list ap;
408 char buf[BIGPATHBUFLEN];
409 size_t len;
411 va_start(ap, format);
412 len = vsnprintf(buf, sizeof buf, format, ap);
413 va_end(ap);
415 /* Deal with buffer overruns. Instead of panicking, just
416 * truncate the resulting string. (Note that configure ensures
417 * that we have a vsnprintf() that doesn't ever return -1.) */
418 if (len > sizeof buf - 1) {
419 static const char ellipsis[] = "[...]";
421 /* Reset length, and zero-terminate the end of our buffer */
422 len = sizeof buf - 1;
423 buf[len] = '\0';
425 /* Copy the ellipsis to the end of the string, but give
426 * us one extra character:
428 * v--- null byte at buf[sizeof buf - 1]
429 * abcdefghij0
430 * -> abcd[...]00 <-- now two null bytes at end
432 * If the input format string has a trailing newline,
433 * we copy it into that extra null; if it doesn't, well,
434 * all we lose is one byte. */
435 memcpy(buf+len-sizeof ellipsis, ellipsis, sizeof ellipsis);
436 if (format[strlen(format)-1] == '\n') {
437 buf[len-1] = '\n';
441 rwrite(code, buf, len, 0);
444 /* This is like rprintf, but it also tries to print some
445 * representation of the error code. Normally errcode = errno.
447 * Unlike rprintf, this always adds a newline and there should not be
448 * one in the format string.
450 * Note that since strerror might involve dynamically loading a
451 * message catalog we need to call it once before chroot-ing. */
452 void rsyserr(enum logcode code, int errcode, const char *format, ...)
454 va_list ap;
455 char buf[BIGPATHBUFLEN];
456 size_t len;
458 strlcpy(buf, RSYNC_NAME ": ", sizeof buf);
459 len = (sizeof RSYNC_NAME ": ") - 1;
461 va_start(ap, format);
462 len += vsnprintf(buf + len, sizeof buf - len, format, ap);
463 va_end(ap);
465 if (len < sizeof buf) {
466 len += snprintf(buf + len, sizeof buf - len,
467 ": %s (%d)\n", strerror(errcode), errcode);
469 if (len >= sizeof buf)
470 exit_cleanup(RERR_MESSAGEIO);
472 rwrite(code, buf, len, 0);
475 void rflush(enum logcode code)
477 FILE *f;
479 if (am_daemon || code == FLOG)
480 return;
482 if (!am_server && (code == FINFO || code == FCLIENT))
483 f = stdout;
484 else
485 f = stderr;
487 fflush(f);
490 void remember_initial_stats(void)
492 initial_data_read = total_data_read;
493 initial_data_written = total_data_written;
496 /* A generic logging routine for send/recv, with parameter substitiution. */
497 static void log_formatted(enum logcode code, const char *format, const char *op,
498 struct file_struct *file, const char *fname, int iflags,
499 const char *hlink)
501 char buf[MAXPATHLEN+1024], buf2[MAXPATHLEN], fmt[32];
502 char *p, *s, *c;
503 const char *n;
504 size_t len, total;
505 int64 b;
507 *fmt = '%';
509 /* We expand % codes one by one in place in buf. We don't
510 * copy in the terminating null of the inserted strings, but
511 * rather keep going until we reach the null of the format. */
512 total = strlcpy(buf, format, sizeof buf);
513 if (total > MAXPATHLEN) {
514 rprintf(FERROR, "log-format string is WAY too long!\n");
515 exit_cleanup(RERR_MESSAGEIO);
517 buf[total++] = '\n';
518 buf[total] = '\0';
520 for (p = buf; (p = strchr(p, '%')) != NULL; ) {
521 int humanize = 0;
522 s = p++;
523 c = fmt + 1;
524 while (*p == '\'') {
525 humanize++;
526 p++;
528 if (*p == '-')
529 *c++ = *p++;
530 while (isDigit(p) && c - fmt < (int)(sizeof fmt) - 8)
531 *c++ = *p++;
532 while (*p == '\'') {
533 humanize++;
534 p++;
536 if (!*p)
537 break;
538 *c = '\0';
539 n = NULL;
541 /* Note for %h and %a: it doesn't matter what fd we pass to
542 * client_{name,addr} because rsync_module will already have
543 * forced the answer to be cached (assuming, of course, for %h
544 * that lp_reverse_lookup(module_id) is true). */
545 switch (*p) {
546 case 'h':
547 if (am_daemon) {
548 n = lp_reverse_lookup(module_id)
549 ? client_name(0) : undetermined_hostname;
551 break;
552 case 'a':
553 if (am_daemon)
554 n = client_addr(0);
555 break;
556 case 'l':
557 strlcat(fmt, "s", sizeof fmt);
558 snprintf(buf2, sizeof buf2, fmt,
559 do_big_num(F_LENGTH(file), humanize, NULL));
560 n = buf2;
561 break;
562 case 'U':
563 strlcat(fmt, "u", sizeof fmt);
564 snprintf(buf2, sizeof buf2, fmt,
565 uid_ndx ? F_OWNER(file) : 0);
566 n = buf2;
567 break;
568 case 'G':
569 if (!gid_ndx || file->flags & FLAG_SKIP_GROUP)
570 n = "DEFAULT";
571 else {
572 strlcat(fmt, "u", sizeof fmt);
573 snprintf(buf2, sizeof buf2, fmt,
574 F_GROUP(file));
575 n = buf2;
577 break;
578 case 'p':
579 strlcat(fmt, "d", sizeof fmt);
580 snprintf(buf2, sizeof buf2, fmt, (int)getpid());
581 n = buf2;
582 break;
583 case 'M':
584 n = c = timestring(file->modtime);
585 while ((c = strchr(c, ' ')) != NULL)
586 *c = '-';
587 break;
588 case 'B':
589 c = buf2 + MAXPATHLEN - PERMSTRING_SIZE - 1;
590 permstring(c, file->mode);
591 n = c + 1; /* skip the type char */
592 break;
593 case 'o':
594 n = op;
595 break;
596 case 'f':
597 if (fname) {
598 c = f_name_buf();
599 strlcpy(c, fname, MAXPATHLEN);
600 } else
601 c = f_name(file, NULL);
602 if (am_sender && F_PATHNAME(file)) {
603 pathjoin(buf2, sizeof buf2,
604 F_PATHNAME(file), c);
605 clean_fname(buf2, 0);
606 if (fmt[1]) {
607 strlcpy(c, buf2, MAXPATHLEN);
608 n = c;
609 } else
610 n = buf2;
611 } else if (am_daemon && *c != '/') {
612 pathjoin(buf2, sizeof buf2,
613 curr_dir + module_dirlen, c);
614 clean_fname(buf2, 0);
615 if (fmt[1]) {
616 strlcpy(c, buf2, MAXPATHLEN);
617 n = c;
618 } else
619 n = buf2;
620 } else {
621 clean_fname(c, 0);
622 n = c;
624 if (*n == '/')
625 n++;
626 break;
627 case 'n':
628 if (fname) {
629 c = f_name_buf();
630 strlcpy(c, fname, MAXPATHLEN);
631 } else
632 c = f_name(file, NULL);
633 if (S_ISDIR(file->mode))
634 strlcat(c, "/", MAXPATHLEN);
635 n = c;
636 break;
637 case 'L':
638 if (hlink && *hlink) {
639 n = hlink;
640 strlcpy(buf2, " => ", sizeof buf2);
641 } else if (S_ISLNK(file->mode) && !fname) {
642 n = F_SYMLINK(file);
643 strlcpy(buf2, " -> ", sizeof buf2);
644 } else {
645 n = "";
646 if (!fmt[1])
647 break;
648 strlcpy(buf2, " ", sizeof buf2);
650 strlcat(fmt, "s", sizeof fmt);
651 snprintf(buf2 + 4, sizeof buf2 - 4, fmt, n);
652 n = buf2;
653 break;
654 case 'm':
655 n = lp_name(module_id);
656 break;
657 case 't':
658 n = timestring(time(NULL));
659 break;
660 case 'P':
661 n = full_module_path;
662 break;
663 case 'u':
664 n = auth_user;
665 break;
666 case 'b':
667 case 'c':
668 if (!(iflags & ITEM_TRANSFER))
669 b = 0;
670 else if ((!!am_sender) ^ (*p == 'c'))
671 b = total_data_written - initial_data_written;
672 else
673 b = total_data_read - initial_data_read;
674 strlcat(fmt, "s", sizeof fmt);
675 snprintf(buf2, sizeof buf2, fmt,
676 do_big_num(b, humanize, NULL));
677 n = buf2;
678 break;
679 case 'C':
680 n = NULL;
681 if (S_ISREG(file->mode)) {
682 if (always_checksum && canonical_checksum(checksum_type))
683 n = sum_as_hex(checksum_type, F_SUM(file), 1);
684 else if (iflags & ITEM_TRANSFER && canonical_checksum(xfersum_type))
685 n = sum_as_hex(xfersum_type, sender_file_sum, 0);
687 if (!n) {
688 int sum_len = csum_len_for_type(always_checksum ? checksum_type : xfersum_type,
689 always_checksum);
690 memset(buf2, ' ', sum_len*2);
691 buf2[sum_len*2] = '\0';
692 n = buf2;
694 break;
695 case 'i':
696 if (iflags & ITEM_DELETED) {
697 n = "*deleting ";
698 break;
700 n = c = buf2 + MAXPATHLEN - 32;
701 c[0] = iflags & ITEM_LOCAL_CHANGE
702 ? iflags & ITEM_XNAME_FOLLOWS ? 'h' : 'c'
703 : !(iflags & ITEM_TRANSFER) ? '.'
704 : !local_server && *op == 's' ? '<' : '>';
705 if (S_ISLNK(file->mode)) {
706 c[1] = 'L';
707 c[3] = '.';
708 c[4] = !(iflags & ITEM_REPORT_TIME) ? '.'
709 : !preserve_times || !receiver_symlink_times
710 || (iflags & ITEM_REPORT_TIMEFAIL) ? 'T' : 't';
711 } else {
712 c[1] = S_ISDIR(file->mode) ? 'd'
713 : IS_SPECIAL(file->mode) ? 'S'
714 : IS_DEVICE(file->mode) ? 'D' : 'f';
715 c[3] = !(iflags & ITEM_REPORT_SIZE) ? '.' : 's';
716 c[4] = !(iflags & ITEM_REPORT_TIME) ? '.'
717 : !preserve_times ? 'T' : 't';
719 c[2] = !(iflags & ITEM_REPORT_CHANGE) ? '.' : 'c';
720 c[5] = !(iflags & ITEM_REPORT_PERMS) ? '.' : 'p';
721 c[6] = !(iflags & ITEM_REPORT_OWNER) ? '.' : 'o';
722 c[7] = !(iflags & ITEM_REPORT_GROUP) ? '.' : 'g';
723 c[8] = !(iflags & ITEM_REPORT_ATIME) ? '.'
724 : S_ISLNK(file->mode) ? 'U' : 'u';
725 c[9] = !(iflags & ITEM_REPORT_ACL) ? '.' : 'a';
726 c[10] = !(iflags & ITEM_REPORT_XATTR) ? '.' : 'x';
727 c[11] = '\0';
729 if (iflags & (ITEM_IS_NEW|ITEM_MISSING_DATA)) {
730 char ch = iflags & ITEM_IS_NEW ? '+' : '?';
731 int i;
732 for (i = 2; c[i]; i++)
733 c[i] = ch;
734 } else if (c[0] == '.' || c[0] == 'h' || c[0] == 'c') {
735 int i;
736 for (i = 2; c[i]; i++) {
737 if (c[i] != '.')
738 break;
740 if (!c[i]) {
741 for (i = 2; c[i]; i++)
742 c[i] = ' ';
745 break;
748 /* "n" is the string to be inserted in place of this % code. */
749 if (!n)
750 continue;
751 if (n != buf2 && fmt[1]) {
752 strlcat(fmt, "s", sizeof fmt);
753 snprintf(buf2, sizeof buf2, fmt, n);
754 n = buf2;
756 len = strlen(n);
758 /* Subtract the length of the escape from the string's size. */
759 total -= p - s + 1;
761 if (len + total >= (size_t)sizeof buf) {
762 rprintf(FERROR,
763 "buffer overflow expanding %%%c -- exiting\n",
764 p[0]);
765 exit_cleanup(RERR_MESSAGEIO);
768 /* Shuffle the rest of the string along to make space for n */
769 if (len != (size_t)(p - s + 1))
770 memmove(s + len, p + 1, total - (s - buf) + 1);
771 total += len;
773 /* Insert the contents of string "n", but NOT its null. */
774 if (len)
775 memcpy(s, n, len);
777 /* Skip over inserted string; continue looking */
778 p = s + len;
781 rwrite(code, buf, total, 0);
784 /* Return 1 if the format escape is in the log-format string (e.g. look for
785 * the 'b' in the "%9b" format escape). */
786 int log_format_has(const char *format, char esc)
788 const char *p;
790 if (!format)
791 return 0;
793 for (p = format; (p = strchr(p, '%')) != NULL; ) {
794 for (p++; *p == '\''; p++) {} /*SHARED ITERATOR*/
795 if (*p == '-')
796 p++;
797 while (isDigit(p))
798 p++;
799 while (*p == '\'') p++;
800 if (!*p)
801 break;
802 if (*p == esc)
803 return 1;
805 return 0;
808 /* Log the transfer of a file. If the code is FCLIENT, the output just goes
809 * to stdout. If it is FLOG, it just goes to the log file. Otherwise we
810 * output to both. */
811 void log_item(enum logcode code, struct file_struct *file, int iflags, const char *hlink)
813 const char *s_or_r = am_sender ? "send" : "recv";
815 if (code != FLOG && stdout_format && !am_server)
816 log_formatted(FCLIENT, stdout_format, s_or_r, file, NULL, iflags, hlink);
817 if (code != FCLIENT && logfile_format && *logfile_format)
818 log_formatted(FLOG, logfile_format, s_or_r, file, NULL, iflags, hlink);
821 void maybe_log_item(struct file_struct *file, int iflags, int itemizing,
822 const char *buf)
824 int significant_flags = iflags & SIGNIFICANT_ITEM_FLAGS;
825 int see_item = itemizing && (significant_flags || *buf
826 || stdout_format_has_i > 1 || (INFO_GTE(NAME, 2) && stdout_format_has_i));
827 int local_change = iflags & ITEM_LOCAL_CHANGE && significant_flags;
828 if (am_server) {
829 if (logfile_name && !dry_run && see_item
830 && (significant_flags || logfile_format_has_i))
831 log_item(FLOG, file, iflags, buf);
832 } else if (see_item || local_change || *buf
833 || (S_ISDIR(file->mode) && significant_flags)) {
834 enum logcode code = significant_flags || logfile_format_has_i ? FINFO : FCLIENT;
835 log_item(code, file, iflags, buf);
839 void log_delete(const char *fname, int mode)
841 static struct {
842 union file_extras ex[4]; /* just in case... */
843 struct file_struct file;
844 } x; /* Zero-initialized due to static declaration. */
845 int len = strlen(fname);
846 const char *fmt;
848 x.file.mode = mode;
850 if (am_server && protocol_version >= 29 && len < MAXPATHLEN) {
851 if (S_ISDIR(mode))
852 len++; /* directories include trailing null */
853 send_msg(MSG_DELETED, fname, len, am_generator);
854 } else if (!INFO_GTE(DEL, 1) && !stdout_format)
856 else {
857 fmt = stdout_format_has_o_or_i ? stdout_format : "deleting %n";
858 log_formatted(FCLIENT, fmt, "del.", &x.file, fname, ITEM_DELETED, NULL);
861 if (!logfile_name || dry_run || !logfile_format)
862 return;
864 fmt = logfile_format_has_o_or_i ? logfile_format : "deleting %n";
865 log_formatted(FLOG, fmt, "del.", &x.file, fname, ITEM_DELETED, NULL);
869 * Called when the transfer is interrupted for some reason.
871 * Code is one of the RERR_* codes from errcode.h, or terminating
872 * successfully.
874 void log_exit(int code, const char *file, int line)
876 if (code == 0) {
877 rprintf(FLOG,"sent %s bytes received %s bytes total size %s\n",
878 big_num(stats.total_written),
879 big_num(stats.total_read),
880 big_num(stats.total_size));
881 } else if (am_server != 2) {
882 const char *name;
884 name = rerr_name(code);
885 if (!name)
886 name = "unexplained error";
888 /* VANISHED is not an error, only a warning */
889 if (code == RERR_VANISHED) {
890 rprintf(FWARNING, "rsync warning: %s (code %d) at %s(%d) [%s=%s]\n",
891 name, code, file, line, who_am_i(), RSYNC_VERSION);
892 } else {
893 rprintf(FERROR, "rsync error: %s (code %d) at %s(%d) [%s=%s]\n",
894 name, code, file, line, who_am_i(), RSYNC_VERSION);