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