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