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