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