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