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