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