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