Made log_init() call timestring() instead of just localtime() prior to a
[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 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         if (log_initialised)
158                 return;
159         log_initialised = 1;
160
161         /* This looks pointless, but it is needed in order for the
162          * C library on some systems to fetch the timezone info
163          * before the chroot. */
164         timestring(time(NULL));
165
166         /* optionally use a log file instead of syslog */
167         logfname = lp_log_file();
168         if (logfname && *logfname)
169                 logfile_open();
170         else
171                 syslog_init();
172 }
173
174 void logfile_close(void)
175 {
176         if (logfile) {
177                 logfile_was_closed = 1;
178                 fclose(logfile);
179                 logfile = NULL;
180         }
181 }
182
183 void logfile_reopen(void)
184 {
185         if (logfile_was_closed) {
186                 logfile_was_closed = 0;
187                 logfile_open();
188         }
189 }
190
191 static void filtered_fwrite(FILE *f, const char *buf, int len, int use_isprint)
192 {
193         const char *s, *end = buf + len;
194         for (s = buf; s < end; s++) {
195                 if ((s < end - 4
196                   && *s == '\\' && s[1] == '#'
197                   && isdigit(*(uchar*)(s+2))
198                   && isdigit(*(uchar*)(s+3))
199                   && isdigit(*(uchar*)(s+4)))
200                  || (*s != '\t'
201                   && ((use_isprint && !isprint(*(uchar*)s))
202                    || *(uchar*)s < ' '))) {
203                         if (s != buf && fwrite(buf, s - buf, 1, f) != 1)
204                                 exit_cleanup(RERR_MESSAGEIO);
205                         fprintf(f, "\\#%03o", *(uchar*)s);
206                         buf = s + 1;
207                 }
208         }
209         if (buf != end && fwrite(buf, end - buf, 1, f) != 1)
210                 exit_cleanup(RERR_MESSAGEIO);
211 }
212
213 /* this is the underlying (unformatted) rsync debugging function. Call
214  * it with FINFO, FERROR or FLOG.  Note: recursion can happen with
215  * certain fatal conditions. */
216 void rwrite(enum logcode code, char *buf, int len)
217 {
218         int trailing_CR_or_NL;
219         FILE *f = NULL;
220
221         if (len < 0)
222                 exit_cleanup(RERR_MESSAGEIO);
223
224         if (quiet && code == FINFO)
225                 return;
226
227         if (am_server && msg_fd_out >= 0) {
228                 /* Pass the message to our sibling. */
229                 send_msg((enum msgcode)code, buf, len);
230                 return;
231         }
232
233         if (code == FSOCKERR) /* This gets simplified for a non-sibling. */
234                 code = FERROR;
235
236         if (code == FCLIENT)
237                 code = FINFO;
238         else if (am_daemon) {
239                 static int in_block;
240                 char msg[2048];
241                 int priority = code == FERROR ? LOG_WARNING : LOG_INFO;
242
243                 if (in_block)
244                         return;
245                 in_block = 1;
246                 if (!log_initialised)
247                         log_init();
248                 strlcpy(msg, buf, MIN((int)sizeof msg, len + 1));
249                 logit(priority, msg);
250                 in_block = 0;
251
252                 if (code == FLOG || !am_server)
253                         return;
254         } else if (code == FLOG)
255                 return;
256
257         if (am_server) {
258                 /* Pass the message to the non-server side. */
259                 if (io_multiplex_write((enum msgcode)code, buf, len))
260                         return;
261                 if (am_daemon) {
262                         /* TODO: can we send the error to the user somehow? */
263                         return;
264                 }
265         }
266
267         switch (code) {
268         case FERROR:
269                 log_got_error = 1;
270                 f = stderr;
271                 goto pre_scan;
272         case FINFO:
273                 f = am_server ? stderr : stdout;
274         pre_scan:
275                 while (len > 1 && *buf == '\n') {
276                         fputc(*buf, f);
277                         buf++;
278                         len--;
279                 }
280                 break;
281         case FNAME:
282                 f = am_server ? stderr : stdout;
283                 break;
284         default:
285                 exit_cleanup(RERR_MESSAGEIO);
286         }
287
288         trailing_CR_or_NL = len && (buf[len-1] == '\n' || buf[len-1] == '\r')
289                           ? buf[--len] : 0;
290
291 #if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H
292         if (ic_chck != (iconv_t)-1) {
293                 char convbuf[1024];
294                 char *in_buf = buf, *out_buf = convbuf;
295                 size_t in_cnt = len, out_cnt = sizeof convbuf - 1;
296
297                 iconv(ic_chck, NULL, 0, NULL, 0);
298                 while (iconv(ic_chck, &in_buf,&in_cnt,
299                                  &out_buf,&out_cnt) == (size_t)-1) {
300                         if (out_buf != convbuf) {
301                                 filtered_fwrite(f, convbuf, out_buf - convbuf, 0);
302                                 out_buf = convbuf;
303                                 out_cnt = sizeof convbuf - 1;
304                         }
305                         if (errno == E2BIG)
306                                 continue;
307                         fprintf(f, "\\#%03o", *(uchar*)in_buf++);
308                         in_cnt--;
309                 }
310                 if (out_buf != convbuf)
311                         filtered_fwrite(f, convbuf, out_buf - convbuf, 0);
312         } else
313 #endif
314                 filtered_fwrite(f, buf, len, !allow_8bit_chars);
315
316         if (trailing_CR_or_NL) {
317                 fputc(trailing_CR_or_NL, f);
318                 fflush(f);
319         }
320 }
321
322 /* This is the rsync debugging function. Call it with FINFO, FERROR or
323  * FLOG. */
324 void rprintf(enum logcode code, const char *format, ...)
325 {
326         va_list ap;
327         char buf[BIGPATHBUFLEN];
328         size_t len;
329
330         va_start(ap, format);
331         len = vsnprintf(buf, sizeof buf, format, ap);
332         va_end(ap);
333
334         /* Deal with buffer overruns.  Instead of panicking, just
335          * truncate the resulting string.  (Note that configure ensures
336          * that we have a vsnprintf() that doesn't ever return -1.) */
337         if (len > sizeof buf - 1) {
338                 static const char ellipsis[] = "[...]";
339
340                 /* Reset length, and zero-terminate the end of our buffer */
341                 len = sizeof buf - 1;
342                 buf[len] = '\0';
343
344                 /* Copy the ellipsis to the end of the string, but give
345                  * us one extra character:
346                  *
347                  *                  v--- null byte at buf[sizeof buf - 1]
348                  *        abcdefghij0
349                  *     -> abcd[...]00  <-- now two null bytes at end
350                  *
351                  * If the input format string has a trailing newline,
352                  * we copy it into that extra null; if it doesn't, well,
353                  * all we lose is one byte.  */
354                 memcpy(buf+len-sizeof ellipsis, ellipsis, sizeof ellipsis);
355                 if (format[strlen(format)-1] == '\n') {
356                         buf[len-1] = '\n';
357                 }
358         }
359
360         rwrite(code, buf, len);
361 }
362
363 /* This is like rprintf, but it also tries to print some
364  * representation of the error code.  Normally errcode = errno.
365  *
366  * Unlike rprintf, this always adds a newline and there should not be
367  * one in the format string.
368  *
369  * Note that since strerror might involve dynamically loading a
370  * message catalog we need to call it once before chroot-ing. */
371 void rsyserr(enum logcode code, int errcode, const char *format, ...)
372 {
373         va_list ap;
374         char buf[BIGPATHBUFLEN];
375         size_t len;
376
377         strcpy(buf, RSYNC_NAME ": ");
378         len = (sizeof RSYNC_NAME ": ") - 1;
379
380         va_start(ap, format);
381         len += vsnprintf(buf + len, sizeof buf - len, format, ap);
382         va_end(ap);
383
384         if (len < sizeof buf) {
385                 len += snprintf(buf + len, sizeof buf - len,
386                                 ": %s (%d)\n", strerror(errcode), errcode);
387         }
388         if (len >= sizeof buf)
389                 exit_cleanup(RERR_MESSAGEIO);
390
391         rwrite(code, buf, len);
392 }
393
394 void rflush(enum logcode code)
395 {
396         FILE *f = NULL;
397
398         if (am_daemon) {
399                 return;
400         }
401
402         if (code == FLOG) {
403                 return;
404         }
405
406         if (code == FERROR) {
407                 f = stderr;
408         }
409
410         if (code == FINFO) {
411                 if (am_server)
412                         f = stderr;
413                 else
414                         f = stdout;
415         }
416
417         if (!f) exit_cleanup(RERR_MESSAGEIO);
418         fflush(f);
419 }
420
421 /* a generic logging routine for send/recv, with parameter
422  * substitiution */
423 static void log_formatted(enum logcode code, char *format, char *op,
424                           struct file_struct *file, struct stats *initial_stats,
425                           int iflags, char *hlink)
426 {
427         char buf[MAXPATHLEN+1024], buf2[MAXPATHLEN], fmt[32];
428         char *p, *s, *n;
429         size_t len, total;
430         int64 b;
431
432         *fmt = '%';
433
434         /* We expand % codes one by one in place in buf.  We don't
435          * copy in the terminating null of the inserted strings, but
436          * rather keep going until we reach the null of the format. */
437         total = strlcpy(buf, format, sizeof buf);
438         if (total > MAXPATHLEN) {
439                 rprintf(FERROR, "log-format string is WAY too long!\n");
440                 exit_cleanup(RERR_MESSAGEIO);
441         }
442         buf[total++] = '\n';
443         buf[total] = '\0';
444
445         for (p = buf; (p = strchr(p, '%')) != NULL; ) {
446                 s = p++;
447                 n = fmt + 1;
448                 if (*p == '-')
449                         *n++ = *p++;
450                 while (isdigit(*(uchar*)p) && n - fmt < (int)(sizeof fmt) - 8)
451                         *n++ = *p++;
452                 if (!*p)
453                         break;
454                 *n = '\0';
455                 n = NULL;
456
457                 switch (*p) {
458                 case 'h':
459                         if (am_daemon)
460                                 n = client_name(0);
461                         break;
462                 case 'a':
463                         if (am_daemon)
464                                 n = client_addr(0);
465                         break;
466                 case 'l':
467                         strlcat(fmt, ".0f", sizeof fmt);
468                         snprintf(buf2, sizeof buf2, fmt,
469                                  (double)file->length);
470                         n = buf2;
471                         break;
472                 case 'U':
473                         strlcat(fmt, "ld", sizeof fmt);
474                         snprintf(buf2, sizeof buf2, fmt,
475                                  (long)file->uid);
476                         n = buf2;
477                         break;
478                 case 'G':
479                         if (file->gid == GID_NONE)
480                                 n = "DEFAULT";
481                         else {
482                                 strlcat(fmt, "ld", sizeof fmt);
483                                 snprintf(buf2, sizeof buf2, fmt,
484                                          (long)file->gid);
485                                 n = buf2;
486                         }
487                         break;
488                 case 'p':
489                         strlcat(fmt, "ld", sizeof fmt);
490                         snprintf(buf2, sizeof buf2, fmt,
491                                  (long)getpid());
492                         n = buf2;
493                         break;
494                 case 'M':
495                         n = timestring(file->modtime);
496                         {
497                                 char *cp = n;
498                                 while ((cp = strchr(cp, ' ')) != NULL)
499                                         *cp = '-';
500                         }
501                         break;
502                 case 'B':
503                         n = buf2 + MAXPATHLEN - PERMSTRING_SIZE;
504                         permstring(n - 1, file->mode); /* skip the type char */
505                         break;
506                 case 'o':
507                         n = op;
508                         break;
509                 case 'f':
510                         n = f_name(file, NULL);
511                         if (am_sender && file->dir.root) {
512                                 pathjoin(buf2, sizeof buf2,
513                                          file->dir.root, n);
514                                 clean_fname(buf2, 0);
515                                 if (fmt[1])
516                                         strlcpy(n, buf2, MAXPATHLEN);
517                                 else
518                                         n = buf2;
519                         } else
520                                 clean_fname(n, 0);
521                         if (*n == '/')
522                                 n++;
523                         break;
524                 case 'n':
525                         n = f_name(file, NULL);
526                         if (S_ISDIR(file->mode))
527                                 strlcat(n, "/", MAXPATHLEN);
528                         break;
529                 case 'L':
530                         if (hlink && *hlink) {
531                                 n = hlink;
532                                 strcpy(buf2, " => ");
533                         } else if (S_ISLNK(file->mode) && file->u.link) {
534                                 n = file->u.link;
535                                 strcpy(buf2, " -> ");
536                         } else {
537                                 n = "";
538                                 if (!fmt[1])
539                                         break;
540                                 strcpy(buf2, "    ");
541                         }
542                         strlcat(fmt, "s", sizeof fmt);
543                         snprintf(buf2 + 4, sizeof buf2 - 4, fmt, n);
544                         n = buf2;
545                         break;
546                 case 'm':
547                         n = lp_name(module_id);
548                         break;
549                 case 't':
550                         n = timestring(time(NULL));
551                         break;
552                 case 'P':
553                         n = lp_path(module_id);
554                         break;
555                 case 'u':
556                         n = auth_user;
557                         break;
558                 case 'b':
559                         if (am_sender) {
560                                 b = stats.total_written -
561                                         initial_stats->total_written;
562                         } else {
563                                 b = stats.total_read -
564                                         initial_stats->total_read;
565                         }
566                         strlcat(fmt, ".0f", sizeof fmt);
567                         snprintf(buf2, sizeof buf2, fmt, (double)b);
568                         n = buf2;
569                         break;
570                 case 'c':
571                         if (!am_sender) {
572                                 b = stats.total_written -
573                                         initial_stats->total_written;
574                         } else {
575                                 b = stats.total_read -
576                                         initial_stats->total_read;
577                         }
578                         strlcat(fmt, ".0f", sizeof fmt);
579                         snprintf(buf2, sizeof buf2, fmt, (double)b);
580                         n = buf2;
581                         break;
582                 case 'i':
583                         if (iflags & ITEM_DELETED) {
584                                 n = "*deleting";
585                                 break;
586                         }
587                         n = buf2 + MAXPATHLEN - 32;
588                         n[0] = iflags & ITEM_LOCAL_CHANGE
589                               ? iflags & ITEM_XNAME_FOLLOWS ? 'h' : 'c'
590                              : !(iflags & ITEM_TRANSFER) ? '.'
591                              : !local_server && *op == 's' ? '<' : '>';
592                         n[1] = S_ISDIR(file->mode) ? 'd'
593                              : IS_SPECIAL(file->mode) ? 'S'
594                              : IS_DEVICE(file->mode) ? 'D'
595                              : S_ISLNK(file->mode) ? 'L' : 'f';
596                         n[2] = !(iflags & ITEM_REPORT_CHECKSUM) ? '.' : 'c';
597                         n[3] = !(iflags & ITEM_REPORT_SIZE) ? '.' : 's';
598                         n[4] = !(iflags & ITEM_REPORT_TIME) ? '.'
599                              : !preserve_times || S_ISLNK(file->mode) ? 'T' : 't';
600                         n[5] = !(iflags & ITEM_REPORT_PERMS) ? '.' : 'p';
601                         n[6] = !(iflags & ITEM_REPORT_OWNER) ? '.' : 'o';
602                         n[7] = !(iflags & ITEM_REPORT_GROUP) ? '.' : 'g';
603                         n[8] = '.';
604                         n[9] = '\0';
605
606                         if (iflags & (ITEM_IS_NEW|ITEM_MISSING_DATA)) {
607                                 char ch = iflags & ITEM_IS_NEW ? '+' : '?';
608                                 int i;
609                                 for (i = 2; n[i]; i++)
610                                         n[i] = ch;
611                         } else if (n[0] == '.' || n[0] == 'h'
612                                 || (n[0] == 'c' && n[1] == 'f')) {
613                                 int i;
614                                 for (i = 2; n[i]; i++) {
615                                         if (n[i] != '.')
616                                                 break;
617                                 }
618                                 if (!n[i]) {
619                                         for (i = 2; n[i]; i++)
620                                                 n[i] = ' ';
621                                 }
622                         }
623                         break;
624                 }
625
626                 /* "n" is the string to be inserted in place of this % code. */
627                 if (!n)
628                         continue;
629                 if (n != buf2 && fmt[1]) {
630                         strlcat(fmt, "s", sizeof fmt);
631                         snprintf(buf2, sizeof buf2, fmt, n);
632                         n = buf2;
633                 }
634                 len = strlen(n);
635
636                 /* Subtract the length of the escape from the string's size. */
637                 total -= p - s + 1;
638
639                 if (len + total >= (size_t)sizeof buf) {
640                         rprintf(FERROR,
641                                 "buffer overflow expanding %%%c -- exiting\n",
642                                 p[0]);
643                         exit_cleanup(RERR_MESSAGEIO);
644                 }
645
646                 /* Shuffle the rest of the string along to make space for n */
647                 if (len != (size_t)(p - s + 1))
648                         memmove(s + len, p + 1, total - (s - buf) + 1);
649                 total += len;
650
651                 /* Insert the contents of string "n", but NOT its null. */
652                 if (len)
653                         memcpy(s, n, len);
654
655                 /* Skip over inserted string; continue looking */
656                 p = s + len;
657         }
658
659         rwrite(code, buf, total);
660 }
661
662 /* Return 1 if the format escape is in the log-format string (e.g. look for
663  * the 'b' in the "%9b" format escape). */
664 int log_format_has(const char *format, char esc)
665 {
666         const char *p;
667
668         if (!format)
669                 return 0;
670
671         for (p = format; (p = strchr(p, '%')) != NULL; ) {
672                 if (*++p == '-')
673                         p++;
674                 while (isdigit(*(uchar*)p))
675                         p++;
676                 if (!*p)
677                         break;
678                 if (*p == esc)
679                         return 1;
680         }
681         return 0;
682 }
683
684 /* log the transfer of a file */
685 void log_item(struct file_struct *file, struct stats *initial_stats,
686               int iflags, char *hlink)
687 {
688         char *s_or_r = am_sender ? "send" : "recv";
689
690         if (lp_transfer_logging(module_id)) {
691                 log_formatted(FLOG, lp_log_format(module_id), s_or_r,
692                               file, initial_stats, iflags, hlink);
693         } else if (log_format && !am_server) {
694                 log_formatted(FNAME, log_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                 || log_format_has_i > 1 || (verbose > 1 && log_format_has_i));
705         int local_change = iflags & ITEM_LOCAL_CHANGE && significant_flags;
706         if (am_server) {
707                 if (am_daemon && !dry_run && see_item)
708                         log_item(file, &stats, iflags, buf);
709         } else if (see_item || local_change || *buf
710             || (S_ISDIR(file->mode) && significant_flags))
711                 log_item(file, &stats, iflags, buf);
712 }
713
714 void log_delete(char *fname, int mode)
715 {
716         static struct file_struct file;
717         int len = strlen(fname);
718         char *fmt;
719
720         file.mode = mode;
721         file.basename = fname;
722
723         if (!verbose && !log_format)
724                 ;
725         else if (am_server && protocol_version >= 29 && len < MAXPATHLEN) {
726                 if (S_ISDIR(mode))
727                         len++; /* directories include trailing null */
728                 send_msg(MSG_DELETED, fname, len);
729         } else {
730                 fmt = log_format_has_o_or_i ? log_format : "deleting %n";
731                 log_formatted(FCLIENT, fmt, "del.", &file, &stats,
732                               ITEM_DELETED, NULL);
733         }
734
735         if (!am_daemon || dry_run || !lp_transfer_logging(module_id))
736                 return;
737
738         fmt = daemon_log_format_has_o_or_i ? lp_log_format(module_id) : "deleting %n";
739         log_formatted(FLOG, fmt, "del.", &file, &stats, ITEM_DELETED, NULL);
740 }
741
742 /*
743  * Called when the transfer is interrupted for some reason.
744  *
745  * Code is one of the RERR_* codes from errcode.h, or terminating
746  * successfully.
747  */
748 void log_exit(int code, const char *file, int line)
749 {
750         if (code == 0) {
751                 rprintf(FLOG,"sent %.0f bytes  received %.0f bytes  total size %.0f\n",
752                         (double)stats.total_written,
753                         (double)stats.total_read,
754                         (double)stats.total_size);
755         } else {
756                 const char *name;
757
758                 name = rerr_name(code);
759                 if (!name)
760                         name = "unexplained error";
761
762                 /* VANISHED is not an error, only a warning */
763                 if (code == RERR_VANISHED) {
764                         rprintf(FINFO, "rsync warning: %s (code %d) at %s(%d) [%s=%s]\n",
765                                 name, code, file, line, who_am_i(), RSYNC_VERSION);
766                 } else {
767                         rprintf(FERROR, "rsync error: %s (code %d) at %s(%d) [%s=%s]\n",
768                                 name, code, file, line, who_am_i(), RSYNC_VERSION);
769                 }
770         }
771 }