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