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