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