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