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