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