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 | |
04c84119 | 132 | openlog("rsyncd", options, lp_syslog_facility(module_id)); |
6afb9077 WD |
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 | ||
04c84119 | 157 | void log_init(int restart) |
6afb9077 | 158 | { |
04c84119 WD |
159 | if (log_initialised) { |
160 | if (!restart) | |
161 | return; | |
162 | if (strcmp(logfile_name, lp_log_file(module_id)) != 0) { | |
163 | if (logfile_fp) { | |
164 | fclose(logfile_fp); | |
165 | logfile_fp = NULL; | |
166 | } else | |
167 | closelog(); | |
168 | logfile_name = NULL; | |
169 | } else if (*logfile_name) | |
170 | return; /* unchanged, non-empty "log file" names */ | |
171 | else if (lp_syslog_facility(-1) != lp_syslog_facility(module_id)) | |
172 | closelog(); | |
173 | else | |
174 | return; /* unchanged syslog settings */ | |
175 | } else | |
176 | log_initialised = 1; | |
1a016bfd | 177 | |
38de2866 | 178 | /* This looks pointless, but it is needed in order for the |
4a7319be | 179 | * C library on some systems to fetch the timezone info |
38de2866 WD |
180 | * before the chroot. */ |
181 | timestring(time(NULL)); | |
958f3735 | 182 | |
13b597fa WD |
183 | /* Optionally use a log file instead of syslog. (Non-daemon |
184 | * rsyncs will have already set logfile_name, as needed.) */ | |
185 | if (am_daemon && !logfile_name) | |
04c84119 | 186 | logfile_name = lp_log_file(module_id); |
13b597fa | 187 | if (logfile_name && *logfile_name) |
64c37826 WD |
188 | logfile_open(); |
189 | else | |
190 | syslog_init(); | |
1a016bfd | 191 | } |
1a016bfd | 192 | |
64c37826 | 193 | void logfile_close(void) |
4a239e98 | 194 | { |
13b597fa | 195 | if (logfile_fp) { |
64c37826 | 196 | logfile_was_closed = 1; |
13b597fa WD |
197 | fclose(logfile_fp); |
198 | logfile_fp = NULL; | |
4a239e98 WD |
199 | } |
200 | } | |
201 | ||
64c37826 | 202 | void logfile_reopen(void) |
4a239e98 | 203 | { |
64c37826 WD |
204 | if (logfile_was_closed) { |
205 | logfile_was_closed = 0; | |
206 | logfile_open(); | |
4a239e98 WD |
207 | } |
208 | } | |
209 | ||
3648ab3a | 210 | static void filtered_fwrite(FILE *f, const char *buf, int len, int use_isprint) |
be0602ec WD |
211 | { |
212 | const char *s, *end = buf + len; | |
213 | for (s = buf; s < end; s++) { | |
214 | if ((s < end - 4 | |
3648ab3a | 215 | && *s == '\\' && s[1] == '#' |
be0602ec WD |
216 | && isdigit(*(uchar*)(s+2)) |
217 | && isdigit(*(uchar*)(s+3)) | |
218 | && isdigit(*(uchar*)(s+4))) | |
3648ab3a WD |
219 | || (*s != '\t' |
220 | && ((use_isprint && !isprint(*(uchar*)s)) | |
221 | || *(uchar*)s < ' '))) { | |
be0602ec WD |
222 | if (s != buf && fwrite(buf, s - buf, 1, f) != 1) |
223 | exit_cleanup(RERR_MESSAGEIO); | |
3648ab3a | 224 | fprintf(f, "\\#%03o", *(uchar*)s); |
be0602ec WD |
225 | buf = s + 1; |
226 | } | |
227 | } | |
228 | if (buf != end && fwrite(buf, end - buf, 1, f) != 1) | |
229 | exit_cleanup(RERR_MESSAGEIO); | |
230 | } | |
231 | ||
554e0a8d | 232 | /* this is the underlying (unformatted) rsync debugging function. Call |
be0602ec WD |
233 | * it with FINFO, FERROR or FLOG. Note: recursion can happen with |
234 | * certain fatal conditions. */ | |
ff41a59f | 235 | void rwrite(enum logcode code, char *buf, int len) |
0b76cd63 | 236 | { |
be0602ec | 237 | int trailing_CR_or_NL; |
8fcdc444 | 238 | FILE *f = NULL; |
8d9dc9f9 | 239 | |
a644fc3c WD |
240 | if (len < 0) |
241 | exit_cleanup(RERR_MESSAGEIO); | |
0b76cd63 | 242 | |
0bb4d176 WD |
243 | if (am_server && msg_fd_out >= 0) { |
244 | /* Pass the message to our sibling. */ | |
245 | send_msg((enum msgcode)code, buf, len); | |
11a5a3c7 AT |
246 | return; |
247 | } | |
248 | ||
b66e31bf WD |
249 | if (code == FSOCKERR) /* This gets simplified for a non-sibling. */ |
250 | code = FERROR; | |
251 | ||
1eec003a WD |
252 | if (code == FCLIENT) |
253 | code = FINFO; | |
d0133e6e | 254 | else if (am_daemon || logfile_name) { |
0bb4d176 | 255 | static int in_block; |
d0133e6e | 256 | char msg[2048]; |
0bb4d176 WD |
257 | int priority = code == FERROR ? LOG_WARNING : LOG_INFO; |
258 | ||
259 | if (in_block) | |
a644fc3c | 260 | return; |
0bb4d176 WD |
261 | in_block = 1; |
262 | if (!log_initialised) | |
04c84119 | 263 | log_init(0); |
0bb4d176 | 264 | strlcpy(msg, buf, MIN((int)sizeof msg, len + 1)); |
d0133e6e | 265 | logit(priority, msg); |
0bb4d176 WD |
266 | in_block = 0; |
267 | ||
13b597fa | 268 | if (code == FLOG || (am_daemon && !am_server)) |
a644fc3c | 269 | return; |
0bb4d176 | 270 | } else if (code == FLOG) |
ff8b29b8 | 271 | return; |
0bb4d176 | 272 | |
b3e8e7c7 WD |
273 | if (quiet && code != FERROR) |
274 | return; | |
275 | ||
0bb4d176 WD |
276 | if (am_server) { |
277 | /* Pass the message to the non-server side. */ | |
f1c9bcd0 | 278 | if (send_msg((enum msgcode)code, buf, len)) |
0bb4d176 WD |
279 | return; |
280 | if (am_daemon) { | |
281 | /* TODO: can we send the error to the user somehow? */ | |
282 | return; | |
283 | } | |
0b76cd63 AT |
284 | } |
285 | ||
be0602ec WD |
286 | switch (code) { |
287 | case FERROR: | |
ff81e809 | 288 | log_got_error = 1; |
ff8b29b8 | 289 | f = stderr; |
be0602ec | 290 | break; |
d0133e6e | 291 | case FINFO: |
be0602ec WD |
292 | f = am_server ? stderr : stdout; |
293 | break; | |
294 | default: | |
0bb4d176 | 295 | exit_cleanup(RERR_MESSAGEIO); |
be0602ec | 296 | } |
0b76cd63 | 297 | |
be0602ec WD |
298 | trailing_CR_or_NL = len && (buf[len-1] == '\n' || buf[len-1] == '\r') |
299 | ? buf[--len] : 0; | |
8d9dc9f9 | 300 | |
595251de | 301 | #if defined HAVE_ICONV_OPEN && defined HAVE_ICONV_H |
9a31746b WD |
302 | if (ic_chck != (iconv_t)-1) { |
303 | char convbuf[1024]; | |
304 | char *in_buf = buf, *out_buf = convbuf; | |
305 | size_t in_cnt = len, out_cnt = sizeof convbuf - 1; | |
306 | ||
307 | iconv(ic_chck, NULL, 0, NULL, 0); | |
308 | while (iconv(ic_chck, &in_buf,&in_cnt, | |
309 | &out_buf,&out_cnt) == (size_t)-1) { | |
310 | if (out_buf != convbuf) { | |
3648ab3a | 311 | filtered_fwrite(f, convbuf, out_buf - convbuf, 0); |
9a31746b WD |
312 | out_buf = convbuf; |
313 | out_cnt = sizeof convbuf - 1; | |
314 | } | |
315 | if (errno == E2BIG) | |
316 | continue; | |
3648ab3a | 317 | fprintf(f, "\\#%03o", *(uchar*)in_buf++); |
9a31746b WD |
318 | in_cnt--; |
319 | } | |
320 | if (out_buf != convbuf) | |
3648ab3a | 321 | filtered_fwrite(f, convbuf, out_buf - convbuf, 0); |
9a31746b WD |
322 | } else |
323 | #endif | |
507433f6 | 324 | filtered_fwrite(f, buf, len, !allow_8bit_chars); |
be0602ec WD |
325 | |
326 | if (trailing_CR_or_NL) { | |
327 | fputc(trailing_CR_or_NL, f); | |
0bb4d176 | 328 | fflush(f); |
be0602ec | 329 | } |
0b76cd63 | 330 | } |
0455cd93 | 331 | |
a039749b MP |
332 | /* This is the rsync debugging function. Call it with FINFO, FERROR or |
333 | * FLOG. */ | |
334 | void rprintf(enum logcode code, const char *format, ...) | |
554e0a8d | 335 | { |
4a7319be | 336 | va_list ap; |
b74b3d53 | 337 | char buf[BIGPATHBUFLEN]; |
8fcdc444 | 338 | size_t len; |
554e0a8d AT |
339 | |
340 | va_start(ap, format); | |
ef74f5d6 | 341 | len = vsnprintf(buf, sizeof buf, format, ap); |
554e0a8d AT |
342 | va_end(ap); |
343 | ||
b2f02464 | 344 | /* Deal with buffer overruns. Instead of panicking, just |
8fcdc444 WD |
345 | * truncate the resulting string. (Note that configure ensures |
346 | * that we have a vsnprintf() that doesn't ever return -1.) */ | |
347 | if (len > sizeof buf - 1) { | |
b74b3d53 | 348 | static const char ellipsis[] = "[...]"; |
b2f02464 MP |
349 | |
350 | /* Reset length, and zero-terminate the end of our buffer */ | |
ef74f5d6 | 351 | len = sizeof buf - 1; |
b2f02464 MP |
352 | buf[len] = '\0'; |
353 | ||
354 | /* Copy the ellipsis to the end of the string, but give | |
355 | * us one extra character: | |
356 | * | |
ef74f5d6 | 357 | * v--- null byte at buf[sizeof buf - 1] |
b2f02464 MP |
358 | * abcdefghij0 |
359 | * -> abcd[...]00 <-- now two null bytes at end | |
360 | * | |
361 | * If the input format string has a trailing newline, | |
362 | * we copy it into that extra null; if it doesn't, well, | |
363 | * all we lose is one byte. */ | |
b74b3d53 | 364 | memcpy(buf+len-sizeof ellipsis, ellipsis, sizeof ellipsis); |
b2f02464 MP |
365 | if (format[strlen(format)-1] == '\n') { |
366 | buf[len-1] = '\n'; | |
367 | } | |
368 | } | |
554e0a8d | 369 | |
ff41a59f | 370 | rwrite(code, buf, len); |
554e0a8d | 371 | } |
0b76cd63 | 372 | |
a039749b MP |
373 | /* This is like rprintf, but it also tries to print some |
374 | * representation of the error code. Normally errcode = errno. | |
375 | * | |
376 | * Unlike rprintf, this always adds a newline and there should not be | |
377 | * one in the format string. | |
378 | * | |
379 | * Note that since strerror might involve dynamically loading a | |
380 | * message catalog we need to call it once before chroot-ing. */ | |
381 | void rsyserr(enum logcode code, int errcode, const char *format, ...) | |
382 | { | |
4a7319be | 383 | va_list ap; |
b74b3d53 | 384 | char buf[BIGPATHBUFLEN]; |
8fcdc444 WD |
385 | size_t len; |
386 | ||
387 | strcpy(buf, RSYNC_NAME ": "); | |
388 | len = (sizeof RSYNC_NAME ": ") - 1; | |
a039749b MP |
389 | |
390 | va_start(ap, format); | |
8fcdc444 | 391 | len += vsnprintf(buf + len, sizeof buf - len, format, ap); |
a039749b MP |
392 | va_end(ap); |
393 | ||
8fcdc444 WD |
394 | if (len < sizeof buf) { |
395 | len += snprintf(buf + len, sizeof buf - len, | |
396 | ": %s (%d)\n", strerror(errcode), errcode); | |
397 | } | |
398 | if (len >= sizeof buf) | |
4a7319be | 399 | exit_cleanup(RERR_MESSAGEIO); |
a039749b | 400 | |
a039749b MP |
401 | rwrite(code, buf, len); |
402 | } | |
403 | ||
ff41a59f | 404 | void rflush(enum logcode code) |
0b76cd63 AT |
405 | { |
406 | FILE *f = NULL; | |
0455cd93 | 407 | |
13b597fa | 408 | if (am_daemon || code == FLOG) |
0b76cd63 | 409 | return; |
0b76cd63 | 410 | |
13b597fa | 411 | if (code == FERROR || am_server) |
0b76cd63 | 412 | f = stderr; |
13b597fa WD |
413 | else |
414 | f = stdout; | |
0b76cd63 | 415 | |
0b76cd63 AT |
416 | fflush(f); |
417 | } | |
418 | ||
b5343510 | 419 | /* A generic logging routine for send/recv, with parameter substitiution. */ |
afc65a5a WD |
420 | static void log_formatted(enum logcode code, char *format, char *op, |
421 | struct file_struct *file, struct stats *initial_stats, | |
422 | int iflags, char *hlink) | |
e08bfe12 | 423 | { |
ddd74b67 WD |
424 | char buf[MAXPATHLEN+1024], buf2[MAXPATHLEN], fmt[32]; |
425 | char *p, *s, *n; | |
ef74f5d6 | 426 | size_t len, total; |
1b7c47cb | 427 | int64 b; |
e08bfe12 | 428 | |
ddd74b67 WD |
429 | *fmt = '%'; |
430 | ||
aa126974 | 431 | /* We expand % codes one by one in place in buf. We don't |
126e7aff WD |
432 | * copy in the terminating null of the inserted strings, but |
433 | * rather keep going until we reach the null of the format. */ | |
ef74f5d6 | 434 | total = strlcpy(buf, format, sizeof buf); |
d9c0051f WD |
435 | if (total > MAXPATHLEN) { |
436 | rprintf(FERROR, "log-format string is WAY too long!\n"); | |
437 | exit_cleanup(RERR_MESSAGEIO); | |
438 | } | |
439 | buf[total++] = '\n'; | |
440 | buf[total] = '\0'; | |
0455cd93 | 441 | |
16f960fe | 442 | for (p = buf; (p = strchr(p, '%')) != NULL; ) { |
ddd74b67 WD |
443 | s = p++; |
444 | n = fmt + 1; | |
9baed760 WD |
445 | if (*p == '-') |
446 | *n++ = *p++; | |
b4bf2b5a | 447 | while (isdigit(*(uchar*)p) && n - fmt < (int)(sizeof fmt) - 8) |
ddd74b67 | 448 | *n++ = *p++; |
e145d51b WD |
449 | if (!*p) |
450 | break; | |
ddd74b67 | 451 | *n = '\0'; |
e08bfe12 | 452 | n = NULL; |
e08bfe12 | 453 | |
0455cd93 | 454 | switch (*p) { |
b74b3d53 WD |
455 | case 'h': |
456 | if (am_daemon) | |
457 | n = client_name(0); | |
458 | break; | |
459 | case 'a': | |
460 | if (am_daemon) | |
461 | n = client_addr(0); | |
462 | break; | |
4a7319be | 463 | case 'l': |
ddd74b67 WD |
464 | strlcat(fmt, ".0f", sizeof fmt); |
465 | snprintf(buf2, sizeof buf2, fmt, | |
4a7319be | 466 | (double)file->length); |
e08bfe12 AT |
467 | n = buf2; |
468 | break; | |
427b6179 WD |
469 | case 'U': |
470 | strlcat(fmt, "ld", sizeof fmt); | |
471 | snprintf(buf2, sizeof buf2, fmt, | |
472 | (long)file->uid); | |
473 | n = buf2; | |
474 | break; | |
475 | case 'G': | |
998113fe WD |
476 | if (file->gid == GID_NONE) |
477 | n = "DEFAULT"; | |
478 | else { | |
479 | strlcat(fmt, "ld", sizeof fmt); | |
480 | snprintf(buf2, sizeof buf2, fmt, | |
481 | (long)file->gid); | |
482 | n = buf2; | |
483 | } | |
427b6179 | 484 | break; |
4a7319be | 485 | case 'p': |
126e7aff | 486 | strlcat(fmt, "ld", sizeof fmt); |
ddd74b67 | 487 | snprintf(buf2, sizeof buf2, fmt, |
126e7aff | 488 | (long)getpid()); |
e08bfe12 AT |
489 | n = buf2; |
490 | break; | |
427b6179 WD |
491 | case 'M': |
492 | n = timestring(file->modtime); | |
493 | { | |
998113fe WD |
494 | char *cp = n; |
495 | while ((cp = strchr(cp, ' ')) != NULL) | |
427b6179 WD |
496 | *cp = '-'; |
497 | } | |
498 | break; | |
499 | case 'B': | |
500 | n = buf2 + MAXPATHLEN - PERMSTRING_SIZE; | |
501 | permstring(n - 1, file->mode); /* skip the type char */ | |
502 | break; | |
b74b3d53 WD |
503 | case 'o': |
504 | n = op; | |
505 | break; | |
4a7319be | 506 | case 'f': |
be0602ec | 507 | n = f_name(file, NULL); |
9baed760 WD |
508 | if (am_sender && file->dir.root) { |
509 | pathjoin(buf2, sizeof buf2, | |
510 | file->dir.root, n); | |
0ee6ca98 | 511 | clean_fname(buf2, 0); |
dcbae654 | 512 | if (fmt[1]) |
0ee6ca98 | 513 | strlcpy(n, buf2, MAXPATHLEN); |
dcbae654 WD |
514 | else |
515 | n = buf2; | |
0ee6ca98 WD |
516 | } else |
517 | clean_fname(n, 0); | |
9baed760 WD |
518 | if (*n == '/') |
519 | n++; | |
ab7104da | 520 | break; |
ef74f5d6 | 521 | case 'n': |
be0602ec | 522 | n = f_name(file, NULL); |
0ee6ca98 WD |
523 | if (S_ISDIR(file->mode)) |
524 | strlcat(n, "/", MAXPATHLEN); | |
ef74f5d6 WD |
525 | break; |
526 | case 'L': | |
afc65a5a | 527 | if (hlink && *hlink) { |
0ee6ca98 | 528 | n = hlink; |
126e7aff | 529 | strcpy(buf2, " => "); |
afc65a5a | 530 | } else if (S_ISLNK(file->mode) && file->u.link) { |
0ee6ca98 | 531 | n = file->u.link; |
126e7aff WD |
532 | strcpy(buf2, " -> "); |
533 | } else { | |
ef74f5d6 | 534 | n = ""; |
126e7aff WD |
535 | if (!fmt[1]) |
536 | break; | |
537 | strcpy(buf2, " "); | |
538 | } | |
539 | strlcat(fmt, "s", sizeof fmt); | |
540 | snprintf(buf2 + 4, sizeof buf2 - 4, fmt, n); | |
541 | n = buf2; | |
ef74f5d6 | 542 | break; |
b74b3d53 WD |
543 | case 'm': |
544 | n = lp_name(module_id); | |
545 | break; | |
546 | case 't': | |
547 | n = timestring(time(NULL)); | |
548 | break; | |
549 | case 'P': | |
550 | n = lp_path(module_id); | |
551 | break; | |
552 | case 'u': | |
553 | n = auth_user; | |
554 | break; | |
4a7319be | 555 | case 'b': |
1b7c47cb | 556 | if (am_sender) { |
4a7319be | 557 | b = stats.total_written - |
1b7c47cb AT |
558 | initial_stats->total_written; |
559 | } else { | |
4a7319be | 560 | b = stats.total_read - |
1b7c47cb AT |
561 | initial_stats->total_read; |
562 | } | |
0455cd93 | 563 | strlcat(fmt, ".0f", sizeof fmt); |
ddd74b67 | 564 | snprintf(buf2, sizeof buf2, fmt, (double)b); |
1b7c47cb AT |
565 | n = buf2; |
566 | break; | |
4a7319be | 567 | case 'c': |
1b7c47cb | 568 | if (!am_sender) { |
4a7319be | 569 | b = stats.total_written - |
1b7c47cb AT |
570 | initial_stats->total_written; |
571 | } else { | |
4a7319be | 572 | b = stats.total_read - |
1b7c47cb AT |
573 | initial_stats->total_read; |
574 | } | |
0455cd93 | 575 | strlcat(fmt, ".0f", sizeof fmt); |
ddd74b67 | 576 | snprintf(buf2, sizeof buf2, fmt, (double)b); |
1b7c47cb AT |
577 | n = buf2; |
578 | break; | |
ef74f5d6 | 579 | case 'i': |
19bc826d | 580 | if (iflags & ITEM_DELETED) { |
d5609e96 | 581 | n = "*deleting"; |
19bc826d WD |
582 | break; |
583 | } | |
b4bf2b5a | 584 | n = buf2 + MAXPATHLEN - 32; |
fd84673e WD |
585 | n[0] = iflags & ITEM_LOCAL_CHANGE |
586 | ? iflags & ITEM_XNAME_FOLLOWS ? 'h' : 'c' | |
ca62acc3 | 587 | : !(iflags & ITEM_TRANSFER) ? '.' |
c1759b9f | 588 | : !local_server && *op == 's' ? '<' : '>'; |
19bc826d | 589 | n[1] = S_ISDIR(file->mode) ? 'd' |
f8d47c1c | 590 | : IS_SPECIAL(file->mode) ? 'S' |
19bc826d | 591 | : IS_DEVICE(file->mode) ? 'D' |
ef74f5d6 | 592 | : S_ISLNK(file->mode) ? 'L' : 'f'; |
d4e01963 WD |
593 | n[2] = !(iflags & ITEM_REPORT_CHECKSUM) ? '.' : 'c'; |
594 | n[3] = !(iflags & ITEM_REPORT_SIZE) ? '.' : 's'; | |
595 | n[4] = !(iflags & ITEM_REPORT_TIME) ? '.' | |
4cff7c50 | 596 | : !preserve_times || S_ISLNK(file->mode) ? 'T' : 't'; |
d4e01963 WD |
597 | n[5] = !(iflags & ITEM_REPORT_PERMS) ? '.' : 'p'; |
598 | n[6] = !(iflags & ITEM_REPORT_OWNER) ? '.' : 'o'; | |
599 | n[7] = !(iflags & ITEM_REPORT_GROUP) ? '.' : 'g'; | |
503f1634 WD |
600 | n[8] = '.'; |
601 | n[9] = '\0'; | |
ef74f5d6 WD |
602 | |
603 | if (iflags & (ITEM_IS_NEW|ITEM_MISSING_DATA)) { | |
604 | char ch = iflags & ITEM_IS_NEW ? '+' : '?'; | |
605 | int i; | |
606 | for (i = 2; n[i]; i++) | |
607 | n[i] = ch; | |
6d4ecad1 WD |
608 | } else if (n[0] == '.' || n[0] == 'h' |
609 | || (n[0] == 'c' && n[1] == 'f')) { | |
19bc826d WD |
610 | int i; |
611 | for (i = 2; n[i]; i++) { | |
612 | if (n[i] != '.') | |
613 | break; | |
614 | } | |
615 | if (!n[i]) { | |
616 | for (i = 2; n[i]; i++) | |
617 | n[i] = ' '; | |
19bc826d | 618 | } |
ef74f5d6 WD |
619 | } |
620 | break; | |
e08bfe12 AT |
621 | } |
622 | ||
ddd74b67 WD |
623 | /* "n" is the string to be inserted in place of this % code. */ |
624 | if (!n) | |
625 | continue; | |
9baed760 WD |
626 | if (n != buf2 && fmt[1]) { |
627 | strlcat(fmt, "s", sizeof fmt); | |
628 | snprintf(buf2, sizeof buf2, fmt, n); | |
629 | n = buf2; | |
630 | } | |
ef74f5d6 | 631 | len = strlen(n); |
e08bfe12 | 632 | |
e145d51b | 633 | /* Subtract the length of the escape from the string's size. */ |
0455cd93 | 634 | total -= p - s + 1; |
e145d51b | 635 | |
d9c0051f | 636 | if (len + total >= (size_t)sizeof buf) { |
1e7098b5 WD |
637 | rprintf(FERROR, |
638 | "buffer overflow expanding %%%c -- exiting\n", | |
0455cd93 | 639 | p[0]); |
65417579 | 640 | exit_cleanup(RERR_MESSAGEIO); |
e08bfe12 AT |
641 | } |
642 | ||
aa126974 | 643 | /* Shuffle the rest of the string along to make space for n */ |
0455cd93 WD |
644 | if (len != (size_t)(p - s + 1)) |
645 | memmove(s + len, p + 1, total - (s - buf) + 1); | |
ddd74b67 | 646 | total += len; |
aa126974 | 647 | |
ddd74b67 | 648 | /* Insert the contents of string "n", but NOT its null. */ |
ef74f5d6 | 649 | if (len) |
ddd74b67 | 650 | memcpy(s, n, len); |
e08bfe12 | 651 | |
aa126974 | 652 | /* Skip over inserted string; continue looking */ |
ddd74b67 | 653 | p = s + len; |
e08bfe12 AT |
654 | } |
655 | ||
d9c0051f | 656 | rwrite(code, buf, total); |
e08bfe12 AT |
657 | } |
658 | ||
0455cd93 WD |
659 | /* Return 1 if the format escape is in the log-format string (e.g. look for |
660 | * the 'b' in the "%9b" format escape). */ | |
16f960fe WD |
661 | int log_format_has(const char *format, char esc) |
662 | { | |
663 | const char *p; | |
664 | ||
665 | if (!format) | |
666 | return 0; | |
667 | ||
668 | for (p = format; (p = strchr(p, '%')) != NULL; ) { | |
669 | if (*++p == '-') | |
670 | p++; | |
671 | while (isdigit(*(uchar*)p)) | |
672 | p++; | |
673 | if (!*p) | |
674 | break; | |
675 | if (*p == esc) | |
676 | return 1; | |
677 | } | |
678 | return 0; | |
679 | } | |
680 | ||
d0133e6e | 681 | /* Log the transfer of a file. If the code is FCLIENT, the output just goes |
b5343510 WD |
682 | * to stdout. If it is FLOG, it just goes to the log file. Otherwise we |
683 | * output to both. */ | |
684 | void log_item(enum logcode code, struct file_struct *file, | |
685 | struct stats *initial_stats, int iflags, char *hlink) | |
11a5a3c7 | 686 | { |
afc65a5a | 687 | char *s_or_r = am_sender ? "send" : "recv"; |
11a5a3c7 | 688 | |
b5343510 | 689 | if (code != FLOG && stdout_format && !am_server) { |
d0133e6e | 690 | log_formatted(FCLIENT, stdout_format, s_or_r, |
afc65a5a | 691 | file, initial_stats, iflags, hlink); |
b5343510 | 692 | } |
d0133e6e | 693 | if (code != FCLIENT && logfile_format && *logfile_format) { |
13b597fa WD |
694 | log_formatted(FLOG, logfile_format, s_or_r, |
695 | file, initial_stats, iflags, hlink); | |
11a5a3c7 AT |
696 | } |
697 | } | |
698 | ||
1c3e3679 WD |
699 | void maybe_log_item(struct file_struct *file, int iflags, int itemizing, |
700 | char *buf) | |
701 | { | |
f2b6fe44 | 702 | int significant_flags = iflags & SIGNIFICANT_ITEM_FLAGS; |
6d4ecad1 | 703 | int see_item = itemizing && (significant_flags || *buf |
b5343510 | 704 | || stdout_format_has_i > 1 || (verbose > 1 && stdout_format_has_i)); |
6d4ecad1 | 705 | int local_change = iflags & ITEM_LOCAL_CHANGE && significant_flags; |
1c3e3679 | 706 | if (am_server) { |
d0133e6e WD |
707 | if (logfile_name && !dry_run && see_item |
708 | && (significant_flags || logfile_format_has_i)) | |
b5343510 | 709 | log_item(FLOG, file, &stats, iflags, buf); |
f2b6fe44 | 710 | } else if (see_item || local_change || *buf |
d0133e6e WD |
711 | || (S_ISDIR(file->mode) && significant_flags)) { |
712 | enum logcode code = significant_flags || logfile_format_has_i ? FINFO : FCLIENT; | |
713 | log_item(code, file, &stats, iflags, buf); | |
714 | } | |
1c3e3679 WD |
715 | } |
716 | ||
19bc826d WD |
717 | void log_delete(char *fname, int mode) |
718 | { | |
719 | static struct file_struct file; | |
720 | int len = strlen(fname); | |
19bc826d WD |
721 | char *fmt; |
722 | ||
723 | file.mode = mode; | |
724 | file.basename = fname; | |
725 | ||
b5343510 | 726 | if (!verbose && !stdout_format) |
41b5b5e7 WD |
727 | ; |
728 | else if (am_server && protocol_version >= 29 && len < MAXPATHLEN) { | |
19bc826d WD |
729 | if (S_ISDIR(mode)) |
730 | len++; /* directories include trailing null */ | |
731 | send_msg(MSG_DELETED, fname, len); | |
19bc826d | 732 | } else { |
b5343510 | 733 | fmt = stdout_format_has_o_or_i ? stdout_format : "deleting %n"; |
afc65a5a WD |
734 | log_formatted(FCLIENT, fmt, "del.", &file, &stats, |
735 | ITEM_DELETED, NULL); | |
19bc826d WD |
736 | } |
737 | ||
13b597fa | 738 | if (!logfile_name || dry_run || !logfile_format) |
1eec003a WD |
739 | return; |
740 | ||
13b597fa | 741 | fmt = logfile_format_has_o_or_i ? logfile_format : "deleting %n"; |
afc65a5a | 742 | log_formatted(FLOG, fmt, "del.", &file, &stats, ITEM_DELETED, NULL); |
19bc826d | 743 | } |
af642a61 | 744 | |
af642a61 MP |
745 | /* |
746 | * Called when the transfer is interrupted for some reason. | |
747 | * | |
748 | * Code is one of the RERR_* codes from errcode.h, or terminating | |
749 | * successfully. | |
750 | */ | |
a9766ef1 | 751 | void log_exit(int code, const char *file, int line) |
9b73d1c0 AT |
752 | { |
753 | if (code == 0) { | |
088aff1a | 754 | rprintf(FLOG,"sent %.0f bytes received %.0f bytes total size %.0f\n", |
9b73d1c0 AT |
755 | (double)stats.total_written, |
756 | (double)stats.total_read, | |
757 | (double)stats.total_size); | |
758 | } else { | |
4a7319be WD |
759 | const char *name; |
760 | ||
761 | name = rerr_name(code); | |
762 | if (!name) | |
763 | name = "unexplained error"; | |
af642a61 | 764 | |
1bca1de6 WD |
765 | /* VANISHED is not an error, only a warning */ |
766 | if (code == RERR_VANISHED) { | |
0f78b815 | 767 | rprintf(FINFO, "rsync warning: %s (code %d) at %s(%d) [%s=%s]\n", |
43eae40e | 768 | name, code, file, line, who_am_i(), RSYNC_VERSION); |
1bca1de6 | 769 | } else { |
43eae40e WD |
770 | rprintf(FERROR, "rsync error: %s (code %d) at %s(%d) [%s=%s]\n", |
771 | name, code, file, line, who_am_i(), RSYNC_VERSION); | |
1bca1de6 | 772 | } |
9b73d1c0 AT |
773 | } |
774 | } |