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