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