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