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