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