Got rid of the description of 'a' in the %i flags.
[rsync/rsync.git] / log.c
CommitLineData
a039749b 1/* -*- c-file-style: "linux"; -*-
4a7319be 2
0c5a792a 3 Copyright (C) 1998-2001 by Andrew Tridgell <tridge@samba.org>
18c71e96 4 Copyright (C) 2000-2001 by Martin Pool <mbp@samba.org>
4a7319be 5
0b76cd63
AT
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
4a7319be 10
0b76cd63
AT
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
4a7319be 15
0b76cd63
AT
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19*/
20
21/*
3e3dcd62 22 Logging and utility functions.
0c5a792a 23 tridge, May 1998
0b76cd63 24
3e3dcd62
MP
25 Mapping to human-readable messages added by Martin Pool
26 <mbp@samba.org>, Oct 2000.
0b76cd63
AT
27 */
28#include "rsync.h"
29
41b5b5e7 30extern int verbose;
088aff1a 31extern int dry_run;
548abf96
WD
32extern int am_daemon;
33extern int am_server;
34extern int am_sender;
c1759b9f 35extern int local_server;
548abf96
WD
36extern int quiet;
37extern int module_id;
a644fc3c 38extern int msg_fd_out;
19bc826d 39extern int protocol_version;
ef74f5d6 40extern int preserve_times;
6d4ecad1 41extern int log_format_has_i;
d6707171 42extern int log_format_has_o_or_i;
1eec003a 43extern int daemon_log_format_has_o_or_i;
548abf96
WD
44extern char *auth_user;
45extern char *log_format;
46
30e8c8e1 47static int log_initialised;
64c37826 48static int logfile_was_closed;
45a83540 49static char *logfname;
4f6325c3 50static FILE *logfile;
b35d0d8e 51struct stats stats;
e0414f42 52
8fcdc444 53int log_got_error = 0;
af642a61
MP
54
55struct {
56 int code;
57 char const *name;
58} const rerr_names[] = {
4a7319be
WD
59 { RERR_SYNTAX , "syntax or usage error" },
60 { RERR_PROTOCOL , "protocol incompatibility" },
61 { RERR_FILESELECT , "errors selecting input/output files, dirs" },
62 { RERR_UNSUPPORTED, "requested action not supported" },
63 { RERR_STARTCLIENT, "error starting client-server protocol" },
64 { RERR_SOCKETIO , "error in socket IO" },
65 { RERR_FILEIO , "error in file IO" },
66 { RERR_STREAMIO , "error in rsync protocol data stream" },
67 { RERR_MESSAGEIO , "errors with program diagnostics" },
68 { RERR_IPC , "error in IPC code" },
60168410 69 { RERR_CRASHED , "sibling process crashed" },
0047f535 70 { RERR_TERMINATED , "sibling process terminated abnormally" },
4a50a217
WD
71 { RERR_SIGNAL1 , "received SIGUSR1" },
72 { RERR_SIGNAL , "received SIGINT, SIGTERM, or SIGHUP" },
f14a65d9 73 { RERR_WAITCHILD , "waitpid() failed" },
4a7319be
WD
74 { RERR_MALLOC , "error allocating core memory buffers" },
75 { RERR_PARTIAL , "some files could not be transferred" },
584ba4eb 76 { RERR_VANISHED , "some files vanished before they could be transferred" },
4a7319be 77 { RERR_TIMEOUT , "timeout in data send/receive" },
19b27a48
AT
78 { RERR_CMD_FAILED , "remote shell failed" },
79 { RERR_CMD_KILLED , "remote shell killed" },
24cecf13
WD
80 { RERR_CMD_RUN , "remote command could not be run" },
81 { RERR_CMD_NOTFOUND,"remote command not found" },
26718401 82 { RERR_DEL_LIMIT , "the --max-delete limit stopped deletions" },
4a7319be 83 { 0, NULL }
af642a61
MP
84};
85
86
af642a61
MP
87/*
88 * Map from rsync error code to name, or return NULL.
89 */
90static char const *rerr_name(int code)
91{
4a7319be
WD
92 int i;
93 for (i = 0; rerr_names[i].name; i++) {
94 if (rerr_names[i].code == code)
95 return rerr_names[i].name;
96 }
97 return NULL;
af642a61
MP
98}
99
4f6325c3
AT
100static void logit(int priority, char *buf)
101{
64c37826
WD
102 if (logfile_was_closed)
103 logfile_reopen();
104 if (logfile) {
4a7319be 105 fprintf(logfile,"%s [%d] %s",
f7632fc6 106 timestring(time(NULL)), (int)getpid(), buf);
4f6325c3
AT
107 fflush(logfile);
108 } else {
109 syslog(priority, "%s", buf);
110 }
111}
e42c9458 112
6afb9077 113static void syslog_init()
1a016bfd 114{
6afb9077 115 static int been_here = 0;
1a016bfd 116 int options = LOG_PID;
6afb9077
WD
117
118 if (been_here)
119 return;
120 been_here = 1;
121
122#ifdef LOG_NDELAY
123 options |= LOG_NDELAY;
124#endif
125
126#ifdef LOG_DAEMON
127 openlog("rsyncd", options, lp_syslog_facility());
128#else
129 openlog("rsyncd", options);
130#endif
131
132#ifndef LOG_NDELAY
133 logit(LOG_INFO, "rsyncd started\n");
134#endif
135}
136
64c37826
WD
137static void logfile_open(void)
138{
139 extern int orig_umask;
140 int old_umask = umask(022 | orig_umask);
141 logfile = fopen(logfname, "a");
142 umask(old_umask);
143 if (!logfile) {
144 int fopen_errno = errno;
145 /* Rsync falls back to using syslog on failure. */
146 syslog_init();
147 rsyserr(FERROR, fopen_errno,
148 "failed to open log-file %s", logfname);
149 rprintf(FINFO, "Ignoring \"log file\" setting.\n");
150 }
151}
152
6afb9077
WD
153void log_init(void)
154{
bcf5b133 155 time_t t;
1a016bfd 156
0bb4d176
WD
157 if (log_initialised)
158 return;
30e8c8e1 159 log_initialised = 1;
1a016bfd 160
958f3735 161 /* this looks pointless, but it is needed in order for the
4a7319be
WD
162 * C library on some systems to fetch the timezone info
163 * before the chroot */
958f3735
AT
164 t = time(NULL);
165 localtime(&t);
166
167 /* optionally use a log file instead of syslog */
45a83540 168 logfname = lp_log_file();
64c37826
WD
169 if (logfname && *logfname)
170 logfile_open();
171 else
172 syslog_init();
1a016bfd 173}
1a016bfd 174
64c37826 175void logfile_close(void)
4a239e98 176{
64c37826
WD
177 if (logfile) {
178 logfile_was_closed = 1;
179 fclose(logfile);
180 logfile = NULL;
4a239e98
WD
181 }
182}
183
64c37826 184void logfile_reopen(void)
4a239e98 185{
64c37826
WD
186 if (logfile_was_closed) {
187 logfile_was_closed = 0;
188 logfile_open();
4a239e98
WD
189 }
190}
191
be0602ec
WD
192static void filtered_fwrite(const char *buf, int len, FILE *f)
193{
194 const char *s, *end = buf + len;
195 for (s = buf; s < end; s++) {
196 if ((s < end - 4
197 && *s == '\\' && s[1] == '0'
198 && isdigit(*(uchar*)(s+2))
199 && isdigit(*(uchar*)(s+3))
200 && isdigit(*(uchar*)(s+4)))
201 || !isprint(*(uchar*)s)
202 || *(uchar*)s < ' ') {
203 if (s != buf && fwrite(buf, s - buf, 1, f) != 1)
204 exit_cleanup(RERR_MESSAGEIO);
205 fprintf(f, "\\%04o", *(uchar*)s);
206 buf = s + 1;
207 }
208 }
209 if (buf != end && fwrite(buf, end - buf, 1, f) != 1)
210 exit_cleanup(RERR_MESSAGEIO);
211}
212
554e0a8d 213/* this is the underlying (unformatted) rsync debugging function. Call
be0602ec
WD
214 * it with FINFO, FERROR or FLOG. Note: recursion can happen with
215 * certain fatal conditions. */
ff41a59f 216void rwrite(enum logcode code, char *buf, int len)
0b76cd63 217{
be0602ec 218 int trailing_CR_or_NL;
8fcdc444 219 FILE *f = NULL;
8d9dc9f9 220
a644fc3c
WD
221 if (quiet && code == FINFO)
222 return;
b86f0cef 223
a644fc3c
WD
224 if (len < 0)
225 exit_cleanup(RERR_MESSAGEIO);
0b76cd63 226
0bb4d176
WD
227 if (am_server && msg_fd_out >= 0) {
228 /* Pass the message to our sibling. */
229 send_msg((enum msgcode)code, buf, len);
11a5a3c7
AT
230 return;
231 }
232
1eec003a
WD
233 if (code == FCLIENT)
234 code = FINFO;
235 else if (am_daemon) {
0bb4d176
WD
236 static int in_block;
237 char msg[2048];
238 int priority = code == FERROR ? LOG_WARNING : LOG_INFO;
239
240 if (in_block)
a644fc3c 241 return;
0bb4d176
WD
242 in_block = 1;
243 if (!log_initialised)
244 log_init();
245 strlcpy(msg, buf, MIN((int)sizeof msg, len + 1));
246 logit(priority, msg);
247 in_block = 0;
248
249 if (code == FLOG || !am_server)
a644fc3c 250 return;
0bb4d176 251 } else if (code == FLOG)
ff8b29b8 252 return;
0bb4d176
WD
253
254 if (am_server) {
255 /* Pass the message to the non-server side. */
256 if (io_multiplex_write((enum msgcode)code, buf, len))
257 return;
258 if (am_daemon) {
259 /* TODO: can we send the error to the user somehow? */
260 return;
261 }
0b76cd63
AT
262 }
263
be0602ec
WD
264 switch (code) {
265 case FERROR:
ff81e809 266 log_got_error = 1;
ff8b29b8 267 f = stderr;
0ace7b25 268 goto pre_scan;
be0602ec 269 case FINFO:
0bb4d176 270 f = am_server ? stderr : stdout;
0ace7b25
WD
271 pre_scan:
272 while (len > 1 && (*buf == '\n' || *buf == '\t')) {
be0602ec
WD
273 fputc(*buf, f);
274 buf++;
275 len--;
276 }
277 break;
278 case FNAME:
279 f = am_server ? stderr : stdout;
280 break;
281 default:
0bb4d176 282 exit_cleanup(RERR_MESSAGEIO);
be0602ec 283 }
0b76cd63 284
be0602ec
WD
285 trailing_CR_or_NL = len && (buf[len-1] == '\n' || buf[len-1] == '\r')
286 ? buf[--len] : 0;
8d9dc9f9 287
be0602ec
WD
288 filtered_fwrite(buf, len, f);
289
290 if (trailing_CR_or_NL) {
291 fputc(trailing_CR_or_NL, f);
0bb4d176 292 fflush(f);
be0602ec 293 }
0b76cd63 294}
0455cd93 295
a039749b
MP
296/* This is the rsync debugging function. Call it with FINFO, FERROR or
297 * FLOG. */
298void rprintf(enum logcode code, const char *format, ...)
554e0a8d 299{
4a7319be 300 va_list ap;
b74b3d53 301 char buf[BIGPATHBUFLEN];
8fcdc444 302 size_t len;
554e0a8d
AT
303
304 va_start(ap, format);
ef74f5d6 305 len = vsnprintf(buf, sizeof buf, format, ap);
554e0a8d
AT
306 va_end(ap);
307
b2f02464 308 /* Deal with buffer overruns. Instead of panicking, just
8fcdc444
WD
309 * truncate the resulting string. (Note that configure ensures
310 * that we have a vsnprintf() that doesn't ever return -1.) */
311 if (len > sizeof buf - 1) {
b74b3d53 312 static const char ellipsis[] = "[...]";
b2f02464
MP
313
314 /* Reset length, and zero-terminate the end of our buffer */
ef74f5d6 315 len = sizeof buf - 1;
b2f02464
MP
316 buf[len] = '\0';
317
318 /* Copy the ellipsis to the end of the string, but give
319 * us one extra character:
320 *
ef74f5d6 321 * v--- null byte at buf[sizeof buf - 1]
b2f02464
MP
322 * abcdefghij0
323 * -> abcd[...]00 <-- now two null bytes at end
324 *
325 * If the input format string has a trailing newline,
326 * we copy it into that extra null; if it doesn't, well,
327 * all we lose is one byte. */
b74b3d53 328 memcpy(buf+len-sizeof ellipsis, ellipsis, sizeof ellipsis);
b2f02464
MP
329 if (format[strlen(format)-1] == '\n') {
330 buf[len-1] = '\n';
331 }
332 }
554e0a8d 333
ff41a59f 334 rwrite(code, buf, len);
554e0a8d 335}
0b76cd63 336
a039749b
MP
337/* This is like rprintf, but it also tries to print some
338 * representation of the error code. Normally errcode = errno.
339 *
340 * Unlike rprintf, this always adds a newline and there should not be
341 * one in the format string.
342 *
343 * Note that since strerror might involve dynamically loading a
344 * message catalog we need to call it once before chroot-ing. */
345void rsyserr(enum logcode code, int errcode, const char *format, ...)
346{
4a7319be 347 va_list ap;
b74b3d53 348 char buf[BIGPATHBUFLEN];
8fcdc444
WD
349 size_t len;
350
351 strcpy(buf, RSYNC_NAME ": ");
352 len = (sizeof RSYNC_NAME ": ") - 1;
a039749b
MP
353
354 va_start(ap, format);
8fcdc444 355 len += vsnprintf(buf + len, sizeof buf - len, format, ap);
a039749b
MP
356 va_end(ap);
357
8fcdc444
WD
358 if (len < sizeof buf) {
359 len += snprintf(buf + len, sizeof buf - len,
360 ": %s (%d)\n", strerror(errcode), errcode);
361 }
362 if (len >= sizeof buf)
4a7319be 363 exit_cleanup(RERR_MESSAGEIO);
a039749b 364
a039749b
MP
365 rwrite(code, buf, len);
366}
367
ff41a59f 368void rflush(enum logcode code)
0b76cd63
AT
369{
370 FILE *f = NULL;
0455cd93 371
0b76cd63
AT
372 if (am_daemon) {
373 return;
374 }
375
ff41a59f 376 if (code == FLOG) {
e08bfe12 377 return;
4a7319be 378 }
e08bfe12 379
ff41a59f 380 if (code == FERROR) {
0b76cd63 381 f = stderr;
4a7319be 382 }
0b76cd63 383
ff41a59f 384 if (code == FINFO) {
4a7319be 385 if (am_server)
0b76cd63
AT
386 f = stderr;
387 else
388 f = stdout;
4a7319be 389 }
0b76cd63 390
65417579 391 if (!f) exit_cleanup(RERR_MESSAGEIO);
0b76cd63
AT
392 fflush(f);
393}
394
e08bfe12 395/* a generic logging routine for send/recv, with parameter
4a7319be 396 * substitiution */
afc65a5a
WD
397static void log_formatted(enum logcode code, char *format, char *op,
398 struct file_struct *file, struct stats *initial_stats,
399 int iflags, char *hlink)
e08bfe12 400{
ddd74b67
WD
401 char buf[MAXPATHLEN+1024], buf2[MAXPATHLEN], fmt[32];
402 char *p, *s, *n;
ef74f5d6 403 size_t len, total;
1b7c47cb 404 int64 b;
e08bfe12 405
ddd74b67
WD
406 *fmt = '%';
407
aa126974 408 /* We expand % codes one by one in place in buf. We don't
126e7aff
WD
409 * copy in the terminating null of the inserted strings, but
410 * rather keep going until we reach the null of the format. */
ef74f5d6 411 total = strlcpy(buf, format, sizeof buf);
d9c0051f
WD
412 if (total > MAXPATHLEN) {
413 rprintf(FERROR, "log-format string is WAY too long!\n");
414 exit_cleanup(RERR_MESSAGEIO);
415 }
416 buf[total++] = '\n';
417 buf[total] = '\0';
0455cd93 418
16f960fe 419 for (p = buf; (p = strchr(p, '%')) != NULL; ) {
ddd74b67
WD
420 s = p++;
421 n = fmt + 1;
9baed760
WD
422 if (*p == '-')
423 *n++ = *p++;
b4bf2b5a 424 while (isdigit(*(uchar*)p) && n - fmt < (int)(sizeof fmt) - 8)
ddd74b67 425 *n++ = *p++;
e145d51b
WD
426 if (!*p)
427 break;
ddd74b67 428 *n = '\0';
e08bfe12 429 n = NULL;
e08bfe12 430
0455cd93 431 switch (*p) {
b74b3d53
WD
432 case 'h':
433 if (am_daemon)
434 n = client_name(0);
435 break;
436 case 'a':
437 if (am_daemon)
438 n = client_addr(0);
439 break;
4a7319be 440 case 'l':
ddd74b67
WD
441 strlcat(fmt, ".0f", sizeof fmt);
442 snprintf(buf2, sizeof buf2, fmt,
4a7319be 443 (double)file->length);
e08bfe12
AT
444 n = buf2;
445 break;
4a7319be 446 case 'p':
126e7aff 447 strlcat(fmt, "ld", sizeof fmt);
ddd74b67 448 snprintf(buf2, sizeof buf2, fmt,
126e7aff 449 (long)getpid());
e08bfe12
AT
450 n = buf2;
451 break;
b74b3d53
WD
452 case 'o':
453 n = op;
454 break;
4a7319be 455 case 'f':
be0602ec 456 n = f_name(file, NULL);
9baed760
WD
457 if (am_sender && file->dir.root) {
458 pathjoin(buf2, sizeof buf2,
459 file->dir.root, n);
0ee6ca98 460 clean_fname(buf2, 0);
dcbae654 461 if (fmt[1])
0ee6ca98 462 strlcpy(n, buf2, MAXPATHLEN);
dcbae654
WD
463 else
464 n = buf2;
0ee6ca98
WD
465 } else
466 clean_fname(n, 0);
9baed760
WD
467 if (*n == '/')
468 n++;
ab7104da 469 break;
ef74f5d6 470 case 'n':
be0602ec 471 n = f_name(file, NULL);
0ee6ca98
WD
472 if (S_ISDIR(file->mode))
473 strlcat(n, "/", MAXPATHLEN);
ef74f5d6
WD
474 break;
475 case 'L':
afc65a5a 476 if (hlink && *hlink) {
0ee6ca98 477 n = hlink;
126e7aff 478 strcpy(buf2, " => ");
afc65a5a 479 } else if (S_ISLNK(file->mode) && file->u.link) {
0ee6ca98 480 n = file->u.link;
126e7aff
WD
481 strcpy(buf2, " -> ");
482 } else {
ef74f5d6 483 n = "";
126e7aff
WD
484 if (!fmt[1])
485 break;
486 strcpy(buf2, " ");
487 }
488 strlcat(fmt, "s", sizeof fmt);
489 snprintf(buf2 + 4, sizeof buf2 - 4, fmt, n);
490 n = buf2;
ef74f5d6 491 break;
b74b3d53
WD
492 case 'm':
493 n = lp_name(module_id);
494 break;
495 case 't':
496 n = timestring(time(NULL));
497 break;
498 case 'P':
499 n = lp_path(module_id);
500 break;
501 case 'u':
502 n = auth_user;
503 break;
4a7319be 504 case 'b':
1b7c47cb 505 if (am_sender) {
4a7319be 506 b = stats.total_written -
1b7c47cb
AT
507 initial_stats->total_written;
508 } else {
4a7319be 509 b = stats.total_read -
1b7c47cb
AT
510 initial_stats->total_read;
511 }
0455cd93 512 strlcat(fmt, ".0f", sizeof fmt);
ddd74b67 513 snprintf(buf2, sizeof buf2, fmt, (double)b);
1b7c47cb
AT
514 n = buf2;
515 break;
4a7319be 516 case 'c':
1b7c47cb 517 if (!am_sender) {
4a7319be 518 b = stats.total_written -
1b7c47cb
AT
519 initial_stats->total_written;
520 } else {
4a7319be 521 b = stats.total_read -
1b7c47cb
AT
522 initial_stats->total_read;
523 }
0455cd93 524 strlcat(fmt, ".0f", sizeof fmt);
ddd74b67 525 snprintf(buf2, sizeof buf2, fmt, (double)b);
1b7c47cb
AT
526 n = buf2;
527 break;
ef74f5d6 528 case 'i':
19bc826d 529 if (iflags & ITEM_DELETED) {
d5609e96 530 n = "*deleting";
19bc826d
WD
531 break;
532 }
b4bf2b5a 533 n = buf2 + MAXPATHLEN - 32;
fd84673e
WD
534 n[0] = iflags & ITEM_LOCAL_CHANGE
535 ? iflags & ITEM_XNAME_FOLLOWS ? 'h' : 'c'
ca62acc3 536 : !(iflags & ITEM_TRANSFER) ? '.'
c1759b9f 537 : !local_server && *op == 's' ? '<' : '>';
19bc826d
WD
538 n[1] = S_ISDIR(file->mode) ? 'd'
539 : IS_DEVICE(file->mode) ? 'D'
ef74f5d6 540 : S_ISLNK(file->mode) ? 'L' : 'f';
d4e01963
WD
541 n[2] = !(iflags & ITEM_REPORT_CHECKSUM) ? '.' : 'c';
542 n[3] = !(iflags & ITEM_REPORT_SIZE) ? '.' : 's';
543 n[4] = !(iflags & ITEM_REPORT_TIME) ? '.'
ef74f5d6
WD
544 : !preserve_times || IS_DEVICE(file->mode)
545 || S_ISLNK(file->mode) ? 'T' : 't';
d4e01963
WD
546 n[5] = !(iflags & ITEM_REPORT_PERMS) ? '.' : 'p';
547 n[6] = !(iflags & ITEM_REPORT_OWNER) ? '.' : 'o';
548 n[7] = !(iflags & ITEM_REPORT_GROUP) ? '.' : 'g';
ca62acc3 549 n[8] = !(iflags & ITEM_REPORT_XATTRS) ? '.' : 'a';
afc65a5a 550 n[9] = '\0';
ef74f5d6
WD
551
552 if (iflags & (ITEM_IS_NEW|ITEM_MISSING_DATA)) {
553 char ch = iflags & ITEM_IS_NEW ? '+' : '?';
554 int i;
555 for (i = 2; n[i]; i++)
556 n[i] = ch;
6d4ecad1
WD
557 } else if (n[0] == '.' || n[0] == 'h'
558 || (n[0] == 'c' && n[1] == 'f')) {
19bc826d
WD
559 int i;
560 for (i = 2; n[i]; i++) {
561 if (n[i] != '.')
562 break;
563 }
564 if (!n[i]) {
565 for (i = 2; n[i]; i++)
566 n[i] = ' ';
19bc826d 567 }
ef74f5d6
WD
568 }
569 break;
e08bfe12
AT
570 }
571
ddd74b67
WD
572 /* "n" is the string to be inserted in place of this % code. */
573 if (!n)
574 continue;
9baed760
WD
575 if (n != buf2 && fmt[1]) {
576 strlcat(fmt, "s", sizeof fmt);
577 snprintf(buf2, sizeof buf2, fmt, n);
578 n = buf2;
579 }
ef74f5d6 580 len = strlen(n);
e08bfe12 581
e145d51b 582 /* Subtract the length of the escape from the string's size. */
0455cd93 583 total -= p - s + 1;
e145d51b 584
d9c0051f 585 if (len + total >= (size_t)sizeof buf) {
1e7098b5
WD
586 rprintf(FERROR,
587 "buffer overflow expanding %%%c -- exiting\n",
0455cd93 588 p[0]);
65417579 589 exit_cleanup(RERR_MESSAGEIO);
e08bfe12
AT
590 }
591
aa126974 592 /* Shuffle the rest of the string along to make space for n */
0455cd93
WD
593 if (len != (size_t)(p - s + 1))
594 memmove(s + len, p + 1, total - (s - buf) + 1);
ddd74b67 595 total += len;
aa126974 596
ddd74b67 597 /* Insert the contents of string "n", but NOT its null. */
ef74f5d6 598 if (len)
ddd74b67 599 memcpy(s, n, len);
e08bfe12 600
aa126974 601 /* Skip over inserted string; continue looking */
ddd74b67 602 p = s + len;
e08bfe12
AT
603 }
604
d9c0051f 605 rwrite(code, buf, total);
e08bfe12
AT
606}
607
0455cd93
WD
608/* Return 1 if the format escape is in the log-format string (e.g. look for
609 * the 'b' in the "%9b" format escape). */
16f960fe
WD
610int log_format_has(const char *format, char esc)
611{
612 const char *p;
613
614 if (!format)
615 return 0;
616
617 for (p = format; (p = strchr(p, '%')) != NULL; ) {
618 if (*++p == '-')
619 p++;
620 while (isdigit(*(uchar*)p))
621 p++;
622 if (!*p)
623 break;
624 if (*p == esc)
625 return 1;
626 }
627 return 0;
628}
629
afc65a5a
WD
630/* log the transfer of a file */
631void log_item(struct file_struct *file, struct stats *initial_stats,
632 int iflags, char *hlink)
11a5a3c7 633{
afc65a5a 634 char *s_or_r = am_sender ? "send" : "recv";
11a5a3c7 635
11a5a3c7 636 if (lp_transfer_logging(module_id)) {
afc65a5a
WD
637 log_formatted(FLOG, lp_log_format(module_id), s_or_r,
638 file, initial_stats, iflags, hlink);
b6062654 639 } else if (log_format && !am_server) {
be0602ec 640 log_formatted(FNAME, log_format, s_or_r,
afc65a5a 641 file, initial_stats, iflags, hlink);
11a5a3c7
AT
642 }
643}
644
1c3e3679
WD
645void maybe_log_item(struct file_struct *file, int iflags, int itemizing,
646 char *buf)
647{
f2b6fe44 648 int significant_flags = iflags & SIGNIFICANT_ITEM_FLAGS;
6d4ecad1 649 int see_item = itemizing && (significant_flags || *buf
a9e47626 650 || log_format_has_i > 1 || (verbose > 1 && log_format_has_i));
6d4ecad1 651 int local_change = iflags & ITEM_LOCAL_CHANGE && significant_flags;
1c3e3679
WD
652 if (am_server) {
653 if (am_daemon && !dry_run && see_item)
654 log_item(file, &stats, iflags, buf);
f2b6fe44
WD
655 } else if (see_item || local_change || *buf
656 || (S_ISDIR(file->mode) && significant_flags))
1c3e3679
WD
657 log_item(file, &stats, iflags, buf);
658}
659
19bc826d
WD
660void log_delete(char *fname, int mode)
661{
662 static struct file_struct file;
663 int len = strlen(fname);
19bc826d
WD
664 char *fmt;
665
666 file.mode = mode;
667 file.basename = fname;
668
41b5b5e7
WD
669 if (!verbose && !log_format)
670 ;
671 else if (am_server && protocol_version >= 29 && len < MAXPATHLEN) {
19bc826d
WD
672 if (S_ISDIR(mode))
673 len++; /* directories include trailing null */
674 send_msg(MSG_DELETED, fname, len);
19bc826d 675 } else {
d5609e96 676 fmt = log_format_has_o_or_i ? log_format : "deleting %n";
afc65a5a
WD
677 log_formatted(FCLIENT, fmt, "del.", &file, &stats,
678 ITEM_DELETED, NULL);
19bc826d
WD
679 }
680
41b5b5e7 681 if (!am_daemon || dry_run || !lp_transfer_logging(module_id))
1eec003a
WD
682 return;
683
d5609e96 684 fmt = daemon_log_format_has_o_or_i ? lp_log_format(module_id) : "deleting %n";
afc65a5a 685 log_formatted(FLOG, fmt, "del.", &file, &stats, ITEM_DELETED, NULL);
19bc826d 686}
af642a61 687
af642a61
MP
688/*
689 * Called when the transfer is interrupted for some reason.
690 *
691 * Code is one of the RERR_* codes from errcode.h, or terminating
692 * successfully.
693 */
a9766ef1 694void log_exit(int code, const char *file, int line)
9b73d1c0
AT
695{
696 if (code == 0) {
088aff1a 697 rprintf(FLOG,"sent %.0f bytes received %.0f bytes total size %.0f\n",
9b73d1c0
AT
698 (double)stats.total_written,
699 (double)stats.total_read,
700 (double)stats.total_size);
701 } else {
4a7319be
WD
702 const char *name;
703
704 name = rerr_name(code);
705 if (!name)
706 name = "unexplained error";
af642a61 707
1bca1de6
WD
708 /* VANISHED is not an error, only a warning */
709 if (code == RERR_VANISHED) {
710 rprintf(FINFO, "rsync warning: %s (code %d) at %s(%d)\n",
711 name, code, file, line);
712 } else {
713 rprintf(FERROR, "rsync error: %s (code %d) at %s(%d)\n",
714 name, code, file, line);
715 }
9b73d1c0
AT
716 }
717}