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