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