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