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