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