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