- Typical tranfers now save 12-20 bytes per file because several vars
[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>
6 * Copyright (C) 2003, 2004, 2005, 2006 Wayne Davison
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
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
WD
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
0f78b815 21 */
0b76cd63 22
0b76cd63 23#include "rsync.h"
c8563142 24#if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H
9a31746b
WD
25#include <iconv.h>
26#endif
0b76cd63 27
41b5b5e7 28extern int verbose;
088aff1a 29extern int dry_run;
548abf96
WD
30extern int am_daemon;
31extern int am_server;
32extern int am_sender;
c1759b9f 33extern int local_server;
548abf96
WD
34extern int quiet;
35extern int module_id;
a644fc3c 36extern int msg_fd_out;
507433f6 37extern int allow_8bit_chars;
19bc826d 38extern int protocol_version;
ef74f5d6 39extern int preserve_times;
82ad07c4
WD
40extern int preserve_uid;
41extern int preserve_gid;
42extern int flist_extra_ndx;
43extern int file_struct_len;
b5343510
WD
44extern int stdout_format_has_i;
45extern int stdout_format_has_o_or_i;
d0133e6e 46extern int logfile_format_has_i;
13b597fa 47extern int logfile_format_has_o_or_i;
c5b7aa15 48extern mode_t orig_umask;
548abf96 49extern char *auth_user;
b5343510 50extern char *stdout_format;
13b597fa
WD
51extern char *logfile_format;
52extern char *logfile_name;
595251de 53#if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H
9a31746b
WD
54extern iconv_t ic_chck;
55#endif
6fb7cc38
WD
56extern char curr_dir[];
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
8fcdc444 64int log_got_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
be0602ec
WD
238 * it with FINFO, FERROR or FLOG. Note: recursion can happen with
239 * certain fatal conditions. */
4a19c3b2 240void rwrite(enum logcode code, const char *buf, int len)
0b76cd63 241{
be0602ec 242 int trailing_CR_or_NL;
8fcdc444 243 FILE *f = NULL;
8d9dc9f9 244
a644fc3c 245 if (len < 0)
25f637a3 246 exit_cleanup(RERR_MESSAGEIO);
0b76cd63 247
0bb4d176
WD
248 if (am_server && msg_fd_out >= 0) {
249 /* Pass the message to our sibling. */
250 send_msg((enum msgcode)code, buf, len);
11a5a3c7
AT
251 return;
252 }
253
b66e31bf
WD
254 if (code == FSOCKERR) /* This gets simplified for a non-sibling. */
255 code = FERROR;
256
1eec003a
WD
257 if (code == FCLIENT)
258 code = FINFO;
d0133e6e 259 else if (am_daemon || logfile_name) {
0bb4d176 260 static int in_block;
d0133e6e 261 char msg[2048];
0bb4d176
WD
262 int priority = code == FERROR ? LOG_WARNING : LOG_INFO;
263
264 if (in_block)
a644fc3c 265 return;
0bb4d176
WD
266 in_block = 1;
267 if (!log_initialised)
04c84119 268 log_init(0);
0bb4d176 269 strlcpy(msg, buf, MIN((int)sizeof msg, len + 1));
d0133e6e 270 logit(priority, msg);
0bb4d176
WD
271 in_block = 0;
272
13b597fa 273 if (code == FLOG || (am_daemon && !am_server))
a644fc3c 274 return;
0bb4d176 275 } else if (code == FLOG)
ff8b29b8 276 return;
0bb4d176 277
b3e8e7c7
WD
278 if (quiet && code != FERROR)
279 return;
280
0bb4d176
WD
281 if (am_server) {
282 /* Pass the message to the non-server side. */
f1c9bcd0 283 if (send_msg((enum msgcode)code, buf, len))
0bb4d176
WD
284 return;
285 if (am_daemon) {
286 /* TODO: can we send the error to the user somehow? */
287 return;
288 }
0b76cd63
AT
289 }
290
be0602ec
WD
291 switch (code) {
292 case FERROR:
ff81e809 293 log_got_error = 1;
ff8b29b8 294 f = stderr;
be0602ec 295 break;
d0133e6e 296 case FINFO:
be0602ec
WD
297 f = am_server ? stderr : stdout;
298 break;
299 default:
25f637a3 300 exit_cleanup(RERR_MESSAGEIO);
be0602ec 301 }
0b76cd63 302
be0602ec
WD
303 trailing_CR_or_NL = len && (buf[len-1] == '\n' || buf[len-1] == '\r')
304 ? buf[--len] : 0;
8d9dc9f9 305
595251de 306#if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H
60d465db
WD
307#ifndef ICONV_CONST
308#define ICONV_CONST
309#endif
9a31746b
WD
310 if (ic_chck != (iconv_t)-1) {
311 char convbuf[1024];
60d465db 312 ICONV_CONST char *in_buf = (ICONV_CONST char *)buf;
4a19c3b2 313 char *out_buf = convbuf;
9a31746b
WD
314 size_t in_cnt = len, out_cnt = sizeof convbuf - 1;
315
316 iconv(ic_chck, NULL, 0, NULL, 0);
317 while (iconv(ic_chck, &in_buf,&in_cnt,
60d465db 318 &out_buf,&out_cnt) == (size_t)-1) {
9a31746b 319 if (out_buf != convbuf) {
3648ab3a 320 filtered_fwrite(f, convbuf, out_buf - convbuf, 0);
9a31746b
WD
321 out_buf = convbuf;
322 out_cnt = sizeof convbuf - 1;
323 }
324 if (errno == E2BIG)
325 continue;
3648ab3a 326 fprintf(f, "\\#%03o", *(uchar*)in_buf++);
9a31746b
WD
327 in_cnt--;
328 }
329 if (out_buf != convbuf)
3648ab3a 330 filtered_fwrite(f, convbuf, out_buf - convbuf, 0);
9a31746b
WD
331 } else
332#endif
507433f6 333 filtered_fwrite(f, buf, len, !allow_8bit_chars);
be0602ec
WD
334
335 if (trailing_CR_or_NL) {
336 fputc(trailing_CR_or_NL, f);
0bb4d176 337 fflush(f);
be0602ec 338 }
0b76cd63 339}
0455cd93 340
a039749b
MP
341/* This is the rsync debugging function. Call it with FINFO, FERROR or
342 * FLOG. */
343void rprintf(enum logcode code, const char *format, ...)
554e0a8d 344{
4a7319be 345 va_list ap;
b74b3d53 346 char buf[BIGPATHBUFLEN];
8fcdc444 347 size_t len;
554e0a8d
AT
348
349 va_start(ap, format);
ef74f5d6 350 len = vsnprintf(buf, sizeof buf, format, ap);
554e0a8d
AT
351 va_end(ap);
352
b2f02464 353 /* Deal with buffer overruns. Instead of panicking, just
8fcdc444
WD
354 * truncate the resulting string. (Note that configure ensures
355 * that we have a vsnprintf() that doesn't ever return -1.) */
356 if (len > sizeof buf - 1) {
b74b3d53 357 static const char ellipsis[] = "[...]";
b2f02464
MP
358
359 /* Reset length, and zero-terminate the end of our buffer */
ef74f5d6 360 len = sizeof buf - 1;
b2f02464
MP
361 buf[len] = '\0';
362
363 /* Copy the ellipsis to the end of the string, but give
364 * us one extra character:
365 *
ef74f5d6 366 * v--- null byte at buf[sizeof buf - 1]
b2f02464
MP
367 * abcdefghij0
368 * -> abcd[...]00 <-- now two null bytes at end
369 *
370 * If the input format string has a trailing newline,
371 * we copy it into that extra null; if it doesn't, well,
372 * all we lose is one byte. */
b74b3d53 373 memcpy(buf+len-sizeof ellipsis, ellipsis, sizeof ellipsis);
b2f02464
MP
374 if (format[strlen(format)-1] == '\n') {
375 buf[len-1] = '\n';
376 }
377 }
554e0a8d 378
ff41a59f 379 rwrite(code, buf, len);
554e0a8d 380}
0b76cd63 381
a039749b
MP
382/* This is like rprintf, but it also tries to print some
383 * representation of the error code. Normally errcode = errno.
384 *
385 * Unlike rprintf, this always adds a newline and there should not be
386 * one in the format string.
387 *
388 * Note that since strerror might involve dynamically loading a
389 * message catalog we need to call it once before chroot-ing. */
390void rsyserr(enum logcode code, int errcode, const char *format, ...)
391{
4a7319be 392 va_list ap;
b74b3d53 393 char buf[BIGPATHBUFLEN];
8fcdc444
WD
394 size_t len;
395
c9bce0b8 396 strlcpy(buf, RSYNC_NAME ": ", sizeof buf);
8fcdc444 397 len = (sizeof RSYNC_NAME ": ") - 1;
a039749b
MP
398
399 va_start(ap, format);
8fcdc444 400 len += vsnprintf(buf + len, sizeof buf - len, format, ap);
a039749b
MP
401 va_end(ap);
402
8fcdc444
WD
403 if (len < sizeof buf) {
404 len += snprintf(buf + len, sizeof buf - len,
405 ": %s (%d)\n", strerror(errcode), errcode);
406 }
407 if (len >= sizeof buf)
25f637a3 408 exit_cleanup(RERR_MESSAGEIO);
a039749b 409
a039749b
MP
410 rwrite(code, buf, len);
411}
412
ff41a59f 413void rflush(enum logcode code)
0b76cd63
AT
414{
415 FILE *f = NULL;
0455cd93 416
13b597fa 417 if (am_daemon || code == FLOG)
0b76cd63 418 return;
0b76cd63 419
13b597fa 420 if (code == FERROR || am_server)
0b76cd63 421 f = stderr;
13b597fa
WD
422 else
423 f = stdout;
0b76cd63 424
0b76cd63
AT
425 fflush(f);
426}
427
b5343510 428/* A generic logging routine for send/recv, with parameter substitiution. */
4a19c3b2 429static void log_formatted(enum logcode code, const char *format, const char *op,
afc65a5a 430 struct file_struct *file, struct stats *initial_stats,
4a19c3b2 431 int iflags, const char *hlink)
e08bfe12 432{
ddd74b67 433 char buf[MAXPATHLEN+1024], buf2[MAXPATHLEN], fmt[32];
4a19c3b2
WD
434 char *p, *s, *c;
435 const char *n;
ef74f5d6 436 size_t len, total;
1b7c47cb 437 int64 b;
e08bfe12 438
ddd74b67
WD
439 *fmt = '%';
440
aa126974 441 /* We expand % codes one by one in place in buf. We don't
126e7aff
WD
442 * copy in the terminating null of the inserted strings, but
443 * rather keep going until we reach the null of the format. */
ef74f5d6 444 total = strlcpy(buf, format, sizeof buf);
d9c0051f
WD
445 if (total > MAXPATHLEN) {
446 rprintf(FERROR, "log-format string is WAY too long!\n");
25f637a3 447 exit_cleanup(RERR_MESSAGEIO);
d9c0051f
WD
448 }
449 buf[total++] = '\n';
450 buf[total] = '\0';
0455cd93 451
16f960fe 452 for (p = buf; (p = strchr(p, '%')) != NULL; ) {
ddd74b67 453 s = p++;
4a19c3b2 454 c = fmt + 1;
9baed760 455 if (*p == '-')
4a19c3b2
WD
456 *c++ = *p++;
457 while (isDigit(p) && c - fmt < (int)(sizeof fmt) - 8)
458 *c++ = *p++;
e145d51b
WD
459 if (!*p)
460 break;
4a19c3b2 461 *c = '\0';
e08bfe12 462 n = NULL;
e08bfe12 463
0455cd93 464 switch (*p) {
b74b3d53
WD
465 case 'h':
466 if (am_daemon)
467 n = client_name(0);
468 break;
469 case 'a':
470 if (am_daemon)
471 n = client_addr(0);
472 break;
4a7319be 473 case 'l':
ddd74b67
WD
474 strlcat(fmt, ".0f", sizeof fmt);
475 snprintf(buf2, sizeof buf2, fmt,
4a7319be 476 (double)file->length);
e08bfe12
AT
477 n = buf2;
478 break;
427b6179
WD
479 case 'U':
480 strlcat(fmt, "ld", sizeof fmt);
481 snprintf(buf2, sizeof buf2, fmt,
82ad07c4 482 preserve_uid ? (long)F_UID(file) : 0);
427b6179
WD
483 n = buf2;
484 break;
485 case 'G':
82ad07c4 486 if (!preserve_gid || F_GID(file) == GID_NONE)
998113fe
WD
487 n = "DEFAULT";
488 else {
489 strlcat(fmt, "ld", sizeof fmt);
490 snprintf(buf2, sizeof buf2, fmt,
82ad07c4 491 (long)F_GID(file));
998113fe
WD
492 n = buf2;
493 }
427b6179 494 break;
4a7319be 495 case 'p':
126e7aff 496 strlcat(fmt, "ld", sizeof fmt);
ddd74b67 497 snprintf(buf2, sizeof buf2, fmt,
126e7aff 498 (long)getpid());
e08bfe12
AT
499 n = buf2;
500 break;
427b6179 501 case 'M':
4a19c3b2
WD
502 n = c = timestring(file->modtime);
503 while ((c = strchr(p, ' ')) != NULL)
504 *c = '-';
427b6179
WD
505 break;
506 case 'B':
4a19c3b2
WD
507 c = buf2 + MAXPATHLEN - PERMSTRING_SIZE - 1;
508 permstring(c, file->mode);
509 n = c + 1; /* skip the type char */
427b6179 510 break;
b74b3d53
WD
511 case 'o':
512 n = op;
513 break;
4a7319be 514 case 'f':
4a19c3b2 515 c = f_name(file, NULL);
9baed760
WD
516 if (am_sender && file->dir.root) {
517 pathjoin(buf2, sizeof buf2,
4a19c3b2 518 file->dir.root, c);
0ee6ca98 519 clean_fname(buf2, 0);
4a19c3b2
WD
520 if (fmt[1]) {
521 strlcpy(c, buf2, MAXPATHLEN);
522 n = c;
523 } else
dcbae654 524 n = buf2;
4a19c3b2 525 } else if (*c != '/') {
ec55b4f2 526 pathjoin(buf2, sizeof buf2,
4a19c3b2 527 curr_dir + module_dirlen, c);
ec55b4f2 528 clean_fname(buf2, 0);
4a19c3b2
WD
529 if (fmt[1]) {
530 strlcpy(c, buf2, MAXPATHLEN);
531 n = c;
532 } else
ec55b4f2 533 n = buf2;
4a19c3b2
WD
534 } else {
535 clean_fname(c, 0);
536 n = c;
537 }
9baed760
WD
538 if (*n == '/')
539 n++;
ab7104da 540 break;
ef74f5d6 541 case 'n':
4a19c3b2 542 c = f_name(file, NULL);
0ee6ca98 543 if (S_ISDIR(file->mode))
4a19c3b2
WD
544 strlcat(c, "/", MAXPATHLEN);
545 n = c;
ef74f5d6
WD
546 break;
547 case 'L':
afc65a5a 548 if (hlink && *hlink) {
0ee6ca98 549 n = hlink;
c9bce0b8 550 strlcpy(buf2, " => ", sizeof buf2);
82ad07c4
WD
551 } else if (S_ISLNK(file->mode)) {
552 n = F_SYMLINK(file);
c9bce0b8 553 strlcpy(buf2, " -> ", sizeof buf2);
126e7aff 554 } else {
ef74f5d6 555 n = "";
126e7aff
WD
556 if (!fmt[1])
557 break;
c9bce0b8 558 strlcpy(buf2, " ", sizeof buf2);
126e7aff
WD
559 }
560 strlcat(fmt, "s", sizeof fmt);
561 snprintf(buf2 + 4, sizeof buf2 - 4, fmt, n);
562 n = buf2;
ef74f5d6 563 break;
b74b3d53
WD
564 case 'm':
565 n = lp_name(module_id);
566 break;
567 case 't':
568 n = timestring(time(NULL));
569 break;
570 case 'P':
571 n = lp_path(module_id);
572 break;
573 case 'u':
574 n = auth_user;
575 break;
4a7319be 576 case 'b':
1b7c47cb 577 if (am_sender) {
4a7319be 578 b = stats.total_written -
1b7c47cb
AT
579 initial_stats->total_written;
580 } else {
4a7319be 581 b = stats.total_read -
1b7c47cb
AT
582 initial_stats->total_read;
583 }
0455cd93 584 strlcat(fmt, ".0f", sizeof fmt);
ddd74b67 585 snprintf(buf2, sizeof buf2, fmt, (double)b);
1b7c47cb
AT
586 n = buf2;
587 break;
4a7319be 588 case 'c':
1b7c47cb 589 if (!am_sender) {
4a7319be 590 b = stats.total_written -
1b7c47cb
AT
591 initial_stats->total_written;
592 } else {
4a7319be 593 b = stats.total_read -
1b7c47cb
AT
594 initial_stats->total_read;
595 }
0455cd93 596 strlcat(fmt, ".0f", sizeof fmt);
ddd74b67 597 snprintf(buf2, sizeof buf2, fmt, (double)b);
1b7c47cb
AT
598 n = buf2;
599 break;
ef74f5d6 600 case 'i':
19bc826d 601 if (iflags & ITEM_DELETED) {
d5609e96 602 n = "*deleting";
19bc826d
WD
603 break;
604 }
4a19c3b2
WD
605 n = c = buf2 + MAXPATHLEN - 32;
606 c[0] = iflags & ITEM_LOCAL_CHANGE
fd84673e 607 ? iflags & ITEM_XNAME_FOLLOWS ? 'h' : 'c'
ca62acc3 608 : !(iflags & ITEM_TRANSFER) ? '.'
c1759b9f 609 : !local_server && *op == 's' ? '<' : '>';
4a19c3b2 610 c[1] = S_ISDIR(file->mode) ? 'd'
f8d47c1c 611 : IS_SPECIAL(file->mode) ? 'S'
19bc826d 612 : IS_DEVICE(file->mode) ? 'D'
ef74f5d6 613 : S_ISLNK(file->mode) ? 'L' : 'f';
4a19c3b2
WD
614 c[2] = !(iflags & ITEM_REPORT_CHECKSUM) ? '.' : 'c';
615 c[3] = !(iflags & ITEM_REPORT_SIZE) ? '.' : 's';
616 c[4] = !(iflags & ITEM_REPORT_TIME) ? '.'
4cff7c50 617 : !preserve_times || S_ISLNK(file->mode) ? 'T' : 't';
4a19c3b2
WD
618 c[5] = !(iflags & ITEM_REPORT_PERMS) ? '.' : 'p';
619 c[6] = !(iflags & ITEM_REPORT_OWNER) ? '.' : 'o';
620 c[7] = !(iflags & ITEM_REPORT_GROUP) ? '.' : 'g';
621 c[8] = '.';
622 c[9] = '\0';
ef74f5d6
WD
623
624 if (iflags & (ITEM_IS_NEW|ITEM_MISSING_DATA)) {
625 char ch = iflags & ITEM_IS_NEW ? '+' : '?';
626 int i;
4a19c3b2
WD
627 for (i = 2; c[i]; i++)
628 c[i] = ch;
629 } else if (c[0] == '.' || c[0] == 'h' || c[0] == 'c') {
19bc826d 630 int i;
4a19c3b2
WD
631 for (i = 2; c[i]; i++) {
632 if (c[i] != '.')
19bc826d
WD
633 break;
634 }
4a19c3b2
WD
635 if (!c[i]) {
636 for (i = 2; c[i]; i++)
637 c[i] = ' ';
19bc826d 638 }
ef74f5d6
WD
639 }
640 break;
e08bfe12
AT
641 }
642
ddd74b67
WD
643 /* "n" is the string to be inserted in place of this % code. */
644 if (!n)
645 continue;
9baed760
WD
646 if (n != buf2 && fmt[1]) {
647 strlcat(fmt, "s", sizeof fmt);
648 snprintf(buf2, sizeof buf2, fmt, n);
649 n = buf2;
650 }
ef74f5d6 651 len = strlen(n);
e08bfe12 652
e145d51b 653 /* Subtract the length of the escape from the string's size. */
0455cd93 654 total -= p - s + 1;
e145d51b 655
d9c0051f 656 if (len + total >= (size_t)sizeof buf) {
1e7098b5
WD
657 rprintf(FERROR,
658 "buffer overflow expanding %%%c -- exiting\n",
0455cd93 659 p[0]);
25f637a3 660 exit_cleanup(RERR_MESSAGEIO);
e08bfe12
AT
661 }
662
aa126974 663 /* Shuffle the rest of the string along to make space for n */
0455cd93
WD
664 if (len != (size_t)(p - s + 1))
665 memmove(s + len, p + 1, total - (s - buf) + 1);
ddd74b67 666 total += len;
aa126974 667
ddd74b67 668 /* Insert the contents of string "n", but NOT its null. */
ef74f5d6 669 if (len)
ddd74b67 670 memcpy(s, n, len);
e08bfe12 671
aa126974 672 /* Skip over inserted string; continue looking */
ddd74b67 673 p = s + len;
e08bfe12
AT
674 }
675
d9c0051f 676 rwrite(code, buf, total);
e08bfe12
AT
677}
678
0455cd93
WD
679/* Return 1 if the format escape is in the log-format string (e.g. look for
680 * the 'b' in the "%9b" format escape). */
16f960fe
WD
681int log_format_has(const char *format, char esc)
682{
683 const char *p;
684
685 if (!format)
686 return 0;
687
688 for (p = format; (p = strchr(p, '%')) != NULL; ) {
689 if (*++p == '-')
690 p++;
2dc7b8bd 691 while (isDigit(p))
16f960fe
WD
692 p++;
693 if (!*p)
694 break;
695 if (*p == esc)
696 return 1;
697 }
698 return 0;
699}
700
d0133e6e 701/* Log the transfer of a file. If the code is FCLIENT, the output just goes
b5343510
WD
702 * to stdout. If it is FLOG, it just goes to the log file. Otherwise we
703 * output to both. */
704void log_item(enum logcode code, struct file_struct *file,
4a19c3b2 705 struct stats *initial_stats, int iflags, const char *hlink)
11a5a3c7 706{
4a19c3b2 707 const char *s_or_r = am_sender ? "send" : "recv";
11a5a3c7 708
b5343510 709 if (code != FLOG && stdout_format && !am_server) {
d0133e6e 710 log_formatted(FCLIENT, stdout_format, s_or_r,
afc65a5a 711 file, initial_stats, iflags, hlink);
b5343510 712 }
d0133e6e 713 if (code != FCLIENT && logfile_format && *logfile_format) {
13b597fa
WD
714 log_formatted(FLOG, logfile_format, s_or_r,
715 file, initial_stats, iflags, hlink);
11a5a3c7
AT
716 }
717}
718
1c3e3679 719void maybe_log_item(struct file_struct *file, int iflags, int itemizing,
4a19c3b2 720 const char *buf)
1c3e3679 721{
f2b6fe44 722 int significant_flags = iflags & SIGNIFICANT_ITEM_FLAGS;
6d4ecad1 723 int see_item = itemizing && (significant_flags || *buf
b5343510 724 || stdout_format_has_i > 1 || (verbose > 1 && stdout_format_has_i));
6d4ecad1 725 int local_change = iflags & ITEM_LOCAL_CHANGE && significant_flags;
1c3e3679 726 if (am_server) {
d0133e6e
WD
727 if (logfile_name && !dry_run && see_item
728 && (significant_flags || logfile_format_has_i))
b5343510 729 log_item(FLOG, file, &stats, iflags, buf);
f2b6fe44 730 } else if (see_item || local_change || *buf
d0133e6e
WD
731 || (S_ISDIR(file->mode) && significant_flags)) {
732 enum logcode code = significant_flags || logfile_format_has_i ? FINFO : FCLIENT;
733 log_item(code, file, &stats, iflags, buf);
734 }
1c3e3679
WD
735}
736
4a19c3b2 737void log_delete(const char *fname, int mode)
19bc826d
WD
738{
739 static struct file_struct file;
740 int len = strlen(fname);
4a19c3b2 741 const char *fmt;
19bc826d
WD
742
743 file.mode = mode;
744 file.basename = fname;
745
b5343510 746 if (!verbose && !stdout_format)
41b5b5e7
WD
747 ;
748 else if (am_server && protocol_version >= 29 && len < MAXPATHLEN) {
19bc826d
WD
749 if (S_ISDIR(mode))
750 len++; /* directories include trailing null */
751 send_msg(MSG_DELETED, fname, len);
19bc826d 752 } else {
b5343510 753 fmt = stdout_format_has_o_or_i ? stdout_format : "deleting %n";
afc65a5a
WD
754 log_formatted(FCLIENT, fmt, "del.", &file, &stats,
755 ITEM_DELETED, NULL);
19bc826d
WD
756 }
757
13b597fa 758 if (!logfile_name || dry_run || !logfile_format)
1eec003a
WD
759 return;
760
13b597fa 761 fmt = logfile_format_has_o_or_i ? logfile_format : "deleting %n";
afc65a5a 762 log_formatted(FLOG, fmt, "del.", &file, &stats, ITEM_DELETED, NULL);
19bc826d 763}
af642a61 764
af642a61
MP
765/*
766 * Called when the transfer is interrupted for some reason.
767 *
768 * Code is one of the RERR_* codes from errcode.h, or terminating
769 * successfully.
770 */
a9766ef1 771void log_exit(int code, const char *file, int line)
9b73d1c0
AT
772{
773 if (code == 0) {
088aff1a 774 rprintf(FLOG,"sent %.0f bytes received %.0f bytes total size %.0f\n",
9b73d1c0
AT
775 (double)stats.total_written,
776 (double)stats.total_read,
777 (double)stats.total_size);
778 } else {
4a7319be
WD
779 const char *name;
780
781 name = rerr_name(code);
782 if (!name)
783 name = "unexplained error";
af642a61 784
1bca1de6
WD
785 /* VANISHED is not an error, only a warning */
786 if (code == RERR_VANISHED) {
0f78b815 787 rprintf(FINFO, "rsync warning: %s (code %d) at %s(%d) [%s=%s]\n",
43eae40e 788 name, code, file, line, who_am_i(), RSYNC_VERSION);
1bca1de6 789 } else {
43eae40e
WD
790 rprintf(FERROR, "rsync error: %s (code %d) at %s(%d) [%s=%s]\n",
791 name, code, file, line, who_am_i(), RSYNC_VERSION);
1bca1de6 792 }
9b73d1c0
AT
793 }
794}