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