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