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