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