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