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