There's no need to send --size-only to a remote sender.
[rsync/rsync.git] / log.c
CommitLineData
a039749b 1/* -*- c-file-style: "linux"; -*-
4a7319be 2
0c5a792a 3 Copyright (C) 1998-2001 by Andrew Tridgell <tridge@samba.org>
18c71e96 4 Copyright (C) 2000-2001 by Martin Pool <mbp@samba.org>
4a7319be 5
0b76cd63
AT
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
4a7319be 10
0b76cd63
AT
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
4a7319be 15
0b76cd63
AT
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19*/
20
21/*
3e3dcd62 22 Logging and utility functions.
0c5a792a 23 tridge, May 1998
0b76cd63 24
3e3dcd62
MP
25 Mapping to human-readable messages added by Martin Pool
26 <mbp@samba.org>, Oct 2000.
0b76cd63
AT
27 */
28#include "rsync.h"
c8563142 29#if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H
9a31746b
WD
30#include <iconv.h>
31#endif
0b76cd63 32
41b5b5e7 33extern int verbose;
088aff1a 34extern int dry_run;
548abf96
WD
35extern int am_daemon;
36extern int am_server;
37extern int am_sender;
c1759b9f 38extern int local_server;
548abf96
WD
39extern int quiet;
40extern int module_id;
a644fc3c 41extern int msg_fd_out;
507433f6 42extern int allow_8bit_chars;
19bc826d 43extern int protocol_version;
ef74f5d6 44extern int preserve_times;
6d4ecad1 45extern int log_format_has_i;
d6707171 46extern int log_format_has_o_or_i;
1eec003a 47extern int daemon_log_format_has_o_or_i;
548abf96
WD
48extern char *auth_user;
49extern char *log_format;
595251de 50#if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H
9a31746b
WD
51extern iconv_t ic_chck;
52#endif
548abf96 53
30e8c8e1 54static int log_initialised;
64c37826 55static int logfile_was_closed;
45a83540 56static char *logfname;
4f6325c3 57static FILE *logfile;
b35d0d8e 58struct stats stats;
e0414f42 59
8fcdc444 60int log_got_error = 0;
af642a61
MP
61
62struct {
63 int code;
64 char const *name;
65} const rerr_names[] = {
4a7319be
WD
66 { RERR_SYNTAX , "syntax or usage error" },
67 { RERR_PROTOCOL , "protocol incompatibility" },
68 { RERR_FILESELECT , "errors selecting input/output files, dirs" },
69 { RERR_UNSUPPORTED, "requested action not supported" },
70 { RERR_STARTCLIENT, "error starting client-server protocol" },
71 { RERR_SOCKETIO , "error in socket IO" },
72 { RERR_FILEIO , "error in file IO" },
73 { RERR_STREAMIO , "error in rsync protocol data stream" },
74 { RERR_MESSAGEIO , "errors with program diagnostics" },
75 { RERR_IPC , "error in IPC code" },
60168410 76 { RERR_CRASHED , "sibling process crashed" },
0047f535 77 { RERR_TERMINATED , "sibling process terminated abnormally" },
4a50a217
WD
78 { RERR_SIGNAL1 , "received SIGUSR1" },
79 { RERR_SIGNAL , "received SIGINT, SIGTERM, or SIGHUP" },
f14a65d9 80 { RERR_WAITCHILD , "waitpid() failed" },
4a7319be
WD
81 { RERR_MALLOC , "error allocating core memory buffers" },
82 { RERR_PARTIAL , "some files could not be transferred" },
584ba4eb 83 { RERR_VANISHED , "some files vanished before they could be transferred" },
4a7319be 84 { RERR_TIMEOUT , "timeout in data send/receive" },
19b27a48
AT
85 { RERR_CMD_FAILED , "remote shell failed" },
86 { RERR_CMD_KILLED , "remote shell killed" },
24cecf13
WD
87 { RERR_CMD_RUN , "remote command could not be run" },
88 { RERR_CMD_NOTFOUND,"remote command not found" },
26718401 89 { RERR_DEL_LIMIT , "the --max-delete limit stopped deletions" },
4a7319be 90 { 0, NULL }
af642a61
MP
91};
92
93
af642a61
MP
94/*
95 * Map from rsync error code to name, or return NULL.
96 */
97static char const *rerr_name(int code)
98{
4a7319be
WD
99 int i;
100 for (i = 0; rerr_names[i].name; i++) {
101 if (rerr_names[i].code == code)
102 return rerr_names[i].name;
103 }
104 return NULL;
af642a61
MP
105}
106
4f6325c3
AT
107static void logit(int priority, char *buf)
108{
64c37826
WD
109 if (logfile_was_closed)
110 logfile_reopen();
111 if (logfile) {
4a7319be 112 fprintf(logfile,"%s [%d] %s",
f7632fc6 113 timestring(time(NULL)), (int)getpid(), buf);
4f6325c3
AT
114 fflush(logfile);
115 } else {
116 syslog(priority, "%s", buf);
117 }
118}
e42c9458 119
6afb9077 120static void syslog_init()
1a016bfd 121{
6afb9077 122 static int been_here = 0;
1a016bfd 123 int options = LOG_PID;
6afb9077
WD
124
125 if (been_here)
126 return;
127 been_here = 1;
128
129#ifdef LOG_NDELAY
130 options |= LOG_NDELAY;
131#endif
132
133#ifdef LOG_DAEMON
134 openlog("rsyncd", options, lp_syslog_facility());
135#else
136 openlog("rsyncd", options);
137#endif
138
139#ifndef LOG_NDELAY
140 logit(LOG_INFO, "rsyncd started\n");
141#endif
142}
143
64c37826
WD
144static void logfile_open(void)
145{
146 extern int orig_umask;
147 int old_umask = umask(022 | orig_umask);
148 logfile = fopen(logfname, "a");
149 umask(old_umask);
150 if (!logfile) {
151 int fopen_errno = errno;
152 /* Rsync falls back to using syslog on failure. */
153 syslog_init();
154 rsyserr(FERROR, fopen_errno,
155 "failed to open log-file %s", logfname);
156 rprintf(FINFO, "Ignoring \"log file\" setting.\n");
157 }
158}
159
6afb9077
WD
160void log_init(void)
161{
bcf5b133 162 time_t t;
1a016bfd 163
0bb4d176
WD
164 if (log_initialised)
165 return;
30e8c8e1 166 log_initialised = 1;
1a016bfd 167
958f3735 168 /* this looks pointless, but it is needed in order for the
4a7319be
WD
169 * C library on some systems to fetch the timezone info
170 * before the chroot */
958f3735
AT
171 t = time(NULL);
172 localtime(&t);
173
174 /* optionally use a log file instead of syslog */
45a83540 175 logfname = lp_log_file();
64c37826
WD
176 if (logfname && *logfname)
177 logfile_open();
178 else
179 syslog_init();
1a016bfd 180}
1a016bfd 181
64c37826 182void logfile_close(void)
4a239e98 183{
64c37826
WD
184 if (logfile) {
185 logfile_was_closed = 1;
186 fclose(logfile);
187 logfile = NULL;
4a239e98
WD
188 }
189}
190
64c37826 191void logfile_reopen(void)
4a239e98 192{
64c37826
WD
193 if (logfile_was_closed) {
194 logfile_was_closed = 0;
195 logfile_open();
4a239e98
WD
196 }
197}
198
3648ab3a 199static void filtered_fwrite(FILE *f, const char *buf, int len, int use_isprint)
be0602ec
WD
200{
201 const char *s, *end = buf + len;
202 for (s = buf; s < end; s++) {
203 if ((s < end - 4
3648ab3a 204 && *s == '\\' && s[1] == '#'
be0602ec
WD
205 && isdigit(*(uchar*)(s+2))
206 && isdigit(*(uchar*)(s+3))
207 && isdigit(*(uchar*)(s+4)))
3648ab3a
WD
208 || (*s != '\t'
209 && ((use_isprint && !isprint(*(uchar*)s))
210 || *(uchar*)s < ' '))) {
be0602ec
WD
211 if (s != buf && fwrite(buf, s - buf, 1, f) != 1)
212 exit_cleanup(RERR_MESSAGEIO);
3648ab3a 213 fprintf(f, "\\#%03o", *(uchar*)s);
be0602ec
WD
214 buf = s + 1;
215 }
216 }
217 if (buf != end && fwrite(buf, end - buf, 1, f) != 1)
218 exit_cleanup(RERR_MESSAGEIO);
219}
220
554e0a8d 221/* this is the underlying (unformatted) rsync debugging function. Call
be0602ec
WD
222 * it with FINFO, FERROR or FLOG. Note: recursion can happen with
223 * certain fatal conditions. */
ff41a59f 224void rwrite(enum logcode code, char *buf, int len)
0b76cd63 225{
be0602ec 226 int trailing_CR_or_NL;
8fcdc444 227 FILE *f = NULL;
8d9dc9f9 228
a644fc3c
WD
229 if (len < 0)
230 exit_cleanup(RERR_MESSAGEIO);
0b76cd63 231
b66e31bf
WD
232 if (quiet && code == FINFO)
233 return;
234
0bb4d176
WD
235 if (am_server && msg_fd_out >= 0) {
236 /* Pass the message to our sibling. */
237 send_msg((enum msgcode)code, buf, len);
11a5a3c7
AT
238 return;
239 }
240
b66e31bf
WD
241 if (code == FSOCKERR) /* This gets simplified for a non-sibling. */
242 code = FERROR;
243
1eec003a
WD
244 if (code == FCLIENT)
245 code = FINFO;
246 else if (am_daemon) {
0bb4d176
WD
247 static int in_block;
248 char msg[2048];
249 int priority = code == FERROR ? LOG_WARNING : LOG_INFO;
250
251 if (in_block)
a644fc3c 252 return;
0bb4d176
WD
253 in_block = 1;
254 if (!log_initialised)
255 log_init();
256 strlcpy(msg, buf, MIN((int)sizeof msg, len + 1));
257 logit(priority, msg);
258 in_block = 0;
259
260 if (code == FLOG || !am_server)
a644fc3c 261 return;
0bb4d176 262 } else if (code == FLOG)
ff8b29b8 263 return;
0bb4d176
WD
264
265 if (am_server) {
266 /* Pass the message to the non-server side. */
267 if (io_multiplex_write((enum msgcode)code, buf, len))
268 return;
269 if (am_daemon) {
270 /* TODO: can we send the error to the user somehow? */
271 return;
272 }
0b76cd63
AT
273 }
274
be0602ec
WD
275 switch (code) {
276 case FERROR:
ff81e809 277 log_got_error = 1;
ff8b29b8 278 f = stderr;
0ace7b25 279 goto pre_scan;
be0602ec 280 case FINFO:
0bb4d176 281 f = am_server ? stderr : stdout;
0ace7b25 282 pre_scan:
427b6179 283 while (len > 1 && *buf == '\n') {
be0602ec
WD
284 fputc(*buf, f);
285 buf++;
286 len--;
287 }
288 break;
289 case FNAME:
290 f = am_server ? stderr : stdout;
291 break;
292 default:
0bb4d176 293 exit_cleanup(RERR_MESSAGEIO);
be0602ec 294 }
0b76cd63 295
be0602ec
WD
296 trailing_CR_or_NL = len && (buf[len-1] == '\n' || buf[len-1] == '\r')
297 ? buf[--len] : 0;
8d9dc9f9 298
595251de 299#if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H
9a31746b
WD
300 if (ic_chck != (iconv_t)-1) {
301 char convbuf[1024];
302 char *in_buf = buf, *out_buf = convbuf;
303 size_t in_cnt = len, out_cnt = sizeof convbuf - 1;
304
305 iconv(ic_chck, NULL, 0, NULL, 0);
306 while (iconv(ic_chck, &in_buf,&in_cnt,
307 &out_buf,&out_cnt) == (size_t)-1) {
308 if (out_buf != convbuf) {
3648ab3a 309 filtered_fwrite(f, convbuf, out_buf - convbuf, 0);
9a31746b
WD
310 out_buf = convbuf;
311 out_cnt = sizeof convbuf - 1;
312 }
313 if (errno == E2BIG)
314 continue;
3648ab3a 315 fprintf(f, "\\#%03o", *(uchar*)in_buf++);
9a31746b
WD
316 in_cnt--;
317 }
318 if (out_buf != convbuf)
3648ab3a 319 filtered_fwrite(f, convbuf, out_buf - convbuf, 0);
9a31746b
WD
320 } else
321#endif
507433f6 322 filtered_fwrite(f, buf, len, !allow_8bit_chars);
be0602ec
WD
323
324 if (trailing_CR_or_NL) {
325 fputc(trailing_CR_or_NL, f);
0bb4d176 326 fflush(f);
be0602ec 327 }
0b76cd63 328}
0455cd93 329
a039749b
MP
330/* This is the rsync debugging function. Call it with FINFO, FERROR or
331 * FLOG. */
332void rprintf(enum logcode code, const char *format, ...)
554e0a8d 333{
4a7319be 334 va_list ap;
b74b3d53 335 char buf[BIGPATHBUFLEN];
8fcdc444 336 size_t len;
554e0a8d
AT
337
338 va_start(ap, format);
ef74f5d6 339 len = vsnprintf(buf, sizeof buf, format, ap);
554e0a8d
AT
340 va_end(ap);
341
b2f02464 342 /* Deal with buffer overruns. Instead of panicking, just
8fcdc444
WD
343 * truncate the resulting string. (Note that configure ensures
344 * that we have a vsnprintf() that doesn't ever return -1.) */
345 if (len > sizeof buf - 1) {
b74b3d53 346 static const char ellipsis[] = "[...]";
b2f02464
MP
347
348 /* Reset length, and zero-terminate the end of our buffer */
ef74f5d6 349 len = sizeof buf - 1;
b2f02464
MP
350 buf[len] = '\0';
351
352 /* Copy the ellipsis to the end of the string, but give
353 * us one extra character:
354 *
ef74f5d6 355 * v--- null byte at buf[sizeof buf - 1]
b2f02464
MP
356 * abcdefghij0
357 * -> abcd[...]00 <-- now two null bytes at end
358 *
359 * If the input format string has a trailing newline,
360 * we copy it into that extra null; if it doesn't, well,
361 * all we lose is one byte. */
b74b3d53 362 memcpy(buf+len-sizeof ellipsis, ellipsis, sizeof ellipsis);
b2f02464
MP
363 if (format[strlen(format)-1] == '\n') {
364 buf[len-1] = '\n';
365 }
366 }
554e0a8d 367
ff41a59f 368 rwrite(code, buf, len);
554e0a8d 369}
0b76cd63 370
a039749b
MP
371/* This is like rprintf, but it also tries to print some
372 * representation of the error code. Normally errcode = errno.
373 *
374 * Unlike rprintf, this always adds a newline and there should not be
375 * one in the format string.
376 *
377 * Note that since strerror might involve dynamically loading a
378 * message catalog we need to call it once before chroot-ing. */
379void rsyserr(enum logcode code, int errcode, const char *format, ...)
380{
4a7319be 381 va_list ap;
b74b3d53 382 char buf[BIGPATHBUFLEN];
8fcdc444
WD
383 size_t len;
384
385 strcpy(buf, RSYNC_NAME ": ");
386 len = (sizeof RSYNC_NAME ": ") - 1;
a039749b
MP
387
388 va_start(ap, format);
8fcdc444 389 len += vsnprintf(buf + len, sizeof buf - len, format, ap);
a039749b
MP
390 va_end(ap);
391
8fcdc444
WD
392 if (len < sizeof buf) {
393 len += snprintf(buf + len, sizeof buf - len,
394 ": %s (%d)\n", strerror(errcode), errcode);
395 }
396 if (len >= sizeof buf)
4a7319be 397 exit_cleanup(RERR_MESSAGEIO);
a039749b 398
a039749b
MP
399 rwrite(code, buf, len);
400}
401
ff41a59f 402void rflush(enum logcode code)
0b76cd63
AT
403{
404 FILE *f = NULL;
0455cd93 405
0b76cd63
AT
406 if (am_daemon) {
407 return;
408 }
409
ff41a59f 410 if (code == FLOG) {
e08bfe12 411 return;
4a7319be 412 }
e08bfe12 413
ff41a59f 414 if (code == FERROR) {
0b76cd63 415 f = stderr;
4a7319be 416 }
0b76cd63 417
ff41a59f 418 if (code == FINFO) {
4a7319be 419 if (am_server)
0b76cd63
AT
420 f = stderr;
421 else
422 f = stdout;
4a7319be 423 }
0b76cd63 424
65417579 425 if (!f) exit_cleanup(RERR_MESSAGEIO);
0b76cd63
AT
426 fflush(f);
427}
428
e08bfe12 429/* a generic logging routine for send/recv, with parameter
4a7319be 430 * substitiution */
afc65a5a
WD
431static void log_formatted(enum logcode code, char *format, char *op,
432 struct file_struct *file, struct stats *initial_stats,
433 int iflags, char *hlink)
e08bfe12 434{
ddd74b67
WD
435 char buf[MAXPATHLEN+1024], buf2[MAXPATHLEN], fmt[32];
436 char *p, *s, *n;
ef74f5d6 437 size_t len, total;
1b7c47cb 438 int64 b;
e08bfe12 439
ddd74b67
WD
440 *fmt = '%';
441
aa126974 442 /* We expand % codes one by one in place in buf. We don't
126e7aff
WD
443 * copy in the terminating null of the inserted strings, but
444 * rather keep going until we reach the null of the format. */
ef74f5d6 445 total = strlcpy(buf, format, sizeof buf);
d9c0051f
WD
446 if (total > MAXPATHLEN) {
447 rprintf(FERROR, "log-format string is WAY too long!\n");
448 exit_cleanup(RERR_MESSAGEIO);
449 }
450 buf[total++] = '\n';
451 buf[total] = '\0';
0455cd93 452
16f960fe 453 for (p = buf; (p = strchr(p, '%')) != NULL; ) {
ddd74b67
WD
454 s = p++;
455 n = fmt + 1;
9baed760
WD
456 if (*p == '-')
457 *n++ = *p++;
b4bf2b5a 458 while (isdigit(*(uchar*)p) && n - fmt < (int)(sizeof fmt) - 8)
ddd74b67 459 *n++ = *p++;
e145d51b
WD
460 if (!*p)
461 break;
ddd74b67 462 *n = '\0';
e08bfe12 463 n = NULL;
e08bfe12 464
0455cd93 465 switch (*p) {
b74b3d53
WD
466 case 'h':
467 if (am_daemon)
468 n = client_name(0);
469 break;
470 case 'a':
471 if (am_daemon)
472 n = client_addr(0);
473 break;
4a7319be 474 case 'l':
ddd74b67
WD
475 strlcat(fmt, ".0f", sizeof fmt);
476 snprintf(buf2, sizeof buf2, fmt,
4a7319be 477 (double)file->length);
e08bfe12
AT
478 n = buf2;
479 break;
427b6179
WD
480 case 'U':
481 strlcat(fmt, "ld", sizeof fmt);
482 snprintf(buf2, sizeof buf2, fmt,
483 (long)file->uid);
484 n = buf2;
485 break;
486 case 'G':
998113fe
WD
487 if (file->gid == GID_NONE)
488 n = "DEFAULT";
489 else {
490 strlcat(fmt, "ld", sizeof fmt);
491 snprintf(buf2, sizeof buf2, fmt,
492 (long)file->gid);
493 n = buf2;
494 }
427b6179 495 break;
4a7319be 496 case 'p':
126e7aff 497 strlcat(fmt, "ld", sizeof fmt);
ddd74b67 498 snprintf(buf2, sizeof buf2, fmt,
126e7aff 499 (long)getpid());
e08bfe12
AT
500 n = buf2;
501 break;
427b6179
WD
502 case 'M':
503 n = timestring(file->modtime);
504 {
998113fe
WD
505 char *cp = n;
506 while ((cp = strchr(cp, ' ')) != NULL)
427b6179
WD
507 *cp = '-';
508 }
509 break;
510 case 'B':
511 n = buf2 + MAXPATHLEN - PERMSTRING_SIZE;
512 permstring(n - 1, file->mode); /* skip the type char */
513 break;
b74b3d53
WD
514 case 'o':
515 n = op;
516 break;
4a7319be 517 case 'f':
be0602ec 518 n = f_name(file, NULL);
9baed760
WD
519 if (am_sender && file->dir.root) {
520 pathjoin(buf2, sizeof buf2,
521 file->dir.root, n);
0ee6ca98 522 clean_fname(buf2, 0);
dcbae654 523 if (fmt[1])
0ee6ca98 524 strlcpy(n, buf2, MAXPATHLEN);
dcbae654
WD
525 else
526 n = buf2;
0ee6ca98
WD
527 } else
528 clean_fname(n, 0);
9baed760
WD
529 if (*n == '/')
530 n++;
ab7104da 531 break;
ef74f5d6 532 case 'n':
be0602ec 533 n = f_name(file, NULL);
0ee6ca98
WD
534 if (S_ISDIR(file->mode))
535 strlcat(n, "/", MAXPATHLEN);
ef74f5d6
WD
536 break;
537 case 'L':
afc65a5a 538 if (hlink && *hlink) {
0ee6ca98 539 n = hlink;
126e7aff 540 strcpy(buf2, " => ");
afc65a5a 541 } else if (S_ISLNK(file->mode) && file->u.link) {
0ee6ca98 542 n = file->u.link;
126e7aff
WD
543 strcpy(buf2, " -> ");
544 } else {
ef74f5d6 545 n = "";
126e7aff
WD
546 if (!fmt[1])
547 break;
548 strcpy(buf2, " ");
549 }
550 strlcat(fmt, "s", sizeof fmt);
551 snprintf(buf2 + 4, sizeof buf2 - 4, fmt, n);
552 n = buf2;
ef74f5d6 553 break;
b74b3d53
WD
554 case 'm':
555 n = lp_name(module_id);
556 break;
557 case 't':
558 n = timestring(time(NULL));
559 break;
560 case 'P':
561 n = lp_path(module_id);
562 break;
563 case 'u':
564 n = auth_user;
565 break;
4a7319be 566 case 'b':
1b7c47cb 567 if (am_sender) {
4a7319be 568 b = stats.total_written -
1b7c47cb
AT
569 initial_stats->total_written;
570 } else {
4a7319be 571 b = stats.total_read -
1b7c47cb
AT
572 initial_stats->total_read;
573 }
0455cd93 574 strlcat(fmt, ".0f", sizeof fmt);
ddd74b67 575 snprintf(buf2, sizeof buf2, fmt, (double)b);
1b7c47cb
AT
576 n = buf2;
577 break;
4a7319be 578 case 'c':
1b7c47cb 579 if (!am_sender) {
4a7319be 580 b = stats.total_written -
1b7c47cb
AT
581 initial_stats->total_written;
582 } else {
4a7319be 583 b = stats.total_read -
1b7c47cb
AT
584 initial_stats->total_read;
585 }
0455cd93 586 strlcat(fmt, ".0f", sizeof fmt);
ddd74b67 587 snprintf(buf2, sizeof buf2, fmt, (double)b);
1b7c47cb
AT
588 n = buf2;
589 break;
ef74f5d6 590 case 'i':
19bc826d 591 if (iflags & ITEM_DELETED) {
d5609e96 592 n = "*deleting";
19bc826d
WD
593 break;
594 }
b4bf2b5a 595 n = buf2 + MAXPATHLEN - 32;
fd84673e
WD
596 n[0] = iflags & ITEM_LOCAL_CHANGE
597 ? iflags & ITEM_XNAME_FOLLOWS ? 'h' : 'c'
ca62acc3 598 : !(iflags & ITEM_TRANSFER) ? '.'
c1759b9f 599 : !local_server && *op == 's' ? '<' : '>';
19bc826d 600 n[1] = S_ISDIR(file->mode) ? 'd'
f8d47c1c 601 : IS_SPECIAL(file->mode) ? 'S'
19bc826d 602 : IS_DEVICE(file->mode) ? 'D'
ef74f5d6 603 : S_ISLNK(file->mode) ? 'L' : 'f';
d4e01963
WD
604 n[2] = !(iflags & ITEM_REPORT_CHECKSUM) ? '.' : 'c';
605 n[3] = !(iflags & ITEM_REPORT_SIZE) ? '.' : 's';
606 n[4] = !(iflags & ITEM_REPORT_TIME) ? '.'
4cff7c50 607 : !preserve_times || S_ISLNK(file->mode) ? 'T' : 't';
d4e01963
WD
608 n[5] = !(iflags & ITEM_REPORT_PERMS) ? '.' : 'p';
609 n[6] = !(iflags & ITEM_REPORT_OWNER) ? '.' : 'o';
610 n[7] = !(iflags & ITEM_REPORT_GROUP) ? '.' : 'g';
4cff7c50 611 n[8] = '\0';
ef74f5d6
WD
612
613 if (iflags & (ITEM_IS_NEW|ITEM_MISSING_DATA)) {
614 char ch = iflags & ITEM_IS_NEW ? '+' : '?';
615 int i;
616 for (i = 2; n[i]; i++)
617 n[i] = ch;
6d4ecad1
WD
618 } else if (n[0] == '.' || n[0] == 'h'
619 || (n[0] == 'c' && n[1] == 'f')) {
19bc826d
WD
620 int i;
621 for (i = 2; n[i]; i++) {
622 if (n[i] != '.')
623 break;
624 }
625 if (!n[i]) {
626 for (i = 2; n[i]; i++)
627 n[i] = ' ';
19bc826d 628 }
ef74f5d6
WD
629 }
630 break;
e08bfe12
AT
631 }
632
ddd74b67
WD
633 /* "n" is the string to be inserted in place of this % code. */
634 if (!n)
635 continue;
9baed760
WD
636 if (n != buf2 && fmt[1]) {
637 strlcat(fmt, "s", sizeof fmt);
638 snprintf(buf2, sizeof buf2, fmt, n);
639 n = buf2;
640 }
ef74f5d6 641 len = strlen(n);
e08bfe12 642
e145d51b 643 /* Subtract the length of the escape from the string's size. */
0455cd93 644 total -= p - s + 1;
e145d51b 645
d9c0051f 646 if (len + total >= (size_t)sizeof buf) {
1e7098b5
WD
647 rprintf(FERROR,
648 "buffer overflow expanding %%%c -- exiting\n",
0455cd93 649 p[0]);
65417579 650 exit_cleanup(RERR_MESSAGEIO);
e08bfe12
AT
651 }
652
aa126974 653 /* Shuffle the rest of the string along to make space for n */
0455cd93
WD
654 if (len != (size_t)(p - s + 1))
655 memmove(s + len, p + 1, total - (s - buf) + 1);
ddd74b67 656 total += len;
aa126974 657
ddd74b67 658 /* Insert the contents of string "n", but NOT its null. */
ef74f5d6 659 if (len)
ddd74b67 660 memcpy(s, n, len);
e08bfe12 661
aa126974 662 /* Skip over inserted string; continue looking */
ddd74b67 663 p = s + len;
e08bfe12
AT
664 }
665
d9c0051f 666 rwrite(code, buf, total);
e08bfe12
AT
667}
668
0455cd93
WD
669/* Return 1 if the format escape is in the log-format string (e.g. look for
670 * the 'b' in the "%9b" format escape). */
16f960fe
WD
671int log_format_has(const char *format, char esc)
672{
673 const char *p;
674
675 if (!format)
676 return 0;
677
678 for (p = format; (p = strchr(p, '%')) != NULL; ) {
679 if (*++p == '-')
680 p++;
681 while (isdigit(*(uchar*)p))
682 p++;
683 if (!*p)
684 break;
685 if (*p == esc)
686 return 1;
687 }
688 return 0;
689}
690
afc65a5a
WD
691/* log the transfer of a file */
692void log_item(struct file_struct *file, struct stats *initial_stats,
693 int iflags, char *hlink)
11a5a3c7 694{
afc65a5a 695 char *s_or_r = am_sender ? "send" : "recv";
11a5a3c7 696
11a5a3c7 697 if (lp_transfer_logging(module_id)) {
afc65a5a
WD
698 log_formatted(FLOG, lp_log_format(module_id), s_or_r,
699 file, initial_stats, iflags, hlink);
b6062654 700 } else if (log_format && !am_server) {
be0602ec 701 log_formatted(FNAME, log_format, s_or_r,
afc65a5a 702 file, initial_stats, iflags, hlink);
11a5a3c7
AT
703 }
704}
705
1c3e3679
WD
706void maybe_log_item(struct file_struct *file, int iflags, int itemizing,
707 char *buf)
708{
f2b6fe44 709 int significant_flags = iflags & SIGNIFICANT_ITEM_FLAGS;
6d4ecad1 710 int see_item = itemizing && (significant_flags || *buf
a9e47626 711 || log_format_has_i > 1 || (verbose > 1 && log_format_has_i));
6d4ecad1 712 int local_change = iflags & ITEM_LOCAL_CHANGE && significant_flags;
1c3e3679
WD
713 if (am_server) {
714 if (am_daemon && !dry_run && see_item)
715 log_item(file, &stats, iflags, buf);
f2b6fe44
WD
716 } else if (see_item || local_change || *buf
717 || (S_ISDIR(file->mode) && significant_flags))
1c3e3679
WD
718 log_item(file, &stats, iflags, buf);
719}
720
19bc826d
WD
721void log_delete(char *fname, int mode)
722{
723 static struct file_struct file;
724 int len = strlen(fname);
19bc826d
WD
725 char *fmt;
726
727 file.mode = mode;
728 file.basename = fname;
729
41b5b5e7
WD
730 if (!verbose && !log_format)
731 ;
732 else if (am_server && protocol_version >= 29 && len < MAXPATHLEN) {
19bc826d
WD
733 if (S_ISDIR(mode))
734 len++; /* directories include trailing null */
735 send_msg(MSG_DELETED, fname, len);
19bc826d 736 } else {
d5609e96 737 fmt = log_format_has_o_or_i ? log_format : "deleting %n";
afc65a5a
WD
738 log_formatted(FCLIENT, fmt, "del.", &file, &stats,
739 ITEM_DELETED, NULL);
19bc826d
WD
740 }
741
41b5b5e7 742 if (!am_daemon || dry_run || !lp_transfer_logging(module_id))
1eec003a
WD
743 return;
744
d5609e96 745 fmt = daemon_log_format_has_o_or_i ? lp_log_format(module_id) : "deleting %n";
afc65a5a 746 log_formatted(FLOG, fmt, "del.", &file, &stats, ITEM_DELETED, NULL);
19bc826d 747}
af642a61 748
af642a61
MP
749/*
750 * Called when the transfer is interrupted for some reason.
751 *
752 * Code is one of the RERR_* codes from errcode.h, or terminating
753 * successfully.
754 */
a9766ef1 755void log_exit(int code, const char *file, int line)
9b73d1c0
AT
756{
757 if (code == 0) {
088aff1a 758 rprintf(FLOG,"sent %.0f bytes received %.0f bytes total size %.0f\n",
9b73d1c0
AT
759 (double)stats.total_written,
760 (double)stats.total_read,
761 (double)stats.total_size);
762 } else {
4a7319be
WD
763 const char *name;
764
765 name = rerr_name(code);
766 if (!name)
767 name = "unexplained error";
af642a61 768
1bca1de6
WD
769 /* VANISHED is not an error, only a warning */
770 if (code == RERR_VANISHED) {
fa9e950b
WD
771 rprintf(FINFO, "rsync warning: %s (code %d) at %s(%d) [%s]\n",
772 name, code, file, line, who_am_i());
1bca1de6 773 } else {
fa9e950b
WD
774 rprintf(FERROR, "rsync error: %s (code %d) at %s(%d) [%s]\n",
775 name, code, file, line, who_am_i());
1bca1de6 776 }
9b73d1c0
AT
777 }
778}