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