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