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