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