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