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