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