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