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