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