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