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