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