Changed the *_abbbrevint() functions to *_varint().
[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>
ba2133d6 6 * Copyright (C) 2003-2007 Wayne Davison
0f78b815
WD
7 *
8 * This program is free software; you can redistribute it and/or modify
ba2133d6
WD
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
0f78b815
WD
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
e7c67065
WD
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
0f78b815 20 */
0b76cd63 21
0b76cd63 22#include "rsync.h"
c8563142 23#if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H
9a31746b
WD
24#include <iconv.h>
25#endif
0b76cd63 26
41b5b5e7 27extern int verbose;
088aff1a 28extern int dry_run;
548abf96
WD
29extern int am_daemon;
30extern int am_server;
31extern int am_sender;
c1759b9f 32extern int local_server;
548abf96
WD
33extern int quiet;
34extern int module_id;
a644fc3c 35extern int msg_fd_out;
507433f6 36extern int allow_8bit_chars;
19bc826d 37extern int protocol_version;
ef74f5d6 38extern int preserve_times;
82ad07c4
WD
39extern int preserve_uid;
40extern int preserve_gid;
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;
c5b7aa15 45extern mode_t orig_umask;
548abf96 46extern char *auth_user;
b5343510 47extern char *stdout_format;
13b597fa
WD
48extern char *logfile_format;
49extern char *logfile_name;
595251de 50#if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H
9a31746b
WD
51extern iconv_t ic_chck;
52#endif
6fb7cc38
WD
53extern char curr_dir[];
54extern unsigned int module_dirlen;
548abf96 55
ceca8cca 56static int log_initialised;
64c37826 57static int logfile_was_closed;
13b597fa 58static FILE *logfile_fp;
b35d0d8e 59struct stats stats;
e0414f42 60
8fcdc444 61int log_got_error = 0;
af642a61
MP
62
63struct {
64 int code;
65 char const *name;
66} const rerr_names[] = {
4a7319be
WD
67 { RERR_SYNTAX , "syntax or usage error" },
68 { RERR_PROTOCOL , "protocol incompatibility" },
69 { RERR_FILESELECT , "errors selecting input/output files, dirs" },
70 { RERR_UNSUPPORTED, "requested action not supported" },
71 { RERR_STARTCLIENT, "error starting client-server protocol" },
72 { RERR_SOCKETIO , "error in socket IO" },
73 { RERR_FILEIO , "error in file IO" },
74 { RERR_STREAMIO , "error in rsync protocol data stream" },
75 { RERR_MESSAGEIO , "errors with program diagnostics" },
76 { RERR_IPC , "error in IPC code" },
60168410 77 { RERR_CRASHED , "sibling process crashed" },
0047f535 78 { RERR_TERMINATED , "sibling process terminated abnormally" },
4a50a217
WD
79 { RERR_SIGNAL1 , "received SIGUSR1" },
80 { RERR_SIGNAL , "received SIGINT, SIGTERM, or SIGHUP" },
f14a65d9 81 { RERR_WAITCHILD , "waitpid() failed" },
4a7319be
WD
82 { RERR_MALLOC , "error allocating core memory buffers" },
83 { RERR_PARTIAL , "some files could not be transferred" },
584ba4eb 84 { RERR_VANISHED , "some files vanished before they could be transferred" },
4a7319be 85 { RERR_TIMEOUT , "timeout in data send/receive" },
19b27a48
AT
86 { RERR_CMD_FAILED , "remote shell failed" },
87 { RERR_CMD_KILLED , "remote shell killed" },
24cecf13
WD
88 { RERR_CMD_RUN , "remote command could not be run" },
89 { RERR_CMD_NOTFOUND,"remote command not found" },
26718401 90 { RERR_DEL_LIMIT , "the --max-delete limit stopped deletions" },
4a7319be 91 { 0, NULL }
af642a61
MP
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
4a19c3b2 107static void logit(int priority, const char *buf)
4f6325c3 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] == '#'
2dc7b8bd
WD
218 && isDigit(s + 2)
219 && isDigit(s + 3)
220 && isDigit(s + 4))
3648ab3a 221 || (*s != '\t'
2dc7b8bd 222 && ((use_isprint && !isPrint(s))
3648ab3a 223 || *(uchar*)s < ' '))) {
be0602ec 224 if (s != buf && fwrite(buf, s - buf, 1, f) != 1)
25f637a3 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)
25f637a3 231 exit_cleanup(RERR_MESSAGEIO);
be0602ec
WD
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. */
4a19c3b2 237void rwrite(enum logcode code, const char *buf, int len)
0b76cd63 238{
be0602ec 239 int trailing_CR_or_NL;
8fcdc444 240 FILE *f = NULL;
8d9dc9f9 241
a644fc3c 242 if (len < 0)
25f637a3 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:
25f637a3 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
60d465db
WD
304#ifndef ICONV_CONST
305#define ICONV_CONST
306#endif
9a31746b
WD
307 if (ic_chck != (iconv_t)-1) {
308 char convbuf[1024];
60d465db 309 ICONV_CONST char *in_buf = (ICONV_CONST char *)buf;
4a19c3b2 310 char *out_buf = convbuf;
9a31746b
WD
311 size_t in_cnt = len, out_cnt = sizeof convbuf - 1;
312
313 iconv(ic_chck, NULL, 0, NULL, 0);
314 while (iconv(ic_chck, &in_buf,&in_cnt,
60d465db 315 &out_buf,&out_cnt) == (size_t)-1) {
9a31746b 316 if (out_buf != convbuf) {
3648ab3a 317 filtered_fwrite(f, convbuf, out_buf - convbuf, 0);
9a31746b
WD
318 out_buf = convbuf;
319 out_cnt = sizeof convbuf - 1;
320 }
321 if (errno == E2BIG)
322 continue;
3648ab3a 323 fprintf(f, "\\#%03o", *(uchar*)in_buf++);
9a31746b
WD
324 in_cnt--;
325 }
326 if (out_buf != convbuf)
3648ab3a 327 filtered_fwrite(f, convbuf, out_buf - convbuf, 0);
9a31746b
WD
328 } else
329#endif
507433f6 330 filtered_fwrite(f, buf, len, !allow_8bit_chars);
be0602ec
WD
331
332 if (trailing_CR_or_NL) {
333 fputc(trailing_CR_or_NL, f);
0bb4d176 334 fflush(f);
be0602ec 335 }
0b76cd63 336}
0455cd93 337
a039749b
MP
338/* This is the rsync debugging function. Call it with FINFO, FERROR or
339 * FLOG. */
340void rprintf(enum logcode code, const char *format, ...)
554e0a8d 341{
4a7319be 342 va_list ap;
b74b3d53 343 char buf[BIGPATHBUFLEN];
8fcdc444 344 size_t len;
554e0a8d
AT
345
346 va_start(ap, format);
ef74f5d6 347 len = vsnprintf(buf, sizeof buf, format, ap);
554e0a8d
AT
348 va_end(ap);
349
b2f02464 350 /* Deal with buffer overruns. Instead of panicking, just
8fcdc444
WD
351 * truncate the resulting string. (Note that configure ensures
352 * that we have a vsnprintf() that doesn't ever return -1.) */
353 if (len > sizeof buf - 1) {
b74b3d53 354 static const char ellipsis[] = "[...]";
b2f02464
MP
355
356 /* Reset length, and zero-terminate the end of our buffer */
ef74f5d6 357 len = sizeof buf - 1;
b2f02464
MP
358 buf[len] = '\0';
359
360 /* Copy the ellipsis to the end of the string, but give
361 * us one extra character:
362 *
ef74f5d6 363 * v--- null byte at buf[sizeof buf - 1]
b2f02464
MP
364 * abcdefghij0
365 * -> abcd[...]00 <-- now two null bytes at end
366 *
367 * If the input format string has a trailing newline,
368 * we copy it into that extra null; if it doesn't, well,
369 * all we lose is one byte. */
b74b3d53 370 memcpy(buf+len-sizeof ellipsis, ellipsis, sizeof ellipsis);
b2f02464
MP
371 if (format[strlen(format)-1] == '\n') {
372 buf[len-1] = '\n';
373 }
374 }
554e0a8d 375
ff41a59f 376 rwrite(code, buf, len);
554e0a8d 377}
0b76cd63 378
a039749b
MP
379/* This is like rprintf, but it also tries to print some
380 * representation of the error code. Normally errcode = errno.
381 *
382 * Unlike rprintf, this always adds a newline and there should not be
383 * one in the format string.
384 *
385 * Note that since strerror might involve dynamically loading a
386 * message catalog we need to call it once before chroot-ing. */
387void rsyserr(enum logcode code, int errcode, const char *format, ...)
388{
4a7319be 389 va_list ap;
b74b3d53 390 char buf[BIGPATHBUFLEN];
8fcdc444
WD
391 size_t len;
392
c9bce0b8 393 strlcpy(buf, RSYNC_NAME ": ", sizeof buf);
8fcdc444 394 len = (sizeof RSYNC_NAME ": ") - 1;
a039749b
MP
395
396 va_start(ap, format);
8fcdc444 397 len += vsnprintf(buf + len, sizeof buf - len, format, ap);
a039749b
MP
398 va_end(ap);
399
8fcdc444
WD
400 if (len < sizeof buf) {
401 len += snprintf(buf + len, sizeof buf - len,
402 ": %s (%d)\n", strerror(errcode), errcode);
403 }
404 if (len >= sizeof buf)
25f637a3 405 exit_cleanup(RERR_MESSAGEIO);
a039749b 406
a039749b
MP
407 rwrite(code, buf, len);
408}
409
ff41a59f 410void rflush(enum logcode code)
0b76cd63
AT
411{
412 FILE *f = NULL;
0455cd93 413
13b597fa 414 if (am_daemon || code == FLOG)
0b76cd63 415 return;
0b76cd63 416
13b597fa 417 if (code == FERROR || am_server)
0b76cd63 418 f = stderr;
13b597fa
WD
419 else
420 f = stdout;
0b76cd63 421
0b76cd63
AT
422 fflush(f);
423}
424
b5343510 425/* A generic logging routine for send/recv, with parameter substitiution. */
4a19c3b2 426static void log_formatted(enum logcode code, const char *format, const char *op,
112d728f
WD
427 struct file_struct *file, const char *fname,
428 struct stats *initial_stats, int iflags,
429 const char *hlink)
e08bfe12 430{
ddd74b67 431 char buf[MAXPATHLEN+1024], buf2[MAXPATHLEN], fmt[32];
4a19c3b2
WD
432 char *p, *s, *c;
433 const char *n;
ef74f5d6 434 size_t len, total;
1b7c47cb 435 int64 b;
e08bfe12 436
ddd74b67
WD
437 *fmt = '%';
438
aa126974 439 /* We expand % codes one by one in place in buf. We don't
126e7aff
WD
440 * copy in the terminating null of the inserted strings, but
441 * rather keep going until we reach the null of the format. */
ef74f5d6 442 total = strlcpy(buf, format, sizeof buf);
d9c0051f
WD
443 if (total > MAXPATHLEN) {
444 rprintf(FERROR, "log-format string is WAY too long!\n");
25f637a3 445 exit_cleanup(RERR_MESSAGEIO);
d9c0051f
WD
446 }
447 buf[total++] = '\n';
448 buf[total] = '\0';
0455cd93 449
16f960fe 450 for (p = buf; (p = strchr(p, '%')) != NULL; ) {
ddd74b67 451 s = p++;
4a19c3b2 452 c = fmt + 1;
9baed760 453 if (*p == '-')
4a19c3b2
WD
454 *c++ = *p++;
455 while (isDigit(p) && c - fmt < (int)(sizeof fmt) - 8)
456 *c++ = *p++;
e145d51b
WD
457 if (!*p)
458 break;
4a19c3b2 459 *c = '\0';
e08bfe12 460 n = NULL;
e08bfe12 461
0455cd93 462 switch (*p) {
b74b3d53
WD
463 case 'h':
464 if (am_daemon)
465 n = client_name(0);
466 break;
467 case 'a':
468 if (am_daemon)
469 n = client_addr(0);
470 break;
4a7319be 471 case 'l':
ddd74b67
WD
472 strlcat(fmt, ".0f", sizeof fmt);
473 snprintf(buf2, sizeof buf2, fmt,
112d728f 474 (double)F_LENGTH(file));
e08bfe12
AT
475 n = buf2;
476 break;
427b6179
WD
477 case 'U':
478 strlcat(fmt, "ld", sizeof fmt);
479 snprintf(buf2, sizeof buf2, fmt,
82ad07c4 480 preserve_uid ? (long)F_UID(file) : 0);
427b6179
WD
481 n = buf2;
482 break;
483 case 'G':
82ad07c4 484 if (!preserve_gid || F_GID(file) == GID_NONE)
998113fe
WD
485 n = "DEFAULT";
486 else {
487 strlcat(fmt, "ld", sizeof fmt);
488 snprintf(buf2, sizeof buf2, fmt,
82ad07c4 489 (long)F_GID(file));
998113fe
WD
490 n = buf2;
491 }
427b6179 492 break;
4a7319be 493 case 'p':
126e7aff 494 strlcat(fmt, "ld", sizeof fmt);
ddd74b67 495 snprintf(buf2, sizeof buf2, fmt,
126e7aff 496 (long)getpid());
e08bfe12
AT
497 n = buf2;
498 break;
427b6179 499 case 'M':
4a19c3b2
WD
500 n = c = timestring(file->modtime);
501 while ((c = strchr(p, ' ')) != NULL)
502 *c = '-';
427b6179
WD
503 break;
504 case 'B':
4a19c3b2
WD
505 c = buf2 + MAXPATHLEN - PERMSTRING_SIZE - 1;
506 permstring(c, file->mode);
507 n = c + 1; /* skip the type char */
427b6179 508 break;
b74b3d53
WD
509 case 'o':
510 n = op;
511 break;
4a7319be 512 case 'f':
112d728f
WD
513 if (fname) {
514 c = f_name_buf();
515 strlcpy(c, fname, MAXPATHLEN);
516 } else
517 c = f_name(file, NULL);
dbd9ea3e 518 if (am_sender && F_ROOTDIR(file)) {
9baed760 519 pathjoin(buf2, sizeof buf2,
dbd9ea3e 520 F_ROOTDIR(file), c);
0ee6ca98 521 clean_fname(buf2, 0);
4a19c3b2
WD
522 if (fmt[1]) {
523 strlcpy(c, buf2, MAXPATHLEN);
524 n = c;
525 } else
dcbae654 526 n = buf2;
1b81f797 527 } else if (am_daemon && *c != '/') {
ec55b4f2 528 pathjoin(buf2, sizeof buf2,
4a19c3b2 529 curr_dir + module_dirlen, c);
ec55b4f2 530 clean_fname(buf2, 0);
4a19c3b2
WD
531 if (fmt[1]) {
532 strlcpy(c, buf2, MAXPATHLEN);
533 n = c;
534 } else
ec55b4f2 535 n = buf2;
4a19c3b2
WD
536 } else {
537 clean_fname(c, 0);
538 n = c;
539 }
9baed760
WD
540 if (*n == '/')
541 n++;
ab7104da 542 break;
ef74f5d6 543 case 'n':
112d728f
WD
544 if (fname) {
545 c = f_name_buf();
546 strlcpy(c, fname, MAXPATHLEN);
547 } else
548 c = f_name(file, NULL);
0ee6ca98 549 if (S_ISDIR(file->mode))
4a19c3b2
WD
550 strlcat(c, "/", MAXPATHLEN);
551 n = c;
ef74f5d6
WD
552 break;
553 case 'L':
afc65a5a 554 if (hlink && *hlink) {
0ee6ca98 555 n = hlink;
c9bce0b8 556 strlcpy(buf2, " => ", sizeof buf2);
112d728f 557 } else if (S_ISLNK(file->mode) && !fname) {
82ad07c4 558 n = F_SYMLINK(file);
c9bce0b8 559 strlcpy(buf2, " -> ", sizeof buf2);
126e7aff 560 } else {
ef74f5d6 561 n = "";
126e7aff
WD
562 if (!fmt[1])
563 break;
c9bce0b8 564 strlcpy(buf2, " ", sizeof buf2);
126e7aff
WD
565 }
566 strlcat(fmt, "s", sizeof fmt);
567 snprintf(buf2 + 4, sizeof buf2 - 4, fmt, n);
568 n = buf2;
ef74f5d6 569 break;
b74b3d53
WD
570 case 'm':
571 n = lp_name(module_id);
572 break;
573 case 't':
574 n = timestring(time(NULL));
575 break;
576 case 'P':
577 n = lp_path(module_id);
578 break;
579 case 'u':
580 n = auth_user;
581 break;
4a7319be 582 case 'b':
1b7c47cb 583 if (am_sender) {
4a7319be 584 b = stats.total_written -
1b7c47cb
AT
585 initial_stats->total_written;
586 } else {
4a7319be 587 b = stats.total_read -
1b7c47cb
AT
588 initial_stats->total_read;
589 }
0455cd93 590 strlcat(fmt, ".0f", sizeof fmt);
ddd74b67 591 snprintf(buf2, sizeof buf2, fmt, (double)b);
1b7c47cb
AT
592 n = buf2;
593 break;
4a7319be 594 case 'c':
1b7c47cb 595 if (!am_sender) {
4a7319be 596 b = stats.total_written -
1b7c47cb
AT
597 initial_stats->total_written;
598 } else {
4a7319be 599 b = stats.total_read -
1b7c47cb
AT
600 initial_stats->total_read;
601 }
0455cd93 602 strlcat(fmt, ".0f", sizeof fmt);
ddd74b67 603 snprintf(buf2, sizeof buf2, fmt, (double)b);
1b7c47cb
AT
604 n = buf2;
605 break;
ef74f5d6 606 case 'i':
19bc826d 607 if (iflags & ITEM_DELETED) {
d5609e96 608 n = "*deleting";
19bc826d
WD
609 break;
610 }
4a19c3b2
WD
611 n = c = buf2 + MAXPATHLEN - 32;
612 c[0] = iflags & ITEM_LOCAL_CHANGE
fd84673e 613 ? iflags & ITEM_XNAME_FOLLOWS ? 'h' : 'c'
ca62acc3 614 : !(iflags & ITEM_TRANSFER) ? '.'
c1759b9f 615 : !local_server && *op == 's' ? '<' : '>';
4a19c3b2 616 c[1] = S_ISDIR(file->mode) ? 'd'
f8d47c1c 617 : IS_SPECIAL(file->mode) ? 'S'
19bc826d 618 : IS_DEVICE(file->mode) ? 'D'
ef74f5d6 619 : S_ISLNK(file->mode) ? 'L' : 'f';
4a19c3b2
WD
620 c[2] = !(iflags & ITEM_REPORT_CHECKSUM) ? '.' : 'c';
621 c[3] = !(iflags & ITEM_REPORT_SIZE) ? '.' : 's';
622 c[4] = !(iflags & ITEM_REPORT_TIME) ? '.'
4cff7c50 623 : !preserve_times || S_ISLNK(file->mode) ? 'T' : 't';
4a19c3b2
WD
624 c[5] = !(iflags & ITEM_REPORT_PERMS) ? '.' : 'p';
625 c[6] = !(iflags & ITEM_REPORT_OWNER) ? '.' : 'o';
626 c[7] = !(iflags & ITEM_REPORT_GROUP) ? '.' : 'g';
1c3344a1
WD
627 c[8] = !(iflags & ITEM_REPORT_ATIME) ? '.' : 'u';
628 c[9] = !(iflags & ITEM_REPORT_ACL) ? '.' : 'a';
629 c[10] = !(iflags & ITEM_REPORT_XATTR) ? '.' : 'x';
630 c[11] = '\0';
ef74f5d6
WD
631
632 if (iflags & (ITEM_IS_NEW|ITEM_MISSING_DATA)) {
633 char ch = iflags & ITEM_IS_NEW ? '+' : '?';
634 int i;
4a19c3b2
WD
635 for (i = 2; c[i]; i++)
636 c[i] = ch;
637 } else if (c[0] == '.' || c[0] == 'h' || c[0] == 'c') {
19bc826d 638 int i;
4a19c3b2
WD
639 for (i = 2; c[i]; i++) {
640 if (c[i] != '.')
19bc826d
WD
641 break;
642 }
4a19c3b2
WD
643 if (!c[i]) {
644 for (i = 2; c[i]; i++)
645 c[i] = ' ';
19bc826d 646 }
ef74f5d6
WD
647 }
648 break;
e08bfe12
AT
649 }
650
ddd74b67
WD
651 /* "n" is the string to be inserted in place of this % code. */
652 if (!n)
653 continue;
9baed760
WD
654 if (n != buf2 && fmt[1]) {
655 strlcat(fmt, "s", sizeof fmt);
656 snprintf(buf2, sizeof buf2, fmt, n);
657 n = buf2;
658 }
ef74f5d6 659 len = strlen(n);
e08bfe12 660
e145d51b 661 /* Subtract the length of the escape from the string's size. */
0455cd93 662 total -= p - s + 1;
e145d51b 663
d9c0051f 664 if (len + total >= (size_t)sizeof buf) {
1e7098b5
WD
665 rprintf(FERROR,
666 "buffer overflow expanding %%%c -- exiting\n",
0455cd93 667 p[0]);
25f637a3 668 exit_cleanup(RERR_MESSAGEIO);
e08bfe12
AT
669 }
670
aa126974 671 /* Shuffle the rest of the string along to make space for n */
0455cd93
WD
672 if (len != (size_t)(p - s + 1))
673 memmove(s + len, p + 1, total - (s - buf) + 1);
ddd74b67 674 total += len;
aa126974 675
ddd74b67 676 /* Insert the contents of string "n", but NOT its null. */
ef74f5d6 677 if (len)
ddd74b67 678 memcpy(s, n, len);
e08bfe12 679
aa126974 680 /* Skip over inserted string; continue looking */
ddd74b67 681 p = s + len;
e08bfe12
AT
682 }
683
d9c0051f 684 rwrite(code, buf, total);
e08bfe12
AT
685}
686
0455cd93
WD
687/* Return 1 if the format escape is in the log-format string (e.g. look for
688 * the 'b' in the "%9b" format escape). */
16f960fe
WD
689int log_format_has(const char *format, char esc)
690{
691 const char *p;
692
693 if (!format)
694 return 0;
695
696 for (p = format; (p = strchr(p, '%')) != NULL; ) {
697 if (*++p == '-')
698 p++;
2dc7b8bd 699 while (isDigit(p))
16f960fe
WD
700 p++;
701 if (!*p)
702 break;
703 if (*p == esc)
704 return 1;
705 }
706 return 0;
707}
708
d0133e6e 709/* Log the transfer of a file. If the code is FCLIENT, the output just goes
b5343510
WD
710 * to stdout. If it is FLOG, it just goes to the log file. Otherwise we
711 * output to both. */
712void log_item(enum logcode code, struct file_struct *file,
4a19c3b2 713 struct stats *initial_stats, int iflags, const char *hlink)
11a5a3c7 714{
4a19c3b2 715 const char *s_or_r = am_sender ? "send" : "recv";
11a5a3c7 716
b5343510 717 if (code != FLOG && stdout_format && !am_server) {
d0133e6e 718 log_formatted(FCLIENT, stdout_format, s_or_r,
112d728f 719 file, NULL, initial_stats, iflags, hlink);
b5343510 720 }
d0133e6e 721 if (code != FCLIENT && logfile_format && *logfile_format) {
13b597fa 722 log_formatted(FLOG, logfile_format, s_or_r,
112d728f 723 file, NULL, initial_stats, iflags, hlink);
11a5a3c7
AT
724 }
725}
726
1c3e3679 727void maybe_log_item(struct file_struct *file, int iflags, int itemizing,
4a19c3b2 728 const char *buf)
1c3e3679 729{
f2b6fe44 730 int significant_flags = iflags & SIGNIFICANT_ITEM_FLAGS;
6d4ecad1 731 int see_item = itemizing && (significant_flags || *buf
b5343510 732 || stdout_format_has_i > 1 || (verbose > 1 && stdout_format_has_i));
6d4ecad1 733 int local_change = iflags & ITEM_LOCAL_CHANGE && significant_flags;
1c3e3679 734 if (am_server) {
d0133e6e
WD
735 if (logfile_name && !dry_run && see_item
736 && (significant_flags || logfile_format_has_i))
b5343510 737 log_item(FLOG, file, &stats, iflags, buf);
f2b6fe44 738 } else if (see_item || local_change || *buf
d0133e6e
WD
739 || (S_ISDIR(file->mode) && significant_flags)) {
740 enum logcode code = significant_flags || logfile_format_has_i ? FINFO : FCLIENT;
741 log_item(code, file, &stats, iflags, buf);
742 }
1c3e3679
WD
743}
744
4a19c3b2 745void log_delete(const char *fname, int mode)
19bc826d 746{
112d728f 747 static struct {
aac5cab8 748 union file_extras ex[4]; /* just in case... */
112d728f 749 struct file_struct file;
112d728f 750 } x;
19bc826d 751 int len = strlen(fname);
4a19c3b2 752 const char *fmt;
19bc826d 753
112d728f 754 x.file.mode = mode;
19bc826d 755
b5343510 756 if (!verbose && !stdout_format)
41b5b5e7
WD
757 ;
758 else if (am_server && protocol_version >= 29 && len < MAXPATHLEN) {
19bc826d
WD
759 if (S_ISDIR(mode))
760 len++; /* directories include trailing null */
761 send_msg(MSG_DELETED, fname, len);
19bc826d 762 } else {
b5343510 763 fmt = stdout_format_has_o_or_i ? stdout_format : "deleting %n";
112d728f 764 log_formatted(FCLIENT, fmt, "del.", &x.file, fname, &stats,
afc65a5a 765 ITEM_DELETED, NULL);
19bc826d
WD
766 }
767
13b597fa 768 if (!logfile_name || dry_run || !logfile_format)
1eec003a
WD
769 return;
770
13b597fa 771 fmt = logfile_format_has_o_or_i ? logfile_format : "deleting %n";
112d728f 772 log_formatted(FLOG, fmt, "del.", &x.file, fname, &stats, ITEM_DELETED, NULL);
19bc826d 773}
af642a61 774
af642a61
MP
775/*
776 * Called when the transfer is interrupted for some reason.
777 *
778 * Code is one of the RERR_* codes from errcode.h, or terminating
779 * successfully.
780 */
a9766ef1 781void log_exit(int code, const char *file, int line)
9b73d1c0
AT
782{
783 if (code == 0) {
088aff1a 784 rprintf(FLOG,"sent %.0f bytes received %.0f bytes total size %.0f\n",
9b73d1c0
AT
785 (double)stats.total_written,
786 (double)stats.total_read,
787 (double)stats.total_size);
788 } else {
4a7319be
WD
789 const char *name;
790
791 name = rerr_name(code);
792 if (!name)
793 name = "unexplained error";
af642a61 794
1bca1de6
WD
795 /* VANISHED is not an error, only a warning */
796 if (code == RERR_VANISHED) {
0f78b815 797 rprintf(FINFO, "rsync warning: %s (code %d) at %s(%d) [%s=%s]\n",
43eae40e 798 name, code, file, line, who_am_i(), RSYNC_VERSION);
1bca1de6 799 } else {
43eae40e
WD
800 rprintf(FERROR, "rsync error: %s (code %d) at %s(%d) [%s=%s]\n",
801 name, code, file, line, who_am_i(), RSYNC_VERSION);
1bca1de6 802 }
9b73d1c0
AT
803 }
804}