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