Commit | Line | Data |
---|---|---|
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 | 30 | extern int verbose; |
088aff1a | 31 | extern int dry_run; |
548abf96 WD |
32 | extern int am_daemon; |
33 | extern int am_server; | |
34 | extern int am_sender; | |
c1759b9f | 35 | extern int local_server; |
548abf96 WD |
36 | extern int quiet; |
37 | extern int module_id; | |
a644fc3c | 38 | extern int msg_fd_out; |
19bc826d | 39 | extern int protocol_version; |
ef74f5d6 | 40 | extern int preserve_times; |
6d4ecad1 | 41 | extern int log_format_has_i; |
d6707171 | 42 | extern int log_format_has_o_or_i; |
1eec003a | 43 | extern int daemon_log_format_has_o_or_i; |
548abf96 WD |
44 | extern char *auth_user; |
45 | extern char *log_format; | |
46 | ||
30e8c8e1 | 47 | static int log_initialised; |
64c37826 | 48 | static int logfile_was_closed; |
45a83540 | 49 | static char *logfname; |
4f6325c3 | 50 | static FILE *logfile; |
b35d0d8e | 51 | struct stats stats; |
e0414f42 | 52 | |
8fcdc444 | 53 | int log_got_error = 0; |
af642a61 MP |
54 | |
55 | struct { | |
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 | */ | |
90 | static 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 |
100 | static 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 | 113 | static 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 |
137 | static 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 |
153 | void 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 | 175 | void 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 | 184 | void 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 |
192 | static 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 | 216 | void 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; |
be0602ec WD |
268 | break; |
269 | case FINFO: | |
0bb4d176 | 270 | f = am_server ? stderr : stdout; |
be0602ec WD |
271 | while (len && (*buf == '\n' || *buf == '\t')) { |
272 | fputc(*buf, f); | |
273 | buf++; | |
274 | len--; | |
275 | } | |
276 | break; | |
277 | case FNAME: | |
278 | f = am_server ? stderr : stdout; | |
279 | break; | |
280 | default: | |
0bb4d176 | 281 | exit_cleanup(RERR_MESSAGEIO); |
be0602ec | 282 | } |
0b76cd63 | 283 | |
be0602ec WD |
284 | trailing_CR_or_NL = len && (buf[len-1] == '\n' || buf[len-1] == '\r') |
285 | ? buf[--len] : 0; | |
8d9dc9f9 | 286 | |
be0602ec WD |
287 | filtered_fwrite(buf, len, f); |
288 | ||
289 | if (trailing_CR_or_NL) { | |
290 | fputc(trailing_CR_or_NL, f); | |
0bb4d176 | 291 | fflush(f); |
be0602ec | 292 | } |
0b76cd63 | 293 | } |
0455cd93 | 294 | |
a039749b MP |
295 | /* This is the rsync debugging function. Call it with FINFO, FERROR or |
296 | * FLOG. */ | |
297 | void rprintf(enum logcode code, const char *format, ...) | |
554e0a8d | 298 | { |
4a7319be | 299 | va_list ap; |
b74b3d53 | 300 | char buf[BIGPATHBUFLEN]; |
8fcdc444 | 301 | size_t len; |
554e0a8d AT |
302 | |
303 | va_start(ap, format); | |
ef74f5d6 | 304 | len = vsnprintf(buf, sizeof buf, format, ap); |
554e0a8d AT |
305 | va_end(ap); |
306 | ||
b2f02464 | 307 | /* Deal with buffer overruns. Instead of panicking, just |
8fcdc444 WD |
308 | * truncate the resulting string. (Note that configure ensures |
309 | * that we have a vsnprintf() that doesn't ever return -1.) */ | |
310 | if (len > sizeof buf - 1) { | |
b74b3d53 | 311 | static const char ellipsis[] = "[...]"; |
b2f02464 MP |
312 | |
313 | /* Reset length, and zero-terminate the end of our buffer */ | |
ef74f5d6 | 314 | len = sizeof buf - 1; |
b2f02464 MP |
315 | buf[len] = '\0'; |
316 | ||
317 | /* Copy the ellipsis to the end of the string, but give | |
318 | * us one extra character: | |
319 | * | |
ef74f5d6 | 320 | * v--- null byte at buf[sizeof buf - 1] |
b2f02464 MP |
321 | * abcdefghij0 |
322 | * -> abcd[...]00 <-- now two null bytes at end | |
323 | * | |
324 | * If the input format string has a trailing newline, | |
325 | * we copy it into that extra null; if it doesn't, well, | |
326 | * all we lose is one byte. */ | |
b74b3d53 | 327 | memcpy(buf+len-sizeof ellipsis, ellipsis, sizeof ellipsis); |
b2f02464 MP |
328 | if (format[strlen(format)-1] == '\n') { |
329 | buf[len-1] = '\n'; | |
330 | } | |
331 | } | |
554e0a8d | 332 | |
ff41a59f | 333 | rwrite(code, buf, len); |
554e0a8d | 334 | } |
0b76cd63 | 335 | |
a039749b MP |
336 | /* This is like rprintf, but it also tries to print some |
337 | * representation of the error code. Normally errcode = errno. | |
338 | * | |
339 | * Unlike rprintf, this always adds a newline and there should not be | |
340 | * one in the format string. | |
341 | * | |
342 | * Note that since strerror might involve dynamically loading a | |
343 | * message catalog we need to call it once before chroot-ing. */ | |
344 | void rsyserr(enum logcode code, int errcode, const char *format, ...) | |
345 | { | |
4a7319be | 346 | va_list ap; |
b74b3d53 | 347 | char buf[BIGPATHBUFLEN]; |
8fcdc444 WD |
348 | size_t len; |
349 | ||
350 | strcpy(buf, RSYNC_NAME ": "); | |
351 | len = (sizeof RSYNC_NAME ": ") - 1; | |
a039749b MP |
352 | |
353 | va_start(ap, format); | |
8fcdc444 | 354 | len += vsnprintf(buf + len, sizeof buf - len, format, ap); |
a039749b MP |
355 | va_end(ap); |
356 | ||
8fcdc444 WD |
357 | if (len < sizeof buf) { |
358 | len += snprintf(buf + len, sizeof buf - len, | |
359 | ": %s (%d)\n", strerror(errcode), errcode); | |
360 | } | |
361 | if (len >= sizeof buf) | |
4a7319be | 362 | exit_cleanup(RERR_MESSAGEIO); |
a039749b | 363 | |
a039749b MP |
364 | rwrite(code, buf, len); |
365 | } | |
366 | ||
ff41a59f | 367 | void rflush(enum logcode code) |
0b76cd63 AT |
368 | { |
369 | FILE *f = NULL; | |
0455cd93 | 370 | |
0b76cd63 AT |
371 | if (am_daemon) { |
372 | return; | |
373 | } | |
374 | ||
ff41a59f | 375 | if (code == FLOG) { |
e08bfe12 | 376 | return; |
4a7319be | 377 | } |
e08bfe12 | 378 | |
ff41a59f | 379 | if (code == FERROR) { |
0b76cd63 | 380 | f = stderr; |
4a7319be | 381 | } |
0b76cd63 | 382 | |
ff41a59f | 383 | if (code == FINFO) { |
4a7319be | 384 | if (am_server) |
0b76cd63 AT |
385 | f = stderr; |
386 | else | |
387 | f = stdout; | |
4a7319be | 388 | } |
0b76cd63 | 389 | |
65417579 | 390 | if (!f) exit_cleanup(RERR_MESSAGEIO); |
0b76cd63 AT |
391 | fflush(f); |
392 | } | |
393 | ||
e08bfe12 | 394 | /* a generic logging routine for send/recv, with parameter |
4a7319be | 395 | * substitiution */ |
afc65a5a WD |
396 | static void log_formatted(enum logcode code, char *format, char *op, |
397 | struct file_struct *file, struct stats *initial_stats, | |
398 | int iflags, char *hlink) | |
e08bfe12 | 399 | { |
ddd74b67 WD |
400 | char buf[MAXPATHLEN+1024], buf2[MAXPATHLEN], fmt[32]; |
401 | char *p, *s, *n; | |
ef74f5d6 | 402 | size_t len, total; |
1b7c47cb | 403 | int64 b; |
e08bfe12 | 404 | |
ddd74b67 WD |
405 | *fmt = '%'; |
406 | ||
aa126974 | 407 | /* We expand % codes one by one in place in buf. We don't |
126e7aff WD |
408 | * copy in the terminating null of the inserted strings, but |
409 | * rather keep going until we reach the null of the format. */ | |
ef74f5d6 | 410 | total = strlcpy(buf, format, sizeof buf); |
d9c0051f WD |
411 | if (total > MAXPATHLEN) { |
412 | rprintf(FERROR, "log-format string is WAY too long!\n"); | |
413 | exit_cleanup(RERR_MESSAGEIO); | |
414 | } | |
415 | buf[total++] = '\n'; | |
416 | buf[total] = '\0'; | |
0455cd93 | 417 | |
16f960fe | 418 | for (p = buf; (p = strchr(p, '%')) != NULL; ) { |
ddd74b67 WD |
419 | s = p++; |
420 | n = fmt + 1; | |
9baed760 WD |
421 | if (*p == '-') |
422 | *n++ = *p++; | |
b4bf2b5a | 423 | while (isdigit(*(uchar*)p) && n - fmt < (int)(sizeof fmt) - 8) |
ddd74b67 | 424 | *n++ = *p++; |
e145d51b WD |
425 | if (!*p) |
426 | break; | |
ddd74b67 | 427 | *n = '\0'; |
e08bfe12 | 428 | n = NULL; |
e08bfe12 | 429 | |
0455cd93 | 430 | switch (*p) { |
b74b3d53 WD |
431 | case 'h': |
432 | if (am_daemon) | |
433 | n = client_name(0); | |
434 | break; | |
435 | case 'a': | |
436 | if (am_daemon) | |
437 | n = client_addr(0); | |
438 | break; | |
4a7319be | 439 | case 'l': |
ddd74b67 WD |
440 | strlcat(fmt, ".0f", sizeof fmt); |
441 | snprintf(buf2, sizeof buf2, fmt, | |
4a7319be | 442 | (double)file->length); |
e08bfe12 AT |
443 | n = buf2; |
444 | break; | |
4a7319be | 445 | case 'p': |
126e7aff | 446 | strlcat(fmt, "ld", sizeof fmt); |
ddd74b67 | 447 | snprintf(buf2, sizeof buf2, fmt, |
126e7aff | 448 | (long)getpid()); |
e08bfe12 AT |
449 | n = buf2; |
450 | break; | |
b74b3d53 WD |
451 | case 'o': |
452 | n = op; | |
453 | break; | |
4a7319be | 454 | case 'f': |
be0602ec | 455 | n = f_name(file, NULL); |
9baed760 WD |
456 | if (am_sender && file->dir.root) { |
457 | pathjoin(buf2, sizeof buf2, | |
458 | file->dir.root, n); | |
0ee6ca98 | 459 | clean_fname(buf2, 0); |
dcbae654 | 460 | if (fmt[1]) |
0ee6ca98 | 461 | strlcpy(n, buf2, MAXPATHLEN); |
dcbae654 WD |
462 | else |
463 | n = buf2; | |
0ee6ca98 WD |
464 | } else |
465 | clean_fname(n, 0); | |
9baed760 WD |
466 | if (*n == '/') |
467 | n++; | |
ab7104da | 468 | break; |
ef74f5d6 | 469 | case 'n': |
be0602ec | 470 | n = f_name(file, NULL); |
0ee6ca98 WD |
471 | if (S_ISDIR(file->mode)) |
472 | strlcat(n, "/", MAXPATHLEN); | |
ef74f5d6 WD |
473 | break; |
474 | case 'L': | |
afc65a5a | 475 | if (hlink && *hlink) { |
0ee6ca98 | 476 | n = hlink; |
126e7aff | 477 | strcpy(buf2, " => "); |
afc65a5a | 478 | } else if (S_ISLNK(file->mode) && file->u.link) { |
0ee6ca98 | 479 | n = file->u.link; |
126e7aff WD |
480 | strcpy(buf2, " -> "); |
481 | } else { | |
ef74f5d6 | 482 | n = ""; |
126e7aff WD |
483 | if (!fmt[1]) |
484 | break; | |
485 | strcpy(buf2, " "); | |
486 | } | |
487 | strlcat(fmt, "s", sizeof fmt); | |
488 | snprintf(buf2 + 4, sizeof buf2 - 4, fmt, n); | |
489 | n = buf2; | |
ef74f5d6 | 490 | break; |
b74b3d53 WD |
491 | case 'm': |
492 | n = lp_name(module_id); | |
493 | break; | |
494 | case 't': | |
495 | n = timestring(time(NULL)); | |
496 | break; | |
497 | case 'P': | |
498 | n = lp_path(module_id); | |
499 | break; | |
500 | case 'u': | |
501 | n = auth_user; | |
502 | break; | |
4a7319be | 503 | case 'b': |
1b7c47cb | 504 | if (am_sender) { |
4a7319be | 505 | b = stats.total_written - |
1b7c47cb AT |
506 | initial_stats->total_written; |
507 | } else { | |
4a7319be | 508 | b = stats.total_read - |
1b7c47cb AT |
509 | initial_stats->total_read; |
510 | } | |
0455cd93 | 511 | strlcat(fmt, ".0f", sizeof fmt); |
ddd74b67 | 512 | snprintf(buf2, sizeof buf2, fmt, (double)b); |
1b7c47cb AT |
513 | n = buf2; |
514 | break; | |
4a7319be | 515 | case 'c': |
1b7c47cb | 516 | if (!am_sender) { |
4a7319be | 517 | b = stats.total_written - |
1b7c47cb AT |
518 | initial_stats->total_written; |
519 | } else { | |
4a7319be | 520 | b = stats.total_read - |
1b7c47cb AT |
521 | initial_stats->total_read; |
522 | } | |
0455cd93 | 523 | strlcat(fmt, ".0f", sizeof fmt); |
ddd74b67 | 524 | snprintf(buf2, sizeof buf2, fmt, (double)b); |
1b7c47cb AT |
525 | n = buf2; |
526 | break; | |
ef74f5d6 | 527 | case 'i': |
19bc826d | 528 | if (iflags & ITEM_DELETED) { |
d5609e96 | 529 | n = "*deleting"; |
19bc826d WD |
530 | break; |
531 | } | |
b4bf2b5a | 532 | n = buf2 + MAXPATHLEN - 32; |
fd84673e WD |
533 | n[0] = iflags & ITEM_LOCAL_CHANGE |
534 | ? iflags & ITEM_XNAME_FOLLOWS ? 'h' : 'c' | |
ca62acc3 | 535 | : !(iflags & ITEM_TRANSFER) ? '.' |
c1759b9f | 536 | : !local_server && *op == 's' ? '<' : '>'; |
19bc826d WD |
537 | n[1] = S_ISDIR(file->mode) ? 'd' |
538 | : IS_DEVICE(file->mode) ? 'D' | |
ef74f5d6 | 539 | : S_ISLNK(file->mode) ? 'L' : 'f'; |
d4e01963 WD |
540 | n[2] = !(iflags & ITEM_REPORT_CHECKSUM) ? '.' : 'c'; |
541 | n[3] = !(iflags & ITEM_REPORT_SIZE) ? '.' : 's'; | |
542 | n[4] = !(iflags & ITEM_REPORT_TIME) ? '.' | |
ef74f5d6 WD |
543 | : !preserve_times || IS_DEVICE(file->mode) |
544 | || S_ISLNK(file->mode) ? 'T' : 't'; | |
d4e01963 WD |
545 | n[5] = !(iflags & ITEM_REPORT_PERMS) ? '.' : 'p'; |
546 | n[6] = !(iflags & ITEM_REPORT_OWNER) ? '.' : 'o'; | |
547 | n[7] = !(iflags & ITEM_REPORT_GROUP) ? '.' : 'g'; | |
ca62acc3 | 548 | n[8] = !(iflags & ITEM_REPORT_XATTRS) ? '.' : 'a'; |
afc65a5a | 549 | n[9] = '\0'; |
ef74f5d6 WD |
550 | |
551 | if (iflags & (ITEM_IS_NEW|ITEM_MISSING_DATA)) { | |
552 | char ch = iflags & ITEM_IS_NEW ? '+' : '?'; | |
553 | int i; | |
554 | for (i = 2; n[i]; i++) | |
555 | n[i] = ch; | |
6d4ecad1 WD |
556 | } else if (n[0] == '.' || n[0] == 'h' |
557 | || (n[0] == 'c' && n[1] == 'f')) { | |
19bc826d WD |
558 | int i; |
559 | for (i = 2; n[i]; i++) { | |
560 | if (n[i] != '.') | |
561 | break; | |
562 | } | |
563 | if (!n[i]) { | |
564 | for (i = 2; n[i]; i++) | |
565 | n[i] = ' '; | |
19bc826d | 566 | } |
ef74f5d6 WD |
567 | } |
568 | break; | |
e08bfe12 AT |
569 | } |
570 | ||
ddd74b67 WD |
571 | /* "n" is the string to be inserted in place of this % code. */ |
572 | if (!n) | |
573 | continue; | |
9baed760 WD |
574 | if (n != buf2 && fmt[1]) { |
575 | strlcat(fmt, "s", sizeof fmt); | |
576 | snprintf(buf2, sizeof buf2, fmt, n); | |
577 | n = buf2; | |
578 | } | |
ef74f5d6 | 579 | len = strlen(n); |
e08bfe12 | 580 | |
e145d51b | 581 | /* Subtract the length of the escape from the string's size. */ |
0455cd93 | 582 | total -= p - s + 1; |
e145d51b | 583 | |
d9c0051f | 584 | if (len + total >= (size_t)sizeof buf) { |
1e7098b5 WD |
585 | rprintf(FERROR, |
586 | "buffer overflow expanding %%%c -- exiting\n", | |
0455cd93 | 587 | p[0]); |
65417579 | 588 | exit_cleanup(RERR_MESSAGEIO); |
e08bfe12 AT |
589 | } |
590 | ||
aa126974 | 591 | /* Shuffle the rest of the string along to make space for n */ |
0455cd93 WD |
592 | if (len != (size_t)(p - s + 1)) |
593 | memmove(s + len, p + 1, total - (s - buf) + 1); | |
ddd74b67 | 594 | total += len; |
aa126974 | 595 | |
ddd74b67 | 596 | /* Insert the contents of string "n", but NOT its null. */ |
ef74f5d6 | 597 | if (len) |
ddd74b67 | 598 | memcpy(s, n, len); |
e08bfe12 | 599 | |
aa126974 | 600 | /* Skip over inserted string; continue looking */ |
ddd74b67 | 601 | p = s + len; |
e08bfe12 AT |
602 | } |
603 | ||
d9c0051f | 604 | rwrite(code, buf, total); |
e08bfe12 AT |
605 | } |
606 | ||
0455cd93 WD |
607 | /* Return 1 if the format escape is in the log-format string (e.g. look for |
608 | * the 'b' in the "%9b" format escape). */ | |
16f960fe WD |
609 | int log_format_has(const char *format, char esc) |
610 | { | |
611 | const char *p; | |
612 | ||
613 | if (!format) | |
614 | return 0; | |
615 | ||
616 | for (p = format; (p = strchr(p, '%')) != NULL; ) { | |
617 | if (*++p == '-') | |
618 | p++; | |
619 | while (isdigit(*(uchar*)p)) | |
620 | p++; | |
621 | if (!*p) | |
622 | break; | |
623 | if (*p == esc) | |
624 | return 1; | |
625 | } | |
626 | return 0; | |
627 | } | |
628 | ||
afc65a5a WD |
629 | /* log the transfer of a file */ |
630 | void log_item(struct file_struct *file, struct stats *initial_stats, | |
631 | int iflags, char *hlink) | |
11a5a3c7 | 632 | { |
afc65a5a | 633 | char *s_or_r = am_sender ? "send" : "recv"; |
11a5a3c7 | 634 | |
11a5a3c7 | 635 | if (lp_transfer_logging(module_id)) { |
afc65a5a WD |
636 | log_formatted(FLOG, lp_log_format(module_id), s_or_r, |
637 | file, initial_stats, iflags, hlink); | |
b6062654 | 638 | } else if (log_format && !am_server) { |
be0602ec | 639 | log_formatted(FNAME, log_format, s_or_r, |
afc65a5a | 640 | file, initial_stats, iflags, hlink); |
11a5a3c7 AT |
641 | } |
642 | } | |
643 | ||
1c3e3679 WD |
644 | void maybe_log_item(struct file_struct *file, int iflags, int itemizing, |
645 | char *buf) | |
646 | { | |
f2b6fe44 | 647 | int significant_flags = iflags & SIGNIFICANT_ITEM_FLAGS; |
6d4ecad1 | 648 | int see_item = itemizing && (significant_flags || *buf |
a9e47626 | 649 | || log_format_has_i > 1 || (verbose > 1 && log_format_has_i)); |
6d4ecad1 | 650 | int local_change = iflags & ITEM_LOCAL_CHANGE && significant_flags; |
1c3e3679 WD |
651 | if (am_server) { |
652 | if (am_daemon && !dry_run && see_item) | |
653 | log_item(file, &stats, iflags, buf); | |
f2b6fe44 WD |
654 | } else if (see_item || local_change || *buf |
655 | || (S_ISDIR(file->mode) && significant_flags)) | |
1c3e3679 WD |
656 | log_item(file, &stats, iflags, buf); |
657 | } | |
658 | ||
19bc826d WD |
659 | void log_delete(char *fname, int mode) |
660 | { | |
661 | static struct file_struct file; | |
662 | int len = strlen(fname); | |
19bc826d WD |
663 | char *fmt; |
664 | ||
665 | file.mode = mode; | |
666 | file.basename = fname; | |
667 | ||
41b5b5e7 WD |
668 | if (!verbose && !log_format) |
669 | ; | |
670 | else if (am_server && protocol_version >= 29 && len < MAXPATHLEN) { | |
19bc826d WD |
671 | if (S_ISDIR(mode)) |
672 | len++; /* directories include trailing null */ | |
673 | send_msg(MSG_DELETED, fname, len); | |
19bc826d | 674 | } else { |
d5609e96 | 675 | fmt = log_format_has_o_or_i ? log_format : "deleting %n"; |
afc65a5a WD |
676 | log_formatted(FCLIENT, fmt, "del.", &file, &stats, |
677 | ITEM_DELETED, NULL); | |
19bc826d WD |
678 | } |
679 | ||
41b5b5e7 | 680 | if (!am_daemon || dry_run || !lp_transfer_logging(module_id)) |
1eec003a WD |
681 | return; |
682 | ||
d5609e96 | 683 | fmt = daemon_log_format_has_o_or_i ? lp_log_format(module_id) : "deleting %n"; |
afc65a5a | 684 | log_formatted(FLOG, fmt, "del.", &file, &stats, ITEM_DELETED, NULL); |
19bc826d | 685 | } |
af642a61 | 686 | |
af642a61 MP |
687 | /* |
688 | * Called when the transfer is interrupted for some reason. | |
689 | * | |
690 | * Code is one of the RERR_* codes from errcode.h, or terminating | |
691 | * successfully. | |
692 | */ | |
a9766ef1 | 693 | void log_exit(int code, const char *file, int line) |
9b73d1c0 AT |
694 | { |
695 | if (code == 0) { | |
088aff1a | 696 | rprintf(FLOG,"sent %.0f bytes received %.0f bytes total size %.0f\n", |
9b73d1c0 AT |
697 | (double)stats.total_written, |
698 | (double)stats.total_read, | |
699 | (double)stats.total_size); | |
700 | } else { | |
4a7319be WD |
701 | const char *name; |
702 | ||
703 | name = rerr_name(code); | |
704 | if (!name) | |
705 | name = "unexplained error"; | |
af642a61 | 706 | |
1bca1de6 WD |
707 | /* VANISHED is not an error, only a warning */ |
708 | if (code == RERR_VANISHED) { | |
709 | rprintf(FINFO, "rsync warning: %s (code %d) at %s(%d)\n", | |
710 | name, code, file, line); | |
711 | } else { | |
712 | rprintf(FERROR, "rsync error: %s (code %d) at %s(%d)\n", | |
713 | name, code, file, line); | |
714 | } | |
9b73d1c0 AT |
715 | } |
716 | } |